How to Set Up ESP32 Wi-Fi and HTTP Server for DHT11 Temperature Data

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

  1. Install Required Libraries
    1. Open Arduino IDE.
    2. Go to Sketch > Include Library > Manage Libraries…
    3. Search for and install the AsyncWebServer library.
    4. Search for and install the DHT sensor library by Adafruit.
  2. Wiring the DHT11 Sensor
    1. 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
  3. Write the Code
    1. Open a new sketch in Arduino IDE.
    2. Include necessary libraries at the top of your sketch:
    3. 
      #include <WiFi.h>
      #include <AsyncWebServer.h>
      #include <DHT.h>
                  
    4. Define your Wi-Fi credentials:
    5. 
      const char* ssid = "YOUR_SSID";
      const char* password = "YOUR_PASSWORD";
                  
    6. Set up DHT sensor configuration:
    7. 
      #define DHTPIN 4
      #define DHTTYPE DHT11
      DHT dht(DHTPIN, DHTTYPE);
                  
    8. Initialize server:
    9. 
      AsyncWebServer server(80);
                  
    10. Write the setup function:
    11. 
      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.");
                  
    12. Set up the HTTP GET route:
    13. 
          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);
          });
                  
    14. Start the server:
    15. 
          server.begin();
      }
                  
    16. Write the loop function:
    17. 
      void loop() {
          // Nothing here for now
      }
                  
  4. Upload the Code
    1. Select your ESP32 board from Tools > Board.
    2. Select the correct COM port from Tools > Port.
    3. Click on the upload button to upload your code to the ESP32.
  5. Access the HTTP Server
    1. Once uploaded, open the Serial Monitor (set baud rate to 115200).
    2. Note the IP address displayed in the Serial Monitor.
    3. 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.

Leave a Comment

Your email address will not be published. Required fields are marked *