Reading from a SDM120 meter using EmonHub

Great, it is a later version to mine so a different set of registers is more than possible. Don’t worry about the side view, it was to hopefully compare against Trystans, and I’m guessing he’s likely to say something similar about it being fitted already.

I haven’t studied the guide, I’ll try and take a look later.

I’m not sure how much time I can find for this over the coming days, but if Richard reaches the same point and gets the same results (or not) that could well shed some light on where we need to look closer. If you are both getting identical, limited values with different usb adapters and wiring types/lengths etc then that would point more towards a code level issue.

I shall investigate this later on this afternoon. I don’t have a 120ohm resistor, but I’ve got some 510, so I could fit four in parallel which would give 127 and be near enough for testing.

Thank you hugely for your help.

No rush from my prospective, I did try last night to get an emonpi up and running with the latest image so I could try what you guys have, with my own sdm120’s. But I’m having difficulties of my own on that front at the moment (ref Emonpi FW version not available in emonhub - #2 by pb66), so it’s not turned out to be the quickest route to helping you guys as I’d hoped. But I haven’t forgotten.

OK. I’m in above my head now (sorry)

I’ve tried adding resistor and reversing polarity. of connections. In one orientation with no resistor, the SDM120 continually restarts itself. With resistor, the is no difference in polarity of connections.

I’ve managed to make the changes that @TrystanLea and @borpin said in post 57 and 59. It is beyond my current skill level to run the script in post 46 - being on night shifts this week is not helping my learning ability for these new skills!

I ran the shell commands in post 15 and got

**pi@emonpi**:**~ $** ls -la /dev/modbus

ls: cannot access '/dev/modbus': No such file or directory

**pi@emonpi**:**~ $** ls -la /dev/{tty{ACM,AMA,S,USB},serial}[0-9] 2>/dev/null

lrwxrwxrwx 1 root root 7 Jan 27 19:40 **/dev/serial0** -> **ttyAMA0**

lrwxrwxrwx 1 root root 5 Jan 27 19:40 **/dev/serial1** -> **ttyS0**

crw-rw---- 1 root dialout 204, 64 Jan 27 19:41 **/dev/ttyAMA0**

crw-rw---- 1 root dialout 4, 64 Jan 27 19:40 **/dev/ttyS0**

crw-rw---- 1 root dialout 188, 0 Jan 27 19:40 **/dev/ttyUSB0**

**pi@emonpi**:**~ $**

My brain hurts now…

I’ve tried ordering the other adapter Sean had success with in post 14, but I’m still getting no joy.

ERROR    SDM120     Not connected to SDM120

Any ideas would be appreciated

Thank you

I had a go at using minimalmodbus while helping @richardsmith99 in a PM and its worked a treat, thanks for the tip @pb66, now to replace the existing emonhub interfacer code with this approach.

Here’s a basic example of reading from the SDM120:

#!/usr/bin/env python

import minimalmodbus
import serial

rs485 = minimalmodbus.Instrument("/dev/ttyUSB1", 1)
rs485.serial.baudrate = 2400
rs485.serial.bytesize = 8
rs485.serial.parity = minimalmodbus.serial.PARITY_NONE
rs485.serial.stopbits = 1
rs485.serial.timeout = 1
rs485.debug = False
rs485.mode = minimalmodbus.MODE_RTU

# index, name, unit, dp, value
register_map = [
    [0,"Vrms","V",1,None],
    [8,"Irms","A",3,None],
    [12,"activePower","W",1,None],
    [18,"apparentPower","VA",1,None],
    [30,"powerFactor","",3,None],
    [70,"frequency","Hz",3,None],
    [72,"activeImport","kWh",3,None],
    [74,"activeExport","kWh",3,None],
    [76,"reactiveImport","kvarh",3,None],
    [84,"systemPower","W",1,None],
    [88,"systemPowerImport","W",1,None],
    [92,"systemPowerExport","W",1,None],
    [258,"current","A",3,None],
    [342,"totalActiveEnergy","kWh",3,None]
]

for i in range(0,len(register_map)):
    register_map[i][4] = rs485.read_float(register_map[i][0], functioncode=4, number_of_registers=2)
    print (("%s %0."+str(register_map[i][3])+"f%s") % (register_map[i][1], register_map[i][4], register_map[i][2]))
1 Like

I’ve implemented the above as an emonhub interfacer, currently available in a development branch for testing.

The interfacer is called: EmonHubMinimalModbusInterfacer and the source code is available here:
https://github.com/openenergymonitor/emonhub/blob/minimalmodbus/src/interfacers/EmonHubMinimalModbusInterfacer.py

To use this interfacer minimalmodbus needs to be installed as root:

sudo pip3 install minimalmodbus

Here’s the emonhub.conf configuration to read from a SDM120:

[[SDM120]]
    Type = EmonHubMinimalModbusInterfacer
    [[[init_settings]]]
        device = /dev/ttyUSB0
        baud = 2400
    [[[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

Note that the registers that we want to read from are defined in the configuration here and that the names and precision that you wish them to map to are given just below.

This makes this interfacer quite flexible as it could be used with any device given the right register map.
At the moment all the registers that it reads from need to be float datatypes, this could be expanded on in a future version to support more datatypes - but is sufficient for the SDM120 meter.

1 Like

Hi Trystan,
Been a while 'til I could get back to this.
So, I followed the above, installed minimalmodbus, commented out the old SDM120 config and replaced it with the one you gave. Restarted emonHub, log showed
2021-03-15 17:51:35,875 INFO MainThread Creating EmonHubMinimalModbusInterfacer 'SDM120'
2021-03-15 17:51:35,876 ERROR MainThread Unable to create 'SDM120' interfacer: module 'emonhub_interfacer' has no attribute 'EmonHubMinimalModbusInterfacer'

Rebooted (and updated) emonPi, no change.
What have I missed, please?

Hello @srt looks like I didn’t give all the steps to switch branch above, could you try:

sudo pip3 install minimalmodbus
cd /opt/openenergymonitor/emonhub
git pull
git checkout minimalmodbus
sudo service emonhub restart

I also would like to use the SDM120 interfacer. I went for this thread, as I think the official guide isn’t up-to-date anymore.

What I have:

  • Lubuntu 20.04.2 LTS (I know, not the usual setup)
  • Emoncms 10.2.7
  • Modules: Administration | EventProcesses | Feed | Input | CoreProcess | Schedule | Time | User | Visualisation
  • Active services: emonhub, redis-server, mosquitto
  • No inputs, no feeds

What I installed:

sudo pip3 install minimalmodbus
cd /opt/openenergymonitor/emonhub
sudo git pull
sudo git checkout minimalmodbus
sudo service emonhub restart

Wrote and tested a script sudo python3 sdm120-basic.py, to know it works (which it did :slight_smile: )

#!/usr/bin/env python3
import minimalmodbus

instrument = minimalmodbus.Instrument('/dev/ttyUSB0', 1)  # port name, slave address (in decimal)

instrument.serial.baudrate = 9600         # Baud
instrument.serial.bytesize = 8
instrument.serial.parity   = minimalmodbus.serial.PARITY_NONE
instrument.serial.stopbits = 1
instrument.serial.timeout  = 1          # seconds
instrument.mode = minimalmodbus.MODE_RTU   # rtu or ascii mode

print (instrument)

Voltage = instrument.read_float(0, 4, 2)
Current = instrument.read_float(6, 4, 2)

print ("Voltage: ", Voltage)
print ("Current:", Current)

Edited /etc/emonhub/emonhub.conf
I removed the [[RFM2Pi]]-interfacer completely from /etc/emonhub/emonhub.conf`: that one gave errors. I added:

[[SDM120]]
    Type = EmonHubMinimalModbusInterfacer
    [[[init_settings]]]
        device = /dev/ttyUSB0
        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

Restarted emonhub, but I don’t see inputs in emoncms, so I checked /var/log/emonhub/emonhub.log

2021-04-02 06:54:07,231 INFO     MainThread EmonHub emonHub (emon-pi variant) v2.1.5
2021-04-02 06:54:07,236 INFO     MainThread Opening hub...
2021-04-02 06:54:07,253 WARNING  MainThread Setting emoncmsorg apikey: obscured

Would anyone have any idea what is wrong?

I think you need a git fetch --all. I think git pull just works on the checked out branch (I could be wrong) Git Pull | Atlassian Git Tutorial. Without the fetch, I’m not sure local git does not know about all the branches.

After a checkout I always do a pull just to be sure :slight_smile:

Thank you for the extra information, but I’m not sure about the steps:

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

Are these correct?

Should work. You do not need sudo for git commands.

I got a error: cannot open .git/FETCH_HEAD: Toegang geweigerd (access denied) on git fetch --all, so I used sudo to “correct” this.

$ cd /opt/openenergymonitor/emonhub
$ sudo git fetch --all
Fetching origin
$ sudo git checkout minimalmodbus
Already on 'minimalmodbus'
Your branch is up to date with 'origin/minimalmodbus'
$ sudo git pull
Already up to date.
   (...rebooted computer to be sure as a emonhub restart gave some errors (will try to fix later) ...)
$ cat /var/log/emonhub/emonhub.log
2021-04-05 07:02:41,358 INFO     MainThread EmonHub emonHub (emon-pi variant) v2.1.5
2021-04-05 07:02:41,362 INFO     MainThread Opening hub...
2021-04-05 07:02:41,362 INFO     MainThread Creating EmonHubMinimalModbusInterfacer 'SDM120'
2021-04-05 07:02:41,365 INFO     MainThread Connecting to Modbus device=/dev/ttyUSB0 baud=9600
2021-04-05 07:02:41,383 INFO     MainThread Setting SDM120 read_interval: 10
2021-04-05 07:02:41,383 INFO     MainThread Setting SDM120 nodename: sdm120
2021-04-05 07:02:41,384 INFO     MainThread Setting SDM120 prefix: 
2021-04-05 07:02:41,384 INFO     MainThread Setting SDM120 datafields: 0,6,12,18,30,70,72,74,76
2021-04-05 07:02:41,384 INFO     MainThread Setting SDM120 names: V,I,P,VA,PF,FR,EI,EE,RI
2021-04-05 07:02:41,384 INFO     MainThread Setting SDM120 precision: 2,3,1,1,3,3,3,3,3
2021-04-05 07:02:41,384 INFO     MainThread Creating EmonHubMqttInterfacer 'MQTT'
2021-04-05 07:02:41,385 INFO     MainThread Setting MQTT node_format_enable: 1
2021-04-05 07:02:41,386 INFO     MainThread Setting MQTT nodevar_format_enable: 1
2021-04-05 07:02:41,386 INFO     MainThread Setting MQTT nodevar_format_basetopic: emon/
2021-04-05 07:02:41,386 INFO     MainThread Setting MQTT node_JSON_enable: 0
2021-04-05 07:02:41,386 INFO     MainThread Creating EmonHubEmoncmsHTTPInterfacer 'emoncmsorg'
2021-04-05 07:02:41,387 WARNING  MainThread Setting emoncmsorg apikey: obscured
2021-04-05 07:02:41,387 INFO     MainThread Setting emoncmsorg url: https://emoncms.org
2021-04-05 07:02:41,387 INFO     MainThread Setting emoncmsorg senddata: 1
2021-04-05 07:02:41,387 INFO     MainThread Setting emoncmsorg sendstatus: 1
2021-04-05 07:02:50,704 INFO     MQTT       Connecting to MQTT Server
2021-04-05 07:02:50,806 INFO     MQTT       connection status: Connection successful
2021-04-05 07:02:50,908 INFO     MQTT       on_subscribe
2021-04-05 07:03:00,631 INFO     MQTT       Publishing 'node' formatted msg

I went to http://localhost/emoncms/input/view, but I don’t see any inputs. As far as I understand there should be a node sdm120. Nothing happens when I click the ‘New device’-button. I checked /var/log/emoncms/emoncms.log and /var/log/emoncms/emonpiupdate.log but both are empty. Lights are flashing on my RS485-USB-adapter and I don’t see any errors in emonhub.log, so I guess the registers from my kWh meter are read correctly.

So it seems to be working, but unfortunately they don’t appear in my inputs on my emoncms. Do I need to do something extra with MQTT or EmonHubEmoncmsHTTPInterfacer? I found ‘Posting Data to Emoncms’ in the guide, but I don’t know if you need it when using emonhub.

@borpin, @TrystanLea I’ve narrowed the problem, so that’s why I updated my last post and deleted the others. The last question (at the moment…) is why my values don’t appear in my inputs on my locally installed emoncms. Do I need to do something extra with MQTT or EmonHubEmoncmsHTTPInterfacer? I found ‘Posting Data to Emoncms’ in the guide, but I don’t know if you need it when using emonhub.

I think the problem may have been you used sudo in front of git. Don’t.

You need to post your emonhub.conf file please.

I suggest setting

node_format_enable: 0
nodevar_format_enable: 0
node_JSON_enable: 1
node_JSON_basetopic = emon/

Ensure the basetopic is emon/

I had to use sudo git to work around the ‘Permission denied’ error. If I want to avoid it, I probably have to change the rights (but I wouldn’t know to what):

/opt/openenergymonitor$ ls -al
totaal 16
drwxr-xr-x 4 root root 4096 mrt 31 16:22 .
drwxr-xr-x 3 root root 4096 mrt 31 16:19 ..
drwxr-xr-x 2 root root 4096 mrt 31 16:22 data
drwxr-xr-x 7 root root 4096 apr  1 19:12 emonhub

I’ve changed it to your suggested settings. My defaults were 0 where your settings were 1 (and vice versa). My /etc/emonhub/emonhub.conf: see emonhub.conf.txt (7.8 KB) . Thank you!

You never posted about that.

You created the folder using sudo - get out of that habit :slight_smile:

Set log level to DEBUG and restart the service.

[hub]
### loglevel must be one of DEBUG, INFO, WARNING, ERROR, and CRITICAL
loglevel = DEBUG

Do you see an entry in the emonhub.log like this (different data obviously).

2021-04-05 10:57:35,559 DEBUG    MQTT       Publishing: emon/emonpi {"power1": 125, "power2": 0, "power1pluspower2": 125, "vrms": 234.33, "t1": 0, "t2": 0, "t3": 0, "t4": 0, "t5": 0, "t6": 0, "pulsecount": 0, "time": 1617616655.2601027}

If not, what do you see in the log?

I “more or less” posted about the error: I wrote about .git/FETCH_HEAD: Toegang geweigerd which I translated to ‘access denied’. It’s only after export LC_ALL=C that I saw it had to be ‘Permission denied’. I’ll try to get rid of the sudo-habit :-). Once I have all of this working, I will reinstall everything from scratch.

I changed log level to DEBUG, restarted the service. More log: emonhub.log.txt (13.6 KB), but a relevant part seems to be:

2021-04-05 12:33:00,584 DEBUG    SDM120     [237.0, 0.0, 0.0, 0.0, 1.0, 50.0, 0.002, 0.0, 0.373]
2021-04-05 12:33:00,585 DEBUG    SDM120     1 NEW FRAME : 
2021-04-05 12:33:00,585 DEBUG    SDM120     1 Timestamp : 1617618780.057264
2021-04-05 12:33:00,586 DEBUG    SDM120     1 From Node : sdm120
2021-04-05 12:33:00,586 DEBUG    SDM120     1    Values : [237, 0, 0, 0, 1, 50, 0.002, 0, 0.373]
2021-04-05 12:33:00,586 DEBUG    SDM120     1 Sent to channel(start)' : ToEmonCMS
2021-04-05 12:33:00,587 DEBUG    SDM120     1 Sent to channel(end)' : ToEmonCMS
2021-04-05 12:33:00,659 INFO     MQTT       Connecting to MQTT Server
2021-04-05 12:33:00,661 INFO     MQTT       Could not connect...
2021-04-05 12:33:00,759 DEBUG    emoncmsorg Buffer size: 1
2021-04-05 12:33:10,557 DEBUG    SDM120     [237.6, 0.0, 0.0, 0.0, 1.0, 50.0, 0.002, 0.0, 0.373]
2021-04-05 12:33:10,558 DEBUG    SDM120     2 NEW FRAME : 
2021-04-05 12:33:10,558 DEBUG    SDM120     2 Timestamp : 1617618790.038961
2021-04-05 12:33:10,559 DEBUG    SDM120     2 From Node : sdm120
2021-04-05 12:33:10,559 DEBUG    SDM120     2    Values : [237.6, 0, 0, 0, 1, 50, 0.002, 0, 0.373]
2021-04-05 12:33:10,559 DEBUG    SDM120     2 Sent to channel(start)' : ToEmonCMS
2021-04-05 12:33:10,559 DEBUG    SDM120     2 Sent to channel(end)' : ToEmonCMS
2021-04-05 12:33:10,690 INFO     MQTT       Connecting to MQTT Server
2021-04-05 12:33:10,792 INFO     MQTT       connection status: Connection successful
2021-04-05 12:33:10,793 DEBUG    MQTT       CONACK => Return code: 0
2021-04-05 12:33:10,903 INFO     MQTT       on_subscribe
2021-04-05 12:33:20,605 DEBUG    SDM120     [237.2, 0.0, 0.0, 0.0, 1.0, 50.0, 0.002, 0.0, 0.373]
2021-04-05 12:33:20,606 DEBUG    SDM120     3 NEW FRAME : 
2021-04-05 12:33:20,606 DEBUG    SDM120     3 Timestamp : 1617618800.084261
2021-04-05 12:33:20,606 DEBUG    SDM120     3 From Node : sdm120
2021-04-05 12:33:20,607 DEBUG    SDM120     3    Values : [237.2, 0, 0, 0, 1, 50, 0.002, 0, 0.373]
2021-04-05 12:33:20,607 DEBUG    SDM120     3 Sent to channel(start)' : ToEmonCMS
2021-04-05 12:33:20,607 DEBUG    SDM120     3 Sent to channel(end)' : ToEmonCMS
2021-04-05 12:33:20,633 DEBUG    MQTT       Publishing: emon/sdm120 {"V": 237.2, "I": 0, "P": 0, "VA": 0, "PF": 1, "FR": 50, "EI": 0.002, "EE": 0, "RI": 0.373, "time": 1617618800.084261}
2021-04-05 12:33:30,564 DEBUG    SDM120     [236.8, 0.0, 0.0, 0.0, 1.0, 50.04, 0.002, 0.0, 0.373]
2021-04-05 12:33:30,565 DEBUG    SDM120     4 NEW FRAME :

So for the first frame there is a MQTT connection error, for the second frame the MQTT connection succeeds and from the third frame on I see a Publishing: emon/sdm120 {"V": 23...

If I understand correctly, the SDM120-interfacer uses the MQTT-interfacer? And does it also use the emoncmsorg-interfacer? In that case: the url setting is set to https://emoncms.org . There is a [nodes]-section as well, with numbers 5 to 26: do I have to mention anything about my SDM120? Or what could be the problem here?

Always is.

So you should see that data in the Inputs on your local emoncms (assuming it is pointing to the same MQTT Broker as emonhub).

What do you see in the emoncms logs?

IIRC you installed in an odd way - did you install the emoncms_mqtt service? This brings data in from the MQTT Broker to emoncms.

image

The ‘input’ interfacers are independent of the ‘output’ interfacers (input brings the data in and output sends it out [in simple terms]) so you bring the data in from 1 or more interfacers and send it out on one or more interfacers. For instance, I send the same data to several different MQTT Brokers.

[edit]
I think this may work but as you have an odd setup, no guarantees as the install scripts do other things.