NodeMCU IoT Project - DHT11




Introduction: NodeMCU IoT Project - DHT11

About: Osoyoo brand products are mainly focused on STEM education.User experience and customer feeling are always our first priority. Our goal is to create the best designed product to help students and hobbyist to...

Please follow us on facebook, find our new released item and share your idea and Video on how to creatively use our products. You can get cash back or giveaway from us!


In this lesson, we will show how to get the real-time environment temperature/humidity data by using NodeMCU and DHT11, then send the data to an MQTT broker via MQTT protocol, the MQTT client will subscribe to these messages.

NodeMCU board x 1

DHT11 temperature/humidity sensor x 1

Breadboard x 1

jumper wires


Arduino IDE(version 1.6.4+)

ESP8266 Board Package and the Serial Port Driver

MQTT Clirnt (MQTTBox here (

Arduino library: PubSubClient (

Arduino library:DHT (

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Connection Graph

NodeMCU-- DHT11 sensor

3.3v --VCC



Step 2: Code

Connect the NodeMCU to computer via USB cable, copy below code to Arduino IDE

/* ___ ___ ___ _ _ ___ ___ ____ ___ ____
* / _ \ /___)/ _ \| | | |/ _ \ / _ \ / ___) _ \| \ *| |_| |___ | |_| | |_| | |_| | |_| ( (__| |_| | | | | * \___/(___/ \___/ \__ |\___/ \___(_)____)___/|_|_|_| * (____/ * Use NodeMCU to drive DHT11 and send temperature/humidity value to MQTT server * Tutorial URL * CopyRight */ #include #include #include dht DHT;

// Define NodeMCU D3 pin to as temperature data pin of DHT11 #define DHT11_PIN D3

// Update these with values suitable for your network. const char* ssid = "******"; const char* password = "******"; const char* mqtt_server = ""; //const char* mqtt_server = "";

WiFiClient espClient; PubSubClient client(espClient); long lastMsg = 0; char msg[50]; int value = 0;

void setup_wifi() { delay(100); // We start by connecting to a WiFi network Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } randomSeed(micros()); Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); }

void callback(char* topic, byte* payload, unsigned int length) { Serial.print("Command is : ["); Serial.print(topic); int p =(char)payload[0]-'0'; int chk = DHT.read11(DHT11_PIN); // if MQTT comes a 0 message, show humidity if(p==0) { Serial.println("to show humidity!]"); Serial.print(" Humidity is: " ); Serial.print(DHT.humidity, 1); Serial.println('%'); } // if MQTT comes a 1 message, show temperature if(p==1) { // digitalWrite(BUILTIN_LED, HIGH); Serial.println(" is to show temperature!] "); int chk = DHT.read11(DHT11_PIN); Serial.print(" Temp is: " ); Serial.print(DHT.temperature, 1); Serial.println(' C'); } Serial.println(); } //end callback

void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Create a random client ID String clientId = "ESP8266Client-"; clientId += String(random(0xffff), HEX); // Attempt to connect //if you MQTT broker has clientID,username and password //please change following line to if (client.connect(clientId,userName,passWord)) if (client.connect(clientId.c_str())) { Serial.println("connected"); //once connected to MQTT broker, subscribe command if any client.subscribe("OsoyooCommand"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 6 seconds before retrying delay(6000); } } } //end reconnect()

void setup() { Serial.begin(115200); setup_wifi(); client.setServer(mqtt_server, 1883); client.setCallback(callback); int chk = DHT.read11(DHT11_PIN); Serial.print(" Starting Humidity: " ); Serial.print(DHT.humidity, 1); Serial.println('%'); Serial.print(" Starting Temparature "); Serial.print(DHT.temperature, 1); Serial.println('C'); }

void loop() { if (!client.connected()) { reconnect(); } client.loop(); long now = millis(); // read DHT11 sensor every 6 seconds if (now - lastMsg > 6000) { lastMsg = now; int chk = DHT.read11(DHT11_PIN); String msg="real time temperature: "; msg= msg+ DHT.temperature; msg = msg+" C ;real time Humidity: " ; msg=msg+DHT.humidity ; msg=msg+"%"; char message[58]; msg.toCharArray(message,58); Serial.println(message); //publish sensor data to MQTT broker client.publish("OsoyooData", message); } }

Edit the code to fit your own WiFi and MQTT settings as following operations:
1)Hotspot Configration: Find below code line,put your own ssid and password on there.

const char* ssid = “your_hotspot_ssid”;
const char* password = “your_hotspot_password”;

2)MQTT Server Address Setting: You can also use some famous free MQTT server to test the project such as “”, “” etc.

const char* mqtt_server = “”;

3)MQTT Client Settings If your MQTT broker require clientID,username and password authentication,you need to change

if (client.connect(clientId.c_str()))


if (client.connect(clientId,userName,passWord)) //put your clientId/userName/passWord here

If not,just keep them as default. After do that,choose the corresponding board type and port type as below,then upload the sketch to the NodeMCU.

  • Board:”NodeMCU 0.9(ESP-12 Module)”
  • CPU Frequency:”80MHz”
  • Flash Size:”4M (3M SPIFFS)”
  • Upload Speed:”115200″
  • Port: Choose your own Serial Port for your NodeMCU

Step 3: MQTT Client Setting

About how to config the MQTT client,check

Topics Settings:
Topic to publish: OsoyooCommand

Topic to subscribe: OsoyooDataRunning


Once the upload done,if wifi hotspot ssid and password setting is ok, all the connections are normal, open the Serial Monitor,you will see following result: the NodeMCU serial port keep output real-time humidity and temperature data every 6 second,and these messages will be published to the MQTT broker via MQTT protocol,then they will be subscribed by the MQTT client.

At the same time,open the MQTT client and choose subscribe to a topic called “OsoyooData” ,you will see the real-time humidity and temperature data

1 Person Made This Project!


  • Tiny Speed Challenge

    Tiny Speed Challenge
  • Clocks Contest

    Clocks Contest
  • PCB Design Challenge

    PCB Design Challenge


Abhinav Peddi
Abhinav Peddi

1 year ago

But dht11 requires 5v input, does it work with 3.3v?