This shows you the differences between two versions of the page.
iothings:laboratoare:2025:lab3 [2025/10/11 00:21] dan.tudose [QoS, Retain & LWT] |
iothings:laboratoare:2025:lab3 [2025/10/11 00:29] (current) dan.tudose [Lab 3. The MQTT Protocol] |
||
---|---|---|---|
Line 3: | Line 3: | ||
MQTT (Message Queuing Telemetry Transport) is a lightweight, publish-subscribe network protocol designed for efficient communication in constrained environments. Originally developed by IBM in the late 1990s, MQTT has become a standard for IoT (Internet of Things) systems due to its low bandwidth requirements and minimal overhead. It is mostly used in Home Automation systems, Industrial IoT applications and Mobile Messaging and Telemetry. | MQTT (Message Queuing Telemetry Transport) is a lightweight, publish-subscribe network protocol designed for efficient communication in constrained environments. Originally developed by IBM in the late 1990s, MQTT has become a standard for IoT (Internet of Things) systems due to its low bandwidth requirements and minimal overhead. It is mostly used in Home Automation systems, Industrial IoT applications and Mobile Messaging and Telemetry. | ||
+ | {{ :iothings:laboratoare:2025:mqtt-pubsub-model.jpg?600 |}} | ||
==== Key Concepts ==== | ==== Key Concepts ==== | ||
Line 408: | Line 409: | ||
</note> | </note> | ||
+ | This example turns the ESP32 into a fully interactive MQTT client that both publishes and subscribes to topics on a broker using QoS and retain messages. When the device connects, it registers itself with a unique client ID and announces its presence by publishing an “online” status message, while also defining a “last will” message that the broker will automatically send as “offline” if the connection is lost unexpectedly. The ESP32 subscribes to a specific topic for LED control commands so that it can receive JSON payloads over MQTT, parse them, and then acknowledge each command by publishing a confirmation message back to a separate acknowledgment topic. This creates a two-way communication channel where the device can be controlled remotely and confirm successful execution of commands. | ||
+ | |||
+ | At the same time, the ESP32 regularly publishes telemetry data through MQTT. It sends heartbeat messages with uptime, signal strength, and network information, and it transmits environmental sensor readings from the BME680 to a designated topic as JSON data. All MQTT communication uses Quality of Service level 1 to ensure that messages are delivered at least once, providing reliable data exchange between the device and the broker. The code also manages automatic reconnection with exponential backoff, meaning it gracefully retries connecting to the MQTT broker when disconnected without flooding the network. In essence, from the MQTT point of view, this device behaves as a resilient, bidirectional IoT client that reports data, accepts remote commands, and maintains a persistent, reliable session with the broker. | ||
<code C main.cpp> | <code C main.cpp> | ||
Line 420: | Line 424: | ||
//////////////// EDIT THESE //////////////// | //////////////// EDIT THESE //////////////// | ||
- | const char* WIFI_SSID = "TP-Link_2A64"; | + | const char* WIFI_SSID = "YOUR_SSID"; |
- | const char* WIFI_PASSWORD = "99481100"; | + | const char* WIFI_PASSWORD = "YOUR_PASSWORD"; |
const char* MQTT_HOST = "test.mosquitto.org"; // or your lab broker | const char* MQTT_HOST = "test.mosquitto.org"; // or your lab broker | ||
const uint16_t MQTT_PORT = 1883; | const uint16_t MQTT_PORT = 1883; | ||
- | const char* BASE_TOPIC = "iot/dantudose"; // change per student | + | const char* BASE_TOPIC = "iot/studentname"; // change per student |
//////////////////////////////////////////// | //////////////////////////////////////////// | ||