AWS_IoT_Publisher class

The AWS_IoT_Publisher subclass of dataPublisher for publishing data to AWS IoT Core using the MQTT protocol.

Base classes

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

Constructors, destructors, conversion operators

AWS_IoT_Publisher()
Construct a new AWS IoT Core Publisher object with no members initialized.
AWS_IoT_Publisher(Logger& baseLogger, int sendEveryX = 1) explicit
Construct a new AWS IoT Core Publisher object.
AWS_IoT_Publisher(Logger& baseLogger, Client* inClient, int sendEveryX = 1)
Construct a new AWS IoT Core Publisher object.
AWS_IoT_Publisher(Logger& baseLogger, const char* awsIoTEndpoint, const char* caCertName, const char* clientCertName, const char* clientKeyName, const char* samplingFeatureUUID, int sendEveryX = 1)
Construct a new AWS IoT Core Publisher object.
AWS_IoT_Publisher(Logger& baseLogger, const char* awsIoTEndpoint, const char* caCertName, const char* clientCertName, const char* clientKeyName, int sendEveryX = 1)
Construct a new AWS IoT Core Publisher object.
AWS_IoT_Publisher(Logger& baseLogger, Client* inClient, const char* awsIoTEndpoint, const char* samplingFeatureUUID, int sendEveryX = 1)
Construct a new AWS IoT Core Publisher object.
~AWS_IoT_Publisher() virtual
Destroy the AWS IoT Core Publisher object.

Public functions

String getEndpoint(void) -> String override
Get the destination for published data - generally the host name of the data receiver.
void setEndpoint(const char* awsIoTEndpoint)
Set the endpoint for your AWS IoT instance.
void setCACertName(const char* caCertName)
Set the name of your certificate authority certificate file.
void setClientCertName(const char* clientCertName)
Set the name of your client certificate file.
void setClientKeyName(const char* clientKeyName)
Set the name of your client private key file.
void setAWSIoTParams(const char* awsIoTEndpoint, const char* caCertName, const char* clientCertName, const char* clientKeyName)
Sets all of the AWS IoT Core parameters.
void setDataPublishTopic(const char* topic)
Set the topic to use for publishing data.
void setMetadataPublishTopic(const char* topic)
Set the topic to use for publishing metadata.
void addSubTopic(const char* topic)
Adds a topic to subscribe to.
void removeSubTopic(const char* topic)
Removes a topic from the subscription list.
void addPublishRequest(const char* topic, String(*)(void) contentGetrFxn)
Adds a topic to subscribe to.
void removePublishRequest(const char* topic)
Removes a topic from the publish list.
PubSubClient& setCallback(MQTT_CALLBACK_SIGNATURE ) -> PubSubClient&
A passthrough to the PubSubClient setCallback function; sets a function to call back when data is received on any of the subscribed topics.
void closeConnection()
Calling this function tells the AWS IoT Publisher to immediately close its connection.
void begin(Logger& baseLogger, Client* inClient, const char* awsIoTEndpoint, const char* samplingFeatureUUID)
Begin the publisher - linking it to the client and logger.
void begin(Logger& baseLogger, Client* inClient, const char* awsIoTEndpoint)
Begin the publisher - linking it to the client and logger.
void begin(Logger& baseLogger, const char* awsIoTEndpoint, const char* caCertName, const char* clientCertName, const char* clientKeyName, const char* samplingFeatureUUID)
Begin the publisher - linking it to the logger but not attaching a client.
void begin(Logger& baseLogger, const char* awsIoTEndpoint, const char* caCertName, const char* clientCertName, const char* clientKeyName)
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 SSL connection to AWS IoT Core 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.

Protected functions

Client* createClient() -> Client* override
Use the connected base logger's logger modem and underlying TinyGSM instance to create a new client for the publisher.
void deleteClient(Client* client) override
Delete a created client. We need to pass this through to avoid a memory leak because we cannot delete from the pointer because the destructor for a client in the Arduino core isn't virtual.

Protected static variables

static const int mqttPort
The MQTT port; this is always 8883 for AWS IoT Core MQTT connections.
static const char* samplingFeatureTag
The JSON feature UUID tag.
static const char* timestampTag
The JSON feature timestamp tag.

Function documentation

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

Construct a new AWS IoT Core 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!

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

Construct a new AWS IoT Core 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!

AWS_IoT_Publisher::AWS_IoT_Publisher(Logger& baseLogger, const char* awsIoTEndpoint, const char* caCertName, const char* clientCertName, const char* clientKeyName, const char* samplingFeatureUUID, int sendEveryX = 1)

Construct a new AWS IoT Core Publisher object.

Parameters
baseLogger The logger supplying the data to be published
awsIoTEndpoint The endpoint for your AWS IoT instance
caCertName The name of your certificate authority certificate file
clientCertName The name of your client certificate file
clientKeyName The name of your client private key file
samplingFeatureUUID The sampling feature UUID for the site on the Monitor My Watershed data portal.
sendEveryX Interval (in units of the logging interval) between attempted data transmissions. NOTE: not implemented by this publisher!

AWS_IoT_Publisher::AWS_IoT_Publisher(Logger& baseLogger, const char* awsIoTEndpoint, const char* caCertName, const char* clientCertName, const char* clientKeyName, int sendEveryX = 1)

Construct a new AWS IoT Core Publisher object.

Parameters
baseLogger The logger supplying the data to be published
awsIoTEndpoint The endpoint for your AWS IoT instance
caCertName The name of your certificate authority certificate file
clientCertName The name of your client certificate file
clientKeyName The name of your client private key file
sendEveryX Interval (in units of the logging interval) between attempted data transmissions. NOTE: not implemented by this publisher!

AWS_IoT_Publisher::AWS_IoT_Publisher(Logger& baseLogger, Client* inClient, const char* awsIoTEndpoint, const char* samplingFeatureUUID, int sendEveryX = 1)

Construct a new AWS IoT Core 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
awsIoTEndpoint The endpoint for your AWS IoT instance
samplingFeatureUUID The sampling feature UUID for the site on the Monitor My Watershed data portal.
sendEveryX Interval (in units of the logging interval) between attempted data transmissions. NOTE: not implemented by this publisher!

String AWS_IoT_Publisher::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 AWS_IoT_Publisher::setEndpoint(const char* awsIoTEndpoint)

Set the endpoint for your AWS IoT instance.

Parameters
awsIoTEndpoint The endpoint for your AWS IoT instance.

Get the broker host/endpoint from AWS IoT Core > Connect > Domain Configurations. The endpoint will be something like "xxx-ats.iot.your-region.amazonaws.com".


void AWS_IoT_Publisher::setCACertName(const char* caCertName)

Set the name of your certificate authority certificate file.

Parameters
caCertName The name of your certificate authority certificate file.

You MUST have already uploaded your certificate to your modem. This will most likely be the Amazon Root CA 1 (RSA 2048 bit key) certificate. You can find Amazon's current CA certificates here: https://docs.aws.amazon.com/iot/latest/developerguide/server-authentication.html. Depending on your module, you may instead need a certificate chain file or to use Amazon's older top-chain certificate (Starfield Services Root Certificate Authority - G2).

This is exactly the same CA certificate as you would use to upload to S3 (ie, the S3 Presigned Publisher). For supported modules you can use the AWS_IOT_SetCertificates sketch in the extras folder to upload your certificate.


void AWS_IoT_Publisher::setClientCertName(const char* clientCertName)

Set the name of your client certificate file.

Parameters
clientCertName The name of your client certificate file.

You MUST have already uploaded your certificate to your modem. You must download the certificate when you create it. Your certificate should be tied to a security policy that allows connection, publishing, and subscribing by thing name.

For supported modules you can use the AWS_IOT_SetCertificates sketch in the extras folder to upload your certificate.


void AWS_IoT_Publisher::setClientKeyName(const char* clientKeyName)

Set the name of your client private key file.

Parameters
clientKeyName The name of your client private key file.

You MUST have have already uploaded your certificate to your modem. You must download the certificate when you create it. Your certificate should be tied to a security policy that allows connection, publishing, and subscribing by thing name.

For supported modules you can use the AWS_IOT_SetCertificates sketch in the extras folder to upload your certificate.


void AWS_IoT_Publisher::setAWSIoTParams(const char* awsIoTEndpoint, const char* caCertName, const char* clientCertName, const char* clientKeyName)

Sets all of the AWS IoT Core parameters.

Parameters
awsIoTEndpoint The endpoint for your AWS IoT instance
caCertName The name of your certificate authority certificate file
clientCertName The name of your client certificate file
clientKeyName The name of your client private key file

void AWS_IoT_Publisher::setDataPublishTopic(const char* topic)

Set the topic to use for publishing data.

Parameters
topic The topic

If not specified, the topic "{LoggerID}/{SamplingFeatureUUID}" will be used.

Make sure you have IAM policies set up to allow your device to publish to the specified topic!


void AWS_IoT_Publisher::setMetadataPublishTopic(const char* topic)

Set the topic to use for publishing metadata.

Parameters
topic The topic

If not specified, the topic "{LoggerID}/metadata" will be used for the main logger metadata. For each variable, the variable number will be appended to the topic (ie, "{LoggerID}/metadata/variable01").

Make sure you have IAM policies set up to allow your device to publish to the specified topics!


void AWS_IoT_Publisher::addSubTopic(const char* topic)

Adds a topic to subscribe to.

Parameters
topic The topic to subscribe to

When publishing data, the publisher will subscribe to the topic.


void AWS_IoT_Publisher::removeSubTopic(const char* topic)

Removes a topic from the subscription list.

Parameters
topic The topic to remove from the subscription list

I don't expect this to be used, but I'm adding it just in case.


void AWS_IoT_Publisher::addPublishRequest(const char* topic, String(*)(void) contentGetrFxn)

Adds a topic to subscribe to.

Parameters
topic The topic to publish to. This should be a static value. If you wish to change the topic, remove the old topic and add a new one.
contentGetrFxn A function to call to get the content to publish. The function should return a pointer to a char array.

When publishing data, the publisher will subscribe to the topic.


void AWS_IoT_Publisher::removePublishRequest(const char* topic)

Removes a topic from the publish list.

Parameters
topic The topic to remove from the publish list

PubSubClient& AWS_IoT_Publisher::setCallback(MQTT_CALLBACK_SIGNATURE )

A passthrough to the PubSubClient setCallback function; sets a function to call back when data is received on any of the subscribed topics.

Returns PubSubClient& A reference to the underlying pubsubclient instance; can be used to chain actions.

This must be a static or main level function! It cannot be a member function of a class! Your callback function should call this publishers AWS_IoT_Publisher::closeConnection() method immediately after getting your desired response to stop the publisher from waiting for more messages.


void AWS_IoT_Publisher::begin(Logger& baseLogger, Client* inClient, const char* awsIoTEndpoint, const char* samplingFeatureUUID)

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
awsIoTEndpoint The endpoint for your AWS IoT instance
samplingFeatureUUID The sampling feature UUID for the site on the Monitor My Watershed data portal.

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


void AWS_IoT_Publisher::begin(Logger& baseLogger, Client* inClient, const char* awsIoTEndpoint)

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
awsIoTEndpoint The endpoint for your AWS IoT instance

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


void AWS_IoT_Publisher::begin(Logger& baseLogger, const char* awsIoTEndpoint, const char* caCertName, const char* clientCertName, const char* clientKeyName, const char* samplingFeatureUUID)

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

Parameters
baseLogger The logger supplying the data to be published
awsIoTEndpoint The endpoint for your AWS IoT instance
caCertName The name of your certificate authority certificate file
clientCertName The name of your client certificate file
clientKeyName The name of your client private key file
samplingFeatureUUID The sampling feature UUID for the site on the Monitor My Watershed data portal.

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


void AWS_IoT_Publisher::begin(Logger& baseLogger, const char* awsIoTEndpoint, const char* caCertName, const char* clientCertName, const char* clientKeyName)

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

Parameters
baseLogger The logger supplying the data to be published
awsIoTEndpoint The endpoint for your AWS IoT instance
caCertName The name of your certificate authority certificate file
clientCertName The name of your client certificate file
clientKeyName The name of your client private key file

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


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

Utilize an attached modem to open a SSL connection to AWS IoT Core 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 AWS_IoT_Publisher::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.


Client* AWS_IoT_Publisher::createClient() override protected

Use the connected base logger's logger modem and underlying TinyGSM instance to create a new client for the publisher.

Returns Client* A pointer to an Arduino client instance

void AWS_IoT_Publisher::deleteClient(Client* client) override protected

Delete a created client. We need to pass this through to avoid a memory leak because we cannot delete from the pointer because the destructor for a client in the Arduino core isn't virtual.

Parameters
client The client to delete