Reading from a SDM120 meter using EmonHub

Do you recall how many? It would be useful to know what model/vintage/version you have and what data is available via emonhub. Do you now if you gat all 14 or all 20 for sure?

Perhaps you could add a temp line to print the whole reply from read_all() to the log file and post it here so we can see if all the values are (consistently?) there.

My meters don’t do the 8 extra values so it will never output the full 20 that I’m expecting Sean’s to output.

Thanks that fits with being later than mine.

It might be wise to call that an adapter rather than an interfacer, especially whilst we are discussing the sdm120 “interfacer” in emonhub. Interfacer was a word I coined as it wasn’t really used anywhere else so it would be clear when talking about emonhub interfacers so it wouldn’t be confused with other tech jargon.

That adapter is based on the FTDI ft232rl which is a respected chip for serial devices, however I dis notice that despite not showing any termination resisters in their wiring diagrams, there was this note!

So maybe a resister would help, do you have access to a 120 ohm resister by any chance? Don’t worry if you don’t we can revisit this if we don’t get anywhere, like I said previously, I tend to try these things just to rule them out, but I do have the resister kicking around so it’s a very quick test for me.

I have no experience with that adapter, the comms may be very brief and the leds are not even reaching a brightness level you can see before extinguishing, try lowering the ambient light whilst you know it’s working. AFAICT the library grabs all the values one by one, so that’s alot of very brief comms. I tend to grab all the data in much larger chunks at a faster baud (9800) so that all the values are relative at a single point in time rather than getting values from marginally different times.

Mine returns version 0202.04. It’s wired into a CU and I’d have to dismantle it to see the diag on the side, but from memory, it’s broadly the same as the image you show. Scans of the leaflet:

Apologies Richard, I had seen your post on my mobile earlier in the day, but when I sat down at the PC briefly in the evening to reply to Trystan, I tagged on a couple of comments, thinking I was replying to Sean at the time.

So it seems you have the same FW as Sean has (he’s just confirmed his version in the post above). Have you read all the earlier posts in this thread? Have you confirmed the device address using the ls command I provided earlier?

ls -la /dev/{tty{ACM,AMA,S,USB},serial}[0-9] 2>/dev/null

Have you updated the emonsd or emonhub and switched emonhub branch to master? (see Post 32)

Have you tried the second independent test script in Post 51? Trystan explains how in Post 46.

If all else fails, have you tried reversing the A & B wires? Once you get one or more values, that confirms the polarity is correct, as well as the address and baud etc.

The 120 ohm resister is also something to try if you can easily lay your hands on one (it was the adapter you linked that said it had no resister fitted), I’m not sure it would make a difference at this point, but if you had one laying around, it’s worth a stab.

Hopefully a daft question, but can we assume the sdm120 is fitted to a live circuit? The modbus part of the device is actually powered via the mains, so won’t work with just the 2 A + B wires.

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!