Real-Time Data Logging using ESP32 and MQTT

Real-Time Data Logging using ESP32 and MQTT

ESP32 MQTT

In the world of IOT (Internet of Things), MQTT protocol stands out due to its lightweight and publish/subscribe messaging model. This tutorial aims at introducing you to real-time data logging using ESP32 and MQTT, we will dive into the details of MQTT, set up a free MQTT server, and log sensor data from ESP32 to the server.

Table of Contents

  1. What is MQTT?
  2. Benefits of MQTT
  3. Free MQTT servers
  4. Setting up MQTT Server
  5. ESP32 and MQTT
  6. Code Examples
  7. Common Questions

What is MQTT?

MQTT or Message Queuing Telemetry Transport is a publish-subscribe based "light weight" messaging protocol for use on top of the TCP/IP protocol. It is designed for connections with remote locations where a "small code footprint" is required or the network bandwidth is limited.

Benefits of MQTT

Free MQTT servers

Setting up MQTT Server

To set up the Mosquitto MQTT server, follow these steps:

Windows:

  1. Download the installer from the Mosquitto downloads page.
  2. Run the installer.
  3. Start the server by navigating to the install location and running mosquitto.exe.

Linux/Mac:

sudo apt update
sudo apt install -y mosquitto mosquitto-clients
sudo systemctl enable mosquitto
sudo systemctl start mosquitto

ESP32 and MQTT

The ESP32 microcontroller can easily connect to the MQTT server and publish sensor data in real-time. There are several MQTT libraries available for the ESP32 platform, but we will use the PubSubClient library in this tutorial.

Code Examples

#include <WiFi.h>
#include <PubSubClient.h>

// Network variables
const char* ssid = "your_ssid";
const char* password =  "your_password";
const char* mqttServer = "mqtt_server_ip";
const int   mqttPort = mqtt_server_port;
const char* mqttUser = "mqtt_user";
const char* mqttPassword = "mqtt_password";

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi");
  }

  client.setServer(mqttServer, mqttPort);
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  publishSensorData();
}

void reconnect() {
  while (!client.connected()) {
    if (client.connect("ESP32Client", mqttUser, mqttPassword )) {
      Serial.println("Connected to MQTT Broker!");
    } else {
      Serial.println("Failed to connect, try again in 5 seconds");
      delay(5000);
    }
  }
}

void publishSensorData() {
  float data = readSensorData();  // Implement this function based on your sensor
  char msg[10];
  dtostrf(data, 4, 2, msg); 
  client.publish("esp32/sensor", msg);
}

Common Questions

1. How secure is MQTT?

While MQTT’s lightweight design does not inherently include built-in security features, you can use secure protocols such as SSL/TLS to encrypt your communication. A secure implementation will require both encrypted connections and correctly configured user access controls.

2. Is MQTT a good choice for real-time data?

Due to its lightweight nature and Quality of Service levels, MQTT is exceptionally well suited to real-time data handling, particularly in situations where network bandwidth is limited.

3. Can MQTT work with other protocols?

MQTT can work alongside HTTP and other data transfer protocols. Since it operates over TCP/IP, it can be used with just about any network type and is not limited to HTTP(S) as in the case of RESTful services.


Enjoy data logging with ESP32 and MQTT!