Connectino Library
|
MQTT module. More...
#include <ConnectinoMqtt.h>
Public Member Functions | |
ConnectinoMqtt (StxHwSerial &refSerial) | |
MQTT module constructor. More... | |
bool | moduleCallback (uint8_t subId, uint8_t reqId, uint16_t dataSize, uint8_t *data) |
Callback function for serial frames of the MQTT module. More... | |
uint8_t | getModId () |
Get the ID of the MQTT module. More... | |
bool | setSessionConfiguration (const char server[], uint16_t port, const char resource[]="", const char clientId[]="", const char userName[]="", const char password[]="", uint8_t connectionType=0, bool cleanSession=true, uint16_t keepaliveInterval=60) |
Set the session configuration. More... | |
void | setWill (const char topic[], const char payload[], const bool retain=false, const uint8_t QoS=0) |
Set a string as the last will. More... | |
void | setWill (const char topic[], const uint8_t payload[], const uint8_t payloadSize, const bool retain=false, const uint8_t QoS=0) |
Set binary data as the last will. More... | |
bool | publish (const char topic[], const char payload[], const bool retain=false, const uint8_t QoS=0) |
Publish a string on a topic. More... | |
bool | publish (const char topic[], const uint8_t payload[], const uint16_t payloadSize, const bool retain=false, const uint8_t QoS=0) |
Publish binary data on a topic. More... | |
void | subscribe (const char topic[], const uint8_t QoS=0) |
Subscribe to a topic. More... | |
bool | isDataAvailable (const char *topic=NULL) |
Check for available data. More... | |
bool | getData (char *payloadBuffer, uint16_t &payloadBufferSize, char *topicBuffer=NULL, uint16_t topicBufferSize=0) |
Receive data from the message buffer. More... | |
bool | getData (uint8_t *payloadBuffer, uint16_t &payloadBufferSize, char *topicBuffer=NULL, uint16_t topicBufferSize=0) |
Receive data from the message buffer. More... | |
bool | isConnected () |
Check connection of the MQTT module. More... | |
void | setState (bool state) |
Set the state of the MQTT module. More... | |
Public Member Functions inherited from ConnectinoModule | |
void | begin () |
Starts the module. | |
Additional Inherited Members | |
Protected Member Functions inherited from ConnectinoModule | |
bool | append (uint8_t **buffer, uint16_t *bufferSize, const char data[], const uint8_t numOfSizeBytes=1) |
Append a string to the frames payload. More... | |
bool | append (uint8_t **buffer, uint16_t *bufferSize, const uint8_t data[], const uint16_t dataSize, const uint8_t numOfSizeBytes=1) |
Append binary data to the frames payload. More... | |
bool | append (uint8_t **buffer, uint16_t *bufferSize, const bool data) |
Append a boolean to the frames payload. More... | |
bool | append (uint8_t **buffer, uint16_t *bufferSize, const uint8_t data) |
Append a uint8_t variable to the frames payload. More... | |
bool | append (uint8_t **buffer, uint16_t *bufferSize, const uint16_t data) |
Append a uint16_t variable to the frames payload. More... | |
bool | append (uint8_t **buffer, uint16_t *bufferSize, const uint32_t data) |
Append a uint32_t variable to the frames payload. More... | |
MQTT module.
The MQTT module can be used to publish data on MQTT servers. Therefore the module needs to be configured with session configuration. When configured the module will connect and data can be published and subscriptions to topics can be set. There is also the possibility to configure a last will message which will be triggered when the module is disconnected ungracefully.
ConnectinoMqtt::ConnectinoMqtt | ( | StxHwSerial & | refSerial | ) |
MQTT module constructor.
Instantiates the MQTT module and connects it to the hardware serial.
refSerial | A reference to the hardware serial. |
|
virtual |
Callback function for serial frames of the MQTT module.
This function will be called for messages sent by the WiFi module which are targeted at the MQTT module.
subId | Sub ID of the message. |
reqId | Request ID of the message. |
dataSize | Size of the received data. |
data | Reference to the received data. |
Returns | whether or not to block the current frame. |
Implements ConnectinoModule.
|
virtual |
Get the ID of the MQTT module.
This function will return the module ID of the MQTT module defined in MODULE.h.
The | module ID. |
Implements ConnectinoModule.
bool ConnectinoMqtt::setSessionConfiguration | ( | const char | server[], |
uint16_t | port, | ||
const char | resource[] = "" , |
||
const char | clientId[] = "" , |
||
const char | userName[] = "" , |
||
const char | password[] = "" , |
||
uint8_t | connectionType = 0 , |
||
bool | cleanSession = true , |
||
uint16_t | keepaliveInterval = 60 |
||
) |
Set the session configuration.
This function will configure the session configuration of the MQTT module.
server | Server address. |
port | Server port. |
resource | Resource for connection. |
clientId | Client ID. |
userName | Optional user name for connection. |
password | Optional password for connection. |
connectionType | Optional connection type 0 - Automatic mode 1 - do not use TLS 2 - use TLS |
cleanSession | Optional flag whether to start a clean session. |
keepaliveInterval | Optional interval between keepalive messages, use 0 to disable. |
True | if success, false otherwise. |
void ConnectinoMqtt::setWill | ( | const char | topic[], |
const char | payload[], | ||
const bool | retain = false , |
||
const uint8_t | QoS = 0 |
||
) |
Set a string as the last will.
This function will set the last will for a specific topic. It will be published on the configured topic when the client disconnects ungracefully.
topic | Topic on which the last will message is published. |
payload | The message which is sent as the last will. |
retain | Optional flag whether the will shall be retained. |
QoS | Optional quality of service: 0 - Deliver at most once 1 - Deliver at least once 2 - Deliver exactly once |
void ConnectinoMqtt::setWill | ( | const char | topic[], |
const uint8_t | payload[], | ||
const uint8_t | payloadSize, | ||
const bool | retain = false , |
||
const uint8_t | QoS = 0 |
||
) |
Set binary data as the last will.
This function will set the last will for a specific topic. It will be published on the configured topic when the client disconnects ungracefully.
topic | Topic on which the last will message is published. |
payload | The binary data which is sent as the last will. |
payloadSize | Size of the payload. |
retain | Optional flag whether the will shall be retained. |
QoS | Optional quality of service: 0 - Deliver at most once 1 - Deliver at least once 2 - Deliver exactly once |
bool ConnectinoMqtt::publish | ( | const char | topic[], |
const char | payload[], | ||
const bool | retain = false , |
||
const uint8_t | QoS = 0 |
||
) |
Publish a string on a topic.
This function will publish the given string on the given topic.
topic | Topic on which the string is published. |
payload | The string which is published. |
retain | Optional flag whether the payload shall be retained. |
QoS | Optional quality of service: 0 - Deliver at most once 1 - Deliver at least once 2 - Deliver exactly once |
True | if success, false otherwise. |
bool ConnectinoMqtt::publish | ( | const char | topic[], |
const uint8_t | payload[], | ||
const uint16_t | payloadSize, | ||
const bool | retain = false , |
||
const uint8_t | QoS = 0 |
||
) |
Publish binary data on a topic.
This function will publish the given binary data on the given topic.
topic | Topic on which the binary data is published. |
payload | The binary data which is published. |
payloadSize | Size of the payload. |
retain | Optional flag whether the payload shall be retained. |
QoS | Optional quality of service: 0 - Deliver at most once 1 - Deliver at least once 2 - Deliver exactly once |
True | if success, false otherwise. |
void ConnectinoMqtt::subscribe | ( | const char | topic[], |
const uint8_t | QoS = 0 |
||
) |
Subscribe to a topic.
This function will subscribe the MQTT module to a topic. Messages published to this topic will be kept in a buffer. If a message is available in the buffer can be checked with isDataAvailable(). The data can then be read out with getData(). When the message was read out with getData() the buffer is ready to receive the next message.
topic | Topic which should be subscribed. |
QoS | Optional quality of service: 0 - Deliver at most once 1 - Deliver at least once 2 - Deliver exactly once |
bool ConnectinoMqtt::isDataAvailable | ( | const char * | topic = NULL | ) |
Check for available data.
With this function the user can check the availability of data in the receive buffer. If a topic name is provided with this function call the availability of a message for this specific topic is checked. If no topic name is given availability for any topic is checked.
topic | Optional topic name on which to check for available data. |
True | if data is available, false otherwise. |
bool ConnectinoMqtt::getData | ( | char * | payloadBuffer, |
uint16_t & | payloadBufferSize, | ||
char * | topicBuffer = NULL , |
||
uint16_t | topicBufferSize = 0 |
||
) |
Receive data from the message buffer.
With this function the user can read out the message in the message buffer. The data is copied to the given buffer and the name of the topic the message was received on is optionally written to the given buffer.
payloadBuffer | Buffer where the payload is copied to. |
payloadBufferSize | Size of the payload buffer. Will be set to the number of bytes written. |
topicBuffer | Optional buffer where the topic name is copied to. |
topicBufferSize | Optional size of the buffer for the topic name. |
True | if success, false otherwise. |
bool ConnectinoMqtt::getData | ( | uint8_t * | payloadBuffer, |
uint16_t & | payloadBufferSize, | ||
char * | topicBuffer = NULL , |
||
uint16_t | topicBufferSize = 0 |
||
) |
Receive data from the message buffer.
With this function the user can read out the message in the message buffer. The data is copied to the given buffer and the name of the topic the message was received on is optionally written to the given buffer.
payloadBuffer | Buffer where the payload is copied to. |
payloadBufferSize | Size of the payload buffer. Will be set to the number of bytes written. |
topicBuffer | Optional buffer where the topic name is copied to. |
topicBufferSize | Optional size of the buffer for the topic name. |
True | if success, false otherwise. |
bool ConnectinoMqtt::isConnected | ( | void | ) |
Check connection of the MQTT module.
Checks whether or not the MQTT module is connected to the configured server.
True | if connected, false otherwise. |
void ConnectinoMqtt::setState | ( | bool | state | ) |
Set the state of the MQTT module.
With this function the user can activate and deactivate the MQTT module. When the state is set to 1, the module starts a session on the configured server.
state | State the module is set to: false - off true - on |