MQTT (Message Queuing Telemetry Transport) is a lightweight messaging protocol widely used in IoT applications. In this tutorial, we'll use MQTT to send sensor data from a microcontroller (like ESP8266/ESP32) to an MQTT broker. MQTT is highly efficient for communication between devices and remote servers, making it ideal for home automation, sensor networks, and IoT platforms.
First, install the PubSubClient library in the Arduino IDE. This library allows you to connect the ESP8266/ESP32 to an MQTT broker and publish or subscribe to topics.
To set up the hardware, connect the sensor to the microcontroller:
Below is a simple code that reads temperature and humidity data from a DHT sensor and publishes the values to an MQTT broker.
#include#include #include #define DHTPIN D4 // GPIO pin for DHT sensor #define DHTTYPE DHT22 // DHT type (DHT11 or DHT22) const char* ssid = "your_SSID"; // Wi-Fi SSID const char* password = "your_PASSWORD"; // Wi-Fi password const char* mqttServer = "broker.hivemq.com"; // MQTT broker (use your broker IP or URL) const int mqttPort = 1883; const char* mqttUser = ""; // Leave blank if no authentication const char* mqttPassword = ""; DHT dht(DHTPIN, DHTTYPE); WiFiClient espClient; PubSubClient client(espClient); void setup() { Serial.begin(115200); // Connect to Wi-Fi WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("Connected to WiFi"); // Connect to MQTT broker client.setServer(mqttServer, mqttPort); while (!client.connected()) { Serial.println("Connecting to MQTT..."); if (client.connect("ESP8266Client", mqttUser, mqttPassword)) { Serial.println("Connected to MQTT"); } else { delay(5000); } } dht.begin(); // Initialize the DHT sensor } void loop() { if (!client.connected()) { while (!client.connected()) { Serial.println("Reconnecting to MQTT..."); if (client.connect("ESP8266Client", mqttUser, mqttPassword)) { Serial.println("Reconnected to MQTT"); } else { delay(5000); } } } client.loop(); // Keep the client connected to the broker // Read data from the sensor float humidity = dht.readHumidity(); float temperature = dht.readTemperature(); if (!isnan(humidity) && !isnan(temperature)) { // Publish the sensor data to the MQTT broker String payload = String("Temperature: ") + String(temperature) + "°C, " + "Humidity: " + String(humidity) + "%"; client.publish("home/temperature", String(temperature).c_str()); client.publish("home/humidity", String(humidity).c_str()); Serial.println(payload); } else { Serial.println("Failed to read from DHT sensor"); } delay(2000); // Wait 2 seconds before sending the next reading }
This code connects the ESP8266/ESP32 to a Wi-Fi network and an MQTT broker, reads data from the DHT22 sensor, and publishes the sensor data to two MQTT topics: home/temperature and home/humidity.
The code begins by connecting the microcontroller to a Wi-Fi network using the provided SSID and password. It then connects to the MQTT broker using the specified server address and port. The DHT sensor is initialized to read temperature and humidity data, which is published to the MQTT broker every 2 seconds. If the connection to the broker is lost, the code attempts to reconnect.