Grafana, D1, light sensor
Wed, 05/25/2016 - 12:04

I want to track and graph everything, which includes power consumption in my flat. But checking and regularly values and writing them down in spreadsheet is tiresome. Then I found this guide and after some modifications I've managed to collect actual power consumption, send it to Prometheus and see graphs in Grafana.

What I needed first

How does it work

Newer pulse meters have signal diode that "blinks" for every Wh consumed (indicated by 1000imp/kWh). And we can track these pulses with phototransistor or photoresistor. Whith every pulse counter value increases and after specified time (60s) is send to server. There the value is saved and displayed as graph.

Software

Download and flash custom NodemMCU build from nodemcu-build.com. You'll need to choose dev branch and these modules: adc, file, gpio, net, node, timer, wifi. Now download lua scripts from nodemcu-elog repository.

git clone git@github.com:ra100/nodemcu-elog.git

Modify config.default.lua and save it as config.lua. It should look like this (with values modified to suite your network configuration):

PIN = 1 -- signal pin
MIN_PW = 20 -- miliseconds
SSID = 'MyNetwork'
PASSWORD = 'MyWifiPassword'
GATEWAYIP = '192.168.0.1'
IP = '192.168.0.50'
RESTARTINTERVAL = 30 -- in seconds
PUSHGATEWAY = 'http://192.168.0.2:9091/metrics/job/power/instance/HOSTNAME'
PUSHINTERVAL = 60 -- in seconds
DEBUG = false

You'll find more information about config values in README file.

Upload all .lua files to NodeMCU module.

Hardware

Wiring is simple

NodeMCU Sensor
GND GND
3V VCC
D1 SIG

Now use tape or something similar to "glue" sensor on power meter right on the pulse output. Connect to power supply and watch your power consumption.

Light sensor Power meter closeup Power meter

Grafana setup

server.lua script exports 4 values, you can find them in get_metrics() function. Values are

  • power-meter - counter value, increases with every pulse
  • nodemcu_heap - for debugging purposes, track current memory heap on NodemMCU
  • nodemcu_vdd33 - voltage on V3 output
  • nodemcu_uptime - module uptime since last restart

To see nice graph, you'll have to use query, something like

rate(power_meter{exported_instance="power"}[5m])

You want to see rate of power consumption no just increasing value. After using query you'll see in prometheus graph instead of this

Prometheus graph

something like this

Prometheus rate graph

and after adding it to Grafana you get nice graph like this

Grafana daily graph

Problems needed to be fixed

Greatest problem is power consumption of module itself (+ sensor), I thought it would be enough to have 1000mAh powerpack to keep it running for few days. I was wrong. It lasted around 20 hours. So I ended up using 18Ah powerpack (few years old, so real capacity is maybe 5 - 10 Ah) and I still need to replace battery every 4 days or so. Problem is probably with that sensor must be active all the time to detect pulse and WiFi is probably running nonstop too. Maybe if PUSHINTERVAL was increased so WiFi could sleep inbetween, it could save some power. Another case could be WiFi signal, power meter is behind metal cover in thick wall and signal is really weak there.

Second problem is difficult to debug, from time to time, sensor stops "receiving" signal. Therefore, I added timer which check every RESTARTINTERVAL (30s) if new pulse has been detected in this time and if not, module restarts itself.

Gallery