Auteur Akshayan Sinha | Dernière modification 25/05/2023 par Akshayansinha
IoT, MQTT, platform, web, mosquitto Mosquitto_MQTT_-_IoT_Platform_Series_mosquitto_mqtt.png Technique
Here, there are 2 sections - Publish and Subscribe. And then there is a middleman - Broker. Let us see in depth
[EXAMPLE] When a user1 publishes an image on social media, then only the user2 subscribed to user1 can view/receive the image. Here, the user1 is the PUBLISHER, user2 is the SUBSCRIBER, and the user1's account is the BROKER.
You must check out PCBWAY for ordering PCBs online for cheap!
You get 10 good-quality PCBs manufactured and shipped to your doorstep for cheap. You will also get a discount on shipping on your first order. Upload your Gerber files onto PCBWAY to get them manufactured with good quality and quick turnaround time. PCBWay now could provide a complete product solution, from design to enclosure production. Check out their online Gerber viewer function. With reward points, you can get free stuff from their gift shop.
Whenever there is a pub-sub model used as a message communication protocol, we require a broker that can transfer the information in the required device. This can be done by sending the message under correct topic.
Let us understand this -
Now that we understand how MQTT works, let us use a cloud MQTT service and send data across the internet. In this article, we'll be using Mosquitto MQTT - test.mosquitto.org
Under the Server section, we can see different ports provide feature-separated servers. These servers act like channels for sharing data over the cloud. Let us understand it first -
We shall be using 1883 port to send data and monitor. As we know, MQTT has 3 services - Publisher, Broker, and Subscriber. In this case, mosquito MQTT Cloud is already playing the role of a broker.
Now, we'd be using ESP32 Dev Board, which has a wifi chip and is able to connect to the Internet, playing the role of a Publisher for sharing its temperature and humidity data from the sensor.
On the other hand, we shall use the PC to view this data as a Subscriber. This will enable us to fully understand the working principle of the MQTT protocol used in IoT Communication between devices.
To set up the ESP32 for MQTT, we need to install a library - PubSubClient
. This library has functions that use variables as mentioned below to send data to the broker.
mqtt_server
: This variable represents the address or IP of the MQTT broker. We shall be using "test.mosquitto.org"mqtt_port
: This variable represents the port number of the MQTT broker. In our case 1883.mqtt_topic
: This variable represents the topic to which the publisher will send messages. For Example "schoolofiot/device1".
Where 'schoolofiot' is the general-most topic level. And 'device1' is a sub-level.The provided code is an Arduino sketch that uses the ESP32 WiFi module and the PubSubClient library to connect to an MQTT broker and publish temperature and humidity data. Let's break down the code step by step:
1. Include necessary libraries:
#include <WiFi.h>
#include <PubSubClient.h>
This code includes the required libraries for the ESP32 WiFi module and the MQTT client functionality.
2. Define WiFi & MQTT Server variables:
const char* ssid = "XXXXXXXXXX";
const char* password = "XXXXXXXXXX";
const char* mqtt_server = "test.mosquitto.org";
These variables store the SSID (network name) and password for the WiFi network you want to connect to. The mqtt_server
variable holds the IP address or hostname of the MQTT broker.
3. Declare global variables and objects:
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
float temperature = 0;
float humidity = 0;
Here, a WiFi client object (espClient
) and an MQTT client object (client
) are declared. The lastMsg
variable stores the timestamp of the last message, and the msg
is a character array for message storage. The value
, temperature
, and humidity
variables are used to hold the respective sensor values.
4. Setup function:
void setup()
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
The setup()
function is called once at the start of the program. It initializes the serial communication, sets up the WiFi connection, configures the MQTT server and port, and sets the callback function to handle incoming messages.
5. WiFi setup function:
void setup_wifi() {
// ...
}
The setup_wifi()
function handles the connection to the WiFi network using the provided SSID and password. It waits until the connection is established and prints the local IP address to the serial monitor.
6. MQTT callback function:
void callback(char* topic, byte* message, unsigned int length) {
// ...
}
This function is called when a message is received from the MQTT broker. It prints the received message along with the corresponding topic.
7. MQTT reconnection function:
void reconnect() {
// ...
}
The reconnect()
function is responsible for reconnecting to the MQTT broker if the connection is lost. It attempts to connect to the broker using a randomly generated client ID. If the connection is successful, it prints a success message. Otherwise, it waits for 5 seconds before retrying.
8. Main loop:
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
sendData();
}
}
The loop()
function is the main program loop that runs continuously after the setup()
function. It checks if the MQTT client is connected and, if not, attempts to reconnect. It also calls the client.loop()
function to maintain the MQTT client's internal state. Every 2 seconds, it calls the sendData()
function to publish temperature and humidity data.
9. Publish sensor data function:
void sendData() {
// ...
}
The sendData()
function is responsible for publishing temperature and humidity data to specific MQTT topics. It generates random values for temperature and humidity, converts them to strings, and publishes them along with the corresponding topic.
- Publish a gap message:
client.publish("schoolofiot/gap", "--------------");
This line publishes a message consisting of a series of dashes (--------------
) to the MQTT topic "schoolofiot/gap". It is used to indicate a separation or gap between different sets of data.
- Read and publish temperature data:
These lines generate a random temperature value between 30 and 40 degrees, store it in thetemperature = random(30, 40);
char tempString[8];
dtostrf(temperature, 1, 2, tempString);
Serial.print("Temperature: ");
Serial.println(tempString);
String tempdata = "Temperature: " + String(tempString);
client.publish("schoolofiot/temperature", tempdata.c_str());
temperature
variable, and usedtostrf()
function to convert decimal point data to String.
dtostrf(floatValue, minStringWidth, numAfterDecimal, charBuf_to_store_string);
This function takes four parameters to convert double into an ASCII value stored inside string:
1. floatValue: The first parameter that takes the float value we want to convert into a string.
2. minStringWidth: This is the second parameter that defines the minimum field width of the output string.
3. numAfterDecimal: The third parameter is precision which describes the number of digits after the decimal point.
The temperature value is then printed to the serial monitor and concatenated with the string "Temperature: ". The resulting string is stored in the4. charBuffer: Final argument is where the string will be stored. This is a kind of char array having a defined size.
tempdata
variable. Finally, the tempdata
string is published to the MQTT topic schoolofiot/temperature using the client.publish()
function.
- Read and publish humidity data:
humidity = random(60, 70);
char humString[8];
dtostrf(humidity, 1, 2, humString);
Serial.print("Humidity: ");
Serial.println(humString);
''mString humdata = "Humidity: " + String(humString);
client.publish("schoolofiot/humidity", humdata.c_str());
These lines generate a random humidity value between 60 and 70 percent, store it in the humidity
variable.
Overall, the sendData()
function generates random temperature and humidity values, converts them to strings, and publishes them to specific MQTT topics for further processing or monitoring.
Final Code can be found in the Code section
But to confirm this, we also need to read the data from other the side - Subscriber.
To set up the Subscriber on PC, we need to install Mosquitto MQTT Applcation. This application can create a broker, publisher & subscriber - all sections
To install Mosquitto MQTT on your PC from the official website and make changes to the configuration file for listener 1883 and allow anonymous connections, you can follow these steps:
1. Download and Install Mosquitto:
2. Edit Configuration File:
mosquitto.conf
file (usually found in the main directory).mosquitto.conf
in a text editor of your choice. Add the below 2 lines -listener 1883
allow_anonymous true
3. Run Mosquitto Subscriber
mosquitto_sub.exe
is present.> .\mosquitto_sub -h test.mosquitto.org -t "schoolofiot/#"
In the above command, if you noticed, I did not subscribe to a specific topic. As per the topics we published (from ESP32), like "schoolofiot/gap", "schoolofiot/temperature" or "schoolofiot/humidity".
> .\mosquitto_sub -h test.mosquitto.org -t "schoolofiot/temperature"
Therefore, no matter what name is put under the general topic, we can subscribe to it and view all of them together.
Hurray! 🎉
We have learned another IoT Platform - Mosquitto MQTT (By Eclipse)
en none 0 Published
Vous avez entré un nom de page invalide, avec un ou plusieurs caractères suivants :
< > @ ~ : * € £ ` + = / \ | [ ] { } ; ? #