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. Hardware
  3. Sensebender with LED pulse count from openenergymonitor.org

Sensebender with LED pulse count from openenergymonitor.org

Scheduled Pinned Locked Moved Hardware
sensebenderled pulse count
14 Posts 3 Posters 6.2k Views 3 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.
  • alexsh1A Offline
    alexsh1A Offline
    alexsh1
    wrote on last edited by
    #3

    Thanks for your reply.
    I took the sketch here:
    http://www.mysensors.org/build/pulse_power

    /**
     * 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       // 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 INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
    #define CHILD_ID 4              // Id of the sensor child
    unsigned long SEND_FREQUENCY = 20000; // Minimum time between send (in milliseconds). We don't wnat 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 Meter", "1.0");
    
      // 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++;
    }
    
    
    

    The LED has the following diagram:

    LED (RJ45) Sensebender
    Pin 2 = VCC -> VCC
    Pin 5 = GND -> GND
    Pin 6 = IRQ1​ -> D3

    SparkmanS 1 Reply Last reply
    0
    • alexsh1A alexsh1

      Thanks for your reply.
      I took the sketch here:
      http://www.mysensors.org/build/pulse_power

      /**
       * 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       // 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 INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
      #define CHILD_ID 4              // Id of the sensor child
      unsigned long SEND_FREQUENCY = 20000; // Minimum time between send (in milliseconds). We don't wnat 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 Meter", "1.0");
      
        // 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++;
      }
      
      
      

      The LED has the following diagram:

      LED (RJ45) Sensebender
      Pin 2 = VCC -> VCC
      Pin 5 = GND -> GND
      Pin 6 = IRQ1​ -> D3

      SparkmanS Offline
      SparkmanS Offline
      Sparkman
      Hero Member
      wrote on last edited by
      #4

      @alexsh1 I would measure the output of the sensor to ensure it goes high when it detects a pulse and put some println's in the code to see if the interrupt gets triggered at all.

      Cheers
      Al

      1 Reply Last reply
      0
      • alexsh1A Offline
        alexsh1A Offline
        alexsh1
        wrote on last edited by
        #5

        Thanks.
        The pulse sensor has got a separate green LED to indicate when a pulse is received - it is not blinking in my case. PrintIn does not show much unfortunately though the sensor is powered up with 3.3V. I need to figure out the pull-up/down resistor value.

        SparkmanS 1 Reply Last reply
        0
        • alexsh1A alexsh1

          Thanks.
          The pulse sensor has got a separate green LED to indicate when a pulse is received - it is not blinking in my case. PrintIn does not show much unfortunately though the sensor is powered up with 3.3V. I need to figure out the pull-up/down resistor value.

          SparkmanS Offline
          SparkmanS Offline
          Sparkman
          Hero Member
          wrote on last edited by
          #6

          @alexsh1 Not sure if you need an extra resistor. I would expect there is some internal circuitry in the sensor already. Have you looked at the schematics of the emonPi, etc. on the OpenEnergyMonitor website to see if they have any additional components on their input for the sensor? I would expect the led to flash if you have Vcc and Gnd connected so perhaps something else is wrong. Are you powering with 3.3v or 5v?

          Cheers
          Al

          1 Reply Last reply
          0
          • alexsh1A Offline
            alexsh1A Offline
            alexsh1
            wrote on last edited by
            #7

            @Sparkman Yes, I did look at the openenergymonitor wiki - does not say much.
            http://openenergymonitor.org/emon/buildingblocks/opticalpulsesensor

            However, managed to find on the their web-site the following:

            LED Pulse counting

            No pull down resistor is required as the pulse / light sensor output is logic level 0 when the pulse is low. However, if you build a pulse counting module with pull down resistors of ~10k it still works with the light sensor, more info to come on this.

            As this is sensebender (powered by two AA batteries), voltage is around 3.1V currently. Are you thinking this is an issue? Will check all the connections to make sure I do not have a bad connection or something.

            SparkmanS 1 Reply Last reply
            0
            • alexsh1A alexsh1

              @Sparkman Yes, I did look at the openenergymonitor wiki - does not say much.
              http://openenergymonitor.org/emon/buildingblocks/opticalpulsesensor

              However, managed to find on the their web-site the following:

              LED Pulse counting

              No pull down resistor is required as the pulse / light sensor output is logic level 0 when the pulse is low. However, if you build a pulse counting module with pull down resistors of ~10k it still works with the light sensor, more info to come on this.

              As this is sensebender (powered by two AA batteries), voltage is around 3.1V currently. Are you thinking this is an issue? Will check all the connections to make sure I do not have a bad connection or something.

              SparkmanS Offline
              SparkmanS Offline
              Sparkman
              Hero Member
              wrote on last edited by
              #8

              @alexsh1 The 3.1v might be a problem. I would power from 3.3v and see if the green led starts blinking. Doesn't sound like you need any pullup/down resistors.

              Cheers
              Al

              1 Reply Last reply
              0
              • alexsh1A Offline
                alexsh1A Offline
                alexsh1
                wrote on last edited by
                #9

                @Sparkman Tried 3.3V and still does not work

                1 Reply Last reply
                0
                • alexsh1A Offline
                  alexsh1A Offline
                  alexsh1
                  wrote on last edited by
                  #10

                  @Sparkman I narrowed down the problem. The sketch and the sensor do not work with the sensebender. I used Arduino Nano instead and it worked though the gateway stopped receiving the signal after 10-20 mins, but that is a different problem. Now I think there is an issue with interruption with the sensebender.

                  Did anyone compiled energy pulse sensor on the sensebender please?

                  SparkmanS 1 Reply Last reply
                  0
                  • alexsh1A alexsh1

                    @Sparkman I narrowed down the problem. The sketch and the sensor do not work with the sensebender. I used Arduino Nano instead and it worked though the gateway stopped receiving the signal after 10-20 mins, but that is a different problem. Now I think there is an issue with interruption with the sensebender.

                    Did anyone compiled energy pulse sensor on the sensebender please?

                    SparkmanS Offline
                    SparkmanS Offline
                    Sparkman
                    Hero Member
                    wrote on last edited by
                    #11

                    @alexsh1 Good to hear you are making progress. With the nano, was it powered by 3.3v or 5v? From what I understand, the interrupt pins on the Nano and SenseBender should work the same, but maybe @tbowmo can confirm.

                    Cheers
                    Al

                    1 Reply Last reply
                    0
                    • alexsh1A Offline
                      alexsh1A Offline
                      alexsh1
                      wrote on last edited by alexsh1
                      #12

                      it was powered with 5V, but works fine with 3V.
                      I have to figure out if the interrupt pins are the same as nano

                      I have it fully up and running under the nano

                      1 Reply Last reply
                      0
                      • alexsh1A Offline
                        alexsh1A Offline
                        alexsh1
                        wrote on last edited by
                        #13

                        The interrupt pins are the same as Nano - I have to understand what was wrong in the first place.

                        1447097106174-prominipdf.png

                        1 Reply Last reply
                        0
                        • tbowmoT Offline
                          tbowmoT Offline
                          tbowmo
                          Admin
                          wrote on last edited by
                          #14

                          @Sparkman

                          You are right. The pin names on the sensebender, corresponds to the pin names on the arduinos, so the interrupt on D3 is the same as on the arduino.

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


                          20

                          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