Capturing Sensor Data: DHT22

Ok, having blinking LED's connected to an ESP8266 SOC is nice but is not very useful in terms of information exchange and/or interaction. This blog builds a simple IoT application which measures temperature and humidity and returns the measured values. This build is relatively straightforward build as we can leverage DHT22 temperature-humidity sensor and the corresponding native NodeMCU dht module.

Breadboard sketch

We need to wire up the DHT22 sensor to the NodeMCU which will provide ground and power - black and red wires respectively. Second the sensor data must be captured using the GPIO4 pin - purple wire. As this is an IoT use case I would like to enable deep sleep capabilities to conserve power in between sensor readings - yellow wire. The breadboard sketch is depicted below.

With the wiring in place we move on to the coding portion of this prototype. This blog assumes you have successfully flashed the NodeMCU with the modules necessary for this. For more details, check out my blog post Flash NodeMCU for more information.

Code

The NodeMCU firmware has a module available to interact with DHT sensors. The code snippets below fully utilize the functions provided by this module. In addition, the wifi module is used to connect the ESP8266 SoC onto the internet.

config.lua

The variables required for this sketch are stored in a separate file for easy maintenance. In this case we need variables for the NodeMCU wifi and dht modules. A constant is declared for the duration of the deep sleep cycle in milliseconds.

dht22.lua

With the necessary variables defined we move forward with the code that does the heavy lifting. If we examine the code below closer we can identify 3 different functions:

First a connection is made to the wifi network using NodeMCU's wifi module. Without connectivity to the network no further actions are taken.

Function func_read_dht leverages NodeMCU's dht module to read the temperature and humidity information as collected by the dht22 sensor. Once the information is retrieved its outputted in the serial window.

The routine func_exec_loop executes the logic as described previously and once successfully completed initiates deep sleep mode using the node.dsleep command. Upon waking up the ESP8266 unit starts up and initiates the init.lua script.

init.lua

NodeMCU uses init.lua as the auto executable which is invoked upon boot-up. The script below calls the dht22.lua. It contains a 15 second delay to allow for cancelling the excution in the situation of an error in order to interrupt and endless loop.

--[[
init.lua | Tiest van Gool
Init.lua is automatically executed on bootup of the NodeMCU. This launcher file loads the actual init_XYZ file only when everything has been tested and debugged.
A 15 second delay has been added in case of error and enable abort.
--]]
-- Set local variable
FileToExecute="dht22.lua"
-- Set timer to abort initialization of actual program
print("You have 15 second to enter file.remove('init.lua') to abort")
tmr.alarm(0, 15000, 0, function()
print("Executing: ".. FileToExecute)
dofile(FileToExecute)
end)

With all the Lua based scripts they can be transferred to the ESP8266 using Luatool. If all scripts have been successfully transferred and NodeMCU is restarted the result should be something as depicted in the screenshot below.

Even though wifi connectivity is established its not leveraged for communication of the sensor data. The next blog entry will introduce the NodeMCU's mqtt module. MQTT is a machine-to-machine (M2M)/"Internet of Things" connectivity protocol. Leveraging this protocol the DHT22 sensor data will be wireless communicated to a broker/server for visualization without a serial communication channel.