ModularSensors > Classes > dataPublisher > ThingSpeakPublisher
ModularSensors > Modules > Supported Data Endpoints > ThingSpeakPublisher


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, uint8_t sendEveryX = 1, uint8_t sendOffset = 0) explicit
Construct a new ThingSpeak Publisher object.
ThingSpeakPublisher(Logger& baseLogger, Client* inClient, uint8_t sendEveryX = 1, uint8_t sendOffset = 0)
Construct a new ThingSpeak Publisher object.
ThingSpeakPublisher(Logger& baseLogger, const char* thingSpeakMQTTKey, const char* thingSpeakChannelID, const char* thingSpeakChannelKey, uint8_t sendEveryX = 1, uint8_t sendOffset = 0)
Construct a new ThingSpeak Publisher object.
ThingSpeakPublisher(Logger& baseLogger, Client* inClient, const char* thingSpeakMQTTKey, const char* thingSpeakChannelID, const char* thingSpeakChannelKey, uint8_t sendEveryX = 1, uint8_t sendOffset = 0)
Construct a new ThingSpeak Publisher object.
~ThingSpeakPublisher() virtual
Destroy the ThingSpeak Publisher object.

Public functions

String getEndpoint(void ) 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) 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(Logger& baseLogger, uint8_t sendEveryX = 1, uint8_t sendOffset = 0) explicit

Construct a new ThingSpeak Publisher object.

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

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

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 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

ThingSpeakPublisher(Logger& baseLogger, const char* thingSpeakMQTTKey, const char* thingSpeakChannelID, const char* thingSpeakChannelKey, uint8_t sendEveryX = 1, uint8_t sendOffset = 0)

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 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

ThingSpeakPublisher(Logger& baseLogger, Client* inClient, const char* thingSpeakMQTTKey, const char* thingSpeakChannelID, const char* thingSpeakChannelKey, uint8_t sendEveryX = 1, uint8_t sendOffset = 0)

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 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

StringgetEndpoint(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

voidsetMQTTKey(const char* thingSpeakMQTTKey)

Set the MQTT API Key from Account > MyProfile.

Parameters
thingSpeakMQTTKey Your MQTT API Key from Account > MyProfile.

voidsetChannelID(const char* thingSpeakChannelID)

Set the ThingSpeak channel ID.

Parameters
thingSpeakChannelID The numeric channel id for your channel

voidsetChannelKey(const char* thingSpeakChannelKey)

Set the channel's write API key.

Parameters
thingSpeakChannelKey The write API key for your channel

voidsetThingSpeakParams(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

voidbegin(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. 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.


voidbegin(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. 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.


int16_tpublishData(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.