MQTT Connected NodeMCU Weather Station

Weather Station Prototype

I’ve been working on a simple weather station for the garden using a NodeMCU dev kit. Originally I was going to use an arduino but once I started experimenting with NodeMCU I decided this was a better choice as it already has full wifi connectivity thanks to the built in ESP8266 chip. I’ve built it to use MQTT to communicate with a Raspberry Pi MQTT server and record temperature, humidity, light levels and rain information.


Part Link to where I bought it (or an equivalent where the item is no longer available) Price I paid
DHT22 Ebay £2.09
BH1750 Ebay £0.99
Rain Sensor Ebay £1.99
NodeMCU Dev Kit Ebay £2.52
Total Cost £7.59

Use the NodeMCU cloud builder to build the firmware, include the following modules: adc, dht, enduser_setup, file, gpio, i2c, mqtt, net, node, tmr, uart, wifi.

Weather Station Fritzing Diagram

The Fritzing diagram above shows how the prototype was wired up.

NodeMCU pin
D6 BH1750 SDA
D5 BH1750 SCL
3v3 BH1750 VCC
D1 DHT22 Data
D7 Rain Sensor Controller VCC
GND Rain Sensor Controller GND
A0 Rain Sensor Controller A0

DHT22 also has a 10k resistor between VCC and Data.

To use the deep sleep function (this preserves the battery) connect DO to RST. For testing purposes I added a button here as well, as shown in the diagram, to wake up the board at any time.

All the code I used can be found on Github at the Fritzing diagram is also included.

Edit the config.lua file with the details of your MQTT server and upload the lua files to your board.

I found that sometimes the MQTT connection went down without activating the MQTT offline callback so I set up a simple ping between the NodeMCU board and my MQTT server, if the board does not get a reply it restarts and attempts to reconnect.
To set this up the server needs to respond to a message with the topic “ping”
and reply with the topic “pingreply” (Very simple to do if you are running Node-RED!). This can be disabled by editing the init.lua file.

I’ve used the deepsleep function, this is activated by sending a message to the board containing the number of seconds you want the board to sleep for with the topic “sleep”. The maximum sleep time is 71 minutes.

Wifi is set up using the Enduser Setup Module which is described here.



Add a Comment

Your email address will not be published. Required fields are marked *