In this project, we will learn How to Monitor Wind Speed with MQTT, to get the wind speed data of a specific place and display it on an ESP32 LCD screen.

1. Overview
Once I failed to control the drone because of the wind, I want to measure the wind speed to know whether it is suitable for the drone flight in the wild. How to measure the wind speed in the field and be viewed anywhere? It requires the MQTT communication to get the speed of wind.

In this project, I want to simply introduce the MQTT and show how to build an MQTT network to get the wind speed of a specific place.

2. What is MQTT
Message Queuing Telemetry Transport (MQTT) is a machine-to-machine protocol designed to widely lightweight publish/subscribe message transportation. It minimizes network bandwidth and resource requirements while ensuring reliability by keeping packet headers as small as possible.

MQTT is a transmission protocol developed for the Internet of Things. It is specially optimized for devices with low computing power. And it makes devices suitable for various IoT application scenarios. Setting up an MQTT system to handle your pub/sub messaging needs can bring powerful benefits to your process:

• Development easily
• Strong scalability
• Transmit and receive information quickly and efficiently.
• Reduce network bandwidth consumption.

MQTT usually runs over TCP/IP but can also use other bi-directional transports. MQTT defines methods to indicate desired actions to be performed on identified resources.

Methods in MQTT are:
• Connect - Waits for connection to be established with the server.
• Disconnect - Waits for the MQTT client to finish any work, which needs to be done and for the TCP/IP session to disconnect.
• Subscribe - Requests the server to let the client subscribe from topics.
• Unsubscribe - Requests the server to let the client unsubscribe from topics.
• Publish - Returns immediately to application thread after passing request to the MQTT client.

An MQTT network consists of MQTT client and MQTT broker. MQTT Client can be any device, from a micro controller to a fully-fledged server, which runs the MQTT library and is connected to MQTT broker over any network. MQTT Broker is responsible for receiving all messages, filtering, decision making and sending messages to subscribed clients.

Instead of communicating with a server, MQTT client devices and applications publish and subscribe from topics handled by a broker. The messages are published to "topics" instead of delivering directly from client to client. The broker then delivers those messages to any subscribed clients. MQTT fundamentally is a publish/subscribe protocol. It allows clients to connect as a publisher, subscriber, or both.

2.1 MQTT Message
The message MQTT transmitted is consisted of “topics” and “payload”. “Topics” is the core for MQTT. “Payload” is the specific content that the subscriber wants.

In the MQTT network, the client must first send a subscribe request to the broker to subscribe from a topic. The subscribe request can include multiple topics. The broker responds to the request with a subscription acknowledged response. Subscriptions also have a QoS setting which can be used to downgrade the QoS of the published message.

Three different Quality of Service (QoS) levels allow network designers to choose between minimizing data transmission and maximizing reliability.
• QoS 0 (zero) is used to ensure that a message reaches its destination no more than once.
• QoS 1 is used when message delivery is critical. This is achieved by queueing messages until the subscriber is able to receive it.
• QoS 2 is used when the message needs to arrive once and only once.
In many cases, the message is always published at the lower QoS setting. MQTT can allow for messages to be stored at the broker until a device is ready to receive it.

2.2 MQTT Broker
Many MQTT brokers have been developed and they are offered in open source, commercial implementations and managed cloud services. A detailed list of MQTT brokers can be found at mqtt.org. I used the developed server to a broker in my project instead of the server built myself. Mosquitto is a readily available, open source MQTT broker that will be used during this tutorial. The Mosquitto server link is test.mosquitto.org. The client can publish and subscribe from the broker by connecting to the Mosquitto server.

3. How to Build the MQTT Network?
According to the above information, I wanted to build two clients to complete a communication with an available broker. Such as the figure, one client gets the data (wind speed, temperature, humidity and so on) and the other receives and display all.

3.1 Maduino A9G as a Client.
I used the maduino A9G as a client and put it outdoors to monitor the wind speed, temperature and humidity. Maduino A9G module can send the message to the MQTT server by A9G. It means the module can publish and subscribe from the MQTT broker. Besides it, A9G has the ability to get GPS, sent messages, take a call, and so on. I wanted to get GPS for preventing the loss of modules placed in the wild. I added a buzzer connected to the module. The buzzer can be Controlled to ring when the module was losing, that I can find it quickly.

I prepared a SIM card to support A9G connects to the network in everywhere.

The preparation:

1. Maduino A9G Module
2. Anemometer
3. DHT11 module
4. Buzzer
5. SIM card
6. Battery
7. Breadboard
8. Jump wires

As the wiring diagram shows, connect the anemometer to Maduino A9G for measuring wind speed, and connect the DHT11 to get the Tem/Hum value.

3.1.1 The Firmware for Maduino A9G
1. The firmware can be obtained from https://github.com/Makerfabs/Project-Wind-weather-station/tree/master/Example/Monitor_Wind_MQTT_V1, and it would be uploaded to the maduino A9G.
2. The AT command for A9G to set the MQTT client and get the GPS. The card ID must be set to be the only one.

AT+CGATT=1       // Connect to the network
AT+CGDCONT=1,”IP”,”CMNET”   // Set PDP Parameters
AT+CGACT=1,1      // Active the PDP
AT+MQTTCONN=” test.mosquitto.org”,1883,”XXXX”,120,0  // Send MQTT connection packet
AT+MQTTSUB=”TOPIC”,1,0      // Send MQTT subscribe packet
AT+MQTTPUB=”TOPIC”,”PAYLOAD”,0,0,0   // Send MQTT publish packet
AT+MQTTDISCONNN    // Disconnect MQTT

 

3. The command for A9G to get the GPS.

AT+GPS=1   //Open the GPS
AT+LOCATION=2   // Get the location
AT+GPS=0     // Close the GPS

 

4. Connect the MQTT broker.

sendData("AT+MQTTCONN=\"test.mosquitto.org\",1883,\"mqttx_0931852d34\",120,0", 1000, DEBUG);

 

5. Subscribe from the message to controlled the buzzer.

sendData("AT+MQTTSUB=\"/public/TEST/makerfabs-B\",1,0", 1000, DEBUG);

 

6. Get the wind speed.

float windspeek = 0;
  if ((millis() - lastDebounceTime) >= debounceDelay) {
    int  currenTime = millis();
    windspeek = Count*0.0875*debounceDelay/(currenTime-lastDebounceTime);
    lastDebounceTime = currenTime;
    Count=0;
    Couttime_t++;
    SerialUSB.print(windspeek);
    SerialUSB.println("m/s");
  }

 

7. Publish the wind speed to the broker, the topic is “/public/TEST/makerfabs-W”,

String topic_W = "AT+MQTTPUB=\"/public/TEST/makerfabs-W\",\""+(String)windspeek+"\",0,0,0";
sendData(topic_W, 1000, DEBUG)

 

3.2 The ESP32 3.2’’ LCD as a Client

I used the ESP32 3.2" LCD Touch Screen  as the other client to subscribe the message of sensor data. ESP32 3.2’’ LCD that runs the MQTT client library of Arduino can be a client to communicate with the MQTT broker through WiFi. And it has an LCD to display the subscribed message. Besides it, ESP32 3.2’’ LCD has the touch ability that the LCD can be as the input device. Touch the screen to publish a message to control the buzzer which connected with the subscriber.

3.2.1 The Firmware for ESP32
1. The firmware can be obtained from https://github.com/Makerfabs/Project-Wind-weather-station/tree/master/Example/Monitor_Wind_MQTT_V1, and it would be uploaded to the ESP32.
2. Connect to the server.

const char *mqtt_server = "test.mosquitto.org";
    client.setServer(mqtt_server, 1883); // the MQTT default port is 1883
    client.setCallback(callback);

 

3. Set the only ID for the client.

String clientId = "mqttx_0931857d";
clientId += String(random(0xffff), HEX);
if (client.connect(clientId.c_str()))

 

4. Subscribe the temperature from the broker. The topic is “/public/TEST/makerfabs-T”

client.subscribe("/public/TEST/makerfabs-T");

 

5. Subscribe the humidity from the broker. The “topic” is “/public/TEST/makerfabs-H”

client.subscribe("/public/TEST/makerfabs-H");

 

6. Subscribe the wind speed from the brother

client.subscribe("/public/TEST/makerfabs-W");

 

7. Subscribe the A9G GPS from the broker

client.subscribe("/public/TEST/makerfabs-G");

 

8. Publish the message for controlling the buzzer to the broker.

client.publish("/public/TEST/makerfabs-B", "buzzerON");

 

9. Get the callback of the subscribed.

 void callback(char *topic, byte *payload, unsigned int length)
{
    Serial.print("Message arrived [");
    Serial.print(topic);
    Serial.print("] ");
    String payload_str;
    for (int i = 0; i < length; i++)
    {
        Serial.print((char)payload[i]);
        payload_str = payload_str+(char)payload[i];
    } /
Serial.println();

 

4. Result

 

 
After the project was complete, I can check the wind speed to suitable the flight of drone at home.

MQTT broker allows multiple clients to publish and subscribe. Devices and applications that run the client library can connect to the broker to subscribe the wind speed and messages.

The MQTT protocol is powerful and complex, and is an important element of successful IoT projects. I hope the information above is helpful for you to build an MQTT project.


If you have further questions or need some  customized PCBA boards based on those IoT boards for your special requirements, pls contact service@makerfabs.com .