Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Announcements
  3. 💬 Power Meter Pulse Sensor

💬 Power Meter Pulse Sensor

Scheduled Pinned Locked Moved Announcements
183 Posts 40 Posters 45.0k Views 37 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • gohanG Offline
    gohanG Offline
    gohan
    Mod
    wrote on last edited by
    #159

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

    1 Reply Last reply
    0
    • bjornhallbergB Offline
      bjornhallbergB Offline
      bjornhallberg
      Hero Member
      wrote on last edited by
      #160

      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.

      1 Reply Last reply
      0
      • gohanG Offline
        gohanG Offline
        gohan
        Mod
        wrote on last edited by
        #161

        I used these https://www.aliexpress.com/item/5-pcs-LM393-Light-Sensor-Photosensitive-Sensitivity-light-Sensor-Module-For-Arduino-Smart-Car/1729424221.html

        1 Reply Last reply
        0
        • F Offline
          F Offline
          FullMetal
          wrote on last edited by
          #162

          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

          mfalkviddM 1 Reply Last reply
          0
          • F FullMetal

            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

            mfalkviddM Offline
            mfalkviddM Offline
            mfalkvidd
            Mod
            wrote on last edited by mfalkvidd
            #163

            Hi @fullmetal, welcome to the forum :-)

            Could you explain what double pricing means?

            1 Reply Last reply
            0
            • F Offline
              F Offline
              FullMetal
              wrote on last edited by
              #164

              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

              1 Reply Last reply
              0
              • F Offline
                F Offline
                FullMetal
                wrote on last edited by
                #165

                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++;
                   }
                }
                
                mfalkviddM 1 Reply Last reply
                0
                • F FullMetal

                  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++;
                     }
                  }
                  
                  mfalkviddM Offline
                  mfalkviddM Offline
                  mfalkvidd
                  Mod
                  wrote on last edited by
                  #166

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

                  F 1 Reply Last reply
                  0
                  • mfalkviddM mfalkvidd

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

                    F Offline
                    F Offline
                    FullMetal
                    wrote on last edited by
                    #167

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

                    Can you help me

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      cstewy
                      wrote on last edited by
                      #168

                      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.

                      1 Reply Last reply
                      1
                      • hekH hek

                        This thread contains comments for the article "Power Meter Pulse Sensor" posted on MySensors.org.

                        I Offline
                        I Offline
                        it-slav
                        wrote on last edited by it-slav
                        #169

                        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++;
                        }
                        
                        mfalkviddM 1 Reply Last reply
                        1
                        • I it-slav

                          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++;
                          }
                          
                          mfalkviddM Offline
                          mfalkviddM Offline
                          mfalkvidd
                          Mod
                          wrote on last edited by mfalkvidd
                          #170

                          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.

                          1 Reply Last reply
                          0
                          • butalukB Offline
                            butalukB Offline
                            butaluk
                            wrote on last edited by
                            #171

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

                            mfalkviddM 1 Reply Last reply
                            0
                            • butalukB butaluk

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

                              mfalkviddM Offline
                              mfalkviddM Offline
                              mfalkvidd
                              Mod
                              wrote on last edited by
                              #172

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

                              butalukB 1 Reply Last reply
                              0
                              • mfalkviddM mfalkvidd

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

                                butalukB Offline
                                butalukB Offline
                                butaluk
                                wrote on last edited by
                                #173

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

                                1 Reply Last reply
                                0
                                • sundberg84S Offline
                                  sundberg84S Offline
                                  sundberg84
                                  Hardware Contributor
                                  wrote on last edited by sundberg84
                                  #174

                                  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)
                                  

                                  Controller: Proxmox VM - Home Assistant
                                  MySensors GW: Arduino Uno - W5100 Ethernet, Gw Shield Nrf24l01+ 2,4Ghz
                                  MySensors GW: Arduino Uno - Gw Shield RFM69, 433mhz
                                  RFLink GW - Arduino Mega + RFLink Shield, 433mhz

                                  K 1 Reply Last reply
                                  1
                                  • S Offline
                                    S Offline
                                    snady
                                    wrote on last edited by
                                    #175

                                    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

                                    1 Reply Last reply
                                    0
                                    • F Offline
                                      F Offline
                                      frydrik
                                      wrote on last edited by
                                      #176

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

                                      1 Reply Last reply
                                      0
                                      • K Offline
                                        K Offline
                                        keithellis
                                        wrote on last edited by
                                        #177

                                        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.

                                        F 1 Reply Last reply
                                        1
                                        • sundberg84S sundberg84

                                          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)
                                          
                                          K Offline
                                          K Offline
                                          keithellis
                                          wrote on last edited by
                                          #178

                                          @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;
                                          	}
                                          }
                                          
                                          1 Reply Last reply
                                          2
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          25

                                          Online

                                          11.7k

                                          Users

                                          11.2k

                                          Topics

                                          113.1k

                                          Posts


                                          Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • MySensors
                                          • OpenHardware.io
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular