ModularSensors > Classes > dataPublisher
ModularSensors > Modules > Primary Object Classes > dataPublisher


dataPublisher class

The dataPublisher class is a virtual class used by other publishers to distribute data online.

A dataPublisher is a abstract concept. It is something that "watches" the logger for new data and correctly formats and sends that data to some online web service.

Derived classes

class DreamHostPublisher
The DreamHostPublisher subclass of dataPublisher is for publishing data to the Stroud Center's now-deprecated DreamHost based live sensor data system.
class EnviroDIYPublisher
The EnviroDIYPublisher subclass of dataPublisher for publishing data to the Monitor My Watershed/EnviroDIY data portal at http://data.enviroDIY.org.
class ThingSpeakPublisher
The ThingSpeakPublisher subclass of dataPublisher for publishing data to ThingSpeak using the MQTT protocol.
class UbidotsPublisher
The UbidotsPublisher subclass of dataPublisher for publishing data to the Ubidots data portal at https://ubidots.com.

Constructors, destructors, conversion operators

dataPublisher()
Construct a new data Publisher object untied to any logger or client.
dataPublisher(Logger& baseLogger, uint8_t sendEveryX = 1, uint8_t sendOffset = 0) explicit
Construct a new data Publisher object using a single client created on the TinyGSM based logger modem.
dataPublisher(Logger& baseLogger, Client* inClient, uint8_t sendEveryX = 1, uint8_t sendOffset = 0)
Construct a new data Publisher object.
~dataPublisher() virtual
Destroy the data Publisher object - no action is taken.

Public functions

void setClient(Client* inClient)
Set the Client object.
void attachToLogger(Logger& baseLogger)
Attach the publisher to a logger.
void setSendFrequency(uint8_t sendEveryX, uint8_t sendOffset)
Set the parameters for frequency of sending and any offset, if needed.
void begin(Logger& baseLogger, Client* inClient)
Begin the publisher - linking it to the client and logger.
void begin(Logger& baseLogger)
Begin the publisher - linking it to the logger but not attaching a client.
String getEndpoint(void ) pure virtual
Get the destination for published data - generally the host name of the data receiver.
int16_t publishData(Client* outClient) pure virtual
Open a socket to the correct receiver and sends out the formatted data.
int16_t publishData() virtual
Open a socket to the correct receiver and send out the formatted data.
int16_t sendData(Client* outClient) deprecated in v0.22.5 virtual
Retained for backwards compatibility; use publishData(Client* outClient) in new code.
int16_t sendData() deprecated in v0.22.5 virtual
Retained for backwards compatibility; use publishData() in new code.
String parseMQTTState(int state)
Translate a PubSubClient code into a String with the code explanation.

Protected static functions

static int bufferFree(void )
Get the number of empty spots in the buffer.
static void emptyTxBuffer(void )
Fill the TX buffer with nulls ('\0').
static void printTxBuffer(Stream* stream, bool addNewLine = false)
Write the TX buffer to a stream and also to the debugging port.

Protected static variables

static char txBuffer
A buffer for outgoing data.
static const char* getHeader
the text "GET "
static const char* postHeader
the text "POST "
static const char* HTTPtag
the text " HTTP/1.1"
static const char* hostHeader
the text "\r\nHost: "

Protected variables

Logger* _baseLogger
The internal pointer to the logger instance to be used.
Client* _inClient
The internal pointer to the client instance to be used.
uint8_t _sendEveryX
Unimplemented; intended for future use to enable caching and bulk publishing.
uint8_t _sendOffset
Unimplemented; intended for future use to enable publishing data at a time slightly delayed from when it is collected.

Function documentation

dataPublisher()

Construct a new data Publisher object untied to any logger or client.


dataPublisher(Logger& baseLogger, uint8_t sendEveryX = 1, uint8_t sendOffset = 0) explicit

Construct a new data Publisher object using a single client created on the TinyGSM based logger modem.

Parameters
baseLogger The logger supplying the data to be published
sendEveryX Currently unimplemented, intended for future use to enable caching and bulk publishing
sendOffset Currently unimplemented, intended for future use to enable publishing data at a time slightly delayed from when it is collected

dataPublisher(Logger& baseLogger, Client* inClient, uint8_t sendEveryX = 1, uint8_t sendOffset = 0)

Construct a new data Publisher object.

Parameters
baseLogger The logger supplying the data to be published
inClient An Arduino client instance to use to print data to. Allows the use of any type of client and multiple clients tied to a single TinyGSM modem instance
sendEveryX Currently unimplemented, intended for future use to enable caching and bulk publishing
sendOffset Currently unimplemented, intended for future use to enable publishing data at a time slightly delayed from when it is collected

void setClient(Client* inClient)

Set the Client object.

Parameters
inClient A pointer to an Arduino client instance

Gives the publisher a client instance to use to "print" data.


void attachToLogger(Logger& baseLogger)

Attach the publisher to a logger.

Parameters
baseLogger A reference to the ModularSensors logger instance

The publisher must be tied to a data loger to provide it with the data to be published.


void setSendFrequency(uint8_t sendEveryX, uint8_t sendOffset)

Set the parameters for frequency of sending and any offset, if needed.

Parameters
sendEveryX Currently unimplemented, intended for future use to enable caching and bulk publishing
sendOffset Currently unimplemented, intended for future use to enable publishing data at a time slightly delayed from when it is collected

void begin(Logger& baseLogger, Client* inClient)

Begin the publisher - linking it to the client and logger.

Parameters
baseLogger The logger supplying the data to be published
inClient An Arduino client instance to use to print data to. Allows the use of any type of client and multiple clients tied to a single TinyGSM modem instance

This can be used as an alternative to adding the logger and client in the constructor. This is slightly "safer" because we expect the publishers to be created in the "global scope" and we cannot control the order in which objects in that global scope will be created. That is, we cannot guarantee that the logger will actually be created before the publisher that wants to attach to it unless we wait to attach the publisher until in the setup or loop function of the main program. In reality, it is very unlikely that this is necessary.


void begin(Logger& baseLogger)

Begin the publisher - linking it to the logger but not attaching a client.

Parameters
baseLogger The logger supplying the data to be published

This can be used as an alternative to adding the logger and client in the constructor. This is slightly "safer" because we expect the publishers to be created in the "global scope" and we cannot control the order in which objects in that global scope will be created. That is, we cannot guarantee that the logger will actually be created before the publisher that wants to attach to it unless we wait to attach the publisher until in the setup or loop function of the main program. In reality, it is very unlikely that this is necessary.


String getEndpoint(void ) pure virtual

Get the destination for published data - generally the host name of the data receiver.

Returns String The URL or HOST to receive published data

int16_t publishData(Client* outClient) pure virtual

Open a socket to the correct receiver and sends out the formatted data.

Parameters
outClient An Arduino client instance to use to print data to. Allows the use of any type of client and multiple clients tied to a single TinyGSM modem instance
Returns int16_t The result of publishing data. May be an http response code or a result code from PubSubClient.

This depends on an internet connection already having been made and a client being available.


int16_t publishData() virtual

Open a socket to the correct receiver and send out the formatted data.

Returns int16_t The result of publishing data. May be an http response code or a result code from PubSubClient.

This depends on an internet connection already having been made and either a client having been linked to the publisher or a logger modem having been linked to the logger linked to the publisher.


int16_t sendData(Client* outClient) deprecated in v0.22.5 virtual

Retained for backwards compatibility; use publishData(Client* outClient) in new code.

Parameters
outClient An Arduino client instance to use to print data to. Allows the use of any type of client and multiple clients tied to a single TinyGSM modem instance
Returns int16_t The result of publishing data. May be an http response code or a result code from PubSubClient.

int16_t sendData() deprecated in v0.22.5 virtual

Retained for backwards compatibility; use publishData() in new code.

Returns int16_t The result of publishing data. May be an http response code or a result code from PubSubClient.

String parseMQTTState(int state)

Translate a PubSubClient code into a String with the code explanation.

Parameters
state A result code returned by a PubSubClient action
Returns String The meaning of the code

static int bufferFree(void )

Get the number of empty spots in the buffer.

Returns int The number of available characters in the buffer

static void printTxBuffer(Stream* stream, bool addNewLine = false)

Write the TX buffer to a stream and also to the debugging port.

Parameters
stream A pointer to an Arduino Stream instance to use to print data
addNewLine True to add a new line character ("\n") at the end of the print