MQTT

warning

The document is a continuation of the previous document, if you have landed directly on this page then, Please read from page Get started.

What is MQTT?#

MQTT is a protocol used to send and receive messages between 2 or more IoT devices.

Overview of how devices communicate using MQTT#

Oops!, No Image to display.
  • Device sending the message is called as Publisher.
  • Device receiving the message is called as Subscriber
  • Topic is the channel through which devices send and receive messages.

List of MQTT features implemented in Shunya stack#

With Shunya stack you can make IoT device to act as a MQTT client i.e you can

  1. Send messages to devices. (Publisher)
  2. Receive messages from devices. (Subscriber)

Using MQTT Shunya stack#

Requirements to use MQTT Shunya stack#

  1. Shunya OS installed (supported Arm devices) or Shunya OS docker container (X86 based windows/linux devices)
  2. MQTT broker URL.

Steps to use MQTT Shunya stack#

  1. Set MQTT broker settings in Shunya.
  2. Send messages via MQTT
  3. Receive messages via MQTT
note

Run the steps given below inside Shunya OS installed (supported Arm devices) or Shunya OS docker container (X86 based windows/linux devices) terminals.

Step 1: Set MQTT broker settings in Shunya.#

Set MQTT Broker details, by editing the config file /etc/shunya/config.json inside Shunya OS.

A simple configuration should contain these parameters.

ConfigDescription
mqttBrokerUrlMQTT broker url
mqttClientIdUnique Identification of the device (user can put any string that is convenient)

Example configuration:#

Writing the below json in /etc/shunya/config.json file, will tell the Shunya stack to connect to MQTT broker at url test.mosquitto.org:1883.

"mqttSettings": {
"mqttBrokerUrl": "test.mosquitto.org:1883",
"mqttClientId": "shunya pi3",
}

Step 2: Send messages via MQTT#

Let's take an example use case: Say we need to

  • Read data from the temperature sensor connected to Shunya and,
  • Send the data to topic "temp".

Steps are :

  1. Start with an ready to use template for sending messages via MQTT

    git clone https://gitlab.iotiot.in/repo-public/examples.git
    cd examples/simple-examples/send-messages-mqtt
  2. Open the send-messages-mqtt.c in an text editor and modify as per your use case.

  3. For our example, we need to first get the temperature data, add the code in your main() function

    float temperature = 76.98; /* For now lets assume that the temperature is 76.89 Celsius*/
  4. Sending temperature data to topic, modify the code

    float temperature = 76.98; /* For now lets assume that the temperature is 76.89 Celsius*/
    /* Create a mqtt object */
    mqttObj mqttBroker = newMqtt("mqttSettings"); /* Argument = JSON Title, Load settings from JSON file */
    /* Connect to broker */
    mqttConnect(&mqttBroker) ;
    /* Send a "76.98 C" to topic "temp" via MQTT*/
    mqttPublish(&mqttBroker, "temp", "%.2f C", temperature);
    mqttDisconnect(&mqttBroker); /* Disconnect MQTT */
  5. Once you are done editing, save and compile the code , by running

    mkdir build && cd build
    cmake ../
    make
  6. Run the code

    sudo ./send-messages-mqtt
  7. More examples to send messages via MQTT shunya stack

    For sending JSON, change code in step 4. with the example given below.

    char json[1024] = "{ "temp": 78.9, "sensor_name": "BME280" }";
    /* Send a json to topic "temp" via MQTT*/
    mqttPublish(&mqttBroker, "temp", "%s", json);

Step 3: Receiving messages via MQTT#

Let's take an example use case: Say we need to

  • Receive the temperature data from the "temp" topic
  • Print the temperature data to a file when the message is received.

Steps are :

  1. Start with an ready to use template for receiving messages via MQTT

    git clone https://gitlab.iotiot.in/repo-public/examples.git
    cd examples/simple-examples/receive-messages-mqtt
  2. Open the receive-messages-mqtt.c in an text editor and modify as per your use case.

  3. For our example, we need to first edit the code to store the received message to at file.

    Add the code to print to a file in processMessages() function

    void processMessages (int topicLen, char *topic, int msgLen, char *msg)
    {
    /* For now lets print the message.*/
    printf("Received message\n\tFrom Topic: %s,\n\t Message: %s", topic, msg);
    /* Print the message to a file */
    FILE *file;
    file = fopen("database", "r");
    if (file != NULL) {
    fprintf(file, "%s\n", msg);
    }
    fclose(file);
    }
  4. Now lets subscribe to the topic "temp", so we can start receiving messages

    /* Create a mqtt object */
    mqttObj mqttBroker = newMqtt("mqttSettings"); /* Argument = JSON Title, Load settings from JSON file */
    /* Connect to broker */
    mqttConnect(&mqttBroker) ;
    /* Send all the messages received to our processing function */
    mqttSetSubCallback(&mqttBroker, processMessages);
    /* Receive messages from topic "temp" via MQTT*/
    mqttSubscribe(&mqttBroker, "temp");
    mqttDisconnect(&mqttBroker); /* Disconnect MQTT */
  5. Once you are done editing, save and compile the code , by running

    mkdir build && cd build
    cmake ../
    make
  6. Run the code

    sudo ./receive-messages-mqtt
  7. More examples on receiving messages via MQTT shunya stack

    For sending multiple variables, change code in step 4. with the example given below.

    void processMessages (int topicLen, char *topic, int msgLen, char *msg)
    {
    /* Parse JSON message using rapidjson */
    rapidjson::Document jsonMsg;
    jsonMsg.Parse(msg);
    }

Facing errors with the component?#