Community
OpenEnergyMonitor

Community

Read the SDM220 modbus with the minimalmodbus python module

Tags: #<Tag:0x00007fc924389d30>

Hello Bill,

I have a new fresh installation of an emonCMS in my own raspberry pi.
I have an USB-RS485 converter and a Eastron SDM220 modbus energy monitor.

My intention is to capture the data and integrate it into the emonCMS.

I’ve read several posts where you pointed to the solution using the minimalmodbus python module and a script you’ve made.
Please, could you explain the steps to implement this solution into the emoncms?

Regards,
Joaquin.

Hi Joaquin,

I use this RS-485 to USB adapter to feed data from an Elkor WattsOn power transducer to a Raspberry Pi.

I’m not a programmer, so I used what I knew how to work with, to get data from the RPi to emonCMS.

A Python script that uses the minimalmodbus module reads the RS-485 data from the RPi USB port and writes it to a text file mounted on a tmpfs partition. (a ramdisk, so-to-speak)

A bash script reads the text file and uses curl to send the data to emonCMS.

I’ll send you the files later today (I’m at work ATM). Hopefully, they’ll give you a place to start.

Regards,

Bill

Thanks Bill!

··· On mar., 15 ago. 2017 16:14 Bill Thomson < [email protected]> wrote:

Bill.Thomson https://community.openenergymonitor.org/u/bill.thomson Bill
Thomson https://community.openenergymonitor.org/u/bill.thomson
August 15

Hi Joaquin,

I use this
http://www.ebay.com/itm/For-XP-RS485-Vista-Support-OS-B3-USB-Raspberry-Converter-Adapter-To-RS485-/252987628320?epid=24002975678&hash=item3ae73cdb20:g:7IUAAOSwceNZPrG~
RS-485 to USB adapter to feed data from an Elkor WattsOn power transducer
to a Raspberry Pi.

I’m not a programmer, so I used what I knew how to work with, to get data
from the RPi to emonCMS.

A Python script that uses the minimalmodbus module reads the RS-485 data
from the RPi USB port and writes it to a text file mounted on a tmpfs
partition. (a ramdisk, so-to-speak)

A bash script reads the text file and uses curl to send the data to
emonCMS.

I’ll send you the files later today (I’m at work ATM). Hopefully, they’ll
give you a place to start.

Regards,

Bill

Visit Message
https://community.openenergymonitor.org/t/read-the-sdm220-modbus-with-the-minimalmodbus-python-module/4819/2
or reply to this email to respond.

To unsubscribe from these emails, click here
https://community.openenergymonitor.org/email/unsubscribe/1223200ac8db08bf001a1275f8100a25ace2187f9a9a2b0f7c11210086bce4de
.

Here’s the Python script and bash file…

read_meter.py (2.3 KB)
send-to-eorg.sh (1.2 KB)

Note: You’ll need version 2.1.2 of Advanced Python Scheduler.
APS version 3.x is not compatible with the script as written.

This should help with getting to know modbus: http://www.simplymodbus.ca/faq.htm

Thank you Bill,

both, the python file and the bash script are self-explanatory :slight_smile:

I understand them, but what I need now is to discover how to integrate
the data into my own emoncms that is installed in the same raspberry
pi I use to collect the modbus data.

Into the emoncms I have to define the items that will receive the
data, isn’t it?
Do you know how to do it?
Another possibility would be to use the emoncms.org platform as you’ve done.

Regads,
Joaquin

··· 2017-08-16 3:24 GMT+02:00, Bill Thomson : > > > Here's the Python script and bash file... > > [read_meter.py](https://community.openenergymonitor.org/uploads/default/original/2X/5/58e25099ff7e081d4643961345377d1a4b1e61fc.py) > (2.3 KB) > [send-to-eorg.sh](https://community.openenergymonitor.org/uploads/default/original/2X/b/b55cdce1692a0a1bb8a1e2fae5d13d1b89f2fd89.sh) > (1.2 KB) > > > > > > --- > [Visit > Message](https://community.openenergymonitor.org/t/read-the-sdm220-modbus-with-the-minimalmodbus-python-module/4819/4) > or reply to this email to respond. > > To unsubscribe from these emails, [click > here](https://community.openenergymonitor.org/email/unsubscribe/d14cc024d2acf514770245beeb8a39151a5cd59b74f35b1322efd8fd504ff748). >

It’s quite easy actually. To send the data to your local emonCMS, all you need to do is edit this line:
http://emoncms.org/input/post.json?node=1&csv=$GENW,$NETC&apikey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" > /dev/null

to read

http://your-emonpi-ip-address-goes-here/input/post.json?node=1&csv=$GENW,$NETC&apikey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" > /dev/null

and you should be good to go.

I don’t use MQTT or Node-RED, so unfortunately, I can’t offer any help with those two.

Hello again Bill!!!

I’ve already connected all the things and now I’m able to measure the power
consumption.

Thank you very much for your help!!!

··· 2017-08-16 22:11 GMT+02:00 Bill Thomson < [email protected]>:

Bill.Thomson https://community.openenergymonitor.org/u/bill.thomson Bill
Thomson https://community.openenergymonitor.org/u/bill.thomson
August 16

Joaquin_Lopez:

Do you know how to do it?

It’s quite easy actually. To send the data to your local emonCMS, all you
need to do is edit this line:
http://emoncms.org/input/post.json?node=1&csv=$GENW,$NETC&apikey=
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" > /dev/null

to read

http://your-emonpi-ip-address-goes-here/input/post.json?
node=1&csv=$GENW,$NETC&apikey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" >
/dev/null

and you should be good to go.

I don’t use MQTT or Node-RED, so unfortunately, I can’t offer any help
with those two.

Visit Message
https://community.openenergymonitor.org/t/read-the-sdm220-modbus-with-the-minimalmodbus-python-module/4819/6
or reply to this email to respond.

In Reply To
Joaquin_Lopez https://community.openenergymonitor.org/u/joaquin_lopez
August 16
Thank you Bill, both, the python file and the bash script are
self-explanatory :slight_smile: I understand them, but what I need now is
to discover how to integrate the data into my own emoncms that is installed
in the same raspberry pi I use to collect the modbus data. Into the emoncms
I have …

Visit Message
https://community.openenergymonitor.org/t/read-the-sdm220-modbus-with-the-minimalmodbus-python-module/4819/6
or reply to this email to respond.

To unsubscribe from these emails, click here
https://community.openenergymonitor.org/email/unsubscribe/a1b50c3c8258b136c94fd14e480fd48e77ae3217a2a9d947927afe1f02c5c392
.

Hi Joaquin,

Looks good! Nice to see you got it working.

YVW, S.

Please help! :slight_smile:

I too have a new fresh installation of emonCMS on a RaspPI 3 + USB-RS485 converter + Eastron SDM630 modbus energy monitor!

I’m a TOTAL noob => powered on a Raspberry for the first time ever, today. It’s already running emonCMS.

I’ve downloaded Bill’s script and bash files, but don’t know where to go next. Questions:

  1. How do I get these script/bash to run on the RaspPI? (will continue to google on how to do it)
  2. What editing is needed on the read_mater.py in order to read the correct Modbus parameters of the Eastron SDM630? I’ve downloaded the Modbus manual for this meter (the addresses are similar to the ones on the Eastron SDM220). Joaquin, could you please share your file(s)?

Thanks!!

Hello RuiP, first of all you should test if the program works correctly.
For example I tested this from the Terminal in the raspberry pi :

sdm120c -a 1 -p -q -b 9600 -P N -S 1 -2 /dev/ttyUSB0
sdm120c -a 1 -b 9600 -P N -S 1 -2 /dev/ttyUSB0
sdm120c -a 1 -d 1 -x -p -q -b 9600 -P N -S 1 -2 /dev/ttyUSB0

Then I used this script:

#!/usr/bin/python

import threading, time, subprocess, logging, minimalmodbus
from apscheduler.schedulers.background import BackgroundScheduler
from time import sleep

SDM220 = minimalmodbus.Instrument('/dev/ttyUSB0', 1)
SDM220.serial.baudrate = 9600
SDM220.serial.bytesize = 8
SDM220.serial.parity = minimalmodbus.serial.PARITY_NONE
SDM220.serial.stopbits = 1
SDM220.serial.timeout = 1
SDM220.debug = False
SDM220.mode = minimalmodbus.MODE_RTU
print SDM220

def read_SDM220_meter():

	Volts = rs485.read_float(0, functioncode=4, numberOfRegisters=2)  
	Current = rs485.read_float(6, functioncode=4, numberOfRegisters=2)
	Active_Power = rs485.read_float(12, functioncode=4, numberOfRegisters=2)
	Apparent_Power = rs485.read_float(18, functioncode=4, numberOfRegisters=2)
	Reactive_Power = rs485.read_float(24, functioncode=4, numberOfRegisters=2)
	Power_Factor = rs485.read_float(30, functioncode=4, numberOfRegisters=2)	
	Phase_Angle = rs485.read_float(36, functioncode=4, numberOfRegisters=2)
	Frequency = rs485.read_float(70, functioncode=4, numberOfRegisters=2)
	Import_Active_Energy = rs485.read_float(72, functioncode=4, numberOfRegisters=2) 
	Export_Active_Energy = rs485.read_float(74, functioncode=4, numberOfRegisters=2)
	Import_Reactive_Energy = rs485.read_float(76, functioncode=4, numberOfRegisters=2)
	Export_Reactive_Energy = rs485.read_float(78, functioncode=4, numberOfRegisters=2)
	Total_Active_Energy = rs485.read_float(342, functioncode=4, numberOfRegisters=2)
	Total_Reactive_Energy = rs485.read_float(344, functioncode=4, numberOfRegisters=2)

	file = open('/tmp/meter_data.txt', 'w+')
  	file.write(time.strftime('%Y%m%d %X '))
  	file.write('{} {} {} {} {} {} {} {} {} {} {} {} {} {}\n'.format(Volts, Current, Active_Power, Apparent_Power, Reactive_Power, Power_Factor, Phase_Angle, Frequency, Import_Active_Energy, Export_Active_Energy, Import_Reactive_Energy, Export_Reactive_Energy, Total_Active_Energy, Total_Reactive_Energy))
  	file.close()

    #subprocess.call('/opt/send-to-eorg.sh') # call the bash file to transmit the data

I hope this helps!
Regards,

Thanks! Will try that this afternoon. One quick question though: how did you find the addresses (the numbers) used right after the “read_float(” part?
In the Eastron SDM630M Modbus manual, all Modbus addresses seem to be 5 digit numbers.

Yes, in the manual. I use the SDM220, not the SDM360.
Regards,

So,I managed to get the readings out of the SDM630M, using minimalmodbus in Terminal ! Success!

Now I need to accomplish (what you guys would call) the basic: how to have these readings automated? Where to place a Python script so that it runs on start & continually? What’s the difference between a .py and a .sh file?

I’m REALLY a complete Raspberry / Linux noob.

SystemD has a thing called a service file. They’re easy to create and quite a lot more versatile than crontab.

:::: These are the steps ::::

  1. Create a file /etc/systemd/system/sdm630m.service
    (I’m asuming your script is named sdm630m.py and is in /home/pi/) with the following content :

[Unit]
Description=SDM630M reader

[Service]
ExecStart=/home/pi/sdm630m.py
Restart=always
User=pi
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=SDM630M

[Install]
WantedBy=multi-user.target


  1. Then use sudo systemctl enable sdm630m.service && sudo systemctl start sdm630m.service to get it running.

Success!!!

I had all kinds of suffering & trouble(1), related to never having worked with Raspberry / Linux before, but I’ve managed to get everything working! Thanks A LOT for your coding examples!

Power grid => Eastron SDM630 Modbus => CH340 RS485 to USB converter => Raspberry PI 3 running EmonSD expanded to 32GB

(1) for reference, and hopefully to help others:
had to use apscheduler v2.1.2 in order for it to work,
had trouble with spaces / indentation when copying code to IDLE
had to give permissions to the .sh file (chmod +x send-to-eorg.sh)
and a lot other tiny things and big things (like expanding the SD card to 32GB and installing the PIXEL GUI and some of the standard Raspbian apps)

Good to hear you got it working, Rui.

I’ve edited my post to indicate the version of APScheduler needed to work with the script.

Rui, would you mind sharing your code for the eastron SDM630-M?

Hi,
I am currently working on a project using SDM630,USB-RS485,Raspberry pi 3 b+, solar panel and some loads, I wanted guidance on, how to collect data from SDM630 and then allowing that data to be uploaded on a webpage, I have my setup as SDM630 connected with main supply as 1p2w and iron 1p2w as load, further the SDM630 is connected with USB-RS485 which is then connected to raspberry pi, raspberry pi is installed with raspbian and thats it.
I would be very thankful if you guide me.
Regards