Categories

Tutorial

DHT11 Temperature & Humidity sensor on ESP8266 using LUA

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

Detailed Tutorial

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.

DHT11-Pinout_

3. Circuit

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

DH11-with-amica_

roboindia-DH11-with-esp8266

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

Related Tutorials
Logic Level Converter

Robo India presents tutorial how to use logic level converter. While interfacing with sensor or other electronic device logic level converter is required, if one device works on 5V and other on 3.3V and both have to communicate than this logic level converter will be required between them.

ESP8266 Wiif Module Setup

Robo India presents tutorial how to setup ESP8266 wifi module. Hardware configuration to run ESP 8266 wifi module is explained in this tutorial.

ESP8266 Flash with NodeMCU firmware

Robo India presents tutorial how to flash ESP8266 wifi module with LUA firmware. NodeMCU firmware is used here.

Starting with LUA on ESP8266 Wifi module

Robo India presents tutorial on starting with LUA on ESP8266 wifi module and ESPLORER.

NodeMCU Amica Installation - ESP8266 Based Development Board

Robo India presents tutorial on how to install NodeMCU Amica, ESP8266 wifi module based development board.

Weather Station on ESP8266 using LUA

Robo India presents tutorial to make an online weather station using ESP8266 WiFi module and LUA platform.

Robo India Tutorial portal

Illustrated tutorials and guides.

Feel free to contact us

© Robo India | http://roboindia.com