Menu Close

DHT11 Temperature & Humidity sensor on ESP8266 using LUA

Robo India presents tutorial on how to read temperature and humidity data through DHT11 sensor using NodeMCU ESP8266 wifi module on Arduino IDE.

1. Introduction:

This tutorial explains how to read temperature and humidity data through DHT11 sensor using ESP8266 wifi module on NODEMCU LUA platform.

You need to have basic understanding of ESP8266, below mentioned tutorials of Robo India may help you out in this.

1. This tutorial explains configuration and setup of ESP8266

2. This tutorial explains basic of ESPlorer.

2. Understanding DHT11 sensor

DHT11 sensor gives humidity and temperature data. It has got following pin interface.

3. Circuit

if you have Node MCU AMICA than make the following connections –

General understanding of connections with ESP8266 wifi Module ( if you do not have NODE MCU AMICA make the following connections with your ESP8266 wifi Moudle)

4. Programming

Upload the following file to your ESP8266. Run this uploaded file by the following command –

dofile(“DHT11.lua”);

Every time you need to read data from DHT11 sensor you will need to call following function –

getData();

You will get output on ESPlorer.

You may download DHT11.lua from here.

-- Read temperature and humidity data from DHT11 sensor.
-- Connect Data pin of DHT11 sesnor to PIN 3 (GPIO 0 - GPIO ZERO) of ESP8266 wifi module.
-- Learn: www.roboindia.com/tutorials
-- Ref-esp8266.com

pin = 3


Humidity = 0
HumidityDec=0
Temperature = 0
TemperatureDec=0
Checksum = 0
ChecksumTest=0


function getData()
Humidity = 0
HumidityDec=0
Temperature = 0
TemperatureDec=0
Checksum = 0
ChecksumTest=0


bitStream = {}
for j = 1, 40, 1 do
     bitStream[j]=0
end
bitlength=0

gpio.mode(pin, gpio.OUTPUT)
gpio.write(pin, gpio.LOW)
tmr.delay(20000)
--Use Markus Gritsch trick to speed up read/write on GPIO
gpio_read=gpio.read
gpio_write=gpio.write

gpio.mode(pin, gpio.INPUT)

--bus will always let up eventually, don't bother with timeout
while (gpio_read(pin)==0 ) do end

c=0
while (gpio_read(pin)==1 and c<100) do c=c+1 end

--bus will always let up eventually, don't bother with timeout
while (gpio_read(pin)==0 ) do end

c=0
while (gpio_read(pin)==1 and c<100) do c=c+1 end

--acquisition loop
for j = 1, 40, 1 do
     while (gpio_read(pin)==1 and bitlength          bitlength=bitlength+1
     end
     bitStream[j]=bitlength
     bitlength=0
     --bus will always let up eventually, don't bother with timeout
     while (gpio_read(pin)==0) do end
end

--DHT data acquired, processing it :-

for i = 1, 8, 1 do
     if (bitStream[i+0] > 2) then
          Humidity = Humidity+2^(8-i)
     end
end
for i = 1, 8, 1 do
     if (bitStream[i+8] > 2) then
          HumidityDec = HumidityDec+2^(8-i)
     end
end
for i = 1, 8, 1 do
     if (bitStream[i+16] > 2) then
          Temperature = Temperature+2^(8-i)
     end
end
for i = 1, 8, 1 do
     if (bitStream[i+24] > 2) then
          TemperatureDec = TemperatureDec+2^(8-i)
     end
end
for i = 1, 8, 1 do
     if (bitStream[i+32] > 2) then
          Checksum = Checksum+2^(8-i)
     end
end
ChecksumTest=(Humidity+HumidityDec+Temperature+TemperatureDec) % 0xFF

print ("Temperature: "..Temperature.."."..TemperatureDec)
print ("Humidity: "..Humidity.."."..HumidityDec)
print ("ChecksumReceived: "..Checksum)
print ("ChecksumTest: "..ChecksumTest)
end

This is how to use DHT11 sensor on ESP8266 using LUA

If you have any query please write us at support@roboindia.com

Thanks and Regards
Content Development Team 
Robo India
http://roboindia.com

Leave a Reply