Browse Category


NodeMCU-Connect WiFi and use DHT11 sensor

This is my first post and before starting straight with Azure IoT Hub I want to talk  about a micro-controller (ESP8266) a bit and then move to Azure IoT hub integration later. For this article microcontroller which we would be using is  ESP8266 (NodeMCU as a development board).

NodeMCU is a development kit based on ESP8266.ESP chips are microcontrollers and can be used for a variety of things not just only connecting to WiFi.
Why are we doing this? The next set of blogs deals with Azure IoT Hub so we would use temperature and humidity data which we measure using DHT sensor and save it in Azure IoT hub using NodeMCU.
Here I assumes that you have already configured your NodeMCU to work with the Arduino IDE(check this  link ). We are going to cover these topics in this blogpost.

1) How to connect to WiFi using NodeMCU
2) How to use NodeMCU to measure temperature and humidity using DHT11 Sensor.


Connect to WiFi using NodeMCU

Copy and paste this code in Arduino editor

#include <ESP8266WiFi.h>
const char* ssid = “YourSSID”;
const char* password= “Password_of_Wifi”;
void setup() {
// put your setup code here, to run once:
void loop() {
// put your main code here, to run repeatedly:
void initWiFi()
Serial.print(“Connecting to “);
while (WiFi.status() != WL_CONNECTED)
if(WiFi.status() == WL_CONNECTED)
Serial.println(“WiFi connected”);
Serial.println(“IP address: “);

This code is very simple. In setup block we set the baud rate and then call a function which would have methods to connect to WiFi with a username and password.

We use ESP8266WiFi  library to connect to our WiFi .Please look out for this library incase you get compile time error .

Once connected serial monitor would show the status and IP address


Measure measure temperature and humidity data using a DHT11 sensor

Now let’s see how can we connect and measure temperature and humidity data using a DHT11 sensor.

One of the thing to note with ESP8266 (NodeMCU) is that it works in 3.3 V range, so we need to be careful while connecting sensors or other modules to it.Luckily in this case DHT11 sensor works in 3.3 to 5 V DC.
Now lets see how to connect our DHT11 sensor to NodeMCU.DHT11 has 3 pins ( Vcc (+), GND(-) and output (out) ). Vcc should go to 3.3 V pin of NodeMCU and GND to GND of NodeMCU.You can connect out to any of the data pin of NodeMCU ( in our case we have connected to D1 pin of NodeMCU).

I have removed NodeMCU out of the breadboard so that the wires are clear. Red wire shows connection to Vcc (3V3 pin), Black (GND) and Blue to D1 pin.

The code which we need to read temperature and humidity is below.Please note we define the pin which were are using ie D1 pin along with the type of DHT sensor ie; DHT11
Copy and paste this code in Arduino IDE .We use DHT library fom Adafruit so you would need to search and install the same using Library Manager

#include “DHT.h”
#define DHTPIN D1 // what digital pin we’re connected to
#define DHTTYPE DHT11 // DHT 11
void setup() {
void loop() {
// Wait a few seconds between measurements.
// get humidity reading
float h = dht.readHumidity();
// get temperature reading in Celsius
float t = dht.readTemperature();
// get temperature reading in Fahrenheit
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(“Failed to read from DHT sensor!”);
// display data on serial monitor
Serial.print(“Humidity: “);
Serial.print(” %\t”);
Serial.print(“Temperature: “);
Serial.print(” *C “);
Serial.println(” *F”);

In this code we define variable  which holds the type of sensor (DHT11) and pin (D1) we are using. We create a reference of it and then use this reference to read temperature and humidity values. Please note unlike above all action in this code is in loop block and setup block is used only to set the baud rate (9600) and call begin method.

Once this code is flashed to NodeMCU  we can use Serial Monitor to view results.

Incase there are errors regarding DHT library please search for DHT sensor library from Adafruit and install the same.


Search for Adafruit Unified Sensor library and install the same.

Hope you enjoyed reading this post and have started experimenting with ESP8266. Later we would see how to push this data to Azure IoT hub. Keep experimenting .