Installing Emoncms onto a PC (or Mac) and using with multiple modbus meters (e.g. SDM230)

Let me start by saying that I am by no means a computer whizz. It has taken weeks of tinkering on and off to arrive at the setup I describe below. It is largely a collection of various relevant forum posts and bits of scattered documentation gathered in one place. Feel free to make any suggestions or correct errors.

Emoncms is primarily designed to be installed onto a Raspberry Pi and the help to install the various elements to work with a modbus meter centre around using terminal and linux commands. Fortunately there is a version of the OS to run on PC’s and Mac’s. Head over to the Raspberry Pi site and download ‘Debian Buster with Raspberry Pi desktop’. This version runs with Intel CPU’s rather than the ARM chip the Pi comes with.

I used this guide to install the image onto a 32GB SD card (via a memory stick - the process is install the .iso onto the stick, boot the computer from that and then install the OS on to your 32GB card. Fun ain’t it?!)

https://projects.raspberrypi.org/en/projects/install-raspberry-pi-desktop

(May be of use to some - My Miix300 runs Windows 10. Bios didn’t have the option to boot from USB so I put ‘recovery’ into the search bar by the windows icon, clicked on recovery options, clicked restart now under advanced set up, use a device, then EFI USB Device.)

I used ‘guided-use entire disk’ partitioning. Make sure you select the 32GB SD card here or you could wipe your hard drive! Select ‘all files in one partition’.

Booting from SD card when BIOS doesn’t have that option:
Modern computers should allow booting from SD card but as my computer BIOS didn’t allow booting from SD card and despite installing the GRUB bootloader, it always went straight into Windows. I installed ‘Grub2Win’ freeware. This allows you to configure GRUB via a GUI in Windows. I used ‘Diskinternals Linux Reader’ freeware to allow Windows to see Linux filesystems and used that to copy the ‘boot’ folder on the SD card onto the Windows ‘C’ main drive. This allows me to point GRUB at that folder to load the initial Linux system which will load the SD card drivers and then point it to the root directory of the card to finish loading the rest of it. So Linux will be running off the SD card, I just need to load initial components from the Windows drive to enable that to happen.
In Grub2Win – make a new entry with type – debian, load the kernel by custom code and put this in:

linux /boot/vmlinuz-4.19.0-13-amd64 root=/dev/mmcblk2p2  verbose
initrd /boot/initrd.img-4.19.0-13-amd64
if [$? -ne 0] ; then g2werror Linux load error ; fi

You will likely need to alter a few bits to suit your system:
(hd0,3) this is Linux’s way of referring to the Windows partition. Use Grub2Win’s partition list button to check yours – you are looking to check which partition number Windows boot C is on (3 for me) on which disk (0 for me).
When you copy the boot folder from the card to C make a note of the vmlinuz and initrd file names (mine had the 4.19.0-13-amd64 on the end – yours may be different.

The root /dev/mmcblk2p2 points the bootloader to the root folder on the SD card to load the rest of the system. Before I had this line in, when I tried to boot I just got an error and was dumped to a command line shell. From here I used ‘dmesg’ command and watched what it showed when inserting an SD card. p2 refers to the second partition – the Linux filesystem which Grub2Win shows in the partition list. Mmcblk2 refers to the SD card ‘drive’. Mmcblk1 on my system is the internal storage but could be your SD card drive on your system.

Once you have booted into your new OS, I enabled SSH from the preferences/Pi config menu and disabled screen blanking (various things can take a long time and if the screen goes off, moving the mouse may not bring you back to where you want to be while all the processing is going on leaving you wondering what’s happening. From Windows, you can use the freeware Putty to access your Pi OS via SSH should that be necessary/you encounter issues).

Open a terminal and run these commands to update the system:

sudo apt update
sudo apt full-upgrade
sudo apt clean

Reboot into your Pi install USB stick using the ‘run without persistence’ option. Connect to the internet and install from the preferences/add software menu ‘Gparted’ and use that to shrink the main Pi linux partition on the 32GB sd card to say 12GB and form a new EXT2 partition in the new space.

Reboot into new Pi install, open a terminal and run:

sudo mkfs.ext2 -b 1024 /dev/mmcblk2p4  (the 2p4 refers to the new partition you just created and may be different on yours but Gparted will show you how it is labelled). (Will need to unmount the drive first with the eject button in file manager.)
sudo mkdir /var/opt/emoncms
sudo chown www-data /var/opt/emoncms
wget https://raw.githubusercontent.com/openenergymonitor/EmonScripts/stable/defaults/etc/fstab
sudo cp fstab /etc/fstab
sudo nano /etc/fstab  (you will likely need to edit fstab to suit your partitions)

Now to install Emoncms: (On a re-install I kept getting no room in /var/tmp. So I edited fstab to make it 500M rather than 1M)

wget https://raw.githubusercontent.com/openenergymonitor/EmonScripts/stable/install/init.sh
chmod +x init.sh && ./init.sh

You’ll be asked if you want to review the install script – select yes.
Edit the config.ini file and comment out (#) the following – I.e.

#install_emoncms_emonpi_modules=true
#install_firmware=true
#install_emonpilcd=true
#install_emonsd=true
#install_wifiap=true

Ctrl O to write, Ctrl X to exit
Type ./main.sh to continue installing

Watch the MYSQL install – it seems prone to failure and not setting up the database. I re-ran main.sh several times and eventually it worked – maybe there were issues on the server side? (Also ran sudo apt-get update and sudo apt autoremove as it suggested)

The Emonhub config module can be installed by :

cd /var/www/emoncms/Modules
git clone https://github.com/emoncms/config
sudo chmod 666 /home/pi/emonhub/conf/emonhub.conf (had to search for emonhub.conf in file manager)
sudo cp emonhub-sudoers /etc/sudoers.d/  (I found it was already there)
sudo chmod 0440 /etc/sudoers.d/emonhub-sudoers
sudo usermod -a -G systemd-journal www-data 

To install the Wifi module:

cd /var/www/emoncms/Modules
git clone https://github.com/emoncms/wifi

Then append the following to the end of /etc/sudoers:

sudo nano /etc/sudoers
www-data ALL=(ALL) NOPASSWD:/sbin/ifdown wlan0,/sbin/ifup wlan0,/bin/cat /etc/wpa_supplicant/wpa_supplicant.conf,/bin/cp /tmp/wifidata /etc/wpa_supplicant/wpa_supplicant.conf,/sbin/wpa_cli scan_results,/sbin/wpa_cli scan,/bin/cp /tmp/hostapddata /etc/hostapd/hostapd.conf,/etc/init.d/hostapd start,/etc/init.d/hostapd stop,/etc/init.d/dnsmasq start,/etc/init.d/dnsmasq stop,/bin/cp /tmp/dhcpddata /etc/dnsmasq.conf,/home/pi/emonpi/wifiAP/networklog.sh,/home/pi/emonpi/wifiAP/stopAP.sh

On a re-install, I was getting Emonhub failed to start due to no module named paho to do with mqtt. This was fixed by typing cd /usr/local/bin/emonhub then sudo pip install paho-mqtt -t ./

I also found pressing restart in the Emonhub menu had no effect. Edit /etc/systemd/system/emonhub.service and remove the two mentions of var-log.mount. Restart computer.

Also, minimalmodbus wouldn’t install without error so type sudo pip install minimalmodbus

Now for reading the modbus meter:

cd /opt/openenergymonitor/emonhub
git fetch --all
git checkout minimalmodbus_multiple_meters
git pull
sudo service emonhub restart

Paste this into /etc/emonhub/emonhub.conf after removing the RFM69Pi interfacer:


[[modbus]]
    Type = EmonHubMinimalModbusInterfacer
    [[[init_settings]]]
        device = /dev/ttyUSB0
        baud = 9600
    [[[runtimesettings]]]
        pubchannels = ToEmonCMS,
        read_interval = 5
        nodename = SDM230
        [[[[meters]]]]
            [[[[[Grid]]]]]
                address = 1
                registers = 12,72,74
                names = Active power W, Import active energy kWh, Export active energy kWh
                precision = 1,1,1
            [[[[[Generation]]]]]
                address = 2
                registers = 12,342
                names = Active power W, Total active energy kWh
                precision = 1,1

You will likely want to alter the above settings to suit your requirements. The SDM meters have the following registers – pick the ones you need…

registers = 84,86,88,90,92,94,258,0,6,12,18,24,30,36,70,72,74,76,264,78,342,344
names = Total system power demand W, Maximum total system power demand W, Current system positive power demand W, Maximum system postive power demand W, Current system reverse power demand W, Maximum system reverse power demand W, Current demand A, Line to neutral volts V, Current A, Active power W, Apparent power VA, Reactive power VAr, Power factor, Phase angle degrees, Frequency Hz, Import active energy kWh, Export active energy kWh, Import reactive energy kVArh, Maximum current demand A, Export reactive energy kVArh, Total active energy kWh, Total reactive energy kVArh

Some notes to help explain things for folks new to all this:
I had recently installed solar panels and whilst I had a generation meter and a grid import meter, I had no easy way to see how much electric the house was using at any given time, along with how much the panels were producing and how much was being imported or exported to the grid. I wanted to log this data over the course of a year in order to work out if adding a battery storage system made sense as well as view realtime data on my phone for general interest.

The easy route is to use CT clamps around live cables. This doesn’t require altering wires in the consumer unit but has the drawback of being more of a general guide rather than billing grade accuracy. To my mind this meant I would still need to be checking the data against the meters to see if it tallied, with little options if and when it didn’t (I have read that you can alter some settings to calibrate Emoncms but clearly you need to build up some historical data and keep regular meter readings to do that which defeats the point somewhat). The best solution is to use MID approved (billing grade) meters like the Eastron SDM230 with modbus. These need the live cables wired into them but give accurate (to within 0.5% I believe) readings that take away the uncertainty.
(If you don’t have enough spare ways in your consumer unit to fit these meters – you could always fit another consumer unit beside the existing one in which to house them.)

I was expecting this to be a simple task – how wrong I was! After Googling around the subject I came across OpenEnergyMonitor – a free open source software tool for logging data and displaying graphs. It is largely expected to be installed on a Raspberry Pi computer and the VAST bulk of the help and documentation is focused in this direction. I looked into purchasing a Pi which can be bought for about £25 but thought I could then do with a screen and a mouse and a keyboard… Then it dawned on me that I have spare computers sitting idle in drawers. I had a Lenovo Miix 300 Windows 10 tablet with dockable keyboard and mouse – why not just use that! I did manage to install Emoncms onto it following the guide on the official site but it is outdated, requires some tweaking to the steps and it only gives you a very basic install. No Emonhub or dashboard modules among other things that I would need to get this to work with the modbus meters. All the forum help that I could find quickly got pretty technical and as I say, doing this on a PC was not supported much at all.

I decided the best bet was to install VirtualBox – a free piece of software that allows you to emulate another operating system inside the one your computer is running. Setting that up so that you could resize the screen, share clipboard content across the two OS’s and share files too was a morning’s job in itself. Once done I could install the full version of Emoncms and the additional components for the modbus meters. The trouble with this set up is that it runs fairly slowly and there is very little space left on the internal storage. The raspberry desktop OS comes with a suite of apps and all in takes up around 7GB. Windows 10 (and my data) together with this left me about 700MB on the 32GB storage.
So I bought a 16GB usb memory stick and installed the raspberry OS onto this. The install process took about 3 hours!! I hadn’t realised USB could be that slow. I decided to try installing it onto a SD card and this only took 30 minutes.

While this will all work from a SD card, if you have a power cut or turn off the computer while it happens to still be writing to the card – you will likely corrupt the card requiring a new card and a new install. The fourth time this happened to me I gave up on the SD card route, bought a ZIF hard disk that runs on 0.4A and installed everything again on to this running on a Meegopad computer stick. This is low powered and has no cooling fan running constantly. To access the installation I can use Windows Putty to get to a terminal or open a browser on any device on the same network and access the web interface using 192.168.1.106 in my case – check your router admin page to see which address your setup is assigned.

Edit - formatted text. Moderator, BT

1 Like

Thanks for sharing Mark! Good effort getting all that to work!

1 Like

Ah, that’s because of the desktop environment, which you don’t really need. It takes up a lot of disk space and a lot of CPU time. Try Raspberry PI OS Lite. It’s much smaller and runs much faster.

1 Like

I couldn’t find the lite version of the OS for PC. You could always uninstall a load of programs though I guess.

Running on an x86 (or x64) CPU and with more than the 1 GB of RAM on a Pi 2 or 3
means the performance hit is signifiicantly less than running the GUI on a Pi.
Hence, no real need for a PC Lite version

Performance on a Pi 4 with 8 GB of RAM is actually quite good, though.

As you mentioned above, the GUI can be uninstalled, or the OS can be set to not start the GUI.
i.e boot to the command prompt.

I can’t seem to edit this anymore but just wanted to add that on the MYSQL install - you can try:
cd /opt/openenergymonitor/EmonScripts/install and run ./mysql.sh and then re-run ./main.sh

Also, to get the modbus readings to work I found I had to copy minimalmodbus.py from /usr/local/lib/python2.7/dist-packages to /usr/local/bin/emonhub

Hope that helps.

That shouldn’t have been necessary. It sounds as if Minimalmodbus didn’t get installed correctly.

Phoning a friend. Firstly, thank you to everyone involved in getting multiple SDM meters running over Modbus.

I have one SDM120 running successfully over Modbus into my emonPi, but am having trouble adding additional meters.

I have updated Emonhub to the following and have ensured the ID on the second device changed appropriately, but getting the following errors.

[[SDM120]]
    Type = EmonHubMinimalModbusInterfacer
    [[[init_settings]]]
        device = /dev/serial/by-id/usb-1a86_USB2.0-Ser_-if00-port0
        baud = 9600
    [[[runtimesettings]]]
        pubchannels = ToEmonCMS,
        read_interval = 10
        nodename = sdm120
        # prefix = sdm_
        [[[[meters]]]]
            [[[[[sdm120a]]]]]
                address = 1
                registers = 0,6
                names = V,I
                precision = 2,3
            [[[[[sdm120b]]]]]
                address = 2
                registers = 0,6
                names = V,I
                precision = 2,3

I’m assuming i’'ve missed another configuration somewhere that’s resulting in the Input not being updated. Any help gratefully received…

2022-10-26 14:25:56,060 DEBUG MainThread Signal 15 received.
2022-10-26 14:25:56,209 INFO MainThread Exiting hub…
2022-10-26 14:25:56,336 INFO MainThread Exit completed
2022-10-26 14:25:57,281 INFO MainThread EmonHub emonHub (emon-pi variant) v2.2.6
2022-10-26 14:25:57,281 INFO MainThread Opening hub…
2022-10-26 14:25:57,282 INFO MainThread Logging level set to DEBUG
2022-10-26 14:25:57,282 INFO MainThread Creating EmonHubJeeInterfacer ‘RFM2Pi’
2022-10-26 14:25:57,283 DEBUG MainThread Opening serial port: /dev/ttyAMA0 @ 38400 bits/s
2022-10-26 14:25:59,286 WARNING MainThread Device communication error - check settings
2022-10-26 14:25:59,287 INFO MainThread Setting RFM2Pi baseid: 5 (5i)
2022-10-26 14:26:00,288 INFO MainThread Setting RFM2Pi frequency: 433 (4b)
2022-10-26 14:26:01,290 INFO MainThread Setting RFM2Pi group: 210 (210g)
2022-10-26 14:26:02,292 INFO MainThread Setting RFM2Pi quiet: 1 (1q)
2022-10-26 14:26:03,294 INFO MainThread Setting RFM2Pi calibration: 230V (1p)
2022-10-26 14:26:04,296 DEBUG MainThread Setting RFM2Pi pubchannels: [‘ToEmonCMS’]
2022-10-26 14:26:04,296 DEBUG MainThread Setting RFM2Pi subchannels: [‘ToRFM12’]
2022-10-26 14:26:04,298 INFO MainThread Creating EmonHubMqttInterfacer ‘MQTT’
2022-10-26 14:26:04,300 DEBUG MainThread Setting MQTT pubchannels: [‘ToRFM12’]
2022-10-26 14:26:04,301 DEBUG MainThread Setting MQTT subchannels: [‘ToEmonCMS’]
2022-10-26 14:26:04,301 INFO MainThread Setting MQTT node_format_enable: 1
2022-10-26 14:26:04,301 INFO MainThread Setting MQTT nodevar_format_enable: 1
2022-10-26 14:26:04,302 INFO MainThread Setting MQTT nodevar_format_basetopic: emon/
2022-10-26 14:26:04,302 INFO MainThread Setting MQTT node_JSON_enable: 0
2022-10-26 14:26:04,303 INFO MainThread Creating EmonHubEmoncmsHTTPInterfacer ‘emoncmsorg’
2022-10-26 14:26:04,304 DEBUG MainThread Setting emoncmsorg pubchannels: [‘ToRFM12’]
2022-10-26 14:26:04,305 DEBUG MainThread Setting emoncmsorg subchannels: [‘ToEmonCMS’]
2022-10-26 14:26:04,305 WARNING MainThread Setting emoncmsorg apikey: obscured
2022-10-26 14:26:04,306 INFO MainThread Setting emoncmsorg url: https://emoncms.org
2022-10-26 14:26:04,306 INFO MainThread Setting emoncmsorg senddata: 1
2022-10-26 14:26:04,306 INFO MainThread Setting emoncmsorg sendstatus: 1
2022-10-26 14:26:04,307 INFO MainThread Creating EmonHubMinimalModbusInterfacer ‘SDM120’
2022-10-26 14:26:04,318 INFO MainThread Connecting to Modbus device=/dev/serial/by-id/usb-1a86_USB2.0-Ser_-if00-port0 baud=9600
2022-10-26 14:26:04,329 INFO MainThread Setting SDM120 read_interval: 10
2022-10-26 14:26:04,330 INFO MainThread Setting SDM120 nodename: sdm120
2022-10-26 14:26:04,330 INFO MainThread Setting SDM120 prefix:
2022-10-26 14:26:04,330 ERROR MainThread Unable to create ‘SDM120’ interfacer: sequence item 0: expected str instance, int found

Hello Ben, I cant see what is wrong. Does it work with only one of those meters defined? The error is not giving much to go on either, might be worth approaching by process of elmination, e.g remove one of the meter definitions - see what happens, remove both - does it at least create the interfacer?

Hi,

Just double checking you have the correct baud rate set on both meters and you’ve done this:

1 Like

Thank you, both meters are running at 9600 and work successfully when using the original single meter script.

Thanks Trystan, i should have tried paring back before, thank you for the advice.

Nothing yet. I’ve simplified the config back to single meter below, but unable to get the interfacer to start.

[[SDM120]]
    Type = EmonHubMinimalModbusInterfacer
    [[[init_settings]]]
        device = /dev/serial/by-id/usb-1a86_USB2.0-Ser_-if00-port0
        baud = 9600
    [[[runtimesettings]]]
        pubchannels = ToEmonCMS,
        read_interval = 10
        nodename = sdm120
        # prefix = sdm_
        [[[[meters]]]]
            [[[[[sdm120]]]]]
                address = 1
                registers = 0,6,12,18,30,70,72,74,76
                names = V,I,P,VA,PF,FR,EI,EE,RI
                precision = 2,3,1,1,3,3,3,3,3

The hardware seems fine as this test Pi works well with one meter when I switch back to the config below. The interfacer is created and I get data from either SDM120 immediately.

If the config above looks right to you, then it’s behaving as if I’ve missed configuring or declaring something elsewhere in the config file - or perhaps using a different version of minimalmodbus? With that, it’s a fresh emonSD image with basic updates run. Would I need to fetch anything additional perhaps or another part to the config?

This config below works well for both meters.

[[SDM120]]
    Type = EmonHubMinimalModbusInterfacer
    [[[init_settings]]]
        device = /dev/serial/by-id/usb-1a86_USB2.0-Ser_-if00-port0
        baud = 9600
    [[[runtimesettings]]]
        pubchannels = ToEmonCMS,
        read_interval = 10
        nodename = sdm120
        # prefix = sdm_
        registers = 0,6,12,18,30,70,72,74,76
        names = V,I,P,VA,PF,FR,EI,EE,RI
        precision = 2,3,1,1,3,3,3,3,3

Many thanks in advance.

I suggest you check the system logs as Python errors will show up there.

try

journalctl -f

add -u emonhub to get less (if other stuff is there).

Hello @thedawnbefore thanks for the above, I think you have an older version of emonhub, the version before support for multiple meters. If you run a full update from the emoncms Admin > Update page it should hopefully sort it

1 Like

@thedawnbefore - check the log and make sure it really does update :slight_smile:

As an aside, I don’t think there is anyway of knowing what version you are running from the UI :frowning:

1 Like

It does show the version in the log and on the Admin > Components page?

 2022-10-26 14:25:57,281 INFO MainThread EmonHub emonHub (emon-pi variant) v2.2.6

Looks like version needs to be 2.3.1 or newer for the new multiple meter config format support.

1 Like

Oh yes, missed it, sorry :frowning: might be useful on the Emonhub page as well :slight_smile:

that emon-pi variant note also needs removing now as it’s easily confused with a branch name.

1 Like

Actually that printed version in the log is wrong :man_facepalming: it’s hard coded and has not been updated to reflect the actual version…

1 Like