💬 Power Meter Pulse Sensor



  • One (probably stupid) question about the batter powered option: Since sensor cannot keep track of real time power usage as it sleeps most of the time, would it be possible to do the math on the controller side and get "5min delay real time power usage"?
    By dividing number of pulses since last transmission, is that basically what this sensor is doing when not being powered by batteries?

    I could experiment with 2min delay f.eks, and see how batteries hold. If dht22 temp sensors works over a year with 5min transmission time, i hope 2min delay in pulse meter can hold 6mnt or something?
    Another thing is that the electrical box is all metal, and there is a lot of electricity around the sensor so radio could struggle (I guess a repeater would definitively help)


  • Mod

    Is it only me that is getting a compile error?

    Error: call of overloaded 'set(volatile long unsigned int&)' is ambiguous
    send(wattMsg.set(watt)); \ Send watt value to gw

    error: call of overloaded 'set(volatile long unsigned int&)' is ambiguous
    send(pcMsg.set(pulseCount)); \ Send pulse count value to gw

    followed by all the possible candidates of .set arguments


  • Mod

    @gohan I get the same, both for 2.1.1 and 2.2.0-beta. Strange. I thought there were Jenkins tests that endured the examples compiled.


  • Mod

    Replacing all

    unsigned long
    

    with

    uint32_t
    

    makes the sketch compile fine, but I don't know if that is the correct solution.


  • Mod


  • Admin

    The problem with "unsigned long" is that it can have different size based on platform it's compiled on.

    Like @mfalkvidd says, use int32_t, uint32_t, int16_t and uint16_t which has a fixed width.


  • Mod

    I was suspecting something like that, but I was not sure if changing variable type would affect something else


  • Mod

    Ok, I got it working. On wemos it doesn't count pulses though. On domoticz does it have to configured as the value computed or from device? I'm asking because if you disconnect the sensor, on domoticz I keep seeing the same power reading


  • Mod

    BTW, is there a way to have the pulse meter running on gateway? I got stuck because at startup it asks the controller the last count value and since the controller is not yet connected it just doesn't work.



  • @gohan for the first time use only the watt value....that time oldcounts will not bother.....


  • Mod

    What do you mean? And how do I keep the count of the pulses if I don't get it from controller?


  • Mod

    @hek is it me or the WATT readings are never sent? What could it be? Only V_VAR1 and V_KWH are sent.



  • @gohan How about

    #define MY_TRANSPORT_WAIT_READY_MS 2000
    

    This will start up your code so you won't miss any pulses until controller is ready.
    You may ask for the last known Value in presentation(), then it will be fetched as soon as the controller is really there after startup. This could/should be combined with a bool pcReceived, so you may decide to either add the value received from controller to the counts the node meassured in between or just use the received value (starting from second time value is received from controller).


  • Mod

    That actually is not that critical, as I am going to move to MQTT and that should be working anyway. Right now it is working on a UNO with NRF24, but my biggest issue is the WATT reading that it is missing


  • Mod

    Any more suggestions on the Watt issue?


  • Admin

    Did you by any chance change SLEEP_MODE to true?


  • Mod

    No, I get every pulse reported as soon as it is detected.


  • Mod

    I think I found a problem: the WATT calculation is returning values over 172000 watts and since I put 10000 as the max value, it was not sending anything. I double checked my energy meter and indeed it is 1000 pulses per KWH and this is what I set in sketch


  • Mod

    @hek I said I found the problem, I didn't say I know how to solve it 😅


  • Mod

    Here is the log I get from node

    7230 TSF:MSG:READ,0-0-250,s=1,c=2,t=24,pt=0,l=7,sg=0:1682675
    rece
    
    Received last pulse count from gw:1682675
    
    11817 TSF:MSG:SEND,250-250-0-0,s=1,c=1,t=17,pt=5,l=4,sg=0,ft=0,st=OK:528
    Watt:528
    
    Watt:197065
    
    21820 TSF:MSG:SEND,250-250-0-0,s=1,c=1,t=24,pt=5,l=4,sg=0,ft=0,st=OK:1682677
    21913 TSF:MSG:SEND,250-250-0-0,s=1,c=1,t=18,pt=7,l=5,sg=0,ft=0,st=OK:1682.6770
    Watt:196807
    
    36824 TSF:MSG:SEND,250-250-0-0,s=1,c=1,t=24,pt=5,l=4,sg=0,ft=0,st=OK:1682679
    36917 TSF:MSG:SEND,250-250-0-0,s=1,c=1,t=18,pt=7,l=5,sg=0,ft=0,st=OK:1682.6790
    Watt:197368
    
    51827 TSF:MSG:SEND,250-250-0-0,s=1,c=1,t=24,pt=5,l=4,sg=0,ft=0,st=OK:1682681
    51920 TSF:MSG:SEND,250-250-0-0,s=1,c=1,t=18,pt=7,l=5,sg=0,ft=0,st=OK:1682.6810
    Watt:196850
    

    Am I the only one using this sketch?



  • Please post you sketch.


  • Mod



  • How is the pulse counter set up? Is it connected to the S0 port?
    Edit:
    If you are using S0 it is vital that you use a pull down resistor, if you dont’t have that you get false pulses.
    pulse connection


  • Mod

    I'm using the lm393 light detector. Domoticz is able to do a calculation from the pulses to give a Watt value and it is correct but it does it every 5 minutes and it is too slow for me


  • Hardware Contributor

    @gohan - is Domoticz updating anything (except switches) faster than 5 min? I dont think so...


  • Mod

    If you send the watt reading faster it updates like the switch, anyway I need the watt values for node-red also


  • Mod

    Could it be the Light sensor is triggering more interrupts from each pulse?



  • @niklaso

    Hi Niklas,

    I know this is a very old post, but its worth a try. How did you get out the daily Kwh usage? As default my sensor only gives Watts and Kwh (accumulated from start), I have tried to run a Scene in Vera UI7 where there are a function in the sensor called resetKWh(), but it looks like it doesn't do anything. Any tips would be great, thanks.

    Henning



  • @gohan I think it is a little tricky to update domoticz every minute but try update the sql-file, see the domoticz forum
    https://www.domoticz.com/forum/viewtopic.php?t=10183


  • Mod

    The main issue is the fact that the sensor is not calculating the instant power correctly



  • I created this kWh sensor with the TCRT5000 IR Barrier Line Track Sensor.
    My powermeter is a ferarrismeter with 375 rotations per kWh.
    I also had the problem with spikes in the power usage. But after long...too long investigation I solved the problem.
    At my config it was a double issue.

    Issue 1.
    The problem is the pulse width of the rotation.
    The pulse are at night ( low power) much wider that during the day when more power is consumed. At night the spikes in power usage where huge.

    Issue 2.
    Switching on/off the halogen light causes spikes and related to also strange power measurement.

    Solution:
    I added a second arduino mini pro as a pulse regulator between the TCRT5000 and the power meter arduino.
    this arduino triggers on Rising and will always give a 100ms puls. Als debounce is handled (spikes).
    I don't upload the sketch here, I'm a beginning programmer on arduino. It's made on " trail and error" but it functions as it supposed to do.
    If someone wants a copy feel free to contact mee



  • @gohan Did you solve this?
    I get very high watt usage as well. Sometimes like 40 000 - 130 000 watt if I check the serial monitor.
    Of course, those values will never be sent to the controler.


  • Mod

    Nope, I am looking at other things now. Very little time available for too many things. I'm looking at buying CT clamp and do a more direct measurement.



  • Okay. Really annoying. Check this.

    Received last pulse count from gw:2527
    Watt:121065
    22235 TSF:MSG:SEND,78-78-0-0,s=3,c=1,t=24,pt=5,l=4,sg=0,ft=0,st=OK:2539
    22246 TSF:MSG:SEND,78-78-0-0,s=2,c=1,t=18,pt=7,l=5,sg=0,ft=0,st=OK:2.5390
    Watt:121621
    42238 TSF:MSG:SEND,78-78-0-0,s=3,c=1,t=24,pt=5,l=4,sg=0,ft=0,st=OK:2551
    42250 TSF:MSG:SEND,78-78-0-0,s=2,c=1,t=18,pt=7,l=5,sg=0,ft=0,st=OK:2.5510
    Watt:121342
    62239 TSF:MSG:SEND,78-78-0-0,s=3,c=1,t=24,pt=5,l=4,sg=0,ft=0,st=OK:2563
    62251 TSF:MSG:SEND,78-78-0-0,s=2,c=1,t=18,pt=7,l=5,sg=0,ft=0,st=OK:2.5630
    Watt:121539
    82240 TSF:MSG:SEND,78-78-0-0,s=3,c=1,t=24,pt=5,l=4,sg=0,ft=0,st=OK:2575
    82251 TSF:MSG:SEND,78-78-0-0,s=2,c=1,t=18,pt=7,l=5,sg=0,ft=0,st=OK:2.5750
    Watt:121951
    102239 TSF:MSG:SEND,78-78-0-0,s=3,c=1,t=24,pt=5,l=4,sg=0,ft=0,st=OK:2589
    102250 TSF:MSG:SEND,78-78-0-0,s=2,c=1,t=18,pt=7,l=5,sg=0,ft=0,st=OK:2.5890
    Watt:122067
    122243 TSF:MSG:SEND,78-78-0-0,s=3,c=1,t=24,pt=5,l=4,sg=0,ft=0,st=OK:2601
    122254 TSF:MSG:SEND,78-78-0-0,s=2,c=1,t=18,pt=7,l=5,sg=0,ft=0,st=OK:2.6010
    

    The plus count seems OK, right?

    My energy meter is 1000 pluses /kwh

    Here is my sketch. I use Home assistant so i created three sensors for this as I don't think V_VAR1 is supported with S_POWER.
    Otherwise it is the standard example.

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik EKblad
     *
     * DESCRIPTION
     * This sketch provides an example how to implement a distance sensor using HC-SR04
     * Use this sensor to measure KWH and Watt of your house meeter
     * You need to set the correct pulsefactor of your meeter (blinks per KWH).
     * The sensor starts by fetching current KWH value from gateway.
     * Reports both KWH and Watt back to gateway.
     *
     * Unfortunately millis() won't increment when the Arduino is in
     * sleepmode. So we cannot make this sensor sleep if we also want
     * to calculate/report watt-number.
     * http://www.mysensors.org/build/pulse_power
     */
    
    // Enable debug prints
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    #define MY_NODE_ID 78
    #include <MySensors.h>
    
    #define DIGITAL_INPUT_SENSOR 3  // The digital input you attached your light sensor.  (Only 2 and 3 generates interrupt!)
    #define PULSE_FACTOR 1000       // Nummber of blinks per KWH of your meeter
    #define SLEEP_MODE false        // Watt-value can only be reported when sleep mode is false.
    #define MAX_WATT 20000          // Max watt value to report. This filetrs outliers.
    #define WATT_CHILD_ID 1              // Id of the sensor child
    #define KWH_CHILD_ID 2
    #define PC_CHILD_ID 3
    
    unsigned long SEND_FREQUENCY =
        20000; // Minimum time between send (in milliseconds). We don't wnat to spam the gateway.
    double ppwh = ((double)PULSE_FACTOR)/1000; // Pulses per watt hour
    bool pcReceived = false;
    volatile unsigned long pulseCount = 0;
    volatile unsigned long lastBlink = 0;
    volatile unsigned long watt = 0;
    unsigned long oldPulseCount = 0;
    unsigned long oldWatt = 0;
    double oldKwh;
    unsigned long lastSend;
    MyMessage wattMsg(WATT_CHILD_ID,V_WATT);
    MyMessage kwhMsg(KWH_CHILD_ID,V_KWH);
    MyMessage pcMsg(PC_CHILD_ID,V_VAR1);
    
    
    void setup()
    {
      // Fetch last known pulse count value from gw
      request(PC_CHILD_ID, V_VAR1);
    
      // Use the internal pullup to be able to hook up this sketch directly to an energy meter with S0 output
      // If no pullup is used, the reported usage will be too high because of the floating pin
      pinMode(DIGITAL_INPUT_SENSOR,INPUT_PULLUP);
    
      attachInterrupt(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), onPulse, RISING);
      lastSend=millis();
    }
    
    void presentation()
    {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Energy Meter", "1.0");
    
      // Register this device as power sensor
      present(WATT_CHILD_ID, S_POWER);
      present(KWH_CHILD_ID, S_POWER);
      present(PC_CHILD_ID, S_CUSTOM);
    }
    
    void loop()
    {
      unsigned long now = millis();
      // Only send values at a maximum frequency or woken up from sleep
      bool sendTime = now - lastSend > SEND_FREQUENCY;
      if (pcReceived && (SLEEP_MODE || sendTime)) {
        // New watt value has been calculated
        if (!SLEEP_MODE && watt != oldWatt) {
          // Check that we dont get unresonable large watt value.
          // could hapen when long wraps or false interrupt triggered
          if (watt<((unsigned long)MAX_WATT)) {
            send(wattMsg.set(watt));  // Send watt value to gw
          }
          Serial.print("Watt:");
          Serial.println(watt);
          oldWatt = watt;
        }
    
        // Pulse cout has changed
        if (pulseCount != oldPulseCount) {
          send(pcMsg.set(pulseCount));  // Send pulse count value to gw
          double kwh = ((double)pulseCount/((double)PULSE_FACTOR));
          oldPulseCount = pulseCount;
          if (kwh != oldKwh) {
            send(kwhMsg.set(kwh, 4));  // Send kwh value to gw
            oldKwh = kwh;
          }
        }
        lastSend = now;
      } else if (sendTime && !pcReceived) {
        // No count received. Try requesting it again
        request(PC_CHILD_ID, V_VAR1);
        lastSend=now;
      }
    
      if (SLEEP_MODE) {
        sleep(SEND_FREQUENCY);
      }
    }
    
    void receive(const MyMessage &message)
    {
      if (message.type==V_VAR1) {
        pulseCount = oldPulseCount = message.getLong();
        Serial.print("Received last pulse count from gw:");
        Serial.println(pulseCount);
        pcReceived = true;
      }
    }
    
    void onPulse()
    {
      if (!SLEEP_MODE) {
        unsigned long newBlink = micros();
        unsigned long interval = newBlink-lastBlink;
        if (interval<10000L) { // Sometimes we get interrupt on RISING
          return;
        }
        watt = (3600000000.0 /interval) / ppwh;
        lastBlink = newBlink;
      }
      pulseCount++;
    }
    
    

    Anyone got any ideas?
    BTW. First i tried the original example sketch, unmodified, with the same result.


  • Mod

    That's pretty much the same problem as I have



  • I think i solved this.
    I don't know if it is a good solution.
    Maybe it is possible to solve in the code?
    I added a 0,1uf ceramic capacitor between DO and GND on the LM393.
    Then i tuned the LM393 until i had it blink as expected.

    I think, in my case the LED pulse from my meeter was to short, it gives a very short blink.
    Anyway.
    I tested it all day and it seems fine now.


  • Mod

    Are you getting instant power measurements correct?



  • Yes. I guess.
    I don't have anything to compare with but i think it is accurate.
    I have a 1000blink/kwh
    Example:
    Node send state every 20 sec.
    pulsecount: 8 pulses every 20 sec.
    Instant power in W is about 1400W

    8 pulses x 3 = pulses /minute =24
    24 pulses x 60 min = pulses /hour = 1440.

    Edit.
    Now I get a new value every 20 sec, before I didn't get any value (W) from the node at all because it was too high. I only received pulse count and kWh earlier.


  • Mod

    Besides the cap, what exactly did you do?



  • Nothing more.
    I use the same arduino, haven't uploaded anything new to it since i tried it last time.

    What I had to do was lower the sensitivity on the LM393 because at first the signal-LED glowed a bit but when i adjusted it to lower sensitivity and closed the door to get it totaly dark the led blinked exactly as the meeter.

    I have it on a bredboard and connected the cap between GND and to pin 3 so it is connected close to the arduino but i guess it shouldn't matter.

    Have you tried it?


  • Mod

    I can't believe it, IT WORKS!!!!! I added the cap directly under the pins of the LM939 board. Thanks man!



  • I am glad to hear it works for you too.


  • Mod

    There is probably a sw way to correct this problem, I'll think about it



  • It waswhat I thought too.
    As I said earlier, when I tested this my conclusion was, short blink, almost a flash will counted as a pulse (kWh) but it does something wrong with the calculation of instant power usage.
    A slower blink will correct this.
    My meeter gives a very short blink.


  • Mod

    Yes, could be that a simple debounce could do the trick but I'll have to look deeper into it. @Yveaux what do you think?


  • Mod

    @xydix Could you try to increase the 10000L to 100000L interval in the OnPulse function and see if it works without the capacitor?


  • Contest Winner

    I tried to build something similar and it is working nicely, thanks for sharing the idea! However, I've noticed the light sensor (alone) consumes 1mA constantly which is quite a lot when used for a battery powered project. Even if I report to the controller once per hour (summing up the power consumption along the way), still the light sensor needs to be always on, meaning I'd probably need to replace batteries after a month or so. Is there any workaround or alternative sensor which I can use or am I doing something completely wrong? Thanks



  • @user2684 I had the same issue when I built this with a standalone atmega328p running on 2x AA's. I set it up with wake on interrupt from the It was constantly drawing 1.6mA to power the tsl257 to detect the flashes. My target was 1+ years on 2 x AA's. My solution to achieve this was to sleep for 125ms, wake up, send power to tsl257 to check light state (on/off), if it changed from previous reading then there was a pulse (well half pulse). By measuring every 125ms I can guarantee to capture fast pulses up to 14.4kW. (3600000/125)/2. It is 20kW max draw for residential in my country. Now:
    Average mA Consumption Sleep 0.1
    Average mA Consumption Wake 0.064516129
    Average mA Consumption Transmit 0.002916667
    Battery Life (2xAAs) = 1.7 years


  • Contest Winner

    @cstewy many thanks and very interesting. You basically saying it consumes less by waking up every 125ms rather than being asleep but with the sensor eating up current continuously. Definitely worth trying thanks! Meanwhile I've reduced the number of radio transmission and the batteries lasted for a couple of months but still is not ideal. Thanks! I'll try and report back



  • Hello, i'm stuck with this sensor, because no Interrupt is really working. I've cut down the code to this:

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik Ekblad
     *
     * DESCRIPTION
     * This sketch provides an example how to implement a LM393 PCB
     * Use this sensor to measure kWh and Watt of your house meter
     * You need to set the correct pulsefactor of your meter (blinks per kWh).
     * The sensor starts by fetching current kWh value from gateway.
     * Reports both kWh and Watt back to gateway.
     *
     * Unfortunately millis() won't increment when the Arduino is in
     * sleepmode. So we cannot make this sensor sleep if we also want
     * to calculate/report watt value.
     * http://www.mysensors.org/build/pulse_power
     */
    
    // Enable debug prints
    #define MY_DEBUG
    
    // Enable and select radio type attached
    //#define MY_RADIO_NRF24
    //#define MY_RADIO_NRF5_ESB
    //#define MY_RADIO_RFM69
    //#define MY_RADIO_RFM95
    
    //#include <MySensors.h>
    
    #define DIGITAL_INPUT_SENSOR 2  // The digital input you attached your light sensor.  (Only 2 and 3 generates interrupt!)
    #define PULSE_FACTOR 500       // Number of blinks per of your meter
    #define SLEEP_MODE false        // Watt value can only be reported when sleep mode is false.
    #define MAX_WATT 1000000          // Max watt value to report. This filters outliers.
    #define CHILD_ID 10             // Id of the sensor child
    
    uint32_t SEND_FREQUENCY =
        15000; // Minimum time between send (in milliseconds). We don't want to spam the gateway.
    double ppwh = ((double)PULSE_FACTOR)/1000; // Pulses per watt hour
    bool pcReceived = true;
    volatile uint32_t pulseCount = 0;
    volatile uint32_t lastBlink = 0;
    volatile uint32_t watt = 0;
    uint32_t oldPulseCount = 0;
    uint32_t oldWatt = 0;
    double oldkWh;
    uint32_t lastSend;
    //MyMessage wattMsg(CHILD_ID,V_WATT);
    //MyMessage kWhMsg(CHILD_ID,V_KWH);
    //MyMessage pcMsg(CHILD_ID,V_VAR1);
    
    
    void setup()
    {
      Serial.begin(9600);
      Serial.println("Start setup");
      // Fetch last known pulse count value from gw
      //request(CHILD_ID, V_VAR1);
    
      // Use the internal pullup to be able to hook up this sketch directly to an energy meter with S0 output
      // If no pullup is used, the reported usage will be too high because of the floating pin
      pinMode(DIGITAL_INPUT_SENSOR,INPUT_PULLUP);
    
      attachInterrupt(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), onPulse, RISING);
      lastSend=millis();
      Serial.println("Start setup: last sent: ");
      pinMode(LED_BUILTIN, OUTPUT);
      digitalWrite(LED_BUILTIN, LOW);
    }
    
    void presentation()
    {
      // Send the sketch version information to the gateway and Controller
      //sendSketchInfo("Energy Meter", "1.3");
    
      // Register this device as power sensor
      //present(CHILD_ID, S_POWER);
    }
    
    void loop()
    {
      
    }
    
    
    
    void onPulse()
    {
      Serial.println("onPulse:");
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(1000);                       // wait for a second
      digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
      delay(1000);      
      pulseCount++;
      Serial.println("pulseCount:");
    }
    

    The Problem ist, that only on Start the interrupt ist working once, but no LED blinks.

    Can someone help?

    Thanks.


  • Mod

    @artipi using Serial.print and delay from within an interrupt service routine will mess things up. Interrupt service routines must be executed quickly.

    From https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/:

    Since delay() requires interrupts to work, it will not work if called inside an ISR.

    You need to do the blinking inside loop().



  • Ah, this explains many things. Thank you, now its working:)!


  • Plugin Developer

    @artipi can you share your code?



  • @alowhum: Its the original code. Nothing changed. I've only made some tests with my version...



  • Hey guys

    So i am trying to use this sketch with the sleep mode set to true 'cause i need to run it on battery.
    It works fine running with usb power on it and sleep mode on false.
    But with sleep mode true i am getting nothing in myscontroller after the initial startup.
    All i changed is the sleep mode from false to true.
    Running it on a clone nano with some chinese "flame detector" sensor.
    Been trying to figure out if it has something to do with how it reports when in sleep mode but it should still report whatever the send frequency is set to right?
    I was using a tv remote to fake impulses, with seems to work on usb power, but not on battery.
    Any suggestions?

    Best regards
    Patrik


  • Mod

    @patrikr76 just to make sure we are on the same page, could you clarify which sketch you are using? This thread has discussed lots of sketches and it would be a pity if we’re looking at different code 🙂

    In any case, the debug log from the node will give the best information on what is happening.

    It could also be useful to only change one thing. Right now you change sleep mode and switch to battery, right? How does the node behave with sleep mode on, but still running on usb power? How does the node behave with sleep mode off, but running on battery?



  • @mfalkvidd , you are absolutely right. Let me clarify.
    I am using the example code that is under build and power meter pulse sensor, updated may 1st, 2018.
    Running it with "sleep mode = false" it works fine with both usb and battery power, just drains the battery quite fast.
    Setting it to "sleep mode = true" it doesn't work either on usb or battery power.

    Here's how the serial monitor looks for the startup phase with sleep mode as true.

    23:21:24.219 ->  __  __       ____
    23:21:24.219 -> |  \/  |_   _/ ___|  ___ _ __  ___  ___  _ __ ___
    23:21:24.219 -> | |\/| | | | \___ \ / _ \ `_ \/ __|/ _ \| `__/ __|
    23:21:24.219 -> | |  | | |_| |___| |  __/ | | \__ \  _  | |  \__ \
    23:21:24.219 -> |_|  |_|\__, |____/ \___|_| |_|___/\___/|_|  |___/
    23:21:24.219 ->         |___/                      2.3.0
    23:21:24.219 -> 
    23:21:24.219 -> 16 MCO:BGN:INIT NODE,CP=RNNNA---,VER=2.3.0
    23:21:24.254 -> 25 TSM:INIT
    23:21:24.254 -> 26 TSF:WUR:MS=0
    23:21:24.254 -> 33 TSM:INIT:TSP OK
    23:21:24.254 -> 35 TSF:SID:OK,ID=2
    23:21:24.254 -> 37 TSM:FPAR
    73 TSF:MSG:SEND,2-2-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    201 TSF:MSG:READ,0-0-2,s=255,c=3,t=8,pt=1,l=1,sg=0:0
    23:21:24.428 -> 205 TSF:MSG:FPAR OK,ID=0,D=1
    2080 TSM:FPAR:OK
    23:21:26.306 -> 2081 TSM:ID
    23:21:26.306 -> 2082 TSM:ID:OK
    23:21:26.306 -> 2084 TSM:UPL
    23:21:26.306 -> 2087 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=OK:1
    23:21:26.306 -> 2095 TSF:MSG:READ,0-0-2,s=255,c=3,t=25,pt=1,l=1,sg=0:1
    23:21:26.306 -> 2100 TSF:MSG:PONG RECV,HP=1
    23:21:26.306 -> 2103 TSM:UPL:OK
    23:21:26.306 -> 2104 TSM:READY:ID=2,PAR=0,DIS=1
    23:21:26.306 -> 2109 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=OK:0100
    23:21:26.341 -> 2118 TSF:MSG:READ,0-0-2,s=255,c=3,t=15,pt=6,l=2,sg=0:0100
    23:21:26.341 -> 2125 TSF:MSG:SEND,2-2-0-0,s=255,c=0,t=17,pt=0,l=5,sg=0,ft=0,st=OK:2.3.0
    23:21:26.341 -> 2134 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=OK:0
    2203 TSF:MSG:READ,0-0-2,s=255,c=3,t=6,pt=0,l=1,sg=0:M
    23:21:26.410 -> 2210 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=11,pt=0,l=12,sg=0,ft=0,st=OK:Energy Meter
    23:21:26.445 -> 2220 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=12,pt=0,l=3,sg=0,ft=0,st=OK:1.0
    23:21:26.445 -> 2228 TSF:MSG:SEND,2-2-0-0,s=1,c=0,t=13,pt=0,l=0,sg=0,ft=0,st=OK:
    23:21:26.445 -> 2234 MCO:REG:REQ
    23:21:26.445 -> 2237 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=OK:2
    2252 TSF:MSG:READ,0-0-2,s=255,c=3,t=27,pt=1,l=1,sg=0:1
    23:21:26.480 -> 2257 MCO:PIM:NODE REG=1
    23:21:26.480 -> 2259 MCO:BGN:STP
    23:21:26.480 -> 2263 TSF:MSG:SEND,2-2-0-0,s=1,c=2,t=24,pt=0,l=0,sg=0,ft=0,st=OK:
    23:21:26.480 -> 2269 MCO:BGN:INIT OK,TSP=1
    23:21:26.480 -> 2272 MCO:SLP:MS=20000,SMS=0,I1=255,M1=255,I2=255,M2=255
    23:21:26.480 -> 2277 TSF:TDI:TSL
    

    As far as i can see it seems fine and also shows up in myscontroller.

    And here is an example how it looks after 20 seconds sleep.

    2414 MCO:SLP:WUP=-1
    23:26:36.346 -> 2416 TSF:TRI:TSB
    23:26:36.346 -> 2418 MCO:SLP:MS=20000,SMS=0,I1=255,M1=255,I2=255,M2=255
    23:26:36.346 -> 2423 TSF:TDI:TSL
    

    I don't see any connection attempts with my gateway, or shouldn't there by any if the sensor is not triggered?

    Here i can show how it looks when tricking the sensor with the ir on the mouse.

    2456 MCO:SLP:WUP=-1
    23:27:48.785 -> 2458 TSF:TRI:TSB
    23:27:48.785 -> 2460 MCO:SLP:MS=20000,SMS=0,I1=255,M1=255,I2=255,M2=255
    23:27:48.785 -> 2465 TSF:TDI:TSL
    2467 MCO:SLP:WUP=-1
    23:27:48.924 -> 2468 TSF:TRI:TSB
    23:27:48.959 -> 2470 MCO:SLP:MS=20000,SMS=0,I1=255,M1=255,I2=255,M2=255
    23:27:48.959 -> 2476 TSF:TDI:TSL
    2478 MCO:SLP:WUP=-1
    23:27:49.098 -> 2480 TSF:TRI:TSB
    23:27:49.098 -> 2481 MCO:SLP:MS=20000,SMS=0,I1=255,M1=255,I2=255,M2=255
    23:27:49.098 -> 2486 TSF:TDI:TSL
    2488 MCO:SLP:WUP=-1
    23:27:49.235 -> 2490 TSF:TRI:TSB
    23:27:49.235 -> 2492 MCO:SLP:MS=20000,SMS=0,I1=255,M1=255,I2=255,M2=255
    23:27:49.270 -> 2497 TSF:TDI:TSL
    2498 MCO:SLP:WUP=-1
    23:27:49.408 -> 2500 TSF:TRI:TSB
    23:27:49.408 -> 2502 MCO:SLP:MS=20000,SMS=0,I1=255,M1=255,I2=255,M2=255
    23:27:49.408 -> 2507 TSF:TDI:TSL
    2509 MCO:SLP:WUP=-1
    23:27:49.546 -> 2510 TSF:TRI:TSB
    23:27:49.546 -> 2512 MCO:SLP:MS=20000,SMS=0,I1=255,M1=255,I2=255,M2=255
    23:27:49.546 -> 2518 TSF:TDI:TSL
    

    Interrupt is triggered and it wakes up, but nothing is sent to the gateway.

    Have i missed something?


  • Mod

    @patrikr76 great info, thanks!

    The -1 in Wup=-1 indicates that the node was waken up by timer and not by interrupt. That’s very strange, since the time stamps clearly show that 20 seconds has not passed.

    Could you add

    Serial.println(pulseCount);
    

    after

    sleep(SEND_FREQUENCY);
    

    to see if pulseCount is increased?



  • @mfalkvidd , Here's the serial monitor after adding the serial print.

    2269 MCO:SLP:WUP=-1
    14:15:15.125 -> 2270 TSF:TRI:TSB
    14:15:15.125 -> 0
    14:15:15.125 -> 2272 MCO:SLP:MS=20000,SMS=0,I1=255,M1=255,I2=255,M2=255
    14:15:15.125 -> 2277 TSF:TDI:TSL
    2279 MCO:SLP:WUP=-1
    14:15:37.264 -> 2281 TSF:TRI:TSB
    14:15:37.264 -> 0
    14:15:37.264 -> 2282 MCO:SLP:MS=20000,SMS=0,I1=255,M1=255,I2=255,M2=255
    14:15:37.264 -> 2287 TSF:TDI:TSL
    2289 MCO:SLP:WUP=-1
    14:15:59.354 -> 2291 TSF:TRI:TSB
    14:15:59.354 -> 0
    14:15:59.354 -> 2293 MCO:SLP:MS=20000,SMS=0,I1=255,M1=255,I2=255,M2=255
    14:15:59.389 -> 2298 TSF:TDI:TSL
    

    As you see, it sleeps about 22 secs with no pulse on it.

    Moving the mouse over it gives this.

    2353 MCO:SLP:WUP=-1
    14:18:09.982 -> 2355 TSF:TRI:TSB
    14:18:09.982 -> 4
    14:18:09.982 -> 2357 MCO:SLP:MS=20000,SMS=0,I1=255,M1=255,I2=255,M2=255
    14:18:09.982 -> 2362 TSF:TDI:TSL
    2363 MCO:SLP:WUP=-1
    14:18:23.059 -> 2365 TSF:TRI:TSB
    14:18:23.059 -> 5
    14:18:23.059 -> 2367 MCO:SLP:MS=20000,SMS=0,I1=255,M1=255,I2=255,M2=255
    14:18:23.059 -> 2372 TSF:TDI:TSL
    

    Looking through the code, this one raises a flag.

    if (pcReceived && (SLEEP_MODE || sendTime))
    

    The pulseCount send is within that if statement which is not fullfilled because it doesn't receive pcReceived.
    Or am i wrong?



  • Found a solution, which is not ideal.
    I was having similar problem with a motion sensor not being able to send, adding a delay after sleep helped on that one, but it was just a 5ms delay needed.

    On this one i had to add a 1 sec delay in the begining of the loop. I tried smaller but then it didn't work.
    I am starting to think it is the nano clone that has crappy components.


  • Mod



  • I have two meters I would like to measure: One that the LED is off most of the time and briefly pulses on per increment (at 3200 pulses per Kwh - so I adjusted the pulse factor accordingly). This works fine. The second meter, the LED pulses off (meaning the led it lit most of the time, and pulses off briefly). Do I need to adjust the code in any way to deal with this meter? Thanks. Really impressed it works virtually out of the box.


  • Mod

    Great work @stkilda
    I think the sketch will work without modification. The easiest way to find out is probably to try it.


  • Mod

    you may need to change the interrupt behavior, but try it first


  • Hero Member

    Is there a suggested replacement for the TSL250? While it can still be found, it seems to be discontinued for the most part.

    I've had no luck with the LM393 solution for my new power meter. Just can't tune it to pick up the led impulses. What sort of diode solution is everyone else using? I'm aware of the possibility of IR communication (https://wiki.hal9k.dk/projects/kamstrup) but it seems a little over the top.


  • Mod



  • Hello
    my skectch works very well, only I have a double pricing, I manage to recover the information. but I have to modify the code in order to have my exact consumption.
    but my code sends me information always on the same IDs.
    thank you


  • Mod

    Hi @fullmetal, welcome to the forum 🙂

    Could you explain what double pricing means?



  • Thank you for your welcome @mfalkvidd

    Our electricity provider offers several rates.

    HP: Full time
    HC: off-peak time

    usually the HP/HC rate is programmed at night by the provider.

    As the consumption is saved by the arduino, I need two different IDs to trace the information to domotics



  • Here is the modified code:

    #define MY_PARENT_NODE_ID 0                   // define if fixed parent
    #define MY_PARENT_NODE_IS_STATIC
    #undef MY_REGISTRATION_FEATURE                  // sketch moves on if no registration
    #define MY_NODE_ID 2                     // fixed node number
    
    // Enable debug prints
    #define MY_DEBUG
    
    // Enable RS485 transport layer
    #define MY_RS485
    
    // Define this to enables DE-pin management on defined pin
    #define MY_RS485_DE_PIN 2
    
    // Set RS485 baud rate to use
    #define MY_RS485_BAUD_RATE 9600
    
    // Set blinking period
    #define MY_DEFAULT_LED_BLINK_PERIOD 300
    
    // Flash leds on rx/tx/err
    #define MY_DEFAULT_ERR_LED_PIN 13  // Error led pin
    #define MY_DEFAULT_RX_LED_PIN  7  // Receive led pin
    #define MY_DEFAULT_TX_LED_PIN  6  // the PCB, on board LED
    
    // Enable and select radio type attached
    //#define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #include <MySensors.h>
    #include <SPI.h>
    #include <Bounce2.h>
    
    #define DIGITAL_INPUT_SENSOR 3  // The digital input you attached your light sensor.  (Only 2 and 3 generates interrupt!)
    #define DIGITAL_INPUT_HCHP 5
    #define PULSE_FACTOR 250       // Nummber of blinks per KWH of your meeter
    #define SLEEP_MODE false        // Watt-value can only be reported when sleep mode is false.
    #define MAX_WATT 10000          // Max watt value to report. This filetrs outliers.
    //#define CHILD_ID 2              // Id of the sensor child
    
    unsigned long SEND_FREQUENCY =
       20000; // Minimum time between send (in milliseconds). We don't wnat to spam the gateway.
    double ppwh = ((double)PULSE_FACTOR)/1000; // Pulses per watt hour
    bool pcReceived = false;
    volatile unsigned long pulseHpCount = 0;
    volatile unsigned long pulseHcCount = 0;
    volatile unsigned long lastBlink = 0;
    unsigned long oldPulseHpCount = 0;
    volatile unsigned long watt = 0;
    unsigned long oldWatt = 0;
    double oldKwhHp;
    unsigned long oldPulseHcCount = 0;
    double oldKwhHc;
    unsigned long lastSend;
    
    Bounce debouncer = Bounce(); 
    int oldValue=-1;
    
    
    MyMessage wattMsg(1,V_WATT);
    MyMessage kwhHpMsg(3,V_KWH);
    MyMessage pcHpMsg(2,V_VAR1);
    MyMessage kwhHcMsg(5,V_KWH);
    MyMessage pcHcMsg(6,V_VAR1);
    MyMessage hchpMsg(7,V_VAR2);
    const int ledPin =  13;      // sortie digitale
    
    void setup()
    {
       // Fetch last known pulse count value from gw
       request(2, V_VAR1);
       request(6, V_VAR1);
    
       // Use the internal pullup to be able to hook up this sketch directly to an energy meter with S0 output
       // If no pullup is used, the reported usage will be too high because of the floating pin
       pinMode(DIGITAL_INPUT_SENSOR,INPUT_PULLUP);
       pinMode(DIGITAL_INPUT_HCHP,INPUT_PULLUP);
       pinMode(ledPin, OUTPUT);
    
       attachInterrupt(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), onPulse, RISING);
       lastSend=millis();
    
        // After setting up the button, setup debouncer
       debouncer.attach(DIGITAL_INPUT_HCHP);
       debouncer.interval(5);
      
    }
    
    void presentation()
    {
       // Send the sketch version information to the gateway and Controller
       sendSketchInfo("Energy Meter", "2.0");
    
       // Register this device as power sensor
       present(1, S_POWER);
       
        // Register this device as power sensor
       present(2, S_POWER);
       
       // Register this device as power sensor
       present(3, S_POWER);
    
       // Register this device as power sensor
       present(5, S_POWER);
    
       // Register this device as power sensor
       present(6, S_POWER);
    
       // Register this device as power sensor
       present(7, S_POWER);
    }
    
    void loop()
    {
       unsigned long now = millis();
       if (digitalRead(DIGITAL_INPUT_SENSOR) == LOW ) {
       // LED Power On
       digitalWrite(ledPin, HIGH);
       // LED Power Off
       digitalWrite(ledPin, LOW);
       }
    
    
    {
     debouncer.update();
     // Get the update value
     int value = debouncer.read();
     Serial.println("");
    
     if (value != oldValue) {
        // Send in the new value
        send(hchpMsg.set(value==HIGH ? 1 : 0));
        oldValue = value;
     }
    } 
    
    
       // Only send values at a maximum frequency or woken up from sleep
       bool sendTime = now - lastSend > SEND_FREQUENCY;
       if (pcReceived && (SLEEP_MODE || sendTime)) {
           // New watt value has been calculated
           if (!SLEEP_MODE && watt != oldWatt) {
               // Check that we dont get unresonable large watt value.
               // could hapen when long wraps or false interrupt triggered
               if (watt<((unsigned long)MAX_WATT)) {
                   send(wattMsg.set(watt));  // Send watt value to gw
               }
               Serial.print("Watt:");
               Serial.println(watt);
               oldWatt = watt;
           }
           
               
       /////HP
             if (digitalRead(DIGITAL_INPUT_SENSOR) == HIGH ) {
           // Pulse cout has changed
              if (pulseHpCount != oldPulseHpCount) {
               send(pcHpMsg.set(pulseHpCount));  // Send pulse count value to gw
               double kwhHp = ((double)pulseHpCount/((double)PULSE_FACTOR));
               oldPulseHpCount = pulseHpCount;
               digitalWrite(ledPin, LOW);
               if (kwhHp != oldKwhHp) {
                   send(kwhHpMsg.set(kwhHp, 4));  // Send kwh value to gw
                   oldKwhHp = kwhHp;
               }
               digitalWrite(ledPin, HIGH);
           }
           lastSend = now;
           
          }else if (digitalRead(DIGITAL_INPUT_SENSOR) == LOW ){
          if (pulseHcCount != oldPulseHcCount) {
               send(pcHcMsg.set(pulseHcCount));  // Send pulse count value to gw
               double kwhHc = ((double)pulseHcCount/((double)PULSE_FACTOR));
               oldPulseHcCount = pulseHcCount;
               digitalWrite(ledPin, LOW);
               if (kwhHc != oldKwhHc) {
                   send(kwhHcMsg.set(kwhHc, 4));  // Send kwh value to gw
                   oldKwhHc = kwhHc;
               }
               digitalWrite(ledPin, HIGH);
           }
           lastSend = now;
          }
          
       }else if (sendTime && !pcReceived) {
           // No count received. Try requesting it again
           request(2, V_VAR1);
           request(6, V_VAR1);
           lastSend=now;
       }   
    
       if (SLEEP_MODE) {
           sleep(SEND_FREQUENCY);
       }
    }
    
    void receive(const MyMessage &message)
    {
      /////HP
       if (digitalRead(DIGITAL_INPUT_SENSOR) == HIGH ) {
       if (message.type==V_VAR1) {
           pulseHpCount = oldPulseHpCount = message.getLong();
           Serial.print("Received last pulse count from gw:");
           Serial.println(pulseHpCount);
           pcReceived = true;
       }
       }
       /////HC
       if (digitalRead(DIGITAL_INPUT_SENSOR) == LOW ) {
        if (message.type==V_VAR1) {
           pulseHcCount = oldPulseHcCount = message.getLong();
           Serial.print("Received last pulse count from gw:");
           Serial.println(pulseHcCount);
           pcReceived = true;
       }
       }
    }
    
    void onPulse()
    {
       /////HP
       if (digitalRead(DIGITAL_INPUT_SENSOR) == HIGH ) {
       if (!SLEEP_MODE) {
           unsigned long newBlink = micros();
           unsigned long interval = newBlink-lastBlink;
           if (interval<10000L) { // Sometimes we get interrupt on RISING
               return;
           }
           watt = (3600000000.0 /interval) / ppwh;
           lastBlink = newBlink;
       }
       pulseHpCount++;
       }
    
       /////HC
       else if (digitalRead(DIGITAL_INPUT_SENSOR) == LOW ) {
         if (!SLEEP_MODE) {
           unsigned long newBlink = micros();
           unsigned long interval = newBlink-lastBlink;
           if (interval<10000L) { // Sometimes we get interrupt on RISING
               return;
           }
           watt = (3600000000.0 /interval) / ppwh;
           lastBlink = newBlink;
       }
       pulseHcCount++;
       }
    }
    

  • Mod

    @fullmetal cool. Thanks for explaining, and for sharing your sketch.



  • In fact, my code does not work as I would like, HC information systematically go back to the HP ID.

    Can you help me



  • Update: Running for over 12 months on 2 regular AA batteries using the method I described above. Basically rather than powering the tsl257 constantly, I wake up every 125ms then send power to the tsl257, check if there has been a state change in the power meter light then go back to sleep.



  • Hi

    My heat exchanger does only give one pulse / kWh which results in that there can be hours between the pulses. When doing the watt calculation micros are used and that counter overflow after 70minutes. See: https://www.arduino.cc/reference/en/language/functions/time/micros/

    I have modified the sketch to use millis instead of micros when the time between the pulses are above one hour and micros if it is less than an hour.

    I hope I did it correct 😉

    Best regards
    Peter Andersson

    /*
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2019 Sensnology AB
     * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik Ekblad
     * Version 1.1 - Peter Andersson added millis watt calculation if time between pulses > 1h 
     *
     * DESCRIPTION
     * This sketch provides an example how to implement a LM393 PCB
     * Use this sensor to measure kWh and Watt of your house meter
     * You need to set the correct pulsefactor of your meter (blinks per kWh).
     * The sensor starts by fetching current kWh value from gateway.
     * Reports both kWh and Watt back to gateway.
     *
     * Unfortunately millis() won't increment when the Arduino is in
     * sleepmode. So we cannot make this sensor sleep if we also want
     * to calculate/report watt value.
     * http://www.mysensors.org/build/pulse_power
     */
    #define MY_REPEATER_FEATURE    //Added
    
    // Enable debug prints
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_RF24
    //#define MY_RADIO_NRF5_ESB
    //#define MY_RADIO_RFM69
    //#define MY_RADIO_RFM95
    
    #include <MySensors.h>
    
    #define DIGITAL_INPUT_SENSOR 3  // The digital input you attached your light sensor.  (Only 2 and 3 generates interrupt!)
    #define PULSE_FACTOR 1       // Number of blinks per of your meter. Normally 1000
    #define SLEEP_MODE false        // Watt value can only be reported when sleep mode is false.
    #define MAX_WATT 100000          // Max watt value to report. This filters outliers. Normally 10000
    #define CHILD_ID 1              // Id of the sensor child
    
    uint32_t SEND_FREQUENCY =
        20000; // Minimum time between send (in milliseconds). We don't want to spam the gateway.
    double ppwh = ((double)PULSE_FACTOR)/1000; // Pulses per watt hour
    bool pcReceived = false;
    volatile uint32_t pulseCount = 0;
    volatile uint32_t lastBlinkmicros = 0;
    volatile uint32_t lastBlinkmillis = 0;
    volatile uint32_t watt = 0;
    uint32_t oldPulseCount = 0;
    uint32_t oldWatt = 0;
    double oldkWh;
    uint32_t lastSend;
    MyMessage wattMsg(CHILD_ID,V_WATT);
    MyMessage kWhMsg(CHILD_ID,V_KWH);
    MyMessage pcMsg(CHILD_ID,V_VAR1);
    
    
    void setup()
    {
        // Fetch last known pulse count value from gw
        request(CHILD_ID, V_VAR1);
    
        // Use the internal pullup to be able to hook up this sketch directly to an energy meter with S0 output
        // If no pullup is used, the reported usage will be too high because of the floating pin
        pinMode(DIGITAL_INPUT_SENSOR,INPUT_PULLUP);
    
        attachInterrupt(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), onPulse, RISING);
        lastSend=millis();
    }
    
    void presentation()
    {
        // Send the sketch version information to the gateway and Controller
        sendSketchInfo("Energy Meter", "1.0");
    
        // Register this device as power sensor
        present(CHILD_ID, S_POWER);
    }
    
    void loop()
    {
        uint32_t now = millis();
        // Only send values at a maximum frequency or woken up from sleep
        bool sendTime = now - lastSend > SEND_FREQUENCY;
        if (pcReceived && (SLEEP_MODE || sendTime)) {
            // New watt value has been calculated
            if (!SLEEP_MODE && watt != oldWatt) {
                // Check that we don't get unreasonable large watt value.
                // could happen when long wraps or false interrupt triggered
                if (watt<((uint32_t)MAX_WATT)) {
                    send(wattMsg.set(watt));  // Send watt value to gw
                }
                Serial.print("Watt:");
                Serial.println(watt);
                oldWatt = watt;
            }
    
            // Pulse count value has changed
            if (pulseCount != oldPulseCount) {
                send(pcMsg.set(pulseCount));  // Send pulse count value to gw
                double kWh = ((double)pulseCount/((double)PULSE_FACTOR));
                oldPulseCount = pulseCount;
                if (kWh != oldkWh) {
                    send(kWhMsg.set(kWh, 4));  // Send kWh value to gw
                    oldkWh = kWh;
                }
            }
            lastSend = now;
        } else if (sendTime && !pcReceived) {
            // No pulse count value received. Try requesting it again
            request(CHILD_ID, V_VAR1);
            lastSend=now;
        }
    
        if (SLEEP_MODE) {
            sleep(SEND_FREQUENCY);
        }
    }
    
    void receive(const MyMessage &message)
    {
        if (message.type==V_VAR1) {
            pulseCount = oldPulseCount = message.getLong();
            Serial.print("Received last pulse count value from gw:");
            Serial.println(pulseCount);
            pcReceived = true;
        }
    }
    
    void onPulse()
    {
        if (!SLEEP_MODE) {
            uint32_t newBlinkmicros = micros();
            uint32_t newBlinkmillis = millis();
            uint32_t intervalmicros = newBlinkmicros-lastBlinkmicros;
            uint32_t intervalmillis = newBlinkmillis-lastBlinkmillis;
    
            if (intervalmicros<10000L && intervalmillis<10L) { // Sometimes we get interrupt on RISING
                return;
            }
            if (intervalmillis<360000) { //Less then an hour since last pulse, use microseconds
               watt = (3600000000.0 /intervalmicros) / ppwh;
            } else {
               watt = (3600000.0 /intervalmillis) / ppwh; //more then an hour since last pulse, use milliseconds as micros will overflow after 70min 
            }
            
            lastBlinkmicros = newBlinkmicros;
            lastBlinkmillis = newBlinkmillis;
        }
        pulseCount++;
    }
    

  • Mod

    Thanks @it-slav

    I have created a pull request based on your sketch.

    https://github.com/mysensors/MySensors/pull/1306

    There were some merge problems, but I think I got them all ironed out.



  • Hello everyone.
    Is it possible to evaluate two electricity meter signals with one sensor?
    Thank you!


  • Mod

    @butaluk yes, as long as they are not too far apart to run the cables



  • @mfalkvidd Thank you. no, the counters are in one place. Do you have an example code for me?


  • Hardware Contributor

    Hi! I have just revised my pulse sensor and have a few suggestions to the code. I have mentioned this before but there can be issues when requesting V_VAR1.

    In current code we request V_VAR1 one time and then goes to sleep, and if you like me have a 10 minute sleep period it can take along time to get V_VAR1 if you have a radio on the outside of your GW range.

    I would suggest adding:

    } else if (now - lastSend > 5000 && !pcReceived) {
        // No pulse count value received from controller. Try requesting it again.
        request(CHILD_ID, V_VAR1);
        recrequestcount = recrequestcount + 1;
    #ifdef MY_DEBUG
        Serial.println("Req p-cnt");
    #endif
        lastSend = now;
        return;
      }
    

    and before sleep:

    else if (!pcReceived) {
        //We dont want to sleep if !pcRecieved. Return and wait 5 sec to request a new value.
        return;
      }
    

    In HomeAssisant you also have the specific problem that if V_VAR1 isnt stored, the controller will not answer - so you can keep asking for a pulsecount forever without an reply. To avoid this the first time I have added:

    else if (recrequestcount == 5 && !pcReceived) {
        //For some controllers, if you dont have any V_VAR1 stored node will not get an answer.
        //Try 5 times, then set V_VAR1 to 0 and update controller
    #ifdef MY_DEBUG
        Serial.println("Set p-cnt and update controller");
    #endif
        pcReceived = true;
        recrequestcount = 0;
        send(pcMsg.set(pulseCount));  // Send pulse count 0 value to gw
        double kWh = ((double)pulseCount / ((double)PULSE_FACTOR));
        send(kWhMsg.set(kWh, 4));  // Send kWh value 0 to gw
        lastSend = now;
      }
    

    This setup works great for me, but I dont have the bigger picture - might break something else.
    I can add a pull request if you think this should fit.

    Here is my full code (but It includes battery measurments)
    https://github.com/sundberg84/HomeAutomation/blob/master/Ver 2.X/2.3/EnergyMeterPulseSensor_Feb2020/EnergyMeterPulseSensor_Feb2020.ino

    Also there is a strange error with SEND_FREQUENCY / timer when you have this interrupt. Works great if you dont have any interrupts but as soon as you get pulses the timer gets about 4 times as fast as it should be - I have added a correction to this, but I dont know whats causing this.

    uint32_t SEND_FREQUENCY = 4*60000*15; // Minimum time between send (in milliseconds). We don't want to spam the gateway. (Corr * 60sek * minutes)
    


  • Hi
    I have done search of this thread and no one seems to have mentioned latency.
    I have a test setup using a LM393 running on a particle photon with a sketch that also runs a flashing led. I have tested this with various led flash lengths and intervals, the highest rate being a 10ms flash with 10ms interval . I have a consistent precise reading of pulse interval and number but have identified that there is a 1ms delay between the flash and the start of the interrupt function. This latency is consistent, whatever the interval between and I can only assume it is a result of latency caused by LM393 and interrupt, though I find it strange that the latency is exactly 1millisecond even when measured in micros. Under low meter loads this discrepancy would most likely go unnoticed but with high loads of 72kWs it results in a significant error when calculating the instant power.
    Has anyone else noticed this or can give another reason for this that I am missing?
    A subtraction of the identified latency from the pulse interval reading, calibrates the setup and produces exact readings



  • Hello, this sensor can be adapted for 2-4 meters ?



  • I've been building one of these pulse meters up, it seems to be working on the bench very nice. Just got to build a case and get a power supply of the Arduino. But I have designed a case to hold the LM393 sensor onto the meter. I removed the light sensor and re-soldered it flush to the board making it easier to mount on the meter. Its a simple box but works quite well.

    IMG_0001.JPG

    IMG_0005.JPG

    IMG_0006.JPG

    The files are on thingiverse here

    Hope this is useful.



  • @sundberg84 Thanks very much for this post. I was struggling to get the sensor appear in Home-Assistant. Your note on the fact that HA never returns V_VAR1 on first use was very useful and pointed me in the right direction.

    My modifications looked like this:

    } else if (sendTime && !pcReceived) {
    		// No pulse count value received from controller. Try requesting it again.
    		request(CHILD_ID, V_VAR1);
        retryCount++;
    		lastSend = now;
    	} else if (retryCount >= 5 && !pcReceived) {
          //For some controllers, if you dont have any V_VAR1 stored node will not get an answer.
          //Try 5 times, then set V_VAR1 to 0 and update controller
        pcReceived = true;
        retryCount = 0;
        send(pcMsg.set(pulseCount));  // Send pulse count 0 value to gw
        double kWh = ((double)pulseCount / ((double)PULSE_FACTOR));
        send(kWhMsg.set(kWh, 4));  // Send kWh value 0 to gw
        lastSend = now;
      }
    

    The whole sketch is here:

    /*
       The MySensors Arduino library handles the wireless radio link and protocol
       between your home built sensors/actuators and HA controller of choice.
       The sensors forms a self healing radio network with optional repeaters. Each
       repeater and gateway builds a routing tables in EEPROM which keeps track of the
       network topology allowing messages to be routed to nodes.
    
       Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
       Copyright (C) 2013-2019 Sensnology AB
       Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
    
       Documentation: http://www.mysensors.org
       Support Forum: http://forum.mysensors.org
    
       This program is free software; you can redistribute it and/or
       modify it under the terms of the GNU General Public License
       version 2 as published by the Free Software Foundation.
    
     *******************************
    
       REVISION HISTORY
       Version 1.0 - Henrik Ekblad
       Version 1.1 - Peter Andersson added millis watt calculation if time between pulses > 1h
    
       DESCRIPTION
       This sketch provides an example how to implement a LM393 PCB
       Use this sensor to measure kWh and Watt of your house meter
       You need to set the correct pulsefactor of your meter (blinks per kWh).
       The sensor starts by fetching current kWh value from gateway.
       Reports both kWh and Watt back to gateway.
    
       Unfortunately millis() won't increment when the Arduino is in
       sleepmode. So we cannot make this sensor sleep if we also want
       to calculate/report watt value.
       http://www.mysensors.org/build/pulse_power
    */
    
    // Enable debug prints
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_RF24
    //#define MY_RADIO_NRF5_ESB
    //#define MY_RADIO_RFM69
    //#define MY_RADIO_RFM95
    
    #include <MySensors.h>
    
    #define DIGITAL_INPUT_SENSOR 3  // The digital input you attached your light sensor.  (Only 2 and 3 generates interrupt!)
    #define PULSE_FACTOR 1000       // Number of blinks per kWh of your meter. Normally 1000.
    #define SLEEP_MODE false        // Watt value can only be reported when sleep mode is false.
    #define MAX_WATT 10000          // Max watt value to report. This filters outliers.
    #define CHILD_ID 1              // Id of the sensor child
    
    uint32_t SEND_FREQUENCY =
        20000; // Minimum time between send (in milliseconds). We don't want to spam the gateway.
    double ppwh = ((double)PULSE_FACTOR) / 1000; // Pulses per watt hour
    bool pcReceived = false;
    volatile uint32_t pulseCount = 0;
    volatile uint32_t lastBlinkmicros = 0;
    volatile uint32_t lastBlinkmillis = 0;
    volatile uint32_t watt = 0;
    uint32_t retryCount = 0;  //If value not being returned from controller, increment this counter
    uint32_t oldPulseCount = 0;
    uint32_t oldWatt = 0;
    double oldkWh;
    uint32_t lastSend;
    MyMessage wattMsg(CHILD_ID, V_WATT);
    MyMessage kWhMsg(CHILD_ID, V_KWH);
    MyMessage pcMsg(CHILD_ID, V_VAR1);
    
    #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
    #define IRQ_HANDLER_ATTR ICACHE_RAM_ATTR
    #else
    #define IRQ_HANDLER_ATTR
    #endif
    
    void IRQ_HANDLER_ATTR onPulse()
    {
    	if (!SLEEP_MODE) {
    		uint32_t newBlinkmicros = micros();
    		uint32_t newBlinkmillis = millis();
    		uint32_t intervalmicros = newBlinkmicros - lastBlinkmicros;
    		uint32_t intervalmillis = newBlinkmillis - lastBlinkmillis;
    		if (intervalmicros < 10000L && intervalmillis < 10L) { // Sometimes we get interrupt on RISING
    			return;
    		}
    		if (intervalmillis < 360000) { // Less than an hour since last pulse, use microseconds
    			watt = (3600000000.0 / intervalmicros) / ppwh;
    		} else {
    			watt = (3600000.0 / intervalmillis) /
    			       ppwh; // more thAn an hour since last pulse, use milliseconds as micros will overflow after 70min
    		}
    		lastBlinkmicros = newBlinkmicros;
    		lastBlinkmillis = newBlinkmillis;
    	}
    	pulseCount++;
    }
    
    void setup()
    {
    	// Fetch last known pulse count value from gw
    	request(CHILD_ID, V_VAR1);
    
    	// Use the internal pullup to be able to hook up this sketch directly to an energy meter with S0 output
    	// If no pullup is used, the reported usage will be too high because of the floating pin
    	pinMode(DIGITAL_INPUT_SENSOR, INPUT_PULLUP);
    
    	attachInterrupt(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), onPulse, RISING);
    	lastSend = millis();
    }
    
    void presentation()
    {
    	// Send the sketch version information to the gateway and Controller
    	sendSketchInfo(F("Energy Meter"), F("1.1"));
    
    	// Register this device as power sensor
    	present(CHILD_ID, S_POWER);
    }
    
    void loop()
    {
    	uint32_t now = millis();
    	// Only send values at a maximum frequency or woken up from sleep
    	bool sendTime = now - lastSend > SEND_FREQUENCY;
    	if (pcReceived && (SLEEP_MODE || sendTime)) {
    		// New watt value has been calculated
    		if (!SLEEP_MODE && watt != oldWatt) {
    			// Check that we don't get unreasonable large watt value, which
    			// could happen when long wraps or false interrupt triggered
    			if (watt < ((uint32_t)MAX_WATT)) {
    				send(wattMsg.set(watt));  // Send watt value to gw
    			}
    			Serial.print("Watt:");
    			Serial.println(watt);
    			oldWatt = watt;
    		}
    
    		// Pulse count value has changed
    		if (pulseCount != oldPulseCount) {
    			send(pcMsg.set(pulseCount));  // Send pulse count value to gw
    			double kWh = ((double)pulseCount / ((double)PULSE_FACTOR));
    			oldPulseCount = pulseCount;
    			if (kWh != oldkWh) {
    				send(kWhMsg.set(kWh, 4));  // Send kWh value to gw
    				oldkWh = kWh;
    			}
    		}
    		lastSend = now;
    	} else if (sendTime && !pcReceived) {
    		// No pulse count value received from controller. Try requesting it again.
    		request(CHILD_ID, V_VAR1);
        retryCount++;
    		lastSend = now;
    	} else if (retryCount >= 5 && !pcReceived) {
          //For some controllers, if you dont have any V_VAR1 stored node will not get an answer.
          //Try 5 times, then set V_VAR1 to 0 and update controller
        pcReceived = true;
        retryCount = 0;
        send(pcMsg.set(pulseCount));  // Send pulse count 0 value to gw
        double kWh = ((double)pulseCount / ((double)PULSE_FACTOR));
        send(kWhMsg.set(kWh, 4));  // Send kWh value 0 to gw
        lastSend = now;
      }
    
    
    	if (SLEEP_MODE) {
    		sleep(SEND_FREQUENCY, false);
    	}
    }
    
    void receive(const MyMessage &message)
    {
    	if (message.getType()==V_VAR1) {
    		pulseCount = oldPulseCount = message.getLong();
    		Serial.print("Received last pulse count value from gw:");
    		Serial.println(pulseCount);
    		pcReceived = true;
    	}
    }
    


  • Got this sensor up and running now. Comparing it to my old clamp meter the kWh reading was reading 2-3 times higher, the Watts was about right. I added a 0.1uF capacity across the ground and data pins of the sensor and it is working well now.
    The graph below shows the comparison of my two sensors. the old clamp meter is Energy previous hour, the new pulse sensor is Temp energy previous hour
    You can see they are not aligned, until about 20:00 when I added the capacitor and they drop in line.Screenshot 2021-09-25 at 22.18.27.png



  • Hi everyone, I just implemented this energy meter and it's working quite good. Thanks for all the help.

    I have 1 question, however. Since my sensors runs on batteries, it sometimes dies. I then need to replace the batteries and "restart" the counting. It's not ideal, but I have no way of plugin the sensor to power next to the meter.

    When the sensor dies, I read the kWh reading of the meter manually and store it in an input variable. How can I use this to set the kWh value of the mysensor variable at initialization?

    I see this line: request(PC_CHILD_ID, V_VAR1);, however it's never running for me. Somehow it does not work properly with home assistant.

    Would request(KWH_CHILD_ID, V_KWH); do the trick? (provided that I store the kWh value from the meter in the same state created by the mysensor S0 pulse counter)

    thanks a lot


  • Banned

    @asgardro Thanks for your nice prompt feedback.


  • Banned

    Pulse Power Measurement: Measuring Instantaneous Power of a Short Pulse. ... Thermopile sensors are often used to measure single shot pulse energy;



  • @keithellis Lovely idea.
    I've followed suit and knocked up one but with the sensor facing the other way, so that it faces the meter, and the preset pot is and LEDs are facing away from the meter.

    That way the sensor side of the case can be stuck to the face of the meter, and the pot adjusted without removing it from the meter. The flashing light on DO flashes in sync with the LED on the meter. Theres a little ring that holds the sensor flush with the face of the case so it can lie flat on the front of the meter.

    Also, its a good idea to use black / dark coloured PLA so as to block out the any stray light that may illuminate the sensor (such as when the meter man comes to read it and shines a bright torch to see the dials).


Log in to reply
 

Suggested Topics

  • 3
  • 584
  • 5
  • 110
  • 109
  • 347

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts