Building a Smart Sensor with ESP32 and DS18B20 for Data Storage

  ·   3 min read

In the world of IoT, creating a smart sensor can be a fun and rewarding project. Today, we will build a temperature sensor using the ESP32 microcontroller and the DS18B20 temperature sensor. We will then explore how to send the collected data to an online service for storage and analysis. This setup is not only great for home automation but can also be adapted for industrial monitoring.

Components Required

  1. ESP32 Development Board - The main microcontroller that will read temperature data and send it to the cloud.
  2. DS18B20 Temperature Sensor - A digital temperature sensor with excellent accuracy.
  3. Pull-up Resistor (4.7kΩ) - Needed for the DS18B20 sensor.
  4. Breadboard and Jumper Wires - For prototyping the circuit.
  5. Online Data Storage Service - We will use an open-source service like ThingSpeak or a free-tier database like Firebase.

Wiring the Components

  1. Connect the DS18B20:
    • Connect the GND pin of the DS18B20 to the GND pin of the ESP32.
    • Connect the VDD pin of the DS18B20 to the 3.3V or 5V pin of the ESP32, depending on your power supply.
    • Connect the DATA pin of the DS18B20 to a GPIO pin on the ESP32 (e.g., GPIO 16). Don’t forget to add the 4.7kΩ pull-up resistor between the DATA and VDD pins.

Setting Up the Software Environment

  1. Install the Arduino IDE: The ESP32 can be programmed using the Arduino IDE. Make sure you have installed the ESP32 board definitions.
  2. Install Required Libraries: You will need the following libraries:
    • OneWire for communicating with the DS18B20.
    • DallasTemperature for temperature reading functionality.
    • HTTPClient (part of the ESP32 core) for sending data to the cloud.

Example Code

Here’s a simple code snippet to get you started:

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

const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
const String serverName = "http://api.thingspeak.com/update?api_key=YOUR_API_KEY";

OneWire oneWire(16); // GPIO Pin 16
DallasTemperature sensors(&oneWire);

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  sensors.begin();
}

void loop() {
  sensors.requestTemperatures();
  float temperature = sensors.getTempCByIndex(0);

  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    http.begin(serverName + "&field1=" + String(temperature));
    int httpResponseCode = http.GET();
    if (httpResponseCode > 0) {
      String response = http.getString();
      Serial.println(httpResponseCode);
      Serial.println(response);
    } else {
      Serial.print("Error on sending: ");
      Serial.println(httpResponseCode);
    }
    http.end();
  } else {
    Serial.println("WiFi Disconnected");
  }
  delay(60000); // Send data every minute
}

Setting Up the Online Service

Using ThingSpeak:

  1. Create an account on ThingSpeak and create a new channel.
  2. Retrieve the API key for the channel and update the serverName variable in the code.
  3. You can visualize the data using the built-in charts from ThingSpeak.

Using Firebase:

  1. Create a Firebase project and set up Firebase Realtime Database.
  2. Follow the Firebase documentation to get the authentication required and modify the HTTP requests in the code to send data to Firebase.

Conclusion

Building a smart sensor using the ESP32 and DS18B20 is an exciting project that demonstrates the power of IoT. By collecting temperature data and sending it to an online service, you can monitor conditions remotely and visualize data trends. The codes provided can be modified to include more sensors and data types, making it versatile for various applications.

Further Reading and Resources

This project showcases not only the practical application of technology but also encourages experimentation and learning within the devops and IoT community. Happy coding!