Introduction
In this tutorial, we will learn how to connect an ESP32 to Wi-Fi and set up a simple web server using the AsyncWebServer library. This will allow us to monitor temperature data in real-time. We’ll be using a DHT11 or DHT22 temperature and humidity sensor for data collection.
Prerequisites
- Basic knowledge of Arduino IDE and programming concepts.
- ESP32 development board.
- DHT11 or DHT22 temperature and humidity sensor.
- Jumper wires for connections.
- Arduino IDE installed on your computer.
- AsyncWebServer library installed in the Arduino IDE.
- DHT sensor library installed in the Arduino IDE.
Parts/Tools
- ESP32 development board
- DHT11 or DHT22 sensor
- Jumper wires
- Computer with Arduino IDE
Steps
- Setup the Hardware
- Connect the DHT sensor to the ESP32 as follows:
- VCC (Sensor) to 3.3V (ESP32)
- GND (Sensor) to GND (ESP32)
- Data (Sensor) to GPIO Pin (e.g., GPIO 23)
- Install Required Libraries
- Open the Arduino IDE.
- Go to Sketch > Include Library > Manage Libraries.
- Search for “AsyncWebServer” and “DHT sensor library”. Install both.
- Write the Code
Open a new sketch in Arduino IDE and use the following code:
#include <WiFi.h> #include <AsyncTCP.h> #include <ESPAsyncWebServer.h> #include <DHT.h> #define DHTPIN 23 #define DHTTYPE DHT22 DHT dht(DHTPIN, DHTTYPE); const char* ssid = "your_SSID"; const char* password = "your_PASSWORD"; 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"); server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ request->send(200, "text/html", "
ESP32 Temperature Monitor
Temperature: " + String(dht.readTemperature()) + " ℃
"); }); server.begin(); } void loop() { // Nothing here }Replace your_SSID and your_PASSWORD with your actual Wi-Fi credentials.
- Upload the Code to ESP32
- Select the correct board from Tools > Board (ESP32 Dev Module).
- Select the correct port from Tools > Port.
- Click on the upload button in the Arduino IDE.
- Access the Web Server
- Open the Serial Monitor in Arduino IDE.
- Note down the IP address displayed after successful Wi-Fi connection.
- Open a web browser and enter the IP address.
Troubleshooting
- Wi-Fi Connection Issues:
If the ESP32 doesn’t connect to Wi-Fi, double-check your SSID and password. Make sure the ESP32 is within range of the Wi-Fi network.
- Sensor Not Working:
If you are not seeing temperature readings, ensure that the sensor is wired correctly and is powered properly.
- Web Server Not Responding:
Make sure the ESP32 has successfully connected to Wi-Fi and check the IP address in the Serial Monitor.
Conclusion
In this tutorial, we successfully connected the ESP32 to Wi-Fi and set up a simple web server using the AsyncWebServer library to monitor temperature data in real-time. You can extend this project by adding more features, such as humidity monitoring or logging data to a cloud service.