Building a Sensor using ESP32, DS18B20, and RabbitMQ

  ·   3 min read

In the ever-evolving world of Internet of Things (IoT), the ability to gather and transmit data from sensors to a backend system is paramount. In this article, we will explore how to build a temperature sensor using the ESP32 microcontroller, the DS18B20 temperature sensor, and RabbitMQ for messaging. This project demonstrates a practical application of IoT concepts while embracing DevOps practices through effective monitoring and communication.

Components Required

  1. ESP32 Microcontroller: A low-cost, low-power system-on-chip with integrated Wi-Fi and dual-mode Bluetooth capabilities.
  2. DS18B20 Temperature Sensor: A widely used digital temperature sensor that communicates over a 1-Wire interface.
  3. RabbitMQ: An open-source message broker that facilitates communication between applications and microservices.
  4. Jumper Wires: For connecting the components.
  5. Breadboard: Optional, for easy prototyping.

Wiring the Components

The DS18B20 sensor connects to the ESP32 through the 1-Wire interface. The wiring is straightforward:

  • DS18B20 Pinout:
    • VDD (Power) connects to the ESP32 3.3V pin.
    • GND connects to ground.
    • Data pin connects to any GPIO pin (let’s use GPIO 21 for this example).

Here’s how the connections look:

ESP32        DS18B20
----------------------
3.3V  -->   VDD
GND   -->   GND
GPIO 21 -->   Data

Setting Up the Development Environment

To program the ESP32, we’ll be using the Arduino IDE due to its simplicity and open-source nature.

  1. Install the ESP32 Board: Follow the instructions on ESP32 Arduino documentation to set up the board in the Arduino IDE.
  2. Install Required Libraries: You’ll need the following libraries:
    • OneWire: For communicating with the DS18B20 sensor.
    • DallasTemperature: For reading temperature from the DS18B20.
    • HTTPClient: To send data to RabbitMQ.

You can install these libraries through the Library Manager in the Arduino IDE.

The Code

Here’s a basic example of how to read the temperature from the DS18B20 sensor and publish it to RabbitMQ using the Arduino IDE:

#include <OneWire.h>
#include <DallasTemperature.h>
#include <WiFi.h>
#include <HTTPClient.h>

// Replace with your network credentials
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";

// RabbitMQ Configuration
const char* rabbitMQServer = "http://YOUR_RABBITMQ_SERVER/api/exchanges/YOUR_VHOST/YOUR_EXCHANGE/publish";
const char* rabbitMQUser = "USERNAME";
const char* rabbitMQPass = "PASSWORD";

#define ONE_WIRE_BUS 21
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

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

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

void loop() {
  sensors.requestTemperatures(); 
  float temperature = sensors.getTempCByIndex(0);
  Serial.print("Temperature: ");
  Serial.println(temperature);

  publishToRabbitMQ(temperature);
  delay(5000); // Publish every 5 seconds
}

void publishToRabbitMQ(float temperature) {
  if(WiFi.status() == WL_CONNECTED) {
    HTTPClient http;

    String message = "{\"properties\":{},\"body\":\"" + String(temperature) + "\"}";
    http.begin(rabbitMQServer);
    http.addHeader("Content-Type", "application/json");
    http.setAuthorization(rabbitMQUser, rabbitMQPass);

    int httpResponseCode = http.POST(message);
 
    if (httpResponseCode > 0) {
      Serial.printf("Message sent: %s\n", message.c_str());
    } else {
      Serial.printf("Error sending message: %s\n", http.errorToString(httpResponseCode).c_str());
    }
    http.end();
  }
}

Explanation of the Code

  1. WiFi Connection: We connect to a Wi-Fi network in the setup() function.
  2. Temperature Reading: The temperature is requested from the sensor every 5 seconds.
  3. Publishing to RabbitMQ: The publishToRabbitMQ() function sends the temperature data to RabbitMQ using an HTTP POST request.

RabbitMQ Setup

Before running the ESP32 code, ensure that you have RabbitMQ installed and configured. You can follow the installation guide from the official RabbitMQ documentation. Once RabbitMQ is up and running, create an exchange where messages will be published.

Conclusion

Building a sensor with ESP32, DS18B20, and RabbitMQ showcases the power of IoT and messaging systems in real-time data collection and processing. By following this guide, you now have the foundational aspects to expand this project further, explore new sensors, or integrate other messaging queues like Apache Kafka or MQTT as the need arises.

Additional Resources

By leveraging these technologies, you not only create a functional application but also gain a deeper understanding of the continuous integration and delivery practices prevalent in modern DevOps environments. Happy coding!