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.
  • M Offline
    M Offline
    mickecarlsson
    wrote on last edited by
    #95

    Here is a code example that works for me. I have an energy meter with S0 output and I use Domoticz as the controller.

    /**
     * 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
     * Version 1.1 - Mikael Carlsson
     *
     * DESCRIPTION
     * This sketch provides an example how to implement a sensor reading from an energy meter
     * 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 meter
    #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 bad data.
    #define CHILD_ID 1              // Id of the sensor child
    
    unsigned long 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 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 happen when long wraps or false interrupt triggered
                // We need to send in all values at one time to get consistent data
                if (watt<((unsigned long)MAX_WATT)) {
                    send(wattMsg.set(watt));  // Send watt value to gw
                    Serial.print("Watt: ");
                    Serial.println(watt);
                    oldWatt = watt;
                    send(pcMsg.set(pulseCount));  // Send pulse count value to gw
                    Serial.print("pulseCount: ");
                    Serial.println(watt);
                    double kwh = ((double)pulseCount/((double)PULSE_FACTOR));
                    oldPulseCount = pulseCount;
                    send(kwhMsg.set(kwh, 4));  // Send kwh value to gw
                    Serial.print("kWh: ");
                    Serial.println(kwh);
                    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
    • D Offline
      D Offline
      dakipro
      wrote on last edited by
      #96

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

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

      C: OpenHAB2 with node-red on linux laptop
      GW: Arduino Nano - W5100 Ethernet, Nrf24l01+ 2,4Ghz mqtt
      GW: Arduino Mega, RFLink 433Mhz

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

        Is it only me that is getting a compile error?

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

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

        followed by all the possible candidates of .set arguments

        mfalkviddM 1 Reply Last reply
        0
        • gohanG gohan

          Is it only me that is getting a compile error?

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

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

          followed by all the possible candidates of .set arguments

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

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

          1 Reply Last reply
          0
          • mfalkviddM Offline
            mfalkviddM Offline
            mfalkvidd
            Mod
            wrote on last edited by
            #99

            Replacing all

            unsigned long
            

            with

            uint32_t
            

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

            1 Reply Last reply
            0
            • mfalkviddM Offline
              mfalkviddM Offline
              mfalkvidd
              Mod
              wrote on last edited by
              #100

              I created an issue for this, https://github.com/mysensors/MySensors/issues/910

              1 Reply Last reply
              0
              • hekH Offline
                hekH Offline
                hek
                Admin
                wrote on last edited by
                #101

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

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

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

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

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

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

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

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

                      A rejoe2R 2 Replies Last reply
                      0
                      • gohanG gohan

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

                        A Offline
                        A Offline
                        arifuleee
                        wrote on last edited by
                        #105

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

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

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

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

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

                            1 Reply Last reply
                            0
                            • gohanG gohan

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

                              rejoe2R Offline
                              rejoe2R Offline
                              rejoe2
                              wrote on last edited by
                              #108

                              @gohan How about

                              #define MY_TRANSPORT_WAIT_READY_MS 2000
                              

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

                              Controller: FHEM; MySensors: 2.3.1, RS485,nRF24,RFM69, serial Gateways

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

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

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

                                  Any more suggestions on the Watt issue?

                                  1 Reply Last reply
                                  0
                                  • hekH Offline
                                    hekH Offline
                                    hek
                                    Admin
                                    wrote on last edited by
                                    #111

                                    Did you by any chance change SLEEP_MODE to true?

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

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

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

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

                                        1 Reply Last reply
                                        1
                                        • hekH hek

                                          Did you by any chance change SLEEP_MODE to true?

                                          gohanG Offline
                                          gohanG Offline
                                          gohan
                                          Mod
                                          wrote on last edited by
                                          #114

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

                                          1 Reply Last reply
                                          0
                                          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