I could not find a topic for the simple S0 energy meter
Aliexpress https://nl.aliexpress.com/item/5-32-A-2 ... 0.0.SuQ44h
or any other cheap counter. This counter has 1000 pulses, if u use a different one recalculate the power use
connections
Port 22 (plus) of the DDS238 is connected to Raspberry 5v (pin 2 or 4)
Port 20 (minus) of the DDS238 is connected to Raspberry GPIO-13 (pin 33)
Function of the script
This script sends the counter and the power
The script reads from domoticz the last value of the meter, if this is a first start, take a preset
Every pulse is 1/1000 Kw = 1 Watt
Calculate the time between the last puls and the current one.
The energy used over one pulse = 1/time_diff * 3600 seconds
If the time between 2 pulses is 4.08 seconds the used power is 1/4.08 * 3600 = 882 W/H over the period of 4 seconds
Debugline --2017-06-07 19:12:33.264379 power 890.8 W/H in 4.041127 seconds
This value is send to Domoticz directly in a python script that calls a json line to 2 virtual switches (energy actual/counter) in 'from device' mode
I made a divider so the interface is only sending every 5 pulses (0.005 kWh)
The counter is send to a logfile every 100 pulses (0.1 kWh) in my case +/- 12 kWh / day = 120 lines a day
Script S0.py
Code: Select all
#!/usr/bin/python
import RPi.GPIO as GPIO
import datetime
import urllib
import urllib2
import json
import base64
from string import whitespace
GPIO.setmode(GPIO.BCM)
GPIO.setup(13, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
# Settings for the domoticz server
domoticzserver="192.168.0.135:8080"
domoticzusername = "user"
domoticzpassword = "password"
switchid="253"
switchid2="254"
watts=1
lastpuls = datetime.datetime.now()
base64string = base64.encodestring('%s:%s' % (domoticzusername, domoticzpassword)).replace('\n', '')
domoticzurl = 'http://'+domoticzserver+'/json.htm?type=devices&filter=all&used=true&order=Name'
debug = True
debug = False
logfile = '/var/log/S0power'
print ("Log in " + logfile)
def domoticzrequest (url):
request = urllib2.Request(url)
request.add_header("Authorization", "Basic %s" % base64string)
response = urllib2.urlopen(request)
return response.read()
def domoticzstatus (switchid):
json_object = json.loads(domoticzrequest(domoticzurl))
switchfound = False
level = ""
ilevel = 1.1
#print json_object
#print json_object["status"]
if json_object["status"] == "OK":
for i, v in enumerate(json_object["result"]):
if json_object["result"][i]["idx"] == switchid:
#print(json_object["result"][i])
switchfound = True
dlevel = json_object["result"][i]["Data"]
slevels = dlevel.split(' ')
level = slevels[0]
return level
# give here the value of your official meter
realmeter= 164670400
# read lastmeter from domoticz in W/H
meter_new = float(domoticzstatus(switchid)) * 1000
# if first start meter is adjusted
if meter_new < realmeter:
meter_new = realmeter
meter_old = meter_new
print('Last value is ' + str(meter_new/1000))
# write restart to logfile
logregel = str(datetime.datetime.now()) + " Restart S0 counter with value " + str(meter_new/1000)
l = open(logfile, 'a')
l.write (logregel + '\n')
l.close()
while True:
GPIO.wait_for_edge(13, GPIO.RISING)
GPIO.wait_for_edge(13, GPIO.FALLING)
now = datetime.datetime.now()
datediff= now - lastpuls
aantalseconden = datediff.total_seconds()
power = (meter_new - meter_old) /aantalseconden * 3600
logregel = str(now) + ' meter_new ' + str(meter_new/1000) + ' kW/h'
if debug == True:
print (logregel)
if meter_new % 100 == 0:
l = open(logfile, 'a')
l.write (logregel + '\n')
l.close()
if (meter_new) % 5 == 0 : # send to Domoticz
print (logregel)
pulsmeter = str(meter_new )
domoticzrequest("http://" + domoticzserver + "/json.htm?type=command¶m=udevice&idx=" + switchid + "&nvalue=0&svalue=" + str(power) + ";" + str(meter_new))
domoticzrequest("http://" + domoticzserver + "/json.htm?type=command¶m=udevice&idx=" + switchid2 + "&nvalue=0&svalue=" "0;" + str(meter_new))
lastpuls = now
meter_old = meter_new
watts = watts + 1
meter_new = meter_new + 1
GPIO.cleanowp()
To start the script at startup of the raspberry a service starts the S0.sh script
Code: Select all
chmod 755 S0.sh
Code: Select all
#!/bin/sh
scriptk='/home/pi/S0/S0.py'
exec /usr/bin/python $scriptk
Code: Select all
#!/bin/sh
### BEGIN INIT INFO
# Provides: S0 energy meter
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Put a short description of the service here
# Description: Put a long description of the service here
### END INIT INFO
# Change the next 3 lines to suit where you install your script and what you want to call it
DIR=/home/pi/S0
DAEMON=$DIR/S0.sh
DAEMON_NAME=S0meter
# Add any command line options for your daemon here
DAEMON_OPTS=""
# This next line determines what user the script runs as.
# Root generally not recommended but necessary if you are using the Raspberry Pi GPIO from Python.
DAEMON_USER=root
# The process ID of the script when it runs is stored here:
PIDFILE=/var/run/$DAEMON_NAME.pid
. /lib/lsb/init-functions
do_start () {
log_daemon_msg "Starting system $DAEMON_NAME daemon"
start-stop-daemon --start --background --nicelevel -19 --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --startas $DAEMON -- $DAEMON_OPTS
log_end_msg $?
}
do_stop () {
log_daemon_msg "Stopping system $DAEMON_NAME daemon"
start-stop-daemon --stop --pidfile $PIDFILE --retry 10
log_end_msg $?
}
case "$1" in
start|stop)
do_${1}
;;
restart|reload|force-reload)
do_stop
do_start
;;
status)
status_of_proc "$DAEMON_NAME" "$DAEMON" && exit 0 || exit $?
;;
*)
echo "Usage: /etc/init.d/$DAEMON_NAME {start|stop|restart|status}"
exit 1
;;
esac
exit 0
Code: Select all
sudo update-rc.d S0meter defaults