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.
  • asgardroA Offline
    asgardroA Offline
    asgardro
    wrote on last edited by
    #39

    sorry for the next noob question but what do you mean by trigginh the pin 3? simulating the sensor? if yes i done that.. i have a led blinking

    F 1 Reply Last reply
    0
    • asgardroA asgardro

      sorry for the next noob question but what do you mean by trigginh the pin 3? simulating the sensor? if yes i done that.. i have a led blinking

      F Offline
      F Offline
      flopp
      wrote on last edited by
      #40

      @asgardro

      what have you connected to digital 3?

      asgardroA 1 Reply Last reply
      0
      • asgardroA Offline
        asgardroA Offline
        asgardro
        wrote on last edited by
        #41

        this is the code now

        /**
         * 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
        
        #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 10000          // Max watt value to report. This filetrs outliers.
        #define CHILD_ID 1              // Id of the sensor child
        
        unsigned long SEND_FREQUENCY = 5000; // 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(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()     
        { 
          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(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++;
        }
        
        1 Reply Last reply
        0
        • F flopp

          @asgardro

          what have you connected to digital 3?

          asgardroA Offline
          asgardroA Offline
          asgardro
          wrote on last edited by
          #42

          @flopp

          i have connected the digital out from the sensor

          F 1 Reply Last reply
          0
          • asgardroA asgardro

            @flopp

            i have connected the digital out from the sensor

            F Offline
            F Offline
            flopp
            wrote on last edited by flopp
            #43

            @asgardro said:

            @flopp

            i have connected the digital out from the sensor

            Do you mean LM393?

            Can you put a flash light in front of the sensor so trigger it. You shall now see some text in serial monitor

            asgardroA 2 Replies Last reply
            0
            • F flopp

              @asgardro said:

              @flopp

              i have connected the digital out from the sensor

              Do you mean LM393?

              Can you put a flash light in front of the sensor so trigger it. You shall now see some text in serial monitor

              asgardroA Offline
              asgardroA Offline
              asgardro
              wrote on last edited by
              #44

              @flopp
              lm393

              F 1 Reply Last reply
              0
              • asgardroA asgardro

                @flopp
                lm393

                F Offline
                F Offline
                flopp
                wrote on last edited by
                #45

                @asgardro said:

                @flopp
                lm393

                Have you trimmed the pot so the LED turns on when your power meter flash?

                1 Reply Last reply
                0
                • F flopp

                  @asgardro said:

                  @flopp

                  i have connected the digital out from the sensor

                  Do you mean LM393?

                  Can you put a flash light in front of the sensor so trigger it. You shall now see some text in serial monitor

                  asgardroA Offline
                  asgardroA Offline
                  asgardro
                  wrote on last edited by
                  #46

                  @flopp
                  i have done that and calibrated the lm393 , i have a blinking light that triggers the sensor. thats how i am testing...but after initializing sends the value first time and thats it
                  sorry to reply so late but have to wait 120seconds between posts lol

                  1 Reply Last reply
                  0
                  • asgardroA Offline
                    asgardroA Offline
                    asgardro
                    wrote on last edited by
                    #47
                    /**
                     * 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_RF24_CHANNEL 74
                    //#define MY_RADIO_RFM69
                    
                    #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 10000          // Max watt value to report. This filetrs outliers.
                    #define CHILD_ID 1              // Id of the sensor child
                    
                    unsigned long SEND_FREQUENCY = 5000; // 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(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();
                      Serial.println("7");
                    }
                    
                    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);
                      Serial.println("7");
                    }
                    //Serial.println("6");
                    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;
                        }
                    Serial.println("5");
                        // 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;
                          }
                          Serial.println("4");
                        }    
                        lastSend = now;
                      } else if (sendTime && !pcReceived) {
                        // No count received. Try requesting it again
                        request(CHILD_ID, V_VAR1);
                        lastSend=now;
                      }
                    Serial.println("3");
                      if (SLEEP_MODE) {
                        sleep(SEND_FREQUENCY);
                      }
                      Serial.println("2");
                    }
                    
                    void receive(const MyMessage &message) {
                    if (message.type==V_VAR1) {
                    Serial.println("rece");
                    pulseCount = oldPulseCount = message.getLong();
                    Serial.print("Received last pulse count from gw:");
                    Serial.println(pulseCount);
                    pcReceived = true;
                    }
                    Serial.println("not_rece");
                    }
                    
                    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++;
                      Serial.println("1");
                      
                    }
                    
                    

                    this

                    1 Reply Last reply
                    0
                    • maghacM Offline
                      maghacM Offline
                      maghac
                      wrote on last edited by
                      #48

                      As the sketch starts by requesting the latest pulse count (e.g starting kWh value) and does not send anything without having received it, how do I "seed" this value to my GW? I am using a MQTT GW, can I construct a MQTT special message containing the value, or how is it supposed to work?

                      F 1 Reply Last reply
                      0
                      • maghacM maghac

                        As the sketch starts by requesting the latest pulse count (e.g starting kWh value) and does not send anything without having received it, how do I "seed" this value to my GW? I am using a MQTT GW, can I construct a MQTT special message containing the value, or how is it supposed to work?

                        F Offline
                        F Offline
                        flopp
                        wrote on last edited by
                        #49

                        @maghac
                        I am using Domoticz ans MyS 1.5.1
                        You don't have to create VAR1, it will be created when you ask for it or it always exists for each Child.
                        If you want VAR1 to be exactly the same as your meter you need to open domoticz.db and set the value or in your sketch.

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

                          Anyone knows why I dont get the sleep() function (time) to work properly.

                          My hardware is EasyPCB, Pro Mini 3.3v 8mhz, Booster 2xAA and NRF24 radio.
                          My software is: standard sketch 2.0.1, with sleep function.

                          The problem is the sleep time is 5 times less than declared so maybe timing?
                          When i enter sleep(10000) for example the nodes sleeps for 2 sec and wakes up.

                          The interupt works fine.

                          My sollution was adding *5 to get the correct sleep time - but why doesnt it work?

                          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

                          1 Reply Last reply
                          0
                          • F flopp

                            @maghac
                            I am using Domoticz ans MyS 1.5.1
                            You don't have to create VAR1, it will be created when you ask for it or it always exists for each Child.
                            If you want VAR1 to be exactly the same as your meter you need to open domoticz.db and set the value or in your sketch.

                            maghacM Offline
                            maghacM Offline
                            maghac
                            wrote on last edited by
                            #51

                            @flopp I had to set pcReceived to true, upload the sketch, run it and then change back to false again and recompile/reupload. I can understand why it does it, since you don't want the pulse counter to reset to 0 if the sensor happens to restart (because you killed the power or whatever).

                            What I don't understand is how I can seed the value with the correct initial value.

                            Is it the controller (in my case HomeAssistant) or the gateway that is supplying the value?

                            maghacM 1 Reply Last reply
                            0
                            • maghacM maghac

                              @flopp I had to set pcReceived to true, upload the sketch, run it and then change back to false again and recompile/reupload. I can understand why it does it, since you don't want the pulse counter to reset to 0 if the sensor happens to restart (because you killed the power or whatever).

                              What I don't understand is how I can seed the value with the correct initial value.

                              Is it the controller (in my case HomeAssistant) or the gateway that is supplying the value?

                              maghacM Offline
                              maghacM Offline
                              maghac
                              wrote on last edited by
                              #52

                              It appears to be the controller that stores the value. I deleted the sensor from HomeAssistant and restarted, when it came back in again it started from zero.

                              martinhjelmareM 1 Reply Last reply
                              0
                              • maghacM maghac

                                It appears to be the controller that stores the value. I deleted the sensor from HomeAssistant and restarted, when it came back in again it started from zero.

                                martinhjelmareM Offline
                                martinhjelmareM Offline
                                martinhjelmare
                                Plugin Developer
                                wrote on last edited by
                                #53

                                @maghac

                                This is a hack, but if using json persistence, you can set the count in the JSON file and restart home assistant, with the node turned off. Then turn on the node.

                                maghacM 1 Reply Last reply
                                0
                                • martinhjelmareM martinhjelmare

                                  @maghac

                                  This is a hack, but if using json persistence, you can set the count in the JSON file and restart home assistant, with the node turned off. Then turn on the node.

                                  maghacM Offline
                                  maghacM Offline
                                  maghac
                                  wrote on last edited by
                                  #54

                                  @martinhjelmare Yep, I figured I could do that. The absolute value is not so important actually, I'm more interested in the daily/weekly delta, which I hope Grafana can tell me.

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

                                    is this the same sensor than the one in the guide?
                                    http://uk.farnell.com/ams/tsl250r-lf/photodiode-sensor-l-volts/dp/1182346

                                    Can I also use also photoresistor? (Since I already have one)

                                    sundberg84S 1 Reply Last reply
                                    0
                                    • gohanG gohan

                                      is this the same sensor than the one in the guide?
                                      http://uk.farnell.com/ams/tsl250r-lf/photodiode-sensor-l-volts/dp/1182346

                                      Can I also use also photoresistor? (Since I already have one)

                                      sundberg84S Offline
                                      sundberg84S Offline
                                      sundberg84
                                      Hardware Contributor
                                      wrote on last edited by sundberg84
                                      #56

                                      @gohan - nope its not the same, and yes you can use a photoresistor (i have just made one) but I needed a transistor for it to work and also it had to be completley dark. Using this on batteries its much better using just a photoresistor because the sensor in the guide draws about 1mA which will drain the batteris very quickly.

                                      I will post my sensor later.

                                      Edit: this is the sensor i build:

                                      0_1486915502085_1.jpg
                                      0_1486915592318_20170212_135834.jpg

                                      The node (including pro mini) draws about 50uA sleeping and 170uA not sleeping.
                                      The photoresistor was 5M ohm in complete dark.

                                      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

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

                                        The guide specifies LM393 Light Sensor or the tsl250r-lf so I thought it was the same since the same name.
                                        Since the node will be powered with it's power supply, I don't have any battery drain problem in this case. But why did you use a transistor? Can't you just check for a threshold value in the analog input to count as a pulse?

                                        sundberg84S 1 Reply Last reply
                                        0
                                        • gohanG gohan

                                          The guide specifies LM393 Light Sensor or the tsl250r-lf so I thought it was the same since the same name.
                                          Since the node will be powered with it's power supply, I don't have any battery drain problem in this case. But why did you use a transistor? Can't you just check for a threshold value in the analog input to count as a pulse?

                                          sundberg84S Offline
                                          sundberg84S Offline
                                          sundberg84
                                          Hardware Contributor
                                          wrote on last edited by sundberg84
                                          #58

                                          Ok, all I can see is that it doesnt look the same at all.
                                          @gohan - I tried, and I thought that should work - but for some reason I could not get it to trigger the interupt on the atmega :(
                                          Looking the the voltage thresholds it was supposed to work but it didnt. I was thinking it was to low uA but i dont know really.

                                          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

                                          gohanG 1 Reply Last reply
                                          1
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          15

                                          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