The Potato Connection

Stuff to share

Open energy monitor on Raspian Stretch

Home Energy Monitor

I recently received my open energy monitor package. I unwrapped an EmonTX module, a base station module and the EmonTH plus some accessories.

After connecting everything and setting up the Raspberry Pi the system was running without any problems.
A big thank you to the guys at OpenEnergyMonitor for an easy to use and hackable product.

The only issue I had was that the Raspian was outdated. Raspbian Stretch (Debian 9) had been just recently released. I decided to give it a try and setup the energy monitor on a stock Raspian Stretch image. This also allowed me to re-configure the emon software to the parts that I actually needed.

The goal was to get to a clean setup with Raspian Stretch, node-red, influxdb and grafana.

[EmonPiArchitecture.png]

Let’s get started

Raspian

Download and install the Lite version of Raspian to a micro SD card. I used an 8 GB card, but the bigger the better.

Use raspi-config to enable SSH and enable the serial hardware. Make sure the serial console is disabled.

Install influxdb

For the time series database we install the Debian version of influxdb, client and server packages.

1
apt-get install influxdb-client influxdb-server

Install NodeJS

We want to use the LTS version of nodejs. Install like this from nodesource:

1
2
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get install -y nodejs

We also install some other required packages such as the python library to controll the GPIO pins of the pi.

1
apt-get install build-essential python-rpi.gpio

Node-red

Now that we have nodejs running we can use npm to install node-red:

1
npm install node-red

The npm install of node-red does not come with service start and stop scripts. The corresponding scripts can be installed by fetching them from https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/. Run the follwing commands with sudo or as root.

1
2
3
4
5
6
sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/nodered.service -O /lib/systemd/system/nodered.service
sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-start -O /usr/bin/node-red-start
sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-stop -O /usr/bin/node-red-stop
sudo chmod +x /usr/bin/node-red-st*
sudo systemctl daemon-reload
sudo systemctl enable nodered.service

grafana

There is no grafana package included in raspian yet. So we install an unofficial ARM package. Check out https://bintray.com/fg2it/deb/grafana-on-raspberry/v4.5.2 for details.

1
wget -O grafana_4.5.2_armhf.deb https://bintray.com/fg2it/deb/download_file?file_path=main%2Fg%2Fgrafana_4.5.2_armhf.deb

then run dpkg to install the downloaded .deb file.

1
sudo dpkg -i grafana_4.5.2_armhf.deb

In case the grafana service is not starting automatically at system boot, you can enable and launch the server with the following command sequence:

1
2
3
sudo /bin/systemctl daemon-reload
sudo systemctl enable grafana-server.service
sudo systemctl start grafana-server

emonhub

Now to the most interesting part. To get the system process messages sent from the various probes we need to install emonhub. Emonhub interprets data from the 433 MHz receiver and publishes the sensor messages to MQTT queues.

1
2
3
sudo apt-get install python-pip python-serial python-configobj git mosquitto
sudo pip install paho-mqtt
sudo pip install pydispatcher

It is recommended to turn off persistence in mosquitto. Edit the mosquitto.conf file and set “persistence” setting to false. Also define the location of the password file. Here is my complete mosquitto.conf file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example
pid_file /var/run/mosquitto.pid
persistence false
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
password_file /etc/mosquitto/passwd
allow_anonymous false

We need to create a user and password for mosquitto. This account will be used by emonhub to connect to mosquitto.

1
sudo mosquitto_passwd -c /etc/mosquitto/passwd pi

Then, to install the emonhub we clone the package from git and run the installer blindly:

1
git clone https://github.com/openenergymonitor/emonhub.git && emonhub/install

One important change I had to make was to configure the serial port correctly. This took some time to find out. The default port /dev/ttyAMA0 was not working. To change the port edit /home/pi/data/emonhub.conf and change com_port = /dev/ttyAMA0 to com_port = /dev/ttyS0. At the same time configure in the MQTT section the username and password used to connect to mosquitto.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
vi /home/pi/data/emonhub.conf
...
[[RFM2Pi]]
Type = EmonHubJeeInterfacer
[[[init_settings]]]
com_port = /dev/ttyS0
com_baud = 38400
...
[[MQTT]]
Type = EmonHubMqttInterfacer
[[[init_settings]]]
mqtt_host = 127.0.0.1
mqtt_port = 1883
mqtt_user = pi
mqtt_passwd = ???????

I also made sure that emonhub is not publishing any data to emoncms.org.

1
2
3
apikey = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
senddata = 0 # Enable sending data to Emoncms.org
sendstatus = 0 # Enable sending WAN IP to Emoncms.org MyIP > https://emoncms.org/myip/l

After saving the config file restart the emonhub service.

1
sudo systemctl restart emonhub

Switch on your sensors and monitor the emonhub log file. You should see your sensor messages appear.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
tail -f /var/log/emonhub/emonhub.log
...
2017-09-29 23:20:26,148 DEBUG RFM2Pi 8 NEW FRAME : OK 23 239 0 243 0 181 1 26 0 1 0 0 0 (-70)
2017-09-29 23:20:26,151 DEBUG RFM2Pi 8 Timestamp : 1506716426.15
2017-09-29 23:20:26,153 DEBUG RFM2Pi 8 From Node : 23
2017-09-29 23:20:26,154 DEBUG RFM2Pi 8 Values : [23.900000000000002, 24.3, 43.7, 2.6, 1]
2017-09-29 23:20:26,155 DEBUG RFM2Pi 8 RSSI : -70
2017-09-29 23:20:26,156 DEBUG RFM2Pi 8 Sent to channel(start)' : ToEmonCMS
2017-09-29 23:20:26,158 DEBUG RFM2Pi 8 Sent to channel(end)' : ToEmonCMS
2017-09-29 23:20:26,244 DEBUG MQTT Publishing: emon/emonth5/temperature 23.9
2017-09-29 23:20:26,247 DEBUG MQTT Publishing: emon/emonth5/external temperature 24.3
2017-09-29 23:20:26,249 DEBUG MQTT Publishing: emon/emonth5/humidity 43.7
2017-09-29 23:20:26,251 DEBUG MQTT Publishing: emon/emonth5/battery 2.6
2017-09-29 23:20:26,253 DEBUG MQTT Publishing: emon/emonth5/pulsecount 1
2017-09-29 23:20:26,256 INFO MQTT Publishing: emon/emonth5/rssi -70
2017-09-29 23:20:26,258 INFO MQTT Publishing: emonhub/rx/23/values 23.9,24.3,43.7,2.6,1
2017-09-29 23:20:26,260 INFO MQTT Publishing: emonhub/rx/23/rssi -70

Time for Sauna

Once all was working I placed the temperature and humidity sensor in the sauna and fired up the sauna stove.

[First Sauna Session Graph.png]

Outlook

We will take a look at how to get the data via MQTT - node-red to influxdb for visualisation in grafana in a future post. Also left behind is how to secure the different web interfaces using nginx as a frontend server.

Links

https://openenergymonitor.org/
https://github.com/openenergymonitor/emonhub.git
https://www.raspberrypi.org/downloads/raspbian/
https://grafana.com/