First experiments with NodeMCU to publish the current settings of my adjustable height working desk to MQTT.
NodeMCU can be found here: https://github.com/nodemcu/nodemcu-firmware
Note that you'll need a current version with support for floats (which the ultrasonic sensor library utilizes), I'm using 0.9.5 2015-03-18 with float support myself.
Support for the HC-SR04 sensor in NodeMCU can be found here: https://github.com/sza2/node_hcsr04
I provided my slightly adjusted version which makes measuring a non-blocking afair, allowing for callbacks when the measurement completes.
For uploading the files to the ESP I used nodemcu-uploader. I compiled both hcsr04.lua
and sensor.lua
with the -c
option, hence they are referenced in the source below as hcsr04.lc
and sensor.lc
.
For prototyping I used an ESP8266-03 Wifi Module Breakout Board by Stephan Limpkin I got myself a couple of weeks back. The HC-SR04 sensor module is connected to it like this:
- VCC -> +5V
- GND -> GND
- Trig -> GPIO2
- Echo -> GPIO0
Together with some mqttwarn magic my desk can now tweet and publish the current measurements to a Google Spreadsheet where I can plot it ;)
Files found here:
- hcsr04.lua: lib for reading measurements from HC-SR04 sensor
- sensor.lua: my ESP8266 "firmware" for repeatedly reading the sensor data and publishing it to the mqtt broker
- init.lua: NodeMCU startup file that starts
sensor.lua
once the node is online - desk.json.jinja2: Jinja2 template for transforming the current measurements from the desk into a json format (put it into the
templates
folder in your mqttwarn install dir) - mqttwarn.ini: excerpts from my mqttwarn configuration showing how I setup publishing to my Google Spreadsheet and my desk's twitter account