I bought this ESP8266 nodeMCU v0.1(this version number is on board) and some wired DHT22 sensors. When I try sensors on arduino uno, they work normal. But, when I connect them to this nodeMCU I get Serial output: Temperature: nan, Humidity: nan.. I used Generic ESP8266 Module for board, and this link in preferance. This is my code:

#include <DHT.h>
#include <DHT_U.h> 
const int refresh=3;//3 seconds
// DHT settings starts

#define DHTPIN 2     // what digital pin we're connected to
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)
float tValue;// 
// ****** DHT settings end (Robojax.com )

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

#ifndef STASSID
#define STASSID "myWifi" // Your WiFi SSID
#define STAPSK  "myPassword" //Your WiFi password

const char* ssid = STASSID;
const char* password = STAPSK;

ESP8266WebServer server(80);

void sendTemp() {

  String page = "<!DOCTYPE html>";
  page +="    <meta http-equiv='refresh' content='";
  page += String(refresh);// how often temperature is read
  page +="'/>";  
  page +="<html>";
  page +="<body>"; 
  page +="<h1>Robojax.com DHT Code</h1>";    
  page +="<p style=\"font-size:50px;\">Temperature<br/>";  
  page +="<p style=\"color:red; font-size:50px;\">";
  page += String(tValue, 2);
  page +="</p>";  
  page +="</body>";  
  page +="</html>";  
 server.send(200,  "text/html",page);


void handleNotFound() {
  String message = "File Not Found";
  message += "URI: ";
  message += server.uri();
  message += "Method: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "Arguments: ";
  message += server.args();
  message += "";
  for (uint8_t i = 0; i < server.args(); i++) {
    message += " " + server.argName(i) + ": " + server.arg(i) + "";
  server.send(404, "text/plain", message);


void setup(void) {
  // Robojax.com code for ESP8266 and DHT11 DHT22
  WiFi.begin(ssid, password);

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
  if (WiFi.status() == WL_CONNECTED)
    uint8_t macAddr[6];

  Serial.print("Connected to ");
  Serial.print("IP address: ");

  if (MDNS.begin("robojaxDHT")) {
    Serial.println("MDNS responder started");

  server.on("/", sendTemp);

  server.on("/inline", []() {
    server.send(200, "text/plain", "this works as well");


  Serial.println("HTTP server started");

void loop(void) {
  // Robojax.com code for ESP8266 and DHT11 DHT22
  float c = dht.readTemperature();// Read temperature as Celsius (the default)
  float h = dht.readHumidity();// Reading humidity 
  float f = dht.readTemperature(true);// Read temperature as Fahrenheit (isFahrenheit = true)
    Serial.println("Temperature: ");
    Serial.println("Humidity: ");
 tValue =c;
  delay(3000);// change this to larger value (1000 or more) if you don't need very often reading
  // Robojax.com code for ESP8266 and DHT11 DHT22

I followed this video for setup, and I open this MDSN robojaxDHT, so ESP8266 is sending data via Wifi but my reading for DHT22 is nan. I tried different wiring combinations,but none worked. Here I connected dht22 data to D2, dht Vcc to 3v in right bottom corner of esp8266, and gnd to GND above this 3v.Any idea what I am doing wrong?

New contributor
Lube is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • 2
    Pin D2 on the ESP8266 is not GPIO pin 2, as you seem to assume in your code. It's GPIO pin 4. GPIOs on the ESP8266 are not numbered the same as their "Dx" counterparts. You can find a description of the various pins and a GPIO to Dx "translation" table in this link.
    – StarCat
    Jun 22 at 19:47
  • I wired to D4 but it didn't work. Then I changed in code to pin 4 and wired to D2 and now it works. Thank you
    – Lube
    Jun 23 at 7:58


Your Answer

Lube is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.