DIY S0-pulsecounter ready2use with Domoticz (Arduino based)

In this subforum you can show projects you have made, or you are busy with. Please create your own topic.

Moderator: leecollings

ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

DIY S0-pulsecounter ready2use with Domoticz (Arduino based)

Post by ThinkPad »

I was interested in how the S0PCM-5 module worked and was thinking of making it myself with an Arduino. And i managed to do it :mrgreen:

I had a look at the Domoticz sourcecode that handles the S0PCM and it is actually quite simple, especially when i found out the description of the data that the original S0PCM spits out:
Spoiler: show
/a:S0 Pulse Counter V0.x
Data record (repeated every interval):
ID:a:I:b:M1:c:d:M2:e:f:M3:g:h:M4:i:j:M5:k:l

Legenda:
a -> Unique ID of the S0PCM
b -> interval between two telegrams in seconds
c -> number of pulses in the last interval of register 1
d -> number of pulses since the last start-up of register 1
e -> number of pulses in the last interval of register 2
f -> number of pulses since the last start-up of register 2
.... etc.... for register 3, 4, 5
Source: http://www.smartmeterdashboard.nl/blog/ ... ects=0&d=1

So you just need to provide Domoticz with two counters (for each device), one counter that contains the counts since the start of the Arduino, and one with the counts since the last interval (=message sent to Domoticz, every 10s in default situation).

I then went on editing an Arduino sketch, it is mainly based on this one.
I came up with the code below:

S0CounterArduino.ino
Spoiler: show

Code: Select all

//Number of pulses, used to measure energy.
volatile unsigned int pulseCount1  = 0;   
volatile unsigned int pulseCount1_sinceStart  = 0; 
volatile unsigned int pulseCount2  = 0;   
volatile unsigned int pulseCount2_sinceStart  = 0; 

volatile unsigned int updateCount = 0;   
volatile unsigned int reportInterval = 10;  //Interval between messages being sent out (in seconds)
int PulseCounterID = 12345;
int PulseCounterVersion = 999;

// The interrupt routine
void onPulse1()
{
//pulseCounter
    pulseCount1++;
    pulseCount1_sinceStart++;
//Blink built-in LED on S0-pulse
digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
delay(80);               // wait for a few miliseconds
digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW    
}

// The interrupt routine
//void onPulse2()
//{  
    //pulseCounter
    //pulseCount2++;
    //pulseCount2_sinceStart++;
//}


void setup()
{
    // KWH interrupt attached to IRQ 0  = pin2 (D2)
    attachInterrupt(0, onPulse1, FALLING);
    //KWH interrupt attached to IRQ 1  = pin3    
    //attachInterrupt(1, onPulse2, FALLING);
    Serial.begin(9600);
    Serial.print("/");
    Serial.print(PulseCounterID);
    Serial.print(":S0 Pulse Counter V");
    Serial.println(PulseCounterVersion);
}

void loop()
{
        updateCount = 0;
        Serial.print("ID:");
        Serial.print(PulseCounterID);
        Serial.print(":I:10:M1:");
        Serial.print(pulseCount1);
        Serial.print(":");
        Serial.print(pulseCount1_sinceStart);
        Serial.print(":");
        Serial.print("M2");
        Serial.print(":");
        Serial.print(pulseCount2);
        Serial.print(":");
        Serial.println(pulseCount2_sinceStart);

        pulseCount1 = 0;
        pulseCount2 = 0;

    updateCount++; 
    delay(reportInterval * 1000); // Need to multiply it by 1000 because we need it in ms
}
To test it, i used a second Arduino and let it blink it's internal LED, pretending to be that it's an flashing kWh-meter LED.
A pulse should last about 30 - 100ms i found in meter datasheet.
In Domoticz i added the Arduino (presents itself as a serial COM-port) and two devices popped up. I then placed the 'kWh-faker' Arduino in front of the photodiode. After a few seconds i saw the current (fake) powerusage of ~1800-2200W, success!!! :twisted: 8-)

For a schema on how to connect a S0-counter, i suppose you can just use the schema from here: http://fun-tech.se/FunTechHouse/ElectricityMeter/

kWhSimulator.ino
Spoiler: show

Code: Select all

/*
This sketch pretends itself as a blinkin LED on a kWh-meter for measuring electricity
*/
 
// Pin 13 has an LED connected on most Arduino boards.
int led = 13;
volatile unsigned int FakeConsumptionWatt = 1800;  //Fill in a value (in Watts) to fake the kWh-reading device with
int PulseDuration = 50; //Most real kWh-meters seem to use values between 30 - 100ms
float TimeBetweenBlinks;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);  
  TimeBetweenBlinks = (FakeConsumptionWatt / 3.6) * 4; //In miliseconds
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);         // turn the LED on (HIGH is the voltage level)
  delay(PulseDuration);                     // wait for a second
  digitalWrite(led, LOW);         // turn the LED off by making the voltage LOW
  delay(TimeBetweenBlinks);   // Calculate and wait, to let the LED blink in a interval that corresponds with the given consumption
}
Image

I quickly hacked this together, so i don't have any experience on how it performs in a production setup. It could be that you need some code like debouncing. I also have a DIN-rail kWh-meter with S0-connector that i will connect to the Arduino so i can test it with a real S0 device. But i don't have much time now, so that will be something for later. First observation: it works, but accuracy is not spot on. But could also be due to the Arduino faking the kWh, it is using 'delay' and that isn't very accurate. Using 'millis' would be better. Will test it later with a S0 meter and with my kWh-meter in the meter cabinet (has a flashing led, 2000imp/kwh).
But i just wanted to share this, so people can fiddle around with it :mrgreen:
Last edited by ThinkPad on Monday 28 September 2015 11:48, edited 10 times in total.
Reason: Added blinking LED on incoming pulse
I am not active on this forum anymore.
joshimosh
Posts: 58
Joined: Friday 29 May 2015 7:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DIY S0-pulsecounter ready2use with Domoticz (Arduino bas

Post by joshimosh »

Very interesting. I have just finished the hardware to transmit the pulses of my three S0 meters via MySensors. The meters give 1000 pulses per kWh.
Since I am also interested in the actual Wattage, I send W and kWh to Domoticz. Since you seem to be an old hand ;-) in this Domoticz business, perhaps you can answer a question. I plan to send the number of pulses to Domoticz. I would like to have the total power consumption (kWh). Does Domoticz sum up the pulses I send (sending the number of pulses measured every minute) or do I have to sum up myself in my MySensors node ?

Thank you.
Cheers
Josh
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: DIY S0-pulsecounter ready2use with Domoticz (Arduino bas

Post by ThinkPad »

I think it depends on the device. When i was fiddling with virtual counters a while ago i had to do the sum myself, but with this meter where the topic is about it seems to sum up itself (calculating the total and today value).
I am not active on this forum anymore.
joshimosh
Posts: 58
Joined: Friday 29 May 2015 7:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DIY S0-pulsecounter ready2use with Domoticz (Arduino bas

Post by joshimosh »

Since my Meters were darn cheap, my best guess is that they just deliver pulses, nothing else.
Giving it a few more thoughts, this is for me probably the best solution.
Having the number of pulses every 6 minutes (so one pulse represents an average of 10 W during the last 6 minutes), I can afterwards fiddling with these value using SQLite.
My only hope is that Domoticz just pushes these values unaltered into the database.
I guess I simply have to try.
Cheers
Josh
Dlanor
Posts: 67
Joined: Sunday 22 March 2015 16:18
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: DIY S0-pulsecounter ready2use with Domoticz (Arduino bas

Post by Dlanor »

I am working on the same solution on my piface. Would an S0 pulse also work on 3.3v?
Domoticz| Zigbee | SONOFF
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: DIY S0-pulsecounter ready2use with Domoticz (Arduino bas

Post by ThinkPad »

Today i had some time to test this in a real life situation.

Cut up an extension cord and placed the DIN-rail kWh-meter i had lying in between. Connected a lightbulb (45W) to it, waited a while (time between pulses is quite long with only 45W) but eventually the sensor in Domoticz changed and showed 45W :)

However (and like i already expected) the accuracy isn't that good. It now shows 48W, but the digital Wattmeter only shows 45W when placed between the lamp.
But maybe it could also be due to the kWh-meter providing the S0-pulses? The brand is 'Dengli', a cheap brand from China i see on Google...

I have it connected like this:
S0-meter connector 20: connected to 3V3 of Arduino
S0-meter connector 21: connected to Arduino pin D2

And between pin D2 and GND a resistor of 4K7 (the FunTech link from my first post uses 10K but i am out of stock on those ones). At first it didn't work at all, Arduino didn't saw the pulses. But then i found out that the S0-connector is polarized and i had the wires reversed.

Something else i noticed: when resetting the Arduino, the 'PulseCountFromStart' is ofcourse lost. Due to this Domoticz increases the total energy counter like crazy (11kWh added in once). So i think the S0PCM-5 stores it in EEPROM or so? Otherwise after a power failure your figures would be totally messed up.
I am not active on this forum anymore.
Dlanor
Posts: 67
Joined: Sunday 22 March 2015 16:18
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: DIY S0-pulsecounter ready2use with Domoticz (Arduino bas

Post by Dlanor »

ThinkPad wrote:Today i had some time to test this in a real life situation.

Cut up an extension cord and placed the DIN-rail kWh-meter i had lying in between. Connected a lightbulb (45W) to it, waited a while (time between pulses is quite long with only 45W) but eventually the sensor in Domoticz changed and showed 45W :)

However (and like i already expected) the accuracy isn't that good. It now shows 48W, but the digital Wattmeter only shows 45W when placed between the lamp.
But maybe it could also be due to the kWh-meter providing the S0-pulses? The brand is 'Dengli', a cheap brand from China i see on Google...

I have it connected like this:
S0-meter connector 20: connected to 3V3 of Arduino
S0-meter connector 21: connected to Arduino pin D2
Thanks! I just remembered I have bought two ABB kWh meters. I only have added one into my distribution panel at home. I will try the same setup with my piface and post the results. Using the 3.3v it saves an extra circuit with an 2n7000.
Domoticz| Zigbee | SONOFF
Dlanor
Posts: 67
Joined: Sunday 22 March 2015 16:18
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: DIY S0-pulsecounter ready2use with Domoticz (Arduino bas

Post by Dlanor »

Thanks! I just remembered I have bought two ABB kWh meters. I only have added one into my distribution panel at home. I will try the same setup with my piface and post the results. Using the 3.3v it saves an extra circuit with an 2n7000.
I have connected the ABB kWh directly to the input of the piface. It works perfect!
Last edited by ThinkPad on Sunday 02 August 2015 13:48, edited 1 time in total.
Reason: Fixed quote tags ;)
Domoticz| Zigbee | SONOFF
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: DIY S0-pulsecounter ready2use with Domoticz (Arduino bas

Post by ThinkPad »

Good to hear! What do you use it for?
I am not active on this forum anymore.
Dlanor
Posts: 67
Joined: Sunday 22 March 2015 16:18
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: DIY S0-pulsecounter ready2use with Domoticz (Arduino bas

Post by Dlanor »

ThinkPad wrote:Good to hear! What do you use it for?
Energy consumption. I have an smart meter but it can only read by IR. Couldn't get it working so got this.
Domoticz| Zigbee | SONOFF
User avatar
bbqkees
Posts: 407
Joined: Sunday 17 August 2014 21:01
Target OS: Linux
Domoticz version: 4.1x
Location: The Netherlands
Contact:

Re: DIY S0-pulsecounter ready2use with Domoticz (Arduino bas

Post by bbqkees »

ThinkPad wrote:Today i had some time to test this in a real life situation.
Something else i noticed: when resetting the Arduino, the 'PulseCountFromStart' is ofcourse lost. Due to this Domoticz increases the total energy counter like crazy (11kWh added in once). So i think the S0PCM-5 stores it in EEPROM or so? Otherwise after a power failure your figures would be totally messed up.
Arduino's also have a small internal EEPROM.
Just periodically store the value there.
Bosch / Nefit / Buderus / Junkers / Worcester / Sieger EMS bus Wi-Fi MQTT Gateway and interface boards: https://bbqkees-electronics.nl/
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: DIY S0-pulsecounter ready2use with Domoticz (Arduino bas

Post by ThinkPad »

bbqkees wrote:
ThinkPad wrote:Today i had some time to test this in a real life situation.
Something else i noticed: when resetting the Arduino, the 'PulseCountFromStart' is ofcourse lost. Due to this Domoticz increases the total energy counter like crazy (11kWh added in once). So i think the S0PCM-5 stores it in EEPROM or so? Otherwise after a power failure your figures would be totally messed up.
Arduino's also have a small internal EEPROM.
Just periodically store the value there.
I know, but i have no idea how to use it.

And i'm not doing anything with this project. It was more a fun project to help myself through a boring rainy Sunday afternoon :lol: Maybe someone can use the basics and extend it further to make it work really good.
I am not active on this forum anymore.
BasPost
Posts: 13
Joined: Thursday 25 June 2015 15:14
Target OS: Raspberry Pi / ODroid
Domoticz version: V2.2563
Contact:

Re: DIY S0-pulsecounter ready2use with Domoticz (Arduino bas

Post by BasPost »

ThinkPad wrote:
bbqkees wrote:
ThinkPad wrote:Today i had some time to test this in a real life situation.
Something else i noticed: when resetting the Arduino, the 'PulseCountFromStart' is ofcourse lost. Due to this Domoticz increases the total energy counter like crazy (11kWh added in once). So i think the S0PCM-5 stores it in EEPROM or so? Otherwise after a power failure your figures would be totally messed up.
Arduino's also have a small internal EEPROM.
Just periodically store the value there.
I know, but i have no idea how to use it.

And i'm not doing anything with this project. It was more a fun project to help myself through a boring rainy Sunday afternoon :lol: Maybe someone can use the basics and extend it further to make it work really good.
This is getting very interesting! Is there somebody who is willing to expand the script with EEPROM storage on the Arduino?
I if I could, I would do it myself, but I'm a beginner/n00b with this coding.
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: DIY S0-pulsecounter ready2use with Domoticz (Arduino bas

Post by ThinkPad »

I would start with this: https://www.arduino.cc/en/Tutorial/EEPROMWrite and see if you can get the PulseCountFromStart memorized in the EEPROM.
And then this: https://www.arduino.cc/en/Tutorial/EEPROMRead to retrieve it at boot of the Arduino (and set it to zero if there is no value in EEPROM).

I don't have much time unfortunately, and like i said i don't have a very big need for this myself. So don't expect anything from me soon regarding this :p
But to be honest, it looks easier than i thought (only 1-2 lines to write it into memory), so can't be that hard.
I am not active on this forum anymore.
Stee
Posts: 1
Joined: Wednesday 04 March 2015 13:39
Target OS: Windows
Domoticz version:
Contact:

Re: DIY S0-pulsecounter ready2use with Domoticz (Arduino bas

Post by Stee »

Just a remark on this about the accuracy of domoticz. I found that in my experience, domoticz only shows an estimate of the actual watt usage because the s0pcm sends info every 10 seconds and doesn't report the length of time per pulse.
In other words, if you want accurate readings, you'll need to time the total pulse (low and high time) to know how long it took to use one watt.
So suppose it was 51ms high (the pulse) and 6054ms low, then the watt/h is
3600000 / (51 + 6054) = 589wh (there are 3600000ms in one hour)
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: DIY S0-pulsecounter ready2use with Domoticz (Arduino bas

Post by ThinkPad »

Thanks for your comment. But i'm not using this, it was just a proof of concept that i made during a weekend when i was bored :lol:
The sketch is nowhere near final, like i said in a earlier post it doesn't store the pulse count, so when power to the Arduino is lost, Domoticz will generate a huge number.
I am not active on this forum anymore.
arnoldg
Posts: 15
Joined: Tuesday 29 September 2015 20:35
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Almelo, netherlands
Contact:

Re: DIY S0-pulsecounter ready2use with Domoticz (Arduino bas

Post by arnoldg »

Hi there,

i have run the sketch in the above post, hooked mij arduino nano to a'n usb port on my pi. but there are no sensor's displayd on the pi.
what do i do wrong.

I'm a newby in this. not with arduino and not with the pi.
if i hook up the arduino to my computer and read out the serial data (9600 baud) i get the sensor string, so the nano is working.

pleas help me with this.
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: DIY S0-pulsecounter ready2use with Domoticz (Arduino bas

Post by ThinkPad »

Did you add a 'S0 Meter USB' under Setup > Hardware? If you did that, a device should appear under Setup > Devices.
I am not active on this forum anymore.
arnoldg
Posts: 15
Joined: Tuesday 29 September 2015 20:35
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Almelo, netherlands
Contact:

Re: DIY S0-pulsecounter ready2use with Domoticz (Arduino bas

Post by arnoldg »

now i have, i some how added the mysensor hardware :shock: :shock:

now it works, but i think the reading is not accurate. when my dishwasser is on i have a reading of 300 Watt (i think it should be somthing around 1500 watt)
what could it be.
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: DIY S0-pulsecounter ready2use with Domoticz (Arduino bas

Post by ThinkPad »

You need to adjust the amount of pulses per kWh i think....

But like i said earlier: this is more like a proof of concept, not something to be used in a real life environment. It needs more fixing/code to work completely good.
I don't have the time/knowledge for that unfortunately.
I am not active on this forum anymore.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests