Introduction
In this tutorial, you will learn how to establish a Wi-Fi connection using the ESP32 and set up a simple HTTP server using the AsyncWebServer library. The server will serve temperature data collected from a DHT11 sensor. We will guide you through the necessary prerequisites, parts, setup steps, and troubleshooting tips to ensure a successful implementation.
Prerequisites
- Basic understanding of Arduino IDE
- ESP32 development board
- DHT11 temperature and humidity sensor
- Jumper wires
- Wi-Fi network for connection
- Arduino IDE installed on your computer
Parts/Tools
- ESP32 Development Board
- DHT11 Sensor
- Jumper Wires
- Arduino IDE with ESP32 board support
- AsyncWebServer library
- DHT sensor library
Steps
- Install Required Libraries
- Open Arduino IDE.
- Go to Sketch > Include Library > Manage Libraries…
- Search for and install the AsyncWebServer library.
- Search for and install the DHT sensor library by Adafruit.
- Wiring the DHT11 Sensor
- Connect the DHT11 sensor to the ESP32 as follows:
- VCC to 3.3V on ESP32
- GND to GND on ESP32
- DATA to D4 on ESP32
- Connect the DHT11 sensor to the ESP32 as follows:
- Write the Code
- Open a new sketch in Arduino IDE.
- Include necessary libraries at the top of your sketch:
- Define your Wi-Fi credentials:
- Set up DHT sensor configuration:
- Initialize server:
- Write the setup function:
#include <WiFi.h> #include <AsyncWebServer.h> #include <DHT.h>
const char* ssid = "YOUR_SSID"; const char* password = "YOUR_PASSWORD";
#define DHTPIN 4 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE);
AsyncWebServer server(80);
void setup() { Serial.begin(115200); dht.begin(); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("Connected to WiFi.");
- Set up the HTTP GET route:
- Start the server:
- Write the loop function:
server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
float h = dht.readHumidity();
float t = dht.readTemperature();
String jsonResponse = "{"temperature": " + String(t) + ", "humidity": " + String(h) + "}";
request->send(200, "application/json", jsonResponse);
});
server.begin();
}
void loop() {
// Nothing here for now
}
- Select your ESP32 board from Tools > Board.
- Select the correct COM port from Tools > Port.
- Click on the upload button to upload your code to the ESP32.
- Once uploaded, open the Serial Monitor (set baud rate to 115200).
- Note the IP address displayed in the Serial Monitor.
- Open a web browser and navigate to http:///temperature to view the temperature data.
Troubleshooting
- Wi-Fi Connection Issues: Ensure your SSID and password are correct. Check if the ESP32 is within range of your Wi-Fi network.
- Sensor Not Responding: Verify the wiring of the DHT11 sensor. Ensure that the correct pin is defined in the code.
- HTTP Server Not Responding: Check if the server is running and the IP address is being accessed correctly.
- Code Upload Errors: Make sure the correct board and port are selected in the Arduino IDE.
Conclusion
In this tutorial, you successfully set up a Wi-Fi connection and a simple HTTP server on the ESP32 that serves temperature data from a DHT11 sensor. This project provides a foundation for further exploration into IoT applications and web servers with the ESP32. Feel free to modify and expand upon this code to fit your needs.