Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
87 views31 pages

MQTT With NODE - RED - Version Finale

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 31

MQTT Communication Protocol–Node-RED

MQTT Communication Protocol

Eclipse Mosquitto is an open source message broker that implements the MQTT
protocol versions 5.0, 3.1.1 and 3.1.

Mosquitto is lightweight and is suitable for use on all devices from low power single
board computers to full servers.

The MQTT protocol provides a lightweight method of carrying out messaging using
a publish/subscribe model. This makes it suitable for IoT messaging such as with low
power sensors or mobile devices such as phones, embedded computers or
microcontrollers.

The Mosquitto project also provides a C library for implementing MQTT clients, and
the very popular mosquitto_pub and mosquitto_sub command line MQTT clients.

MQTT running on Windows


1. Downloadthe broker mosquito:
- Download the suitable mosquito broker for windows from the webpage:
(https://mosquitto.org/download/): available version 2.0.13
- Run the application “mosquitto-2.0.13-install-windows-x64. exe”
- Keep default install parameters and keep in mind the install destination
folder (Example: C:\Program Files\mosquitto).

- Once installed, open a command prompt


- Change directory to the mosquito destination folder

- Then, run the moquitto.exe from the command prompt

IoT 3
MQTT Communication Protocol–Node-RED

The service is then working.


2. The first test:

We propose to use locally the mosquitto broker to test the following flow:

The sensor publish the topic temperatuyre. A distant equpment must subscribe to
get this information.

- Prepare the broker in a prompt command :

The broker is then waiting for information from the sensor in order to
publish them
- Now, we have to simulate the sensor behavior using a new prompt
command. Change directory to the mosquito destination folder and check
the mosquito appropriate command to publish the temperature values
(mosquito_pub):

IoT 3
MQTT Communication Protocol–Node-RED

We focus mainly on:


*-h : mqtt host to connect to. Defaults to localhost, means, on which broker
to place this order?
* -t : mqtt topic to publish to (temperature).
*-m : message payload to send: the payload (temperature) value

- Run the following command to publish the value 16°C for the temperature
using the localhost server.

The broker receives the message and it can publish this information to the
subscribers. At this time, we have no subscribers yet.

- Let’s now define a subscriber, open a new prompt command to mimic a


remote subscriber equipment.

IoT 3
MQTT Communication Protocol–Node-RED

- Run the following command to subscribe for the temperature topic:

At this stage, the subscriber doesn’t get the temperature value. It is normal, because
the default QoS is set to QoS0 and the broker did not save the temperature value to
publish it to the subscriber. So, if we run again the publish command, we will get the
result on the subscriber window.

- Try to publish different temperature values and see the obtained results.
- Try to reproduce this example with 2 or more sensors and 2 or more
remote equipments.

IoT 3
MQTT Communication Protocol–Node-RED

Note: The remote equipment can subscribe to both temperature and pressure topics.

MQTT running locally with Node-RED on Windows

In this configuration, the remote equipment will be the node-RED application which
is subscribed to the topics temperature and pressure. The Dashboard, within node-
RED application, enables us to see results in a very illustrative way.

- Simulate the temperature and the pressure sensors


- Run the mosquito server
- Try to reproduce the following flow with the node-RED application
running locally:

IoT 3
MQTT Communication Protocol–Node-RED

mqtt Node:

 change Function Node:

IoT 3
MQTT Communication Protocol–Node-RED

- Try to publish a temperature value and see results in the debug window.

The value 15 is a string output.

After the change node function, the result is:

- Complete the flow with the pressure sensor as follows:

- Prepare a web browser to display the result dashboard


(http://localhost:1880/ui/)

IoT 3
MQTT Communication Protocol–Node-RED

- We can also use node-RED application to publish sensor humidity


information for example, create a subscriber equipment:

- Create the following flow. Deploy, Inject Input and see results in the
subscriber command prompt

- Reproduce the following flow. Deploy, change humidity values from the
dashboard and see results in the subscriber command prompt

IoT 3
MQTT Communication Protocol–Node-RED

IoT Solution with Node-RED MQTTand ESP32/ESP8266


The following figure presents the ESP32 pins configuration:

IoT 3
MQTT Communication Protocol–Node-RED

The following diagram shows the projectoverview

Hardware Environment:
- ESP32
- Temperature and Humidity Sensor DHT11
- PC

Software Environment:

- Arduino IDE
- Node-RED Application

 Prerequisites:

• Installing ESP32 Add-on in Arduino IDE :


To install the ESP32 board in your Arduino IDE, follow these next instructions:
- In your Arduino IDE, go to File>Preferences
- -Enter (https://dl.espressif.com/dl/package_esp32_index.json) into the
“Additional Board Manager URLs” field. Then, click the “OK” button

Note: if you already have the ESP8266 boards URL, you can separate the URLs
with a comma as follows:

- Open the Boards Manager. Go to Tools>Board>Boards Manager…


- Search for ESP32 and press install button for the “ESP32 by Espressif
Systems“
- That’s it. It should be installed after a few seconds.

IoT 3
MQTT Communication Protocol–Node-RED

- Testing the installation: Plug the ESP32 board to your computer. With your
Arduino IDE open, follow these steps:
 Select your Board in Tools > Board menu
 Check the ESP board presence
 Select the Tools >Port (if you don’t see the COM Port in your
Arduino IDE, you need to install the USB to UART Bridge VCP
Drivers from this site :
https://www.silabs.com/developers/usb-to-uart-bridge-vcp-drivers
 Open the following example under File>Examples>WiFi
(ESP32)>WiFiScan
 A new sketch opens in your Arduino IDE

 Press the Upload button in the Arduino IDE. Wait a few seconds
while the code compiles and uploads to your board.
 If everything went as expected, you should see a “Done uploading.”
message.

 Open the Arduino IDE Serial Monitor at a baud rate of 115200


 Press the ESP32 on-board Enable button and you should see the
networks available near your ESP32

Note:If you try to upload a new sketch to your ESP32 and you get this error
message “A fatal error occurred: Failed to connect to ESP32: Timed out…
Connecting…“. It means that your ESP32 is not in flashing/uploading mode.
Having the right board name and COM port selected, follow these steps:

IoT 3
MQTT Communication Protocol–Node-RED

o Hold-down the “BOOT” button in your ESP32 board


o Press the “Upload” button in the Arduino IDE to upload your sketch
o After you see the “Connecting….” message in your Arduino IDE,
release the finger from the “BOOT” button
o After that, you should see the “Done uploading” message
o That’s it. Your ESP32 should have the new sketch running. Press the
“ENABLE” button to restart the ESP32 and run the new uploaded
sketch.

• MQTT Library :

 Installing the Async MQTT Client Library:


To use MQTT with the ESP32, we’ll use theAsync MQTT Client Library.
- Click here to download the Async MQTT client library:
https://github.com/marvinroger/async-mqtt-client
You should have a .zip folder in your Downloads folder
- Unzip the .zip folder and you should get async-mqtt-client-
master folderRename your folder
from async_mqtt_client_masterto async_mqtt_client
- Move the async_mqtt_client folder to your Arduino IDE installation libraries
folder.
- Finally, re-open your Arduino IDE
Note:Alternatively, you can go to Sketch >Include Library>Add . ZIP library
and select the library you’ve just downloaded

 Installing the Async TCP Library:

To use MQTT with the ESP, you also need the Async TCP library.
- Click here to download the Async TCP library
https://github.com/me-no-dev/AsyncTCP
You should have a .zip folder in your Downloads folder
- Unzip the .zip folder and you should get AsyncTCP-master folder
- Rename your folder from AsyncTCP-master to AsyncTCP
- Move the AsyncTCP folder to your Arduino IDE installation libraries
folder
- Finally, re-open your Arduino IDE

Alternatively, you can go to Sketch >Include Library>Add . ZIP library and select
the library you’ve just downloaded.

• DHT11 Sensor Libraries

IoT 3
MQTT Communication Protocol–Node-RED

- Open your Arduino IDE and go to Sketch > Include Library > Manage
Libraries. The Library Manager should open.
- Search for “DHT11” on the Search box and install the DHT library from
Adafruit
- After installing the libraries, restart your Arduino IDE.

 Schematic diagram

- Wire the sensor to the ESP32 as shown in the following schematic diagram
with the data pin connected to GPIO4. However, you can use any other
suitable digital pin.
- Connect a 4.7 KΩ pull-up Resistor between Data Pin and VIN (or 3.3V) of
ESP32.

• Mosquitto Configuration on windows

This step is necessary to attribute the PC’IP to the broker Mosquito which is running
locally.

Note: To find the local IP address of your PC, open a prompt command and run
directly the following command ** ipconfig, you will find the address in the line
« Adresse IPv4 »

- Download the mosquito broker for windows from the webpage


- Open a prompt command from the mosquito directory
- Run the following command to check the mosquito version

IoT 3
MQTT Communication Protocol–Node-RED

- Open service application from the search tab windows

- Search for Mosquitto Broker

- Click on Mosquitto broker


- Edit to get the following configuration

IoT 3
MQTT Communication Protocol–Node-RED

- Click on Apply and then OK


- Browse to the following directory

- Select the mosquito.conf file


- Open the file
- Go to the “PSK based SSL/TLS support” section
- Add these two lines

- Open the windows Defender Firewall


- Select “Réglage du traffic entrant” (window on the left)
- Select “Nouvelle règle” from the right window
- Select Port

- Click on Next
- Type 1883 in the “Ports locaux spécifiques” field

IoT 3
MQTT Communication Protocol–Node-RED

- Click on Next
- Keep defaut configurations

- Click on Next
- Keep defaut configurations

IoT 3
MQTT Communication Protocol–Node-RED

- Click on Next
- Edit the name as follows:

- Click on Finish
- Check the presence of a mosquitto flow in “Règles de traffic entrant”

IoT 3
MQTT Communication Protocol–Node-RED

- Reproduce the same steps with “Règles de traffic sortant”

IoT 3
MQTT Communication Protocol–Node-RED

IoT 3
MQTT Communication Protocol–Node-RED

IoT 3
MQTT Communication Protocol–Node-RED

IoT 3
MQTT Communication Protocol–Node-RED

- Restart your laptop and run netstat -a in a command prompt

IoT 3
MQTT Communication Protocol–Node-RED

• Arduino IDE Code

Copy the following code to your Arduino IDE. To make it work for you, you need
to insert your network credentials as well as the MQTT broker details.

The following section imports all the required libraries

Include your network credentials on the following lines

Insert your local PC IP address, so that the ESP32 connects


to your broker.
If you’re using a cloud MQTT broker, insert the broker
domain name, for example:

Define the MQTT port.

The temperature and humidity will be published on the


following topics

Define the GPIO that the DHT sensor data pin is connected
to. In our case, it is connected to GPIO4

Define the DHT sensor type you’re using. In our example,


we’re using the DHT22.
The same for DHT11

Initialize the DHT sensor on the pin and type defined


earlier.

The temp and hum variables will hold the temperature and
humidity values from the DHT22 sensor.

Create an AsyncMqttClient object called mqttClient to


handle the MQTT client and timers to reconnect to your
MQTT broker and router when it disconnects.

Then, create some auxiliary timer variables to publish the


readings every 10 seconds. You can change the delay time
on the interval variable
Note:the DHT11 and DHT22 have a low sampling rate.
You can only request DHT11 readings every second, or
every two seconds for the DHT22.
For example, the connectToWifi() connects your ESP32 to
your router:

IoT 3
MQTT Communication Protocol–Node-RED

The connectToMqtt() connects your ESP32 to your MQTT


broker

The WiFiEvent() function is responsible for handling the


Wi-Fi events. For example, after a successful connection
with the router and MQTT broker, it prints the ESP32 IP
address. On the other hand, if the connection is lost, it
starts a timer and tries to reconnect.

The onMqttConnect() function runs after starting a session


with the broker.

If the ESP32 loses connection with the MQTT broker, it


calls the onMqttDisconnect function that prints that
message in the serial monitor.

When you publish a message to an MQTT topic, the


onMqttPublish() function is called. It prints the packet id in
the Serial Monitor.

Basically, all these functions that we’ve just mentioned are callback functions. So, they are executed asynchronously.

IoT 3
MQTT Communication Protocol–Node-RED

Now, let’s proceedto the setup().


Initialize the DHT sensor.

The lines create timers that will allow both the MQTT
broker and Wi-Fi connection to reconnect, in case the
connection is lost.

--

This line assigns a callback function, so when the ESP32


connects to your Wi-Fi, it will execute the WiFiEvent()
function to print the details described earlier.
Finally, assign all the callback functions. This means that
these functions will be executed automatically when
needed. For example, when the ESP32 connects to the
broker, it automatically calls the onMqttConnect() function,
and so on.
Note: If your broker requires authentication, add the
following line and insert your credentials (username and
password).

Finally, connect to Wi-Fi.

IoT 3
MQTT Communication Protocol–Node-RED

Finally, let’s proceedto the loop().


In the loop(), you create a timer that will allow you
to get new temperature and humidity readings from
the DHT sensor and publishing them on the
corresponding topic every 10 seconds.

Use these lines to publish the readings on the


corresponding MQTT topics
Basically, use the publish() method on the
mqttClient object to publish data on a topic. The
publish() method accepts the following arguments,
in order:
o MQTT topic (const char*)
o QoS (uint8_t): quality of service – it can be
0, 1 or 2
o retain flag (bool): retain flag
o payload (const char*) – in this case, the
payload corresponds to the sensor reading
The QoS (quality of service) is a way to guarantee
that the message is delivered. It can be one of the
following levels:
o 0: the message will be delivered once or
not at all. The message is not
acknowledged. There is no possibility of
duplicated messages;
o 1: the message will be delivered at least
once, but may be delivered more than
once;
o 2: the message is always delivered exactly
once;
Upload the code to your ESP32

IoT 3
MQTT Communication Protocol–Node-RED

The complete code:

#include "DHT.h"
#include <WiFi.h>
extern "C" {
#include "freertos/FreeRTOS.h"
#include "freertos/timers.h"
}
#include <AsyncMqttClient.h>
#define WIFI_SSID "TOPNET_8F98"
#define WIFI_PASSWORD "xdfqduc4fu"
// Raspberry Pi Mosquitto MQTT Broker
#define MQTT_HOST IPAddress(192, 168, 1, 15)
// For a cloud MQTT broker, type the domain name
//#define MQTT_HOST "localhost"
#define MQTT_PORT 1883
// Temperature MQTT Topics
#define MQTT_PUB_TEMP "esp32/dht/temperature"
#define MQTT_PUB_HUM "esp32/dht/humidity"
// Digital pin connected to the DHT sensor
#define DHTPIN 4
// Uncomment whatever DHT sensor type you're using
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE);
// Variables to hold sensor readings
float temp;
float hum;
AsyncMqttClient mqttClient;
TimerHandle_t mqttReconnectTimer;
TimerHandle_t wifiReconnectTimer;
unsigned long previousMillis = 0; // Stores last time temperature was published
const long interval = 10000; // Interval at which to publish sensor readings
void connectToWifi() {
Serial.println("Connecting to Wi-Fi...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}
void connectToMqtt() {
Serial.println("Connecting to MQTT...");
mqttClient.connect();
}
void WiFiEvent(WiFiEvent_t event) {
Serial.printf("[WiFi-event] event: %d\n", event);
switch(event) {
case SYSTEM_EVENT_STA_GOT_IP:
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
connectToMqtt();
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
Serial.println("WiFi lost connection");
xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting
to Wi-Fi
xTimerStart(wifiReconnectTimer, 0);
break;
}
}

IoT 3
MQTT Communication Protocol–Node-RED

void onMqttConnect(bool sessionPresent) {


Serial.println("Connected to MQTT.");
Serial.print("Session present: ");
Serial.println(sessionPresent);
}
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
Serial.println("Disconnected from MQTT.");
if (WiFi.isConnected()) {
xTimerStart(mqttReconnectTimer, 0);
}
}
/*void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
Serial.println("Subscribe acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
Serial.print(" qos: ");
Serial.println(qos);
}
void onMqttUnsubscribe(uint16_t packetId) {
Serial.println("Unsubscribe acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
}*/
void onMqttPublish(uint16_t packetId) {
Serial.print("Publish acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
}
void setup() {
Serial.begin(115200);
Serial.println();
dht.begin();
mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0,
reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0,
reinterpret_cast<TimerCallbackFunction_t>(connectToWifi));
WiFi.onEvent(WiFiEvent);
mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
//mqttClient.onSubscribe(onMqttSubscribe);
//mqttClient.onUnsubscribe(onMqttUnsubscribe);
mqttClient.onPublish(onMqttPublish);
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
// If your broker requires authentication (username and password), set them below
//mqttClient.setCredentials("REPlACE_WITH_YOUR_USER", "REPLACE_WITH_YOUR_PASSWORD");
connectToWifi();
}
void loop() {
unsigned long currentMillis = millis();
// Every X number of seconds (interval = 10 seconds)
// it publishes a new MQTT message
if (currentMillis - previousMillis >= interval) {
// Save the last time a new reading was published
previousMillis = currentMillis;
// New DHT sensor readings
hum = dht.readHumidity();
// Read temperature as Celsius (the default)
temp = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
//temp = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).

IoT 3
MQTT Communication Protocol–Node-RED

if (isnan(temp) || isnan(hum)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Publish an MQTT message on topic esp32/dht/temperature
uint16_t packetIdPub1 = mqttClient.publish(MQTT_PUB_TEMP, 1, true,
String(temp).c_str());
Serial.printf("Publishing on topic %s at QoS 0, packetId: %i", MQTT_PUB_TEMP,
packetIdPub1);
Serial.printf("Message: %.2f \n", temp);
// Publish an MQTT message on topic esp32/dht/humidity
uint16_t packetIdPub2 = mqttClient.publish(MQTT_PUB_HUM, 1, true, String(hum).c_str());
Serial.printf("Publishing on topic %s at QoS 0, packetId %i: ", MQTT_PUB_HUM,
packetIdPub2);
Serial.printf("Message: %.2f \n", hum);
}
}

- Open the Serial Monitor at a baud rate of 115200 and you’ll see that the ESP32
starts publishing messages on the topics we’ve defined previously.

• Preparing Node-RED Dashboard:

The ESP32 is publishing temperature readings every 10 seconds on the


esp32/dht/temperature and esp32/dht/humidity topics. Now, you can use any
dashboard that supports MQTT or any other device that supports MQTT to subscribe
to those topics and receive the readings.

As an example, we’ll create a simple flow using Node-RED to subscribe to those


topics and display the readings on gauges.

- Having Node-RED running on your local PC, go to this address


http://localhost:1880
- The Node-RED interface should open. Drag two MQTT in nodes, and two
gauge nodes to the flow.

- Click the MQTT node and edit its properties.

IoT 3
MQTT Communication Protocol–Node-RED

The Server field refers to the MQTT broker. In our case, the MQTT broker is
installedin our local PC, so it is set to localhost:1883. If you’re using a Cloud MQTT
broker, you should change that field.
- Insert the topic you want to be subscribed to and the QoS. This previous
MQTT node is subscribed to the esp32/dht/temperaturetopic.
- Click on the other MQTT in node and edit its properties with the same
server, but for the other topic: esp32/dht/humidity.
- Click on the gauge nodes and edit its properties for each reading. The
following node is set for the temperature readings. Edit the other chart
node for the humidity readings

IoT 3
MQTT Communication Protocol–Node-RED

- Wire your nodes as shown below:

- Finally, deploy your flow


- You should get access to the current DHT temperature and humidity
readings on the Dashboard. You can use other dashboard-type nodes to
display the readings on different ways.

IoT 3

You might also like