Back to overview 4.3

Using MQTT

For IoT applications one, if not the most common communication standard is Message Queuing Telemetry Transport [MQTT]. As the Connectino has its IoT controller, you can easily use it as a MQTT client.

It is always essential to include the ConnectinoMqtt-module, to initialize an instance of the module and to setup the StxSerial. The software automatically exectues the start()-function at the start of the ATmega. You can use the loop() to continuously execute functions or test parameters.

If you want to connect to a cloud service we already got some basic examples for Amazon AWS, Microsoft Azure, IBM Watson and Arrow Connect.

Enable and configure module

Download Example: ConnectinoMqtt.ino

	#include <ConnectinoMqtt.h> // include the ConnectinoMqtt-library

	// initialize the mqtt-module on the UART
	ConnectinoMqtt mqtt = ConnectinoMqtt(StxSerial);

	void setup(){
		// enable the uart and add communication via mqtt-module
		StxSerial.begin();
		StxSerial.add(mqtt);

		debug.begin(9600); // start the serial output

		mqtt.setState(0); // turn off mqtt module

		// set MQTT Configuration to connect to MQTT-server
		mqtt.setSessionConfiguration(
			192.168.x.y, // serveradress
			1883, // port (8883 for TLS)
			"", // resource 
			"", // clientId
			"peter", // username
			"P455W0RD" // password
			); 

		mqtt.setState(1); // turn on mqtt
	}
	void loop(){}
	

On this example you disable the mqtt-module, set the configuration and re-enable it.

Publish on topic

	#include <ConnectinoMqtt.h> // include the ConnectinoMqtt-library

	// initialize the mqtt-module on the UART
	ConnectinoMqtt mqtt = ConnectinoMqtt(StxSerial);

	void setup(){
		// enable the uart and add communication via mqtt-module
		StxSerial.begin();
		StxSerial.add(mqtt);

		debug.begin(9600); // start the serial output

		mqtt.setState(0); // turn off mqtt module

		// set MQTT Configuration to connect to MQTT-server
		mqtt.setSessionConfiguration(
			192.168.x.y, // serveradress
			1883, // port (8883 for TLS)
			"", // resource 
			"", // clientId
			"peter", // username
			"P455W0RD" // password
			); 

		mqtt.setState(1); // turn on mqtt
		mqtt.publish("your/debug/publish","connected!"); // debug
	}
	void loop(){ // praises you every 2 seconds
		delay(2000);
		mqtt.publish("your/praising","You are awesome!");
	}
	

When enabled after reconfiguration you publish “connected!” on the first topic and then every two seconds you publish “You are awesome!” on the second topic.

Subscribe to topic

	#include <ConnectinoMqtt.h> // include the ConnectinoMqtt-library

	// initialize the mqtt-module on the UART
	ConnectinoMqtt mqtt = ConnectinoMqtt(StxSerial);

	void setup(){
		// enable the uart and add communication via mqtt-module
		StxSerial.begin();
		StxSerial.add(mqtt);

		debug.begin(9600); // start the serial output

		mqtt.setState(0); // turn off mqtt module

		// set MQTT Configuration to connect to MQTT-server
		mqtt.setSessionConfiguration(
			192.168.x.y, // serveradress
			1883, // port (8883 for TLS)
			"", // resource 
			"", // clientId
			"peter", // username
			"P455W0RD" // password
			); 

		mqtt.setState(1); // turn on mqtt

		mqtt.subscribe("subscription/topic"); // subscribe to topic

	}

	char msg[127]; // buffer to store received message in
	uint16_t size; // maximum message size / message size

	void loop(){

		if(mqtt.isDataAvailable())
		{
			// check if data was received on topic
			size = 127; // maximum buffer size
			mqtt.getData(msg, size);
			msg[size] = '\0'; // cut off chars after the last significant char

			// unsubscribe if received message was "stopit"
			if(strcmp(msg, "stopit") == 0)
				mqtt.unsubscribe(TOP_SOCI);
		}
	}
	

When enabled after reconfiguration you publish “connected!” on the first topic and then every two seconds you publish “You are awesome!” on the second topic.

Be aware! The subscribed topics stay configured on your IoT controller. Even if you upload another sketch. It could cause errors, if you change the mqtt server address and don’t adept the subscribed topics. To delete the topics either use unsubscribe() or perform a factory reset.

TUTORIALS