This example is pretty similar to the IoTaaP – MQTT. But pure MQTT communication is not safe, that’s why we prefere to use MQTTS (MQTT + SSL). In this example we will use Let’s Encrypt’s Cross Signed certificate, IoTaaP Cloud MQTT broker instance (although everything should work with other MQTT brokers).


We must define our features before #include

In setup() function we wil configure WiFi and connect IoTaaP to WiFi AP (with internet access)

Now it’s time to initialize MQTT object and connect IoTaaP to our instance.

iotaap.mqtt.connect("iotaap_client", "", 8883, callback, true, "USERNAME", "PASSWORD", CA_cert);
This line of code will connect IoTaaP to the Mosquitto broker running on IoTaaP Cloud. Notice the new parameter here: CA_cert. In the IoTaaP – MQTT example secure parameter was set to false, now it’s set to true and we have to pass CA Certificate to the function.

We are using Let’s Encrypt Cross Signed certificate for this, if your MQTT broker provider is using different Certificate provider for their server instance, you will probably need to obtain the CA Certificate or Root certificate of their Certificate provider. You can read more about Let’s Encrypt Chain of Trust here.

In order to enhance our security, we are using Cross Signed certificate. Let’s Encrypt Cross signed certificate can be found here.

In order to user CA Certificate you will have to define it just below IoTaaP iotaap; line in your code:

const char *CA_cert = "-----BEGIN CERTIFICATE-----\n"
                      "-----END CERTIFICATE-----\n";
Notice that we are using ‘\n’ and this is the only way to make your certificate work properly, so if you are using different certificate make sure to keep the same formatting.

callback() is the function that will be called every time when something arrives to our subscribed topic(s). We will define this function later.

This line of code will subscribe IoTaaP to “ledstatus/led1” topic. We will define our callback in the next step.

Keep in mind that every CA Certificate or Root certificate has it’s own expiration date. If the certificate is not valid or if it’s expired your MQTTS connection will not work! Make sure to implement some check-update mechanism to update your certificates stored on your IoTaaP magnolia board. The best way to implement this mechanism is to use OTA or FOTA. With OTA or FOTA you will be able to update your device’s firmware remotely with the new version that contains updated certificate.

Callback function

Callback function signature is already defined so it cannot be changed and it looks like this:

void callback(char* topic, byte* message, unsigned int length) 
Now we will define the function body:

void callback(char* topic, byte* message, unsigned int length) {
  String messageTemp;

  for (int i = 0; i < length; i++) {
    messageTemp += (char)message[i];

  if (String(topic) == "ledstatus/led1") {
    if(messageTemp == "on"){
    else if(messageTemp == "off"){
This function will convert our message into String type and it will check if our trigger topic was “ledstatus/led1“, if this is true, our function will check message content. Content must be “on” in order to turn the LED1 on, or “off” to turn it off.


In our main loop we will publish our accelerometer data in jSON format, and we will call mandatory function keepAlive() that will keep our MQTT connection active.

IoTaaP library comes with ArduinoJson (C++ JSON library for Arduino and IoT) for easier handling of the jSON format. We will use only serializeJson() function in this example, but feel free to check other features of ArduinoJson library.

void loop()

  accelerometer acc = iotaap.accelerometer.getRaw();

  doc["x"] = acc.x;
  doc["y"] = acc.y;
  doc["z"] = acc.z;

  char accJson[255];

  serializeJson(doc, accJson);

  iotaap.mqtt.publish("iotaap/accelerometer", accJson);

This code will keep our MQTT connection active, but it will also read accelerometer data (in accelerometer structure). Axis values will be saved to “doc” with appropriate axis names. Function serializeJson(doc,accJson); will convert doc to jSON string, and finally iotaap.mqtt.publish(“iotaap/accelerometer”, accJson); will publish values to “iotaap/accelerometer” topic.

alt text


We will use MQTT.fx for testing. MQTT.fx installation process and usage are described here. Simply connect to your IoTaaP Cloud (or any other MQTT broker) instance and publish on or off to “ledstatus/led1” topic. Be sure to subscribe to the “iotaap/accelerometer” topic and you will se accelerometer coordinates in jSON format

  "x" : 1921,
  "y" : 1936,
  "z" : 1109
Now, create a code that will control 2 LED’s and publish button status, share your solutions with the community.