Node with Interrupt, sleep and batteries
-
Didn't know I could do that. How do I get that value ?
-
Didn't know I could do that. How do I get that value ?
-
@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. -
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.
-
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.
-
@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.
-
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?
-
Maybe you can avoid waking the node if not using the mysensors sleep function? Out of my knowledge scope, though. Have you read this?
-
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 -
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@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
-
@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.
-
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/ArduinoSleepCodeAccording 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 -
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.
-
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