MQTT Connected NodeMCU Weather Station
|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.
Components:
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.
The Fritzing diagram above shows how the prototype was wired up.
NodeMCU pin | |
D6 | BH1750 SDA |
D5 | BH1750 SCL |
GND | BH1750 GND |
3v3 | BH1750 VCC |
Vin | DHT22 VCC |
D1 | DHT22 Data |
GND | DHT22 GND |
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 https://github.com/ktinkerer/nodemcu-mqtt-weather-station 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.
I followed your instructions.
from the explorer I have this answer:
build created on 2018-10-12 22:09
powered by Lua 5.1.4 on SDK 2.2.1(6ab97e9)
> 192.168.1.91 255.255.255.0 192.168.1.254
Starting
Connecting to MQTT
Registered
but if I use mqtt.fx to monitor the mqtt server I do not have any information regarding outsidenode1 topic
It’s quite a while since I wrote this and I am no longer using it, try using MQTT to send a message on outsidenode1/read and see if it replies.