Table of Contents: 

1. What is MQTT API?

MQTT is a publish/subscribe model that runs over TCP/IP sockets or WebSockets. MQTT over WebSockets can be secured with SSL.

2. MQTT Features

The MQTT protocol runs on TCP / IP or other network protocols and provides an ordered, lossless, bidirectional connection. Features include:

3. What does this example shows? How it works?

The server we use here is ThingSpeak which has an intuitive chart to show the test result for our test.The ThingSpeak IoT service now supports MQTT subscriptions to receive instant updates when ThingSpeak channel gets updated. The method here is general and can be used with other IoT servers for MQTT connection as well.

 

image-20220531135402-1.png

Message Queuing Telemetry Transport

image-20220531135449-2.png

working principle

image-20220531135521-3.png

Schematic diagram: uplink.In this section, we will try to program LG01 to uplink data to ThingSpeak. The data flow in this example.We have already tried ① and ② in the above simple LoRa example. Now we will try the step ③ first, after it work as expect, we will integrate these three steps together for a complete uplink example.

image-20220531135607-4.png

Schematic diagram:downlink.In this section, we will try to program LG01 to fetch download data from ThingSpeak, then broadcast this data to local LoRa network. The end node will get this message and check if they need to do something. Similar with Uplink Example, we will first try to do it in PC, then do it in Linux side, and finally integrate it with LoRa.

MQTT agreement to achieve the need: client and server There are three identities in the MQTT protocol: Publish, Broker (server), Subscribe. Among them, the publisher and the subscriber of the message are all clients, the broker is the server, and the publisher can be both the subscriber and the subscriber. MQTT transmission of information is divided into: Topic and payload in two parts Topic, can be understood as the type of message subscribers Subscribe, you will receive the theme of the message content (payload) The payload, which can be understood as the content of the message, refers to the specific content to be used by the subscribers.

To use the server, we need to register an account on ThingSpeak . Then create a channel and type the channel info. As shown below, the Channel ID is the unique ID to store our data in ThingSpeak .

4. Preparation

4.1 Hardware

  1. image-20220531135650-5.png

     

    Listening on the LoRa wireless channel, while there is new LoRa packet arrives, parse it and send out to IoT Server.

     

  2. image-20220531135750-6.png

     

    The LoRa End node keeps getting temperature and humidity from the sensor and sends out via LoRa periodically.

     

4.2 Software

  1. 4.3.4 version
  2. MQTT_Client
  3. MQTT_Simple_Server MQTT_Simeple_Server
  4. DHTlib

4.3 Server(For example)

4.4 Configure IoT Server

  1. image-20220531140054-8.png

    Channel ID,Write API Key

     

  2. image-20220531140326-13.png

     

    Account->My Profile,MQTT API Key

     

  3. image-20220531140304-12.png

     

4.5 Configure LG01 settings

Tips:  Please confirm that your LG01 version is 4.3.3.

image-20220531140248-11.png

Overview

image-20220531140425-14.png

Senosor->IOT Server

image-20220531140458-15.png

MQTT->Configure MQTT Server

image-20220531140538-16.png

MQTT->MQTT Channel

image-20220531140611-17.png

Sensor->LoRa/LoraWAN

image-20220531140651-18.png

Configure network access.Network->Internet Access

4.6 Try MQTT API call with LG01 Linux command

4.6.1 Input the command at the console

  1. image-20220531140811-19.png

     

    Open this Application.

     

  2. image-20220531140842-20.png

     

    Input passward(dragino).After into the console.

     

  3. image-20220531140915-21.png

     

    First, we need to make sure the LG01 has internet access. We can log in the SSH and ping an Internet address and see if it get through. As below

     

  4. image-20220531140955-22.png

     

    LG01 has built-in Linux tool mosquitto. It is a very powerful tool for http communication. We can use this tool to handle MQTT API call in LG01. Input this command and replace your keys.

     

  5. image-20220531141026-23.png

     

    Go to Server check out result.We success to use LG01 to uplink data to ThingSpeak,the mosquitto_pub command is executed in the Linux side, finally, we will have to call mosquitto_pub command with sensor data variable in Arduino side. This is through the process class in Arduino and we will show it in the final sketch.

     

mosquitto_pub -h mqtt.thingspeak.com -p 1883 -u dragino -P UZ4NGHKJMKS9WR5E  -i dragino_Client -t channels/399171/publish/A4NNF6G8G63JOIUV -m "field1=23.5&field2=57.5&status=MQTTPUBLISH"

UZ4NGHKJMKS9WR5E  //MQTT API
399171            //Channel ID
A4NNF6G8G63JOIUV  //Write API Key

4.6.2 Input the command at the console(LG01 new version:4.3.4)

  1. image-20220531141150-24.png

     

    Open this Application.

     

  2. image-20220531141400-27.png

    Check the settings.

     

  3. image-20220531141601-30.png

    input this command.

     

  4. image-20220531141515-29.png

     

    Check the result.

     

4.7 Updata data to Server(Through MQTT)

  1. Read the LG01 configuration information from Linux.
  2. Receive the LoRa node data and store the data in the /tmp/iot/channels
  3. Send reply after then receive LoRa node data.
  4. Sketch will write active content to /tmp/iot/status periodically (every 5 minutes).(Watchdog feature)
  1. image-20220531143822-49.png

     

    Open the sketch MQTT_Simple_Server.(Board:Dragino Yun + UNO or LG01/OLG01,Port:Network ports.)and upload.

     

  2. image-20220531141800-32.png

     

    Open Serial monitor.

     

  3. image-20220531143748-48.png

     

    Open the Sketch MQTT_Client. (Board:Arduino/Genuino Uno.Port:Serial ports.)and upload.

     

  4. image-20220531141927-34.png

     

    Open Serial monitor.

     

  5. Go to IoT Server to check the result.
  6. image-20220531143707-47.png

     

  7. image-20220531143632-46.png

     

5. FAQ

5.1 Configure to support general mqtt server

First, try to run mosquitto_pub in Linux console to send a data to MQTT server.

For example: for ThingSpeak, a MQTT publish command is:

mosquitto_pub -h mqtt.thingspeak.com -p 1883 -u dragino -P QZXTAKE88V3S7O2J  -i dragino_Client -t channels/200893/publish/B9Z0R25QNVEBKIFY -m "field1=34&field2=89&status=MQTTPUBLISH"

What now we need to put in the LG01 is how to combine this command.

  1. LG01 web console -> Sensor -> MQTT
  2. Choose general Server
  3. image-20220531142342-37.png

     

    Refer to your server to fill in.

     

  4. image-20220531142447-38.png

     

    Ex:ThingSpeak Server

     

According to MQTT commands: mosquitto_pub, we need to put

In the mqtt script, the upper CHANNEL will be replaced by the parameter (remote channel in IoT server). and the WRITE_API will be replaced by the settings in write api key. the DATA will be replaced by the value stored in the /var/iot/channels/LOCAL_CHANNEL file.

MQTT script will keep checking the files in /var/iot/channels/ . If it find a match Local channel, then the MQTT script will send out the data of this local channel to a remote channel according to the setting above.

User can also enable MQTT debug in the LG01 --> IoT Server settings and run logread in Linux console to see how the mqtt command is compose. below is an example:

image-20220531142525-39.png

MQTT Logread example

5.2 What is Watchdog feature?

LG01 for 4.3.3 version,we have added watchdog feature. In order to avoid the gateway running process will get stuck. So add writing action on every times.