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* thingSpeakMQTTKey, const char* thingSpeakChannelID, const char* thingSpeakChannelKey, int sendEveryX = 1)
Construct a new ThingSpeak Publisher object.
ThingSpeakPublisher(Logger& baseLogger, Client* inClient, const char* thingSpeakMQTTKey, const char* thingSpeakChannelID, const char* thingSpeakChannelKey, 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 setMQTTKey(const char* thingSpeakMQTTKey)
Set the MQTT API Key from Account > MyProfile.
void setChannelID(const char* thingSpeakChannelID)
Set the ThingSpeak channel ID.
void setChannelKey(const char* thingSpeakChannelKey)
Set the channel's write API key.
void setThingSpeakParams(const char* MQTTKey, const char* channelID, const char* channelKey)
Sets all 3 ThingSpeak parameters.
void begin(Logger& baseLogger, Client* inClient, const char* thingSpeakMQTTKey, const char* thingSpeakChannelID, const char* thingSpeakChannelKey)
Begin the publisher - linking it to the client and logger.
void begin(Logger& baseLogger, const char* thingSpeakMQTTKey, const char* thingSpeakChannelID, const char* thingSpeakChannelKey)
Begin the publisher - linking it to the logger but not attaching a client.
int16_t publishData(Client* outClient) -> int16_t override
Open a socket to the correct receiver and sends out the formatted data.

Portions of the MQTT data publication

static const char* mqttServer protected
The MQTT server.
static const int mqttPort protected
The MQTT port.
static const char* mqttClientName protected
The MQTT client name.
static const char* mqttUser protected
The MQTT user name.

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* thingSpeakMQTTKey, const char* thingSpeakChannelID, const char* thingSpeakChannelKey, int sendEveryX = 1)

Construct a new ThingSpeak Publisher object.

Parameters
baseLogger The logger supplying the data to be published
thingSpeakMQTTKey Your MQTT API Key from Account > MyProfile.
thingSpeakChannelID The numeric channel id for your channel
thingSpeakChannelKey The write API key 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* thingSpeakMQTTKey, const char* thingSpeakChannelID, const char* thingSpeakChannelKey, 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
thingSpeakMQTTKey Your MQTT API Key from Account > MyProfile.
thingSpeakChannelID The numeric channel id for your channel
thingSpeakChannelKey The write API key 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::setMQTTKey(const char* thingSpeakMQTTKey)

Set the MQTT API Key from Account > MyProfile.

Parameters
thingSpeakMQTTKey Your MQTT API Key from Account > MyProfile.

void ThingSpeakPublisher::setChannelID(const char* thingSpeakChannelID)

Set the ThingSpeak channel ID.

Parameters
thingSpeakChannelID The numeric channel id for your channel

void ThingSpeakPublisher::setChannelKey(const char* thingSpeakChannelKey)

Set the channel's write API key.

Parameters
thingSpeakChannelKey The write API key for your channel

void ThingSpeakPublisher::setThingSpeakParams(const char* MQTTKey, const char* channelID, const char* channelKey)

Sets all 3 ThingSpeak parameters.

Parameters
MQTTKey Your MQTT API Key from Account > MyProfile.
channelID The numeric channel id for your channel
channelKey The write API key for your channel

void ThingSpeakPublisher::begin(Logger& baseLogger, Client* inClient, const char* thingSpeakMQTTKey, const char* thingSpeakChannelID, const char* thingSpeakChannelKey)

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
thingSpeakMQTTKey Your MQTT API Key from Account > MyProfile.
thingSpeakChannelID The numeric channel id for your channel
thingSpeakChannelKey The write API key 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* thingSpeakMQTTKey, const char* thingSpeakChannelID, const char* thingSpeakChannelKey)

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

Parameters
baseLogger The logger supplying the data to be published
thingSpeakMQTTKey Your MQTT API Key from Account > MyProfile.
thingSpeakChannelID The numeric channel id for your channel
thingSpeakChannelKey The write API key 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) override

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.