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. Development
  3. Sensor not sleeping properly when Gateway is down?

Sensor not sleeping properly when Gateway is down?

Scheduled Pinned Locked Moved Development
6 Posts 3 Posters 1.6k Views 4 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.
  • T Offline
    T Offline
    Tinimini
    wrote on last edited by
    #1

    I'm starting a new thread about this as previously I hijacked someone else's old thread.

    I'm currently playing around with MySensors and the first sensor I'm trying to build using it is a door sensor. It's battery powered and it's working just fine (using about 6uA while sleeping, so batteries should last a good while). But when I don't have my Gateway (a Raspberry Pi Zero W with MQTT Gateway) online, the power consumption seems to stay pretty high (3mA or so). So it seems that the sensor is not sleeping properly, but instead tries to reconnect to the Gateway all the time?

    This is my sketch:

    // Enable debug prints to serial monitor
    //#define MY_DEBUG
    
    // Set how long to wait for transport ready in milliseconds
    #define MY_TRANSPORT_WAIT_READY_MS 3000
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    
    // Set this nodes id. Needs to be unique between nodes (value = 1-254)
    #define MY_NODE_ID 102
    
    #include <SPI.h>
    #include <MySensors.h>
    
    #define CHILD_ID   3
    #define DOOR_PIN   3     // Arduino Digital I/O pin for button/reed switch
    #define SLEEP_TIME 3000 // Sleep time between heartbeats (seconds * 1000 ms)
    
    int oldValue = -1;
    
    MyMessage msg(CHILD_ID, V_TRIPPED);
    
    void setup() {
      pinMode(DOOR_PIN, INPUT);
    }
    
    void presentation() {
      present(CHILD_ID, S_DOOR);
    }
    
    void loop() {
      int value = digitalRead(DOOR_PIN);
    
      if (value != oldValue) {
         send(msg.set(value == HIGH ? 1 : 0));
         oldValue = value;
      }
    
      wait(10);
    
      Serial.println("Going to sleep now...");
      smartSleep(digitalPinToInterrupt(DOOR_PIN), CHANGE, SLEEP_TIME);
    }
    

    I tried enabling debug and this is what I get in the loop:

    30812 MCO:SLP:MS=0,SMS=0,I1=0,M1=1,I2=1,M2=1
    30818 !MCO:SLP:TNR
    Going to sleep now...
    

    And yes, I know it's not very efficient to only sleep for 3 seconds, that's there only for testing this thing out.

    Nca78N 1 Reply Last reply
    0
    • T Tinimini

      I'm starting a new thread about this as previously I hijacked someone else's old thread.

      I'm currently playing around with MySensors and the first sensor I'm trying to build using it is a door sensor. It's battery powered and it's working just fine (using about 6uA while sleeping, so batteries should last a good while). But when I don't have my Gateway (a Raspberry Pi Zero W with MQTT Gateway) online, the power consumption seems to stay pretty high (3mA or so). So it seems that the sensor is not sleeping properly, but instead tries to reconnect to the Gateway all the time?

      This is my sketch:

      // Enable debug prints to serial monitor
      //#define MY_DEBUG
      
      // Set how long to wait for transport ready in milliseconds
      #define MY_TRANSPORT_WAIT_READY_MS 3000
      
      // Enable and select radio type attached
      #define MY_RADIO_NRF24
      
      // Set this nodes id. Needs to be unique between nodes (value = 1-254)
      #define MY_NODE_ID 102
      
      #include <SPI.h>
      #include <MySensors.h>
      
      #define CHILD_ID   3
      #define DOOR_PIN   3     // Arduino Digital I/O pin for button/reed switch
      #define SLEEP_TIME 3000 // Sleep time between heartbeats (seconds * 1000 ms)
      
      int oldValue = -1;
      
      MyMessage msg(CHILD_ID, V_TRIPPED);
      
      void setup() {
        pinMode(DOOR_PIN, INPUT);
      }
      
      void presentation() {
        present(CHILD_ID, S_DOOR);
      }
      
      void loop() {
        int value = digitalRead(DOOR_PIN);
      
        if (value != oldValue) {
           send(msg.set(value == HIGH ? 1 : 0));
           oldValue = value;
        }
      
        wait(10);
      
        Serial.println("Going to sleep now...");
        smartSleep(digitalPinToInterrupt(DOOR_PIN), CHANGE, SLEEP_TIME);
      }
      

      I tried enabling debug and this is what I get in the loop:

      30812 MCO:SLP:MS=0,SMS=0,I1=0,M1=1,I2=1,M2=1
      30818 !MCO:SLP:TNR
      Going to sleep now...
      

      And yes, I know it's not very efficient to only sleep for 3 seconds, that's there only for testing this thing out.

      Nca78N Offline
      Nca78N Offline
      Nca78
      Hardware Contributor
      wrote on last edited by
      #2

      @Tinimini have you checked with longer sleep duration ?
      Can you provide the log after the "going to sleep now" line ?

      Because in the configuration of the library you can set the duration during which it will try to connect to gateway before going to sleep and default duration is 10s :
      MY_SLEEP_TRANSPORT_RECONNECT_TIMEOUT_MS 10000 Timeout (in ms) to re-establish link if node is put to sleep and transport is not ready.

      From what I see in the code after reconnexion process is finished it will calculate the remaining sleeping time and only sleep for the remaining duration. If your sleep time is less than the reconnect timeout it will not sleep.

      	// Do not sleep if transport not ready
      	if (!isTransportReady()) {
      		CORE_DEBUG(PSTR("!MCO:SLP:TNR\n"));	// sleeping not possible, transport not ready
      		const uint32_t sleepEnterMS = hwMillis();
      		uint32_t sleepDeltaMS = 0;
      		while (!isTransportReady() && (sleepDeltaMS < sleepingTimeMS) &&
      		        (sleepDeltaMS < MY_SLEEP_TRANSPORT_RECONNECT_TIMEOUT_MS)) {
      			_process();
      			sleepDeltaMS = hwMillis() - sleepEnterMS;
      		}
      		// sleep remainder
      		if (sleepDeltaMS < sleepingTimeMS) {
      			sleepingTimeMS -= sleepDeltaMS;		// calculate remaining sleeping time
      			CORE_DEBUG(PSTR("MCO:SLP:MS=%lu\n"), sleepingTimeMS);
      		} else {
      			// no sleeping time left
      			return MY_SLEEP_NOT_POSSIBLE;
      		}
      	}
      
      1 Reply Last reply
      0
      • T Offline
        T Offline
        Tinimini
        wrote on last edited by
        #3

        Okay, that seems to explain this. I changed the sleeping time to 20 seconds and sure enough, after about 10 seconds the current draw dropped down to ~7uA. So looks like I'm good now.

        So what did I learn from this? Well... I better start reading the source code for MySensors :) Either that, or write my own lib so I know what is going on :)

        Thanks for the help!

        mfalkviddM 1 Reply Last reply
        1
        • T Tinimini

          Okay, that seems to explain this. I changed the sleeping time to 20 seconds and sure enough, after about 10 seconds the current draw dropped down to ~7uA. So looks like I'm good now.

          So what did I learn from this? Well... I better start reading the source code for MySensors :) Either that, or write my own lib so I know what is going on :)

          Thanks for the help!

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

          @Tinimini great that you found a solution. It might not be necessary to read the source code. Reading the documentation might be easier. MY_SLEEP_TRANSPORT_RECONNECT_TIMEOUT_MS is documented at https://www.mysensors.org/download/sensor_api_20#configuration

          Nca78N 1 Reply Last reply
          1
          • mfalkviddM mfalkvidd

            @Tinimini great that you found a solution. It might not be necessary to read the source code. Reading the documentation might be easier. MY_SLEEP_TRANSPORT_RECONNECT_TIMEOUT_MS is documented at https://www.mysensors.org/download/sensor_api_20#configuration

            Nca78N Offline
            Nca78N Offline
            Nca78
            Hardware Contributor
            wrote on last edited by
            #5

            @mfalkvidd said in Sensor not sleeping properly when Gateway is down?:

            @Tinimini great that you found a solution. It might not be necessary to read the source code. Reading the documentation might be easier. MY_SLEEP_TRANSPORT_RECONNECT_TIMEOUT_MS is documented at https://www.mysensors.org/download/sensor_api_20#configuration

            Exactly, read the doc ! It just takes a few mins for an overview of the config keys available.
            Then by curiosity you can just search for the config key in the code to see how it was used, but that's in no way mandatory.

            1 Reply Last reply
            0
            • T Offline
              T Offline
              Tinimini
              wrote on last edited by
              #6

              Oh yeah... Read the docs.. Yes, I probably should do that too :) Thanks!

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


              11

              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