ESP8266 IoT DHT22 temperature and humidity

This article documents a first project with the Espressif ESP8266.

The objective is a module that will take periodic temperature and humidity measurements and publish them to an MQTT message broker.

This inital implementation is very basic, it is largely configured in code, though it does use DHCP. Later extensions might include a web interface for configuration of WLAN parameters etc, but for the moment the emphasis is assessment of reliability given some reports on the ‘net.

Hardware

A module was purchased with on board CP210x USB to serial chip. The only other component needed was the DHT22 digital temperature and humidity sensor.

NodeMCU was chosen for the ESP2866 firmware because of the inbuilt support for ‘interesting things', including the DHT22.

Above is a breadboard of the system for development.

Lots of online schematics show a pullup resistor on the DHT22 data wire.

Above is a scope capture of the DHT22 data wire, pulses are quite square and a pullup resistor is not needed, at least for short connections.

See also NodeMCU devkit V1 deep sleep.

Firmware

The firmware was downloaded from https://nodemcu-build.com/ , modules included were adc, dht, file, gpio, mqtt, net, node, ow, tmr, uart, wifi. (Some were not required for this project.)

Code

The lua code was derived from several published articles, and none worked straight off, probably a result of changes to nodeMCU over time.

nodeMCU depends heavily on the use of callbacks. The preferred scheme is to schedule elements asynchronously… which seems a good idea but using the event for “GOT_IP” to initiate MQTT connection resulted in no connection (DHCP negotation has not been completed at this point).

An alternate method of trying to monitor the GOT_IP event did work successfully, reducing operating time and saving some battery.

Here is the first pass of working code.


The code as it evolves is available online https://github.com/owenduffy/dht22iotm .

Problems

For whatever reason, I could not write new firmware on Windows 10 (32), It either didn't work, or resulted in BSOD. I eventually wrote the firmware on Linux using esptool.py which has been reliable (though it did not work in Windows 10.

Similarly, Esplorer cause BSOD on the same machine communicating with the module. It did work reliably on my laptop with Windows 10 (64).

The WEMOS MiniPro D1 does not work with security enabled on a DLINK router where the other module pictured earlier works fine.

Test results

The system is running under test and small changes being explored to improve operation. With recent changes to get some event handling working, average current drawn from 5V is 5mA.

Follow on implementations

WEMOS D1 Mini Pro

Above, the WEMOS D1 Mini Pro with DHT shield with AM2302 fitted.

Above, the WEMOS module with a Ge diode added for the deep sleep reset circuit (NodeMCU devkit V1 deep sleep).