Using RabbitMQ for Messaging with a Homemade ESP32 Sensor

  ·   4 min read

Using RabbitMQ for Messaging with a Homemade ESP32 Sensor

In IoT projects, effective communication between devices is crucial for success. Whether you’re building a smart home setup or deploying sensors for environmental monitoring, a robust message broker like RabbitMQ can help manage data transmission efficiently. In this guide, we’ll show you how to set up a homemade ESP32 sensor to send data to a RabbitMQ server, facilitating reliable and scalable messaging.

If you are not already running RabbitMQ, we encourage you to check out our detailed article on running RabbitMQ in Docker and tuning it for performance, which covers how to set up RabbitMQ quickly and ensure it runs optimally.

Why Use RabbitMQ for IoT Messaging?

  • Scalability: RabbitMQ can handle a high volume of messages, making it suitable for systems with multiple IoT devices.
  • Reliability: It provides guaranteed message delivery, ensuring that your data gets where it needs to go without loss.
  • Versatility: RabbitMQ supports multiple messaging protocols (AMQP, MQTT, WebSockets), allowing seamless integration with various IoT devices, including ESP32 sensors.
  • Open Source: RabbitMQ is free to use and highly customizable, making it an excellent choice for hobbyists and professionals alike.

Setting Up RabbitMQ

To use RabbitMQ with your ESP32 sensor, you need a RabbitMQ server up and running. If you haven’t set it up yet, you can follow our guide on running RabbitMQ in Docker for easy installation and configuration.

Programming the ESP32 to Send Data to RabbitMQ

Now, let’s focus on configuring the ESP32 to send sensor data to RabbitMQ. For this example, we’ll assume you have a temperature and humidity sensor (e.g., DHT11/DHT22) connected to the ESP32.

1. Required Components

  • ESP32 Development Board
  • DHT11/DHT22 Sensor (or another compatible sensor)
  • Jumper wires
  • Breadboard

2. Setting Up the Hardware

Connect the DHT11/DHT22 sensor to the ESP32 as follows:

Sensor Pin Connection to ESP32
VCC 3.3V
GND GND
DATA GPIO 4

3. Programming the ESP32

To communicate with RabbitMQ, the ESP32 will send messages using the MQTT protocol. This protocol is lightweight and supported by RabbitMQ, making it ideal for IoT devices.

  1. Install Arduino IDE Libraries:

    • ESP32 Core: Follow the official guide to set up the ESP32 with Arduino IDE.
    • PubSubClient: For MQTT communication, install the PubSubClient library.
  2. ESP32 Code:

    #include <WiFi.h>
    #include <PubSubClient.h>
    #include <DHT.h>
    
    // WiFi configuration
    const char* ssid = "your-SSID";
    const char* password = "your-PASSWORD";
    
    // MQTT Broker configuration
    const char* mqtt_server = "your-rabbitmq-server-ip";
    const int mqtt_port = 1883;
    const char* mqtt_user = "guest";
    const char* mqtt_password = "guest";
    
    // Sensor configuration
    #define DHTPIN 4
    #define DHTTYPE DHT11
    DHT dht(DHTPIN, DHTTYPE);
    
    WiFiClient espClient;
    PubSubClient client(espClient);
    
    void setup() {
      Serial.begin(115200);
      dht.begin();
      setup_wifi();
      client.setServer(mqtt_server, mqtt_port);
    }
    
    void setup_wifi() {
      delay(10);
      Serial.println();
      Serial.print("Connecting to ");
      Serial.println(ssid);
      WiFi.begin(ssid, password);
    
      while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.print(".");
      }
    
      Serial.println("");
      Serial.println("WiFi connected");
    }
    
    void reconnect() {
      while (!client.connected()) {
        Serial.print("Attempting MQTT connection...");
        if (client.connect("ESP32Client", mqtt_user, mqtt_password)) {
          Serial.println("connected");
        } else {
          Serial.print("failed, rc=");
          Serial.print(client.state());
          Serial.println(" try again in 5 seconds");
          delay(5000);
        }
      }
    }
    
    void loop() {
      if (!client.connected()) {
        reconnect();
      }
      client.loop();
    
      float h = dht.readHumidity();
      float t = dht.readTemperature();
    
      if (isnan(h) || isnan(t)) {
        Serial.println("Failed to read from DHT sensor!");
        return;
      }
    
      String payload = "{\"temperature\": " + String(t) + ", \"humidity\": " + String(h) + "}";
      Serial.print("Sending payload: ");
      Serial.println(payload);
      client.publish("sensor_data_queue", payload.c_str());
      delay(10000);
    }
    

Receiving Data from RabbitMQ

Once your ESP32 starts sending data, you can set up a consumer to receive and process these messages. Below is an example using Python to consume messages from RabbitMQ.

Python Code to Consume Messages

  1. Install Required Libraries:

    pip install pika
    
  2. Python Consumer Code:

    import pika
    
    # RabbitMQ connection settings
    credentials = pika.PlainCredentials('guest', 'guest')
    connection = pika.BlockingConnection(
        pika.ConnectionParameters(host='your-rabbitmq-server-ip', credentials=credentials))
    channel = connection.channel()
    
    # Declare the queue
    channel.queue_declare(queue='sensor_data_queue')
    
    def callback(ch, method, properties, body):
        print(f"Received {body}")
    
    # Set up subscription on the queue
    channel.basic_consume(queue='sensor_data_queue', on_message_callback=callback, auto_ack=True)
    
    print('Waiting for messages. To exit press CTRL+C')
    channel.start_consuming()
    

Conclusion

By integrating RabbitMQ with ESP32 sensors, you can create a scalable and reliable IoT messaging system. RabbitMQ handles the complexity of message routing, acknowledgment, and persistence, allowing your sensors to send data effortlessly. For those new to RabbitMQ, we highly recommend reading our guide on running RabbitMQ in Docker and tuning it for performance to set up and optimize your messaging broker easily.

References

Following this guide, you should be able to set up an efficient communication framework between your homemade ESP32 sensor and RabbitMQ, enabling reliable data collection for your IoT projects.