How to Monitor DHT11 Sensor Data on Web Server using NodeMCU


NodeMCU Weather Station (Web Server)



Creating a project to monitor DHT11 sensor data (Temperature and Humidity) on a Web Server using NodeMCU (ESP8266) is a great way to view sensor data in real-time. In this tutorial, we will use NodeMCU to read data from the DHT11 sensor and host a simple web server to display the data.

Components Needed:

  1. NodeMCU (ESP8266)
  2. DHT11 Sensor (Temperature and Humidity Sensor)
  3. Jumper Wires
  4. Breadboard
  5. Micro USB Cable (for NodeMCU)
  6. Arduino IDE (for programming the NodeMCU)

Wiring the Circuit:

  1. DHT11 to NodeMCU Connections:
    • VCC pin of DHT11 → 3V pin of NodeMCU
    • GND pin of DHT11 → GND pin of NodeMCU
    • DATA pin of DHT11 → D2 (GPIO4) pin of NodeMCU

Steps to Monitor DHT11 Data on a Web Server:

Step 1: Install the Arduino IDE

If you don't have it yet, install the Arduino IDE from the official website.

Step 2: Install NodeMCU Board in Arduino IDE

  1. Open the Arduino IDE.
  2. Go to File → Preferences.
  3. In the Additional Boards Manager URLs field, add the following link:http://arduino.esp8266.com/stable/package_esp8266com_index.json
4. Now go to Tools → Board → Board Manager.
5. Search for esp8266 and click Install.

Step 3: Install the DHT11 Library

  1. In the Arduino IDE, go to Sketch → Include Library → Manage Libraries.
  2. In the Library Manager, search for DHT sensor library and click Install.

Step 4: Write the Code

The code will read temperature and humidity values from the DHT11 sensor and serve them on a web page hosted on the NodeMCU.


#include <ESP8266WiFi.h>
#include <DHT.h>

// DHT11 Sensor Pin
#define DHTPIN D2  // Pin where the DHT11 data pin is connected

// Set up the DHT sensor
#define DHTTYPE DHT11   // Define the sensor type
DHT dht(DHTPIN, DHTTYPE);

// WiFi Credentials
const char* ssid = "YourWiFiSSID";
const char* password = "YourWiFiPassword";

// Create an instance of the WiFi server
WiFiServer server(80);  // HTTP server will listen on port 80

void setup() {
  // Start serial communication
  Serial.begin(115200);
  delay(10);

  // Initialize the DHT sensor
  dht.begin();

  // Connect to WiFi
  Serial.println();
  Serial.print("Connecting to WiFi...");
  WiFi.begin(ssid, password);

  // Wait for WiFi to connect
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("Connected to WiFi");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());

  // Start the server
  server.begin();
}

void loop() {
  // Wait for a client to connect
  WiFiClient client = server.available();
 
  if (client) {
    String currentLine = "";  // Initialize an empty string to read the HTTP request
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        currentLine += c;

        // If the request is complete
        if (c == '\n') {
          // If HTTP request is a GET
          if (currentLine.startsWith("GET / ")) {
            // Read temperature and humidity from DHT11 sensor
            float temperature = dht.readTemperature();
            float humidity = dht.readHumidity();

            // Check if any of the readings failed
            if (isnan(temperature) || isnan(humidity)) {
              Serial.println("Failed to read from DHT sensor");
              return;
            }

            // Serve the HTML page with sensor data
            String html = "<!DOCTYPE HTML><html>";
            html += "<head><meta http-equiv='refresh' content='5'></head>"; // Auto refresh every 5 seconds
            html += "<body><h1>NodeMCU Weather Station</h1>";
            html += "<p>Temperature: " + String(temperature) + " &#8451;</p>";
            html += "<p>Humidity: " + String(humidity) + " %</p>";
            html += "</body></html>";

            client.print("HTTP/1.1 200 OK\r\n");
            client.print("Content-Type: text/html\r\n");
            client.print("Connection: close\r\n\r\n");
            client.print(html);
            break;
          } else {
            // Handle the HTTP request that isn't a GET request
            currentLine = "";
          }
        }
      }
    }
    // Give the client time to receive the data
    delay(10);
    client.stop();
  }
}

Step 5: Explanation of the Code

  • WiFi Setup: The NodeMCU connects to your Wi-Fi network using the provided SSID and password.
  • DHT11 Sensor: The DHT11 sensor reads the temperature and humidity values, which are displayed on the web page.
  • Web Server: The code creates a simple web server that serves an HTML page with the current temperature and humidity. The page automatically refreshes every 5 seconds to show updated data.
  • HTML Page: The webpage displays the temperature and humidity in Celsius and percentage, respectively.

Step 6: Upload the Code

  1. Select the NodeMCU board from the Tools → Board menu.
  2. Select the correct COM port.
  3. Click the Upload button in the Arduino IDE to upload the code to the NodeMCU.

Step 7: Monitor the Data

  1. After the upload, the NodeMCU will connect to your Wi-Fi and start the web server.
  2. Open the Serial Monitor to see the IP address of the NodeMCU.
  3. Enter the IP address shown in the Serial Monitor into your browser's address bar (e.g., http://192.168.1.100).
  4. You should now see a webpage showing the current Temperature and Humidity values. The page will refresh automatically every 5 seconds.

Optional Enhancements:

  1. Styling the Web Page: You can use CSS to improve the look of the webpage.
  2. Multiple Sensors: If you have multiple DHT11 sensors, you can modify the code to read from and display data from all sensors.
  3. Mobile-Friendly Interface: Modify the HTML and CSS to make the page mobile-responsive.

Troubleshooting:

  • Wi-Fi Connectivity Issues: Double-check your Wi-Fi SSID and password.
  • Sensor Readings: Ensure the DHT11 sensor is correctly connected to the NodeMCU. If the readings fail, check the wiring.

  • No Webpage: If the webpage doesn't load, make sure the NodeMCU has connected to Wi-Fi (check the Serial Monitor for the IP address).

Conclusion:

You have successfully built a web server using NodeMCU to monitor DHT11 sensor data for Temperature and Humidity. This setup allows you to access sensor data remotely through any web browser. You can expand this project by adding more sensors, improving the web interface, or even storing data on platforms like ThingSpeak or Firebase.


No comments:

Post a Comment

Wifi Home automation Diy kit

 Wifi Home automation Diy kit By Manmohan Pal Wifi Home Automation Kit Hi, This is Manmohan Pal, I am glad to present a DIY kit for Home Aut...