ThingSpeakPublisher class

The ThingSpeakPublisher subclass of dataPublisher for publishing data to ThingSpeak using the MQTT protocol.

When sending data to ThingSpeak the order of the variables in the variable array attached to your logger is crucial. The results from the variables in the VariableArray will be sent to ThingSpeak in the order they are in the array; that is, the first variable in the array will be sent as Field1, the second as Field2, etc. Any UUID's or custom variable codes are ignored for ThingSpeak. They will only appear in the header of your file on the SD card. Giving a variable a custom variable code like "Field3" will NOT make that variable field 3 on ThingSpeak. The third variable in the array will always be "Field3". Any text names you have given to your fields in ThingSpeak are also irrelevant.

Base classes

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

Constructors, destructors, conversion operators

ThingSpeakPublisher()
Construct a new ThingSpeak Publisher object with no members initialized.
ThingSpeakPublisher(Logger& baseLogger, int sendEveryX = 1) explicit
Construct a new ThingSpeak Publisher object.
ThingSpeakPublisher(Logger& baseLogger, Client* inClient, int sendEveryX = 1)
Construct a new ThingSpeak Publisher object.
ThingSpeakPublisher(Logger& baseLogger, const char* thingSpeakClientName, const char* thingSpeakMQTTUser, const char* thingSpeakMQTTPassword, const char* thingSpeakChannelID, int sendEveryX = 1)
Construct a new ThingSpeak Publisher object.
ThingSpeakPublisher(Logger& baseLogger, Client* inClient, const char* thingSpeakClientName, const char* thingSpeakMQTTUser, const char* thingSpeakMQTTPassword, const char* thingSpeakChannelID, int sendEveryX = 1)
Construct a new ThingSpeak Publisher object.
~ThingSpeakPublisher() virtual
Destroy the ThingSpeak Publisher object.

Public functions

String getEndpoint(void) -> String override
Get the destination for published data - generally the host name of the data receiver.
void setMQTTClient(const char* thingSpeakClientName)
The client name for your MQTT device. This is probably the same as your MQTT device's user name.
void setUserName(const char* thingSpeakMQTTUser)
Set the user name for your MQTT device.
void setPassword(const char* thingSpeakMQTTPassword)
Set the password for your MQTT device.
void setChannelID(const char* thingSpeakChannelID)
Set the ThingSpeak channel ID.
void setRESTAPIKey(const char* thingSpeakAPIKey)
Set the ThingSpeak user REST API key.
void setThingSpeakParams(const char* thingSpeakClientName, const char* thingSpeakMQTTUser, const char* thingSpeakMQTTPassword, const char* thingSpeakChannelID)
Sets all 4 ThingSpeak parameters.
void begin(Logger& baseLogger, Client* inClient, const char* thingSpeakClientName, const char* thingSpeakMQTTUser, const char* thingSpeakMQTTPassword, const char* thingSpeakChannelID)
Begin the publisher - linking it to the client and logger.
void begin(Logger& baseLogger, const char* thingSpeakClientName, const char* thingSpeakMQTTUser, const char* thingSpeakMQTTPassword, const char* thingSpeakChannelID)
Begin the publisher - linking it to the logger but not attaching a client.
int16_t publishData(Client* outClient, bool forceFlush = false) -> int16_t override
Utilize an attached modem to open a TCP connection to ThingSpeak and publish data over that connection.
int16_t publishMetadata(Client* outClient) -> int16_t override
Open a socket to the correct receiver and send out metadata about the current logger setup.

Portions of the MQTT data publication

static const char* apiHost protected
The REST API host.
static const int apiPort protected
The REST API port.
static const char* channelMetaResource protected
The REST API resource to put metadata to.
static const char* mqttServer protected
The MQTT server.
static const int mqttPort protected
The MQTT port.

Function documentation

ThingSpeakPublisher::ThingSpeakPublisher(Logger& baseLogger, int sendEveryX = 1) explicit

Construct a new ThingSpeak Publisher object.

Parameters
baseLogger The logger supplying the data to be published
sendEveryX Interval (in units of the logging interval) between attempted data transmissions. NOTE: not implemented by this publisher!

ThingSpeakPublisher::ThingSpeakPublisher(Logger& baseLogger, Client* inClient, int sendEveryX = 1)

Construct a new ThingSpeak 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 Interval (in units of the logging interval) between attempted data transmissions. NOTE: not implemented by this publisher!

ThingSpeakPublisher::ThingSpeakPublisher(Logger& baseLogger, const char* thingSpeakClientName, const char* thingSpeakMQTTUser, const char* thingSpeakMQTTPassword, const char* thingSpeakChannelID, int sendEveryX = 1)

Construct a new ThingSpeak Publisher object.

Parameters
baseLogger The logger supplying the data to be published
thingSpeakClientName The client name for your MQTT device. This is probably the same as your MQTT device's user name.
thingSpeakMQTTUser The user name for your MQTT device. This is probably the same as your MQTT device's client name.
thingSpeakMQTTPassword The password for your MQTT device.
thingSpeakChannelID The numeric channel id for your channel.
sendEveryX Interval (in units of the logging interval) between attempted data transmissions. NOTE: not implemented by this publisher!

ThingSpeakPublisher::ThingSpeakPublisher(Logger& baseLogger, Client* inClient, const char* thingSpeakClientName, const char* thingSpeakMQTTUser, const char* thingSpeakMQTTPassword, const char* thingSpeakChannelID, int sendEveryX = 1)

Construct a new ThingSpeak 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
thingSpeakClientName The client name for your MQTT device. This is probably the same as your MQTT device's user name.
thingSpeakMQTTUser The user name for your MQTT device. This is probably the same as your MQTT device's client name.
thingSpeakMQTTPassword The password for your MQTT device.
thingSpeakChannelID The numeric channel id for your channel.
sendEveryX Interval (in units of the logging interval) between attempted data transmissions. NOTE: not implemented by this publisher!

String ThingSpeakPublisher::getEndpoint(void) override

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

Returns String The URL or HOST to receive published data

void ThingSpeakPublisher::setMQTTClient(const char* thingSpeakClientName)

The client name for your MQTT device. This is probably the same as your MQTT device's user name.

Parameters
thingSpeakClientName The client name for your MQTT device. This is probably the same as your MQTT device's user name.

void ThingSpeakPublisher::setUserName(const char* thingSpeakMQTTUser)

Set the user name for your MQTT device.

Parameters
thingSpeakMQTTUser The user name for your MQTT device. This is probably the same as your MQTT device's client name.

void ThingSpeakPublisher::setPassword(const char* thingSpeakMQTTPassword)

Set the password for your MQTT device.

Parameters
thingSpeakMQTTPassword The password for your MQTT device.

void ThingSpeakPublisher::setChannelID(const char* thingSpeakChannelID)

Set the ThingSpeak channel ID.

Parameters
thingSpeakChannelID The numeric channel id for your channel

void ThingSpeakPublisher::setRESTAPIKey(const char* thingSpeakAPIKey)

Set the ThingSpeak user REST API key.

Parameters
thingSpeakAPIKey The ThingSpeak user REST API key

This is only used to update the field names on the ThingSpeak channel during the updateMetadata call at boot up. The user REST API key is NOT the same as your channel API keys or your MQTT connection credentials. Find this key in your ThingSpeak account under Account > My Profile.


void ThingSpeakPublisher::setThingSpeakParams(const char* thingSpeakClientName, const char* thingSpeakMQTTUser, const char* thingSpeakMQTTPassword, const char* thingSpeakChannelID)

Sets all 4 ThingSpeak parameters.

Parameters
thingSpeakClientName The client name for your MQTT device. This is probably the same as your MQTT device's user name.
thingSpeakMQTTUser The user name for your MQTT device. This is probably the same as your MQTT device's client name.
thingSpeakMQTTPassword The password for your MQTT device.
thingSpeakChannelID The numeric channel id for your channel.

void ThingSpeakPublisher::begin(Logger& baseLogger, Client* inClient, const char* thingSpeakClientName, const char* thingSpeakMQTTUser, const char* thingSpeakMQTTPassword, const char* thingSpeakChannelID)

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
thingSpeakClientName The client name for your MQTT device. This is probably the same as your MQTT device's user name.
thingSpeakMQTTUser The user name for your MQTT device. This is probably the same as your MQTT device's client name.
thingSpeakMQTTPassword The password for your MQTT device.
thingSpeakChannelID The numeric channel id for your channel.

This can be used as an alternative to adding the logger and client in the constructor.


void ThingSpeakPublisher::begin(Logger& baseLogger, const char* thingSpeakClientName, const char* thingSpeakMQTTUser, const char* thingSpeakMQTTPassword, const char* thingSpeakChannelID)

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

Parameters
baseLogger The logger supplying the data to be published
thingSpeakClientName The client name for your MQTT device. This is probably the same as your MQTT device's user name.
thingSpeakMQTTUser The user name for your MQTT device. This is probably the same as your MQTT device's client name.
thingSpeakMQTTPassword The password for your MQTT device.
thingSpeakChannelID The numeric channel id for your channel.

This can be used as an alternative to adding the logger and client in the constructor.


int16_t ThingSpeakPublisher::publishData(Client* outClient, bool forceFlush = false) override

Utilize an attached modem to open a TCP connection to ThingSpeak and publish data over that connection.

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
forceFlush Ask the publisher to flush buffered data immediately.
Returns int16_t The PubSubClient status code of the response.

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


int16_t ThingSpeakPublisher::publishMetadata(Client* outClient) override

Open a socket to the correct receiver and send out metadata about the current logger setup.

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 is to be used only when the logger is starting up! As the metadata doesn't generally change after start up, there's no reason to call this function any other time. There is no sensor data included in this metadata! If there is metadata that has to be included with each data post, that should be included in the publishData function. Because there is no sensor data included in this metadata and it's only to be called at boot, there's no forceFlush option for this function.

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