Connecting ESP32 with DS18B20 Sensor and Publishing Data Using HTTP POST Request

  ·   3 min read

The ESP32 microcontroller is a powerful and versatile platform for IoT applications, offering built-in Wi-Fi and Bluetooth capabilities. When paired with the DS18B20 temperature sensor, it becomes a robust solution for monitoring environmental conditions. In this article, we’ll explore how to connect the DS18B20 sensor to the ESP32 and publish the temperature data using an HTTP POST request.

Components Required

  1. ESP32 Development Board: A microcontroller with Wi-Fi and Bluetooth capabilities.
  2. DS18B20 Temperature Sensor: A digital temperature sensor with a 1-Wire interface.
  3. 4.7kΩ Resistor: Used as a pull-up resistor for the data line.
  4. Jumper Wires: For making connections.
  5. Breadboard: For prototyping.

Wiring Diagram

To connect the DS18B20 sensor to the ESP32, follow these steps:

  1. Connect the VCC pin of the DS18B20 to the 3.3V pin on the ESP32.
  2. Connect the GND pin of the DS18B20 to a GND pin on the ESP32.
  3. Connect the DATA pin of the DS18B20 to a GPIO pin on the ESP32 (e.g., GPIO 4).
  4. Connect a 4.7kΩ resistor between the DATA pin and the VCC pin of the DS18B20.

Programming the ESP32

To program the ESP32, we’ll use the Arduino IDE. Ensure you have the ESP32 board package installed in your Arduino IDE. Additionally, you’ll need the following libraries:

  • OneWire: For communication with the DS18B20 sensor.
  • DallasTemperature: For reading temperature data from the DS18B20.
  • WiFi: For connecting to a Wi-Fi network.
  • HTTPClient: For making HTTP POST requests.

Code Example

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

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

// Replace with your server URL
const char* serverUrl = "http://yourserver.com/temperature";

// Data wire is plugged into GPIO 4 on the ESP32
#define ONE_WIRE_BUS 4

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

void setup() {
  Serial.begin(115200);
  sensors.begin();

  // 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");
}

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

  if (temperatureC != DEVICE_DISCONNECTED_C) {
    Serial.print("Temperature: ");
    Serial.println(temperatureC);

    if (WiFi.status() == WL_CONNECTED) {
      HTTPClient http;
      http.begin(serverUrl);
      http.addHeader("Content-Type", "application/json");

      String postData = "{\"temperature\": " + String(temperatureC) + "}";
      int httpResponseCode = http.POST(postData);

      if (httpResponseCode > 0) {
        String response = http.getString();
        Serial.println(httpResponseCode);
        Serial.println(response);
      } else {
        Serial.print("Error on sending POST: ");
        Serial.println(httpResponseCode);
      }
      http.end();
    }
  } else {
    Serial.println("Error: Could not read temperature data");
  }

  delay(10000); // Send data every 10 seconds
}

Explanation

  • WiFi Connection: The ESP32 connects to a Wi-Fi network using the provided SSID and password.
  • Temperature Reading: The DS18B20 sensor is read using the DallasTemperature library.
  • HTTP POST Request: The temperature data is sent to a server using an HTTP POST request. The data is formatted as a JSON object.

Conclusion

By following the steps outlined in this article, you can successfully connect an ESP32 to a DS18B20 sensor and publish temperature data to a server using HTTP POST requests. This setup is ideal for IoT applications where remote monitoring of environmental conditions is required. The ESP32’s built-in Wi-Fi capabilities make it a perfect choice for such projects.

References