Hi Everyone,
Today i set up my new breaker board and i installed a DDS238-2 SW KWH meter on it do report my power usage to Domoticz.
This meter has a pulse counter (so no serial port) to send pulses to your controller (In my case a Raspberry Pi 3). So i use 3,3V GPIO input to pin 3 on the KWh meter and pin 4 to the inputpin of the Pi. My version has 1600 pulses per KWh, but i noticed there are versions with different pulse rates. If that is the case, just replace the formula below (replace the 1.6 value with for example 3.2 if you have a 3200 pulse/KWh version)
I will share the steps below in case you want to implement this the same way i did.
1. First create a virtual sensor in domoticz
2. Then create the deamon wich collects the data.
I use a very simple bash script to achieve this:
Replace the X's with your controllers IP and the Y's with the Domoticz Virtual sensor ID
# vi readpulse.sh
-----------------------------
#!/bin/bash
timer=`date +%s`
i="0"
n=0
while [ $i -lt 4 ]
do
kwhpulse=$(gpio read 0)
if [ $kwhpulse -eq 1 ]
then
n=$((n+1))
#echo $n
while [ $kwhpulse -eq 1 ]
do
kwhpulse =$(gpio read 0)
sleep 0.02
done
fi
sleep 0.03
time_now=`date +%s`
time_elapsed=$((time_now - timer))
if [ $time_elapsed -ge 300 ]
then
power=$(echo "scale=1;$n/1.6" | bc)
#echo "KWH laatste 5 minuten" $power "KW"
timer=`date +%s`
n=0
curl -s --connect-timeout 2 --max-time 5 "http://xxx.xxx.xxx.xxx/json.htm?type=command¶m=udevice&idx=YY&nvalue=0&svalue=$power"
fi
done
---------------------------
Make it executable and give proper rights;
chmod +x readpulse.sh
chmod 755 readpulse.sh
3. Make it into a service. (the initgpio service is allso a custom service, so please change this to reflect your situation(after you exported the GPIO's)
# vi /etc/systemd/system/kwhpulse.service
[Unit]
After=initgpio.service
[Service]
ExecStart=/home/jef/readpulse.sh
[Install]
WantedBy=default.target
4. reload systemctl and enable service
systemctl daemon-reload
systemctl enable kwhpulse.service
systemctl start kwhpulse.service
This script has a pretty high CPU usage since it has to poll the input every 30 milliseconds. It uses between 6 and 9% CPU. While this is not a problem for my envirement, it can be good to know in advance.
That should be all. You should start to see the data coming into Domoticz now. (i had a NTP desync wich i solved while making this, so hence the datagap in the graph, but then atleast i can show something :))
Hi, i have Raspberry 3 (latest version raspbian), domoticz (latest stabil version).
BeantwoordenVerwijderenWhen i running this bash script and service i have error:
service status: Loaded: loaded
Active: Failed (Result: exit-code)
Process: 6438 ExecStart=/home/pi/readpulse.sh (code=exited, status=203/EXEC)
help pls
Does it work when u run the script manually (not as a service?)
BeantwoordenVerwijderenwhen i run manually ./readpulse.sh .... without error, but neverending process ... and in list ps -A this script isnt
BeantwoordenVerwijderenopps now ./readpulse write in terminal line 24: bc: command not found
BeantwoordenVerwijderenapt-get install bc should solve your problem.
BeantwoordenVerwijderenyes, now isnt error when i run script manually ... but i see only cursor, this is ok ??
VerwijderenYes, this is normal. The script gives no feedback as it is supposed to run in the background. It sends the pulsecount to domoticz every 5 minutes. For debugging purposes you can uncomment the 'echo $n' line so you see the pulsecount. You need to disable that for production though. Enjoy!
Verwijderenwhen i manually write data in browser (http://10.193.116.242:12000/json.htm?type=command¶m=udevice&idx=55&nvalue=0&svalue=22) ... this is OK and domoticz has 0,22 kwh
VerwijderenNow is OK, super THX for support and this code :-)
VerwijderenAn interesting blog, but I still have not finished it. I have the same kwh meter and rasberry pi 3.
BeantwoordenVerwijderenHow do I export a gpio? What do I have to write exactly in the gpio script? I looked up something, but it is not clear to me.
And in which folder should I save the script?
Do I have to define a specific gpio pin for the incoming pulse?
Step 1 and 2 I have done as described.
Thanks in advance for your response.
Hi,
VerwijderenThe export command is just litterally:
gpio export PINUMBER in/out
example:
gpio export 12 in (for input)
gpio export 12 out (for output)
You have to put in the script exactly whats between the 2 lines in step 2. Change the IP and device ID(idx) and you are done.
Yes, you have to define a specific pin for the incoming pulse. More precise, the pin you connected it to :)? In this example it is GPIO 0 (Physical pin 11, BCM17)
You can choose any folder to save the file, just adjust the path in the service script in step 4.
Thx for you help Jef. I have followed your instructions. Apparently I do not do anything right. Nothing else happens.
Verwijderenstep 2
I select pin16 (gpio 23) voor incoming pulse and changed html into:
curl -s --connect-timeout 2 --max-time 5 "http://192.168.0.120/json.htm?type=command¶m=udevice&idx=YY&nvalue=0&svalue=$power"
# vi readpulse.sh
-----------------------------
gpio export 16 in
#!/bin/bash
timer=`date +%s`
i="0"
n=0
while [ $i -lt 4 ]
do
kwhpulse=$(gpio read 0)
if [ $kwhpulse -eq 1 ]
then
n=$((n+1))
#echo $n
while [ $kwhpulse -eq 1 ]
do
kwhpulse =$(gpio read 0)
sleep 0.02
done
fi
sleep 0.03
time_now=`date +%s`
time_elapsed=$((time_now - timer))
if [ $time_elapsed -ge 300 ]
then
power=$(echo "scale=1;$n/1.6" | bc)
#echo "KWH laatste 5 minuten" $power "KW"
timer=`date +%s`
n=0
curl -s --connect-timeout 2 --max-time 5 "http://192.168.0.120/json.htm?type=command¶m=udevice&idx=YY&nvalue=0&svalue=$power"
fi
done
---------------------------
Make it executable and give proper rights;
chmod +x readpulse.sh
chmod 755 readpulse.sh
step 3
I made with nano a script
sudo nano kwhpulse.service
I changed map into /etc/systemd/system/readpulse.sh
[Unit]
After=initgpio.service
[Service]
ExecStart=/etc/systemd/system/readpulse.sh
[Install]
WantedBy=default.target
crtl X
yes
enter
step 4
systemctl daemon-reload
systemctl enable kwhpulse.service
systemctl start kwhpulse.service
I choose 1 with correct password, but nothing happend
==== AUTHENTICATING FOR org.freedesktop.systemd1.reload-daemon ===
Authentication is required to reload the systemd state.
Multiple identities can be used for authentication:
1. ,,, (pi)
2. root
Choose identity to authenticate as (1-2):
Hi,
BeantwoordenVerwijderenYou have not specified the IDX number in the URL. replace the YY after idx= with the idx number from your Virtual sensor in domoticz (Setup -> Devices -> idx column)
Secondly, the GPIO number is wrong. If you use GPIO 23 on a raspberry pi, this is Physical Pin 33, 13 BCM. If you mean physical pin 16, this is GPIO 4, BCM 23. So please verify you use the correct pin, do a full gpio readout with the command 'gpio readall'
If you use GPIO 23, it should be: gpio export 13 in
If you use GPIO 04, it should be: gpio export 23 in
Thx for your help.
BeantwoordenVerwijderenGPIO numbering quite confusing. I get it now.
https://pinout.xyz/
Everything done as you have indicated. Still waiting for the sun.
The box yield `solar panels' in Domoticz is still red. I know at the end of tomorrow afternoon whether the pulses are measured.
Sorry to have a question again. Apparently I'm not doing something right.
BeantwoordenVerwijderenI can not see values in domoticz.
When script is started manually by means of: sudo sh readpulse.sh nothing happens in domoticz.
The hpadmin test does work.
hpadmin1 januari 2018 om 02:43
when i manually write data in browser (http://10.193.116.242:12000/json.htm?type=command¶m=udevice&idx=55&nvalue=0&svalue=22) ... this is OK and domoticz has 0,22 kwh
The KWh meter does pulsate I have measured. Kwh meter is connected to BCM 4 pin 7.
Is the script below really correct or do I overlook something?
gpio export 4 in
#!/bin/bash
timer=`date +%s`
i="0"
n=0
while [ $i -lt 4 ]
do
kwhpulse=$(gpio read 0)
if [ $kwhpulse -eq 1 ]
then
n=$((n+1))
#echo $n
while [ $kwhpulse -eq 1 ]
do
kwhpulse =$(gpio read 0)
sleep 0.02
done
fi
sleep 0.03
time_now=`date +%s`
time_elapsed=$((time_now - timer))
if [ $time_elapsed -ge 300 ]
then
power=$(echo "scale=1;$n/1.6" | bc)
#echo "KWH laatste 5 minuten" $power "KW"
timer=`date +%s`
n=0
curl -s --connect-timeout 2 --max-time 5 "http://192.168.0.120/json.htm?type=command¶m=udevice&idx=9&nvalue=0&svalue=$power"
fi
done
Hi, Sadly i cannot help you out any further as this is clearly caused by incorrect PIN numbers. If you cannot figure that out you will never get it working. Use this part of the script to just test pin readout(make seperate script):
BeantwoordenVerwijdereni=0
while [ $i -lt 4 ]
do
kwhpulse=$(gpio read 0)
if [ $kwhpulse -eq 1 ]
then
n=$((n+1))
echo $n
done
Then you will get immediate pulse readouts and you dont have to wait. The number should increase by 1 with every pulse. If it stays 0 you have wrong pin number. If you find the correct one use that in the full script and done.
Allso dont put code before the #!/bin/bash statement
BeantwoordenVerwijderenDeze reactie is verwijderd door de auteur.
BeantwoordenVerwijderenDeze reactie is verwijderd door de auteur.
BeantwoordenVerwijderen#!/bin/bash
BeantwoordenVerwijdereni=0
n=0
while [ $i -lt 4 ]
do
kwhpulse=$(gpio read 12)
if [ $kwhpulse -eq 1 ]
then
n=$((n+1))
echo $n
fi
sleep 0.02
done
Why do you not fill in the correct pin numbers??
BeantwoordenVerwijderenchange the 0 ALLSO kwhpulse=$(gpio read PINNUMBER HERE)
So that would be 'kwhpulse=$(gpio read 4)' You have to do that at 2 places