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 asgardro

    Hi. I built this sensor and loaded the example. It appears the sensor registers with my serial gateway and appears in domoticz. The problem is it hasn't updated the value in 24h? Any ideas?
    Thanks

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

    @asgardro
    What does the serial output in arduino says?

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

      In the end it displays the watt value and then just hangs. I haven't modified the example at all. I am @ work now but will be able to check again later

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

        Came back from work and checked. it is sending to the gateway the first value and gets displayed in domoticz. then dead.. it appears it does not get the pulse count from the gateway respectively from domoticz. has anyone got this working with domoticz?

        F 1 Reply Last reply
        0
        • asgardroA asgardro

          Came back from work and checked. it is sending to the gateway the first value and gets displayed in domoticz. then dead.. it appears it does not get the pulse count from the gateway respectively from domoticz. has anyone got this working with domoticz?

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

          @asgardro
          I am using two of these to DZ.
          One has been working perfect for 1,5 month the second I had some problem with initialize after 3 weeks.

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

            would you mind please sharing the sketch? i don't know whats wrong. i even forced it with "bool pcReceived = false" puting it to true. thanks

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

              i do not get Serial.print("Received last pulse count from gw:"); in serial output

              F 2 Replies Last reply
              0
              • asgardroA asgardro

                i do not get Serial.print("Received last pulse count from gw:"); in serial output

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

                @asgardro
                this is my sketch
                i use MS 1.5.1

                /**
                 * 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
                 */
                
                #include <SPI.h>
                #include <MySensor.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 meeter
                #define SLEEP_MODE false        // Watt-value can only be reported when sleep mode is false.
                #define MAX_WATT 15000          // Max watt value to report. This filters outliers.
                #define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
                #define CHILD_ID 1              // Id of the sensor child
                unsigned long SEND_FREQUENCY = 5*60000; // Minimum time between send (in milliseconds). We don't want to spam the gateway.
                MySensor gw;
                double ppwh = ((double)PULSE_FACTOR)/1000; // Pulses per watt hour
                boolean 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()  
                {  
                  gw.begin(incomingMessage);
                
                  // Send the sketch version information to the gateway and Controller
                  gw.sendSketchInfo("Energy Huset", "1.1");
                //1.1 no check for old value, always send
                  // Register this device as power sensor
                  gw.present(CHILD_ID, S_POWER);
                
                  // Fetch last known pulse count value from gw
                  gw.request(CHILD_ID, V_VAR1);
                  
                  attachInterrupt(INTERRUPT, onPulse, RISING);
                  lastSend=millis();
                }
                
                
                void loop()     
                { 
                  gw.process();
                  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)) {
                      
                        gw.send(wattMsg.set(watt));  // Send watt value to gw 
                      }  
                      Serial.print("Watt:");
                      Serial.println(watt);
                      oldWatt = watt;
                    }
                  
                    // Pulse cout has changed
                    
                    if (pulseCount != oldPulseCount) {
                    
                      gw.send(pcMsg.set(pulseCount));  // Send pulse count value to gw 
                      double kwh = ((double)pulseCount/((double)PULSE_FACTOR));     
                      oldPulseCount = pulseCount;
                      
                      //if (kwh != oldKwh) {
                        gw.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
                    gw.request(CHILD_ID, V_VAR1);
                    lastSend=now;
                  }
                  
                  if (SLEEP_MODE) {
                    gw.sleep(SEND_FREQUENCY);
                  }
                }
                
                void incomingMessage(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
                • asgardroA Offline
                  asgardroA Offline
                  asgardro
                  wrote on last edited by
                  #26

                  0 MCO:BGN:INIT NODE,CP=RNNNA--,VER=2.1.0
                  3 TSM:INIT
                  4 TSF:WUR:MS=0
                  11 TSM:INIT:TSP OK
                  13 TSF:SID:OK,ID=254
                  14 TSM:FPAR
                  51 TSF:MSG:SEND,254-254-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
                  2058 !TSM:FPAR:NO REPLY
                  2060 TSM:FPAR
                  2096 TSF:MSG:SEND,254-254-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
                  2121 TSF:MSG:READ,0-0-254,s=255,c=3,t=8,pt=1,l=1,sg=0:0
                  2126 TSF:MSG:FPAR OK,ID=0,D=1
                  4104 TSM:FPAR:OK
                  4105 TSM:ID
                  4106 TSM:ID:OK
                  4108 TSM:UPL
                  4113 TSF:MSG:SEND,254-254-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=OK:1
                  4123 TSF:MSG:READ,0-0-254,s=255,c=3,t=25,pt=1,l=1,sg=0:1
                  4128 TSF:MSG:PONG RECV,HP=1
                  4131 TSM:UPL:OK
                  4132 TSM:READY:ID=254,PAR=0,DIS=1
                  4140 TSF:MSG:SEND,254-254-0-0,s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=OK:0100
                  4164 TSF:MSG:READ,0-0-254,s=255,c=3,t=15,pt=6,l=2,sg=0:0100
                  4172 TSF:MSG:SEND,254-254-0-0,s=255,c=0,t=17,pt=0,l=5,sg=0,ft=0,st=OK:2.1.0
                  4183 TSF:MSG:SEND,254-254-0-0,s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=OK:0
                  4203 TSF:MSG:READ,0-0-254,s=255,c=3,t=6,pt=0,l=1,sg=0:M
                  4210 TSF:MSG:SEND,254-254-0-0,s=255,c=3,t=11,pt=0,l=12,sg=0,ft=0,st=OK:Energy Meter
                  4224 TSF:MSG:SEND,254-254-0-0,s=255,c=3,t=12,pt=0,l=3,sg=0,ft=0,st=OK:1.0
                  4235 TSF:MSG:SEND,254-254-0-0,s=1,c=0,t=13,pt=0,l=0,sg=0,ft=0,st=OK:
                  4241 MCO:REG:REQ
                  4246 TSF:MSG:SEND,254-254-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=OK:2
                  4253 TSF:MSG:READ,0-0-254,s=255,c=3,t=27,pt=1,l=1,sg=0:1
                  4258 MCO:PIM:NODE REG=1
                  4260 MCO:BGN:STP
                  4267 TSF:MSG:SEND,254-254-0-0,s=1,c=2,t=24,pt=0,l=0,sg=0,ft=0,st=OK:
                  4273 MCO:BGN:INIT OK,TSP=1
                  4283 TSF:MSG:READ,0-0-254,s=1,c=2,t=24,pt=0,l=1,sg=0:1
                  Received last pulse count from gw:1

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

                    here is my serial output. and remains like this.
                    i have found your sketch but can not seem to get it to work to gw 2.0

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

                      And this is my log from DZ

                      2017-01-10 22:00:28.664 MySensors: Node: 254, Sketch Name: Energy Meter
                      2017-01-10 22:00:28.675 MySensors: Node: 254, Sketch Version: 1.0
                      2017-01-10 22:00:33.737 (Gate) General/kWh (Meter)
                      2017-01-10 22:00:33.761 (Gate) General/kWh (Meter)
                      2017-01-10 22:01:34.631 (Gate) Temp + Humidity (GatewayBed1)
                      2017-01-10 22:01:37.207 (Gate) Temp (Radio Room)
                      2017-01-10 22:01:46.109 (Gate) General/Barometer (Pressure)
                      2017-01-10 22:02:34.570 (Gate) Temp + Humidity (GatewayBed1)
                      2017-01-10 22:02:34.573 (Gate) Temp + Humidity (GatewayBed1)
                      2017-01-10 22:03:34.510 (Gate) Temp + Humidity (GatewayBed1)
                      2017-01-10 22:03:34.513 (Gate) Temp + Humidity (GatewayBed1)
                      2017-01-10 22:03:50.973 (Gate) Temp (Radio Room)
                      2017-01-10 22:04:03.239 (Gate) Temp (Outside)
                      2017-01-10 22:04:03.249 (Gate) General/Barometer (Pressure)

                      1 Reply Last reply
                      0
                      • F Offline
                        F Offline
                        flopp
                        wrote on last edited by
                        #29

                        can you please print your code?

                        I am not so good to read the Message structure but it look like it present temperature and other stuff

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

                          Ah the gateway works fine with other sensors
                          The code for the pulse power meter is the one in the example

                          F 1 Reply Last reply
                          0
                          • asgardroA asgardro

                            i do not get Serial.print("Received last pulse count from gw:"); in serial output

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

                            @asgardro said:

                            i do not get Serial.print("Received last pulse count from gw:"); in serial output

                            Yes you do, look at the last line and you will see it.

                            1 Reply Last reply
                            0
                            • asgardroA asgardro

                              Ah the gateway works fine with other sensors
                              The code for the pulse power meter is the one in the example

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

                              @asgardro

                              change SEND_FREQUENCY to 5000, upload to Arduino, when you see Received from GW, trig Digital Input 3 and after 5 seconds it should send data to DZ again

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

                                Done that... reset everything..still the same

                                here is my code

                                #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 = true;
                                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++;
                                }

                                F V 3 Replies Last reply
                                0
                                • asgardroA asgardro

                                  Done that... reset everything..still the same

                                  here is my code

                                  #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 = true;
                                  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++;
                                  }

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

                                  @asgardro
                                  don't forget to use CODE function when posting code.

                                  If you don't see any reaction when you trig Digital 3 i don't know what can be the problem,

                                  Have you tried another Arduino?

                                  1 Reply Last reply
                                  0
                                  • asgardroA asgardro

                                    Done that... reset everything..still the same

                                    here is my code

                                    #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 = true;
                                    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++;
                                    }

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

                                    @asgardro

                                    change bool pcReceived to false

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

                                      ooops sorry about that... just found the function lol
                                      the sensor inits itself once and then doesn't do anything/ i don't know through what miracle i got the pulse count.
                                      will try another arduino... i do not think it is a radio problem..
                                      thanks for your help

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

                                        changed it to false.
                                        put it true before to try to force the sensor sending independent of the count.. if i am correct
                                        still the same outcome... will try tomorrow diffrent hardware
                                        anyway, thanks a lot for the help

                                        F 1 Reply Last reply
                                        0
                                        • asgardroA asgardro

                                          changed it to false.
                                          put it true before to try to force the sensor sending independent of the count.. if i am correct
                                          still the same outcome... will try tomorrow diffrent hardware
                                          anyway, thanks a lot for the help

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

                                          @asgardro said:

                                          changed it to false.
                                          put it true before to try to force the sensor sending independent of the count.. if i am correct

                                          Yes it will but it will only send 0 to VAR in DZ so it will be a loop of same value.
                                          Maybe that's why it never sends again.
                                          Try now to trig digital 3

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


                                          12

                                          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