Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

ESP8266 NodeMCU with DHT11 Sensor for Wi-Fi Temperature and Humidity Monitoring

View and Edit Project In Cirkit DesignerImage of ESP8266 NodeMCU with DHT11 Sensor for Wi-Fi Temperature and Humidity Monitoring

Circuit Documentation

Summary

The circuit in question is designed to interface an ESP8266 NodeMCU microcontroller with a DHT11 Sensor V2. The ESP8266 NodeMCU is responsible for reading temperature and humidity data from the DHT11 sensor and then sending this data to a ThingSpeak server over WiFi. The ESP8266 NodeMCU is powered by a 3.3V supply and communicates with the DHT11 sensor via one of its digital pins.

Component List

ESP8266 NodeMCU

DHT11 Sensor V2

Wiring Details

ESP8266 NodeMCU

DHT11 Sensor V2

Documented Code

The following code is written for the ESP8266 NodeMCU to interact with the DHT11 sensor and send data to ThingSpeak.

#include <DHT.h>      // Library for DHT11 sensor
#include <WiFi.h>    // Library for ESP32 WiFi

String apiKey = "DJZHJBBFHBZY0RYK";   // Enter your Write API key from ThingSpeak
const char *ssid = "Redmi 9 Prime";    // Replace with your WiFi SSID
const char *pass = "Redmi 9 Prime";    // Replace with your WiFi password
const char* server = "api.thingspeak.com";

#define DHTPIN 4         // Pin where the DHT11 is connected (change to your actual pin)

DHT dht(DHTPIN, DHT11);
WiFiClient client;

void setup() {
  Serial.begin(115200);
  delay(10);
  dht.begin();

  Serial.println("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, pass);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
}

void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  if (client.connect(server, 80)) {
    String postStr = "api_key=" + apiKey;
    postStr += "&field1=" + String(t);
    postStr += "&field2=" + String(h);
    postStr += "\r\n\r\n";

    client.print("POST /update HTTP/1.1\n");
    client.print("Host: api.thingspeak.com\n");
    client.print("Connection: close\n");
    client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(postStr.length());
    client.print("\n\n");
    client.print(postStr);

    Serial.print("Temperature: ");
    Serial.print(t);
    Serial.print(" degrees Celsius, Humidity: ");
    Serial.print(h);
    Serial.println("% sent to ThingSpeak.");
  }
  client.stop();

  Serial.println("Waiting...");

  // ThingSpeak requires a minimum 15-second delay between updates
  delay(15000);
}

This code initializes the DHT11 sensor and the WiFi connection, reads temperature and humidity data, and sends it to the ThingSpeak server. It also includes error handling for failed sensor readings and prints the data to the serial monitor for debugging purposes.