Connectino Library
Public Member Functions | List of all members
ConnectinoMqtt Class Reference

MQTT module. More...

#include <ConnectinoMqtt.h>

Inheritance diagram for ConnectinoMqtt:
ConnectinoModule

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

Detailed Description

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.

Constructor & Destructor Documentation

◆ ConnectinoMqtt()

ConnectinoMqtt::ConnectinoMqtt ( StxHwSerial &  refSerial)

MQTT module constructor.

Instantiates the MQTT module and connects it to the hardware serial.

Parameters
refSerialA reference to the hardware serial.

Member Function Documentation

◆ moduleCallback()

bool ConnectinoMqtt::moduleCallback ( uint8_t  subId,
uint8_t  reqId,
uint16_t  dataSize,
uint8_t *  data 
)
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.

Parameters
subIdSub ID of the message.
reqIdRequest ID of the message.
dataSizeSize of the received data.
dataReference to the received data.
Return values
Returnswhether or not to block the current frame.

Implements ConnectinoModule.

◆ getModId()

uint8_t ConnectinoMqtt::getModId ( )
virtual

Get the ID of the MQTT module.

This function will return the module ID of the MQTT module defined in MODULE.h.

Return values
Themodule ID.

Implements ConnectinoModule.

◆ setSessionConfiguration()

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.

Parameters
serverServer address.
portServer port.
resourceResource for connection.
clientIdClient ID.
userNameOptional user name for connection.
passwordOptional password for connection.
connectionTypeOptional connection type 0 - Automatic mode 1 - do not use TLS 2 - use TLS
cleanSessionOptional flag whether to start a clean session.
keepaliveIntervalOptional interval between keepalive messages, use 0 to disable.
Return values
Trueif success, false otherwise.
Note
Use the function setState(0) before you change the configuration and setState(1) afterwards to enable the module again.

◆ setWill() [1/2]

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.

Parameters
topicTopic on which the last will message is published.
payloadThe message which is sent as the last will.
retainOptional flag whether the will shall be retained.
QoSOptional quality of service: 0 - Deliver at most once 1 - Deliver at least once 2 - Deliver exactly once
Note
Needs to be set in advance to a session configuration.

◆ setWill() [2/2]

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.

Parameters
topicTopic on which the last will message is published.
payloadThe binary data which is sent as the last will.
payloadSizeSize of the payload.
retainOptional flag whether the will shall be retained.
QoSOptional quality of service: 0 - Deliver at most once 1 - Deliver at least once 2 - Deliver exactly once
Note
Needs to be set in advance to a session configuration.

◆ publish() [1/2]

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.

Parameters
topicTopic on which the string is published.
payloadThe string which is published.
retainOptional flag whether the payload shall be retained.
QoSOptional quality of service: 0 - Deliver at most once 1 - Deliver at least once 2 - Deliver exactly once
Return values
Trueif success, false otherwise.
Note
Needs to be set in advance to a session configuration.

◆ publish() [2/2]

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.

Parameters
topicTopic on which the binary data is published.
payloadThe binary data which is published.
payloadSizeSize of the payload.
retainOptional flag whether the payload shall be retained.
QoSOptional quality of service: 0 - Deliver at most once 1 - Deliver at least once 2 - Deliver exactly once
Return values
Trueif success, false otherwise.
Note
Needs to be set in advance to a session configuration.

◆ subscribe()

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.

Parameters
topicTopic which should be subscribed.
QoSOptional quality of service: 0 - Deliver at most once 1 - Deliver at least once 2 - Deliver exactly once

◆ isDataAvailable()

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.

Parameters
topicOptional topic name on which to check for available data.
Return values
Trueif data is available, false otherwise.

◆ getData() [1/2]

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.

Parameters
payloadBufferBuffer where the payload is copied to.
payloadBufferSizeSize of the payload buffer. Will be set to the number of bytes written.
topicBufferOptional buffer where the topic name is copied to.
topicBufferSizeOptional size of the buffer for the topic name.
Return values
Trueif success, false otherwise.

◆ getData() [2/2]

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.

Parameters
payloadBufferBuffer where the payload is copied to.
payloadBufferSizeSize of the payload buffer. Will be set to the number of bytes written.
topicBufferOptional buffer where the topic name is copied to.
topicBufferSizeOptional size of the buffer for the topic name.
Return values
Trueif success, false otherwise.

◆ isConnected()

bool ConnectinoMqtt::isConnected ( void  )

Check connection of the MQTT module.

Checks whether or not the MQTT module is connected to the configured server.

Return values
Trueif connected, false otherwise.

◆ setState()

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.

Parameters
stateState the module is set to: false - off true - on

The documentation for this class was generated from the following files: