Eastron SDM120C

From Domoticz
Jump to navigation Jump to search

How to get the info from SDM120c DIN-rail kWh meter into Domoticz

Hardware needed

-Eastron SDM120C (approx. €20)
-RS485 to USB convertor (€ 0.70)

Installing SDM120c

sudo apt-get install libmodbus5 libmodbus-dev git

git clone https://github.com/gianfrdp/SDM120C.git

cd SDM120C

make clean && make

sudo make install

cd ..

Domoticz

Add dummy and virtual device Electric(Instant+counter)


Script for reading SDM120C and putting it in Domoticz

Make a file and copy code below

#!/bin/bash
#title           :solar
#description     :Read current from sdm120 and sends it to Domoticz.
#author		 :JM
#date            :20160605
#version         :0.1
#usage		 :./solar
#notes           :
#bash_version    :
#==============================================================================
SERVER="http://###.###.###.###:###"   #server location
IDX=#	  #id of your device

#SDM120C
SDM120="/home/pi/SDM120C/sdm120c" #location sdm120c script
TTY="/dev/ttyUSB0"		  #USB port sdm120c

CURRENT=$($SDM120 $TTY -p -q -z 10 | awk '{print $1}') #get current from sdm120c

# echo "$SERVER/json.htm?type=command&param=udevice&idx=$IDX&nvalue=$CURRENT&svalue=POWER;ENERGY"

x=0 
for gelezenWaardE in $($SDM120 $TTY -z 10  -ip|awk -F ":" '!/OK/ {print $2} '|sed 's/[ a-zA-Z]//g') ; do 

if [ ! $x -eq 1 ]; then
	watt=$gelezenWaardE
	x=1
	else
	kilowattuur=$(($gelezenWaardE/1000))
fi 

done

curl -s {"$SERVER/json.htm?type=command&param=udevice&idx=$IDX&nvalue=0&svalue=${watt};${kilowattuur}"}  #send current to domoticz
curl -s {"$SERVER/json.htm?type=command&param=addlogmessage&message=SolarSDM120Update"}  #send current to domoticz

Change the file to your specific situation and add to cron

SDM220-Modbus adaptation

sudo apt-get install libmodbus5 libmodbus-dev git
git clone https://github.com/gianfrdp/SDM120C.git
cd SDM120C
make clean && make
sudo make install
cd ..

Adding "-P N" could solve "NOK issues" while reading values

sdm120c -P N /dev/ttyUSB0 -p -t -q

Modified sdm220.sh script

File : /home/pi/domoticz/scripts/user/sdm220.sh

SERVER="http://127.0.0.1:8080"   #server location
IDX=1974          #id of your device
VALUES=NOK
#echo $VALUES #Debugging line
while [ "$VALUES" = "NOK" ]
do
        VALUES=`/usr/local/bin/SDM120C/sdm120c /dev/ttyUSB0 -P N -p -t -q` #p=power / t=total / q=short output
#       echo $VALUES #Debugging line
done
        POWER=`echo $VALUES | cut -d ' ' -f1`
        TOTAL=`echo $VALUES | cut -d ' ' -f2`
curl -s {$SERVER"/json.htm?type=command&param=udevice&idx="$IDX"&nvalue=0&svalue="$POWER";"$TOTAL""}  #send current to domoticz

Automating readings

Domoticz logs on 5min periods, let's create a 5min cronjob

crontab -e
*/5 * * * * /home/pi/domoticz/scripts/user/sdm220.sh >/dev/null 2>&1