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. Troubleshooting
  3. Node with Interrupt, sleep and batteries

Node with Interrupt, sleep and batteries

Scheduled Pinned Locked Moved Troubleshooting
interruptsleep
16 Posts 5 Posters 10.9k Views 1 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.
  • sundberg84S Offline
    sundberg84S Offline
    sundberg84
    Hardware Contributor
    wrote on last edited by sundberg84
    #1

    Hi!

    Is there anyone out there with a sketch having a sensor count interrups during sleep-time and then report this for example evert 15min. Im trying this but even if i set 15min as sleep time the node sends every 1-2 minutes...

     /**
     * 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 true        // 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 1              // Id of the sensor child
    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);
    
    #define CHILD_ID 1                              // Id of the sensor child
    #define NODE_ID 8                               // or AUTO to let controller assign
    #define SKETCH_NAME "Energy Meter #8"                // Change to a fancy name you like
    #define SKETCH_VERSION "1.2"                    // Your version
    
    unsigned long SLEEP_TIME = 780000;            // Sleep time (in milliseconds).
    
    //=========================
    // BATTERY VOLTAGE DIVIDER SETUP
    // 1M, 470K divider across battery and using internal ADC ref of 1.1V
    // Sense point is bypassed with 0.1 uF cap to reduce noise at that point
    // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts
    // 3.44/1023 = Volts per bit = 0.003363075
    #define VBAT_PER_BITS 0.003363075  
    #define VMIN 1.9                                  //  Vmin (radio Min Volt)=1.9V (564v)
    #define VMAX 3.0                                  //  Vmax = (2xAA bat)=3.0V (892v)
    int batteryPcnt = 0;                              // Calc value for battery %
    int batLoop = 0;                                  // Loop to help calc average
    int batArray[3];                                  // Array to store value for average calc.
    int BATTERY_SENSE_PIN = A0;                       // select the input pin for the battery sense point
    //=========================
    
    void setup()  
    {  
      
      analogReference(INTERNAL);             // For battery sensing
    
      delay(500); // Allow time for radio if power used as reset
    
      //Begin (Change if you dont want static node_id! (NODE_ID to AUTO)
      gw.begin(incomingMessage, NODE_ID, false);
      
      // Send the Sketch Version Information to the Gateway
      gw.sendSketchInfo(SKETCH_NAME, SKETCH_VERSION);
    
      // 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 currentTime = millis();
    
        //See if we have the counter/pulse from Domoticz - and ask for it if we dont.
        if (!pcReceived && (currentTime - lastSend > 5000)) {      
          gw.request(CHILD_ID, V_VAR1);
          Serial.println("Request pulsecount");
          lastSend=currentTime;
          gw.process();
          return;
        }
        
        if (!pcReceived) {
          return;
        }
      
      
        // Pulse cout has changed
        if (pulseCount > oldPulseCount + 2000) {
          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;
            batM();
          }
        }    
        lastSend = currentTime;
      
        
        gw.sleep(SLEEP_TIME);
    }
    
    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()     
    { 
      pulseCount++;
    }
    
    void batM() //The battery calculations
    {
       delay(500);
       // Battery monitoring reading
       int sensorValue = analogRead(BATTERY_SENSE_PIN);    
       delay(500);
       
       // Calculate the battery in %
       float Vbat  = sensorValue * VBAT_PER_BITS;
       int batteryPcnt = static_cast<int>(((Vbat-VMIN)/(VMAX-VMIN))*100.);
       Serial.print("Battery percent: "); Serial.print(batteryPcnt); Serial.println(" %");  
       
       // Add it to array so we get an average of 3 (3x20min)
       batArray[batLoop] = batteryPcnt;
      
       if (batLoop > 2) {  
         batteryPcnt = (batArray[0] + batArray[1] + batArray[2] + batArray[3]);
         batteryPcnt = batteryPcnt / 3;
     
       if (batteryPcnt > 100) {
         batteryPcnt=100;
     }
     
         Serial.print("Battery Average (Send): "); Serial.print(batteryPcnt); Serial.println(" %");
           gw.sendBatteryLevel(batteryPcnt);
           batLoop = 0;
          }
         else 
         {
         batLoop++;
         }
    }
    

    Log:

    send: 8-8-0-0 s=255,c=0,t=17,pt=0,l=3,sg=0,st=ok:1.5
    send: 8-8-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,st=ok:0
    read: 0-0-8 s=255,c=3,t=6,pt=0,l=1,sg=0:M
    read: 0-0-8 s=255,c=3,t=6,pt=0,l=1,sg=0:M
    sensor started, id=8, parent=0, distance=1
    send: 8-8-0-0 s=255,c=3,t=11,pt=0,l=15,sg=0,st=ok:Energy Meter #8
    send: 8-8-0-0 s=255,c=3,t=12,pt=0,l=3,sg=0,st=ok:1.2
    send: 8-8-0-0 s=1,c=0,t=13,pt=0,l=0,sg=0,st=ok:
    send: 8-8-0-0 s=1,c=2,t=24,pt=0,l=0,sg=0,st=ok:
    read: 0-0-8 s=1,c=2,t=24,pt=0,l=7,sg=0:1388544
    Received last pulse count from gw:1388544
    send: 8-8-0-0 s=1,c=1,t=24,pt=5,l=4,sg=0,st=ok:1390562
    send: 8-8-0-0 s=1,c=1,t=18,pt=7,l=5,sg=0,st=ok:1390.5920
    Battery percent: 138 %
    send: 8-8-0-0 s=1,c=1,t=24,pt=5,l=4,sg=0,st=ok:1392606
    send: 8-8-0-0 s=1,c=1,t=18,pt=7,l=5,sg=0,st=ok:1392.6340
    Battery percent: 112 %
    send: 8-8-0-0 s=1,c=1,t=24,pt=5,l=4,sg=0,st=ok:1394726
    send: 8-8-0-0 s=1,c=1,t=18,pt=7,l=5,sg=0,st=ok:1394.7581
    Battery percent: 111 %
    send: 8-8-0-0 s=1,c=1,t=24,pt=5,l=4,sg=0,st=ok:1396821
    send: 8-8-0-0 s=1,c=1,t=18,pt=7,l=5,sg=0,st=ok:1396.8210
    Battery percent: 112 %
    Battery Average (Send): 100 %
    send: 8-8-0-0 s=255,c=3,t=0,pt=1,l=1,sg=0,st=ok:100
    send: 8-8-0-0 s=255,c=0,t=17,pt=0,l=3,sg=0,st=ok:1.5
    send: 8-8-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,st=ok:0
    read: 0-0-8 s=255,c=3,t=6,pt=0,l=1,sg=0:M
    read: 0-0-8 s=255,c=3,t=6,pt=0,l=1,sg=0:M```

    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

    YveauxY 1 Reply Last reply
    0
    • sundberg84S sundberg84

      Hi!

      Is there anyone out there with a sketch having a sensor count interrups during sleep-time and then report this for example evert 15min. Im trying this but even if i set 15min as sleep time the node sends every 1-2 minutes...

       /**
       * 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 true        // 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 1              // Id of the sensor child
      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);
      
      #define CHILD_ID 1                              // Id of the sensor child
      #define NODE_ID 8                               // or AUTO to let controller assign
      #define SKETCH_NAME "Energy Meter #8"                // Change to a fancy name you like
      #define SKETCH_VERSION "1.2"                    // Your version
      
      unsigned long SLEEP_TIME = 780000;            // Sleep time (in milliseconds).
      
      //=========================
      // BATTERY VOLTAGE DIVIDER SETUP
      // 1M, 470K divider across battery and using internal ADC ref of 1.1V
      // Sense point is bypassed with 0.1 uF cap to reduce noise at that point
      // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts
      // 3.44/1023 = Volts per bit = 0.003363075
      #define VBAT_PER_BITS 0.003363075  
      #define VMIN 1.9                                  //  Vmin (radio Min Volt)=1.9V (564v)
      #define VMAX 3.0                                  //  Vmax = (2xAA bat)=3.0V (892v)
      int batteryPcnt = 0;                              // Calc value for battery %
      int batLoop = 0;                                  // Loop to help calc average
      int batArray[3];                                  // Array to store value for average calc.
      int BATTERY_SENSE_PIN = A0;                       // select the input pin for the battery sense point
      //=========================
      
      void setup()  
      {  
        
        analogReference(INTERNAL);             // For battery sensing
      
        delay(500); // Allow time for radio if power used as reset
      
        //Begin (Change if you dont want static node_id! (NODE_ID to AUTO)
        gw.begin(incomingMessage, NODE_ID, false);
        
        // Send the Sketch Version Information to the Gateway
        gw.sendSketchInfo(SKETCH_NAME, SKETCH_VERSION);
      
        // 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 currentTime = millis();
      
          //See if we have the counter/pulse from Domoticz - and ask for it if we dont.
          if (!pcReceived && (currentTime - lastSend > 5000)) {      
            gw.request(CHILD_ID, V_VAR1);
            Serial.println("Request pulsecount");
            lastSend=currentTime;
            gw.process();
            return;
          }
          
          if (!pcReceived) {
            return;
          }
        
        
          // Pulse cout has changed
          if (pulseCount > oldPulseCount + 2000) {
            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;
              batM();
            }
          }    
          lastSend = currentTime;
        
          
          gw.sleep(SLEEP_TIME);
      }
      
      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()     
      { 
        pulseCount++;
      }
      
      void batM() //The battery calculations
      {
         delay(500);
         // Battery monitoring reading
         int sensorValue = analogRead(BATTERY_SENSE_PIN);    
         delay(500);
         
         // Calculate the battery in %
         float Vbat  = sensorValue * VBAT_PER_BITS;
         int batteryPcnt = static_cast<int>(((Vbat-VMIN)/(VMAX-VMIN))*100.);
         Serial.print("Battery percent: "); Serial.print(batteryPcnt); Serial.println(" %");  
         
         // Add it to array so we get an average of 3 (3x20min)
         batArray[batLoop] = batteryPcnt;
        
         if (batLoop > 2) {  
           batteryPcnt = (batArray[0] + batArray[1] + batArray[2] + batArray[3]);
           batteryPcnt = batteryPcnt / 3;
       
         if (batteryPcnt > 100) {
           batteryPcnt=100;
       }
       
           Serial.print("Battery Average (Send): "); Serial.print(batteryPcnt); Serial.println(" %");
             gw.sendBatteryLevel(batteryPcnt);
             batLoop = 0;
            }
           else 
           {
           batLoop++;
           }
      }
      

      Log:

      send: 8-8-0-0 s=255,c=0,t=17,pt=0,l=3,sg=0,st=ok:1.5
      send: 8-8-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,st=ok:0
      read: 0-0-8 s=255,c=3,t=6,pt=0,l=1,sg=0:M
      read: 0-0-8 s=255,c=3,t=6,pt=0,l=1,sg=0:M
      sensor started, id=8, parent=0, distance=1
      send: 8-8-0-0 s=255,c=3,t=11,pt=0,l=15,sg=0,st=ok:Energy Meter #8
      send: 8-8-0-0 s=255,c=3,t=12,pt=0,l=3,sg=0,st=ok:1.2
      send: 8-8-0-0 s=1,c=0,t=13,pt=0,l=0,sg=0,st=ok:
      send: 8-8-0-0 s=1,c=2,t=24,pt=0,l=0,sg=0,st=ok:
      read: 0-0-8 s=1,c=2,t=24,pt=0,l=7,sg=0:1388544
      Received last pulse count from gw:1388544
      send: 8-8-0-0 s=1,c=1,t=24,pt=5,l=4,sg=0,st=ok:1390562
      send: 8-8-0-0 s=1,c=1,t=18,pt=7,l=5,sg=0,st=ok:1390.5920
      Battery percent: 138 %
      send: 8-8-0-0 s=1,c=1,t=24,pt=5,l=4,sg=0,st=ok:1392606
      send: 8-8-0-0 s=1,c=1,t=18,pt=7,l=5,sg=0,st=ok:1392.6340
      Battery percent: 112 %
      send: 8-8-0-0 s=1,c=1,t=24,pt=5,l=4,sg=0,st=ok:1394726
      send: 8-8-0-0 s=1,c=1,t=18,pt=7,l=5,sg=0,st=ok:1394.7581
      Battery percent: 111 %
      send: 8-8-0-0 s=1,c=1,t=24,pt=5,l=4,sg=0,st=ok:1396821
      send: 8-8-0-0 s=1,c=1,t=18,pt=7,l=5,sg=0,st=ok:1396.8210
      Battery percent: 112 %
      Battery Average (Send): 100 %
      send: 8-8-0-0 s=255,c=3,t=0,pt=1,l=1,sg=0,st=ok:100
      send: 8-8-0-0 s=255,c=0,t=17,pt=0,l=3,sg=0,st=ok:1.5
      send: 8-8-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,st=ok:0
      read: 0-0-8 s=255,c=3,t=6,pt=0,l=1,sg=0:M
      read: 0-0-8 s=255,c=3,t=6,pt=0,l=1,sg=0:M```
      YveauxY Offline
      YveauxY Offline
      Yveaux
      Mod
      wrote on last edited by
      #2

      @sundberg84 Could it be that the Arduino is woken by the interrupt instead of watchdog timer?
      IIRR the gw.sleep() method returns whether it was woken by interrupt or from the watchdog timer.
      Use this return value to determine if it should go to sleep again, or send a message.

      http://yveaux.blogspot.nl

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

        Didn't know I could do that. How do I get that value ?

        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

        YveauxY 1 Reply Last reply
        0
        • sundberg84S sundberg84

          Didn't know I could do that. How do I get that value ?

          YveauxY Offline
          YveauxY Offline
          Yveaux
          Mod
          wrote on last edited by Yveaux
          #4

          @sundberg84 see api & download on the main page. Section 'sleeping' under 1.5 api
          @hek there's a small error in the api describing the sleep-method. The prototype says it returns a bool, but the description talks about an int.

          http://yveaux.blogspot.nl

          hekH 1 Reply Last reply
          2
          • YveauxY Yveaux

            @sundberg84 see api & download on the main page. Section 'sleeping' under 1.5 api
            @hek there's a small error in the api describing the sleep-method. The prototype says it returns a bool, but the description talks about an int.

            hekH Offline
            hekH Offline
            hek
            Admin
            wrote on last edited by
            #5

            @Yveaux said:

            there's a small error in the api describing the sleep-method. The prototype says it returns a bool, but the description talks about an int.

            Eagle-eye... thanks. The last sleep method returns an int8_t. I'll update the doc.

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

              Noob as i am - but i dont think that will work in my case.

              The interrups triggers a loop every 2-3 minutes, and then sends it back to sleep with this function.
              But this will send it back to another 15 minutes of sleep, and then wake up again after 2-3 minutes.
              This will never trigger my gw.send commands i think or can i substract the 2-3 minutes for each time the interupt triggers the loop @Yveaux ??

              I made a if (pulseCount > oldPulseCount + 2000) { based on my average pulsecount and that works... but I dont like that since that might miss sending each hour.

              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
              • martinhjelmareM Offline
                martinhjelmareM Offline
                martinhjelmare
                Plugin Developer
                wrote on last edited by martinhjelmare
                #7

                If you don't need exact time passed, I think you can use a variant of the sensebender sketch sleep and count implementation, but you only increment the counter if woken after timer and not the interrupt. This will not take into account the time that the arduino is awake, so there will be an error. But if you don't do any have lifting or waiting in the loop, it will probably go to sleep quickly. You want to wait for the pulse count from the gateway if you don't have that, but that should only be needed after first boot, correct?

                If you need exact time passed, I can only see two options:

                Either you turn off the interrupt, and only sleep with timer, and add the sleep time to time awake, measured with millis(). This should give you very accurate time passed.

                The other option is asking the gateway/controller for time. Then subtract last time from current time and you will have time passed.

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

                  @martinhjelmare Time is not an issue.
                  I dont want the node to wake up due to the interrupt.

                  I want the node to sleep for 15 minutes (aprox), and count the interrups.
                  Every 15 min it wakes up and sends the counter.

                  What happens is that it wakes up and run the loop (sends counter) after 1-2 minute probably due to an interrupt waking it up. This will not save enough battery.

                  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
                  • martinhjelmareM Offline
                    martinhjelmareM Offline
                    martinhjelmare
                    Plugin Developer
                    wrote on last edited by martinhjelmare
                    #9

                    Ok, but you said there is an issue with sending it to sleep for 15 minutes every 2-3 minutes. From that I gather you need to know time better, since you can't avoid waking the node if you have an interrupt attached.

                    Would sleeping for 1 minute from timer and counting every wake-up by timer be ok battery-wise? 13 counts will then be ~ 15.6 (+/-) 2.6 minutes, since max time error given by wake-up by interrupt is < 1 minute and you said the interrupts occur on average every 2.5 minutes. 13 / 2.5 = 5.2 minutes. So 13 counts is between 13 and 18.2 minutes. You add a condition that only sends the information after 13 counts. Wake-up by interrupt should only add pulse, check count and go back to sleep for 1 minute if count is not 13. Wake-up by timer checks count and adds +1 to count.

                    How would this compare battery-wise to sleeping on average the 2.5 minutes between interrupts and each wake-up use the radio to ask for time from the controller?

                    1 Reply Last reply
                    0
                    • martinhjelmareM Offline
                      martinhjelmareM Offline
                      martinhjelmare
                      Plugin Developer
                      wrote on last edited by
                      #10

                      Maybe you can avoid waking the node if not using the mysensors sleep function? Out of my knowledge scope, though. Have you read this?

                      http://gammon.com.au/interrupts

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

                        The problem is not sending it to sleep. I use sleep(int interrupt, int mode, unsigned long ms=0) where ms = 900000ms. The intetrupt counts aprox 100-150 interrups before something triggers it back/wakes it (2-3min). I dont know why it wakes here... this happens around 2-3minutes (Its a led/pulse counter for electric).
                        Can it run out of memory -128 bytes or something? Its around there (100-150 interrups) it wakes up.

                        The only thing i want the node to wake from is that 15 minutes timer and not the interrupt.

                        Sleeping 1 min and add counter sounds good, i will try that.
                        @martinhjelmare

                        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

                        AWIA 1 Reply Last reply
                        0
                        • sundberg84S sundberg84

                          The problem is not sending it to sleep. I use sleep(int interrupt, int mode, unsigned long ms=0) where ms = 900000ms. The intetrupt counts aprox 100-150 interrups before something triggers it back/wakes it (2-3min). I dont know why it wakes here... this happens around 2-3minutes (Its a led/pulse counter for electric).
                          Can it run out of memory -128 bytes or something? Its around there (100-150 interrups) it wakes up.

                          The only thing i want the node to wake from is that 15 minutes timer and not the interrupt.

                          Sleeping 1 min and add counter sounds good, i will try that.
                          @martinhjelmare

                          AWIA Offline
                          AWIA Offline
                          AWI
                          Hero Member
                          wrote on last edited by
                          #12

                          @sundberg84 Are u sure you are setting the time to 900000ms, using 900000UL? It took me a long time to find out that large (long/ unsigned long) need explicit type specification

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

                            @AWI Thanks - this is pretty much a copy from my other sketches and it works there.
                            But no harm changing it - i will do that.

                            You see my code above - but im pretty sure the interrups wakes the node up.

                            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
                            • martinhjelmareM Offline
                              martinhjelmareM Offline
                              martinhjelmare
                              Plugin Developer
                              wrote on last edited by
                              #14

                              I think the problem might be that you try to combine the attachInterrupt function that should call an ISR and the mysensors sleep function. The mysensors sleep function handles alot of the required interrupt function logic for you. If you use the attachInterrupt function you have to do that yourself. I might be wrong though, I haven't tried this myself. Maybe @hek can comment?

                              For example the following link says that you have to detach the interrupt inside the ISR to not have it trigger again.
                              http://playground.arduino.cc/Learning/ArduinoSleepCode

                              According to this page, all external interrupt types wake the processor. That's also one of the main points with an interrupt; to be able to wake the processor from sleep.
                              http://gammon.com.au/interrupts

                              1 Reply Last reply
                              2
                              • martinhjelmareM Offline
                                martinhjelmareM Offline
                                martinhjelmare
                                Plugin Developer
                                wrote on last edited by
                                #15

                                I suggest using the mysensors sleep function and checking the return value, as @Yveaux suggested, to determine if onPulse() should be called (after interrupt wake-up), or if it's a timer wake-up.

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

                                  Yea, now I understand how to use that!! I will do that, thats probably the best... will try!

                                  Thanks alot everybody involved! @martinhjelmare @AWI @Yveaux

                                  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
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  23

                                  Online

                                  11.7k

                                  Users

                                  11.2k

                                  Topics

                                  113.0k

                                  Posts


                                  Copyright 2019 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