
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](http://www.thalin.se/2015/05/power-meter-pulse-logger-with-esp8266.html)
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
- power meter with pulse signal
- NodeMCU compatible module ([NodemMCU](http://nodemcu.com/index_en.html) or [D1 mini](http://www.wemos.cc/Products/d1_mini.html))
- [Analog Light Intensity Sensor Module 5528 Photo Resistor for AVR Arduino UNO](http://www.ebay.com/itm/200982532672)
- USB powerpack or 5V powersource near electrometer
- some cables (Dupont)
- server with running [Prometheus.io](https://prometheus.io/) and [pushgateway](https://github.com/prometheus/pushgateway)
- [Grafana](http://grafana.org/) or [PromDash](https://github.com/prometheus/promdash) to display graphs
## 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](http://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](https://github.com/ra100/nodemcu-elog) repository.
```shell
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):
```lua
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](https://github.com/ra100/nodemcu-elog/blob/master/README.md) 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.
## 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
```php
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
something like this
and after adding it to Grafana you get nice graph like this
## 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.