The Internet of Things (IoT) has revolutionized the way we interact with devices, enabling seamless communication and data exchange. In this article, we will explore how to connect an ESP32 microcontroller with a DS18B20 temperature sensor and publish the temperature data to the ThingSpeak cloud service using HTTP. This setup is ideal for remote temperature monitoring applications.
Components Required
- ESP32 Development Board: A powerful microcontroller with built-in Wi-Fi and Bluetooth capabilities.
- DS18B20 Temperature Sensor: A digital temperature sensor that provides accurate temperature readings.
- Resistor (4.7k ohm): Used as a pull-up resistor for the DS18B20 data line.
- Jumper Wires: For making connections between the components.
- Breadboard: For prototyping the circuit.
Circuit Diagram
To connect the DS18B20 sensor to the ESP32, follow the circuit diagram below:
- Connect the VDD pin of the DS18B20 to the 3.3V pin of the ESP32.
- Connect the GND pin of the DS18B20 to a GND pin on the ESP32.
- Connect the Data pin of the DS18B20 to GPIO 4 on the ESP32.
- Place a 4.7k ohm resistor between the VDD and Data pins of the DS18B20.
Setting Up the Development Environment
-
Arduino IDE: Install the Arduino IDE from the official website and add the ESP32 board support by following the instructions on the ESP32 Arduino Core GitHub page.
-
Libraries: Install the following libraries from the Arduino Library Manager:
OneWire
: For communication with the DS18B20 sensor.DallasTemperature
: For handling temperature readings from the DS18B20.
Writing the Code
Below is a sample code to read temperature data from the DS18B20 sensor and publish it to ThingSpeak:
#include <WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <HTTPClient.h>
// Replace with your network credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
// ThingSpeak settings
const char* server = "http://api.thingspeak.com";
String apiKey = "YOUR_THINGSPEAK_API_KEY";
// Data wire is connected to GPIO 4
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
sensors.begin();
}
void loop() {
sensors.requestTemperatures();
float temperatureC = sensors.getTempCByIndex(0);
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = server + "/update?api_key=" + apiKey + "&field1=" + String(temperatureC);
http.begin(url);
int httpResponseCode = http.GET();
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 in WiFi connection");
}
delay(20000); // ThingSpeak allows updates every 15 seconds
}
Explanation
- WiFi Connection: The ESP32 connects to a WiFi network using the provided SSID and password.
- Temperature Reading: The DS18B20 sensor is read using the
DallasTemperature
library. - HTTP Request: The temperature data is sent to ThingSpeak using an HTTP GET request. Replace
YOUR_THINGSPEAK_API_KEY
with your actual ThingSpeak API key.
Conclusion
By following the steps outlined in this article, you can successfully connect an ESP32 to a DS18B20 sensor and publish temperature data to ThingSpeak. This setup can be expanded to include multiple sensors or other types of data, making it a versatile solution for various IoT applications.