Leaving sleep() with interrupt, prevents going back to sleep() properly



  • I'm trying building a sensor using the standard motion sketch but are having some issues.

    1. After waking up from sleep without any interrupts triggering, sleep is entered and immediately exited. In the next loop, sleep is entered correctly.
    2. The wake-up seems to trigger the PIR or at least do something with the interrupt pin after sleep. The sensor is modded to only send a very short output.

    In the sketch I have added some debug prints where each loop() starts with a "---->" and then 5 reads of the tripped state with a delay(100) in-between. I have also commented out the send() part for a less verbose log.
    In the log bellow the sensor is coming out of a 30s sleep with no interrupts triggering. The sensor seems to trigger after 100ms and then resumes to un-triggered and the node goes to sleep to immediately wake up. Then it goes back to sleep as it should...
    My main concern is the instant wake-up after sleep since I have seen it in my fire alarm hack. Is it a MYS bug or something with my mini-pros?
    [MYS 2.1.1, Arduino 1.8.1]

    ...
    Tripped: 0
    13207 MCO:SLP:MS=30000,SMS=0,I1=1,M1=3,I2=255,M2=255
    13213 MCO:SLP:TPD
    13217 MCO:SLP:WUP=-1
    --->
    Tripped: 0
    Tripped: 1
    Tripped: 1
    Tripped: 1
    Tripped: 1
    --->
    Tripped: 1
    Tripped: 0
    Tripped: 0
    Tripped: 0
    Tripped: 0
    14024 MCO:SLP:MS=30000,SMS=0,I1=1,M1=3,I2=255,M2=255
    14032 MCO:SLP:TPD
    14034 MCO:SLP:WUP=1
    --->
    Tripped: 0
    Tripped: 0
    Tripped: 0
    Tripped: 0
    Tripped: 0
    14440 MCO:SLP:MS=30000,SMS=0,I1=1,M1=3,I2=255,M2=255
    14446 MCO:SLP:TPD
    --->
    Tripped: 0
    ...
    

  • Mod

    @Efflon Cheap PIR sensors sometimes trigger just by fluctiations on the power supply. Waking the MCU or sending a wireless message can be enough to create a false trigger...
    You could try powering your sensor directly from a pair of AA batteries (no step-up/-down converter) and see if things improve.



  • @Yveaux Great tip but in this case I have tried the PIR with "plain" arduino code and with mysensors. To explain even better, this is the only change I have introduced compared to the online example:

    void loop()
    {
        // Read digital motion value
        bool tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;
    
        Serial.print("tripped: ");
        Serial.println(tripped);
        //send(msg.set(tripped?"1":"0"));  // Send tripped value to gw
        
        // Sleep until interrupt comes in on motion sensor. Send update every two minute.
        sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), RISING, SLEEP_TIME);
    }
    

    Sleep is triggeered by RISING since change triggers both in up and down of the sensor. The log is like this when no trigger occurs:

    4950 TSF:MSG:READ,0-0-2,s=255,c=3,t=27,pt=1,l=1,sg=0:1
    4956 MCO:PIM:NODE REG=1
    4960 MCO:BGN:STP
    4962 MCO:BGN:INIT OK,TSP=1
    tripped: 0
    4964 MCO:SLP:MS=30000,SMS=0,I1=1,M1=3,I2=255,M2=255
    4970 MCO:SLP:TPD
    4972 MCO:SLP:WUP=1
    tripped: 1
    4974 MCO:SLP:MS=30000,SMS=0,I1=1,M1=3,I2=255,M2=255
    4980 MCO:SLP:TPD
    # Here it actually sleeps 30s
    4982 MCO:SLP:WUP=-1
    tripped: 0
    4984 MCO:SLP:MS=30000,SMS=0,I1=1,M1=3,I2=255,M2=255
    4990 MCO:SLP:TPD
    4993 MCO:SLP:WUP=1
    tripped: 1
    4995 MCO:SLP:MS=30000,SMS=0,I1=1,M1=3,I2=255,M2=255
    5001 MCO:SLP:TPD 
    # Here it actually sleeps 30s
    

    So after a long sleep it wakes up, then enters sleep and interrupt is somehow triggered and the pinout reads triggeered. The next long sleep is the same..
    If i add a pulldown resistor from 3 to GND and disconnects the sensor pin after a -1 (wakeup) it enters sleep properly. I'm a complete n00b in electronics but to me it seems like some sort of signal bouncing after wakup. Then sensor is powered by vcc...


  • Mod

    @Efflon pir trigger won't bounce. mechanical switches typically do.
    The sleep() function returns a boolean, telling you if the sensor woke by trigger or timeout. You could use that info instead of reading it yourself and maybe introduce some delay (e. G. 10 sconds) to have a dead time before going to sleep again.



  • @Yveaux Introducing a delay() causes the interrupt to to be triggered immediately with a triggered:0 , with a wait() same thing except triggered: 1 . Super strange imho...



  • @Yveaux I finally did what you said. I powered the sensor by it self and have just GND and input connected. Now I don't get a false trigger after a sleep! Is there anything I could do to prevent this, a capacitor or something?

    After a trigger, it seems impossible to enter sleep no matter how long I delay etc. It always takes one sleep() with a immediate wake up before going to sleep. Is there someway to clear the interrupt flag?


  • Mod

    @Efflon what mysensors version do you use? There are some fixes in this area for 2.1.0



  • @Yveaux I'm using the latest 2.1.1 . The retriggering is possible to handle in the code and all seems possible to run on batteries so I am happy.


Log in to reply
 

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.