How to wake up the lopy with the accelerometer
I'm new with pycom product, I brought a Lopy and a Pytrack for a project. I'd like to know if there is a way to use sleep mode and the accelerometer in the same time ? I'd like to wake up the lopy when I have a high acceleration (6g by example) and send the data on my LorAwan network.
Is it possible ? If not, I'll use an other board with accelerometer to use a interrupt pin of the lopy board.
Thank you by advance for your help !
@Flavio-Frattini Yes I did (sorry for delay in replying)
as said before the code should only call machine.pin_sleep_wakeup once with the setup. Therefore it can either be used to wakeup with the button or the accellerometer, but can't use both.
#Enable interrupts from accelerometer (P13) and button (P14) JMCD: bug report, only use one of these lines, subsequent calls overwrite earlier setup machine.pin_sleep_wakeup(['P13'], mode=machine.WAKEUP_ANY_HIGH, enable_pull = False) # enable activity and disable inactivity interrupts, using the default callback handler py.setup_int_wake_up(True, False) # set the acceleration threshold to 500mG (0.5G) and the min duration to 200ms acc.enable_activity_interrupt(500, 200)
@johnmcdnz Hi, have you achieved any progress with the accelerometer?
I think I agree with your analysis that the call to machine.pin_sleep_wakeup overwrites previous settings.
I'm experimenting also using the example code of the accelerometer.
As provided, the code reliably wakes on button press. But doesn't wake on movement.
If the line for setting up the wakeup for the button P13 is removed. the accellerometer does now wake up!
I'm continuing to work on this and if/when I get reliable code I'll post it.
@CeylordZ I've never used light sleep, so I don't really know how it interacts with the rest (i.e. if
machine.pin_sleep_wakeupactually affects getting out of light sleep or not).
BTW, going very very quickly through the
pin_sleep_wakeupsource and the Espressif docs, I believe the code from @railmonitor above has an issue: it seems to me that each call to
pin_sleep_wakeupwill "replace" all previous settings, so you need to have all pins in the array in the same call, and they should all use the same mode.
I did not actually check or test it, but that's my understanding from a quick glance through the code and docs.
As for the wakeup reason, another quick glance through the source also makes me think it is only initialised (in the firmware) during the boot process, so only after a deep sleep, not a light sleep. This one should be easily fixable in the firmware (adding another separate call I would think, there's probably a reason the value is stored rather than returned directly from the ESP-IDF in the current function).
@jcaron For now I received a 2000 mAh battery but it's only for testing I know it won't be enough for my application.
I will study the possibility to use the ULP co-processor, maybe it is the only way for me to achieve my project, I'll check the LIS2HH12's datasheet in details, maybe there is something interesting I didn't see.
Something I don't understand is the machine.sleep(), when I use it, the program stop working during the time of the sleep then it wake up and execute the rest of the program so I'm not able to let it do something during the machine.sleep.
Furthermore, with @railmonitor's code, interruptions works only with the button but not with the accelerometer, I don't know why...
And during the lightsleep, when I push the putton, the wake-up reason is (0,none)
This post is deleted!
@CeylordZ What kind of battery are you planning to use? To last 2 months with, say, a 2500 mAh battery, you need to draw an average of 1.75 mA or less. Light sleep supposedly gets you down to 3.5 mA, but:
- it should be checked in actual conditions, with the accelerometer enabled in the right mode, etc.
- you have to consider that whenever it wakes up and sends stuff over LoRa, it will draw a lot more, so this will raise your average. Depending on how frequently you wake up and how long you do, this could increase your average just a tiny bit or a a whole lot.
- I'd be curious to see how light sleep and LoRa combine. Last time I looked into it (it was a while ago, though), as soon as LoRa is configured, it'll have a 1 kHz polling loop set up which will kill light sleep performance.
So either you'll have to pack a larger battery (> 5000 mAh, probably more, which becomes quite a large thing), or light sleep won't do. But deep sleep probably won't let you get the value of the acceleration that triggered the wake up. Again, I recommend you check the accelerometer's data sheet and application notes, it might actually be possible with correct configuration of the chip.
An alternative might be to use the ULP co-processor, but AFAIK using this on the Pycom modules is still work in progress.
@jcaron It's problematic, My project would be to put the lopy-pytrack in a case and to let it sleep until it detect a high shock, at this moment it will send the data (with the intensity of the shock) in LoRa then it will go to sleep again until the next wake-up.
But it will impossible to recharge the Lipo battery before 2-3 months so, it's necessary that the lopy consume as little as possible.
@railmonitor how does the machine.sleep() works compared to the deepsleep ?
I know that the deep sleep almost shut down the processor and restart the board when it's waking up but not the machine.sleep()
@jcaron Correct, i am using machine.sleep() which is the lightsleep mode, because, as you state, wakeup from deepsleep is rather slow due to boot time etc.. and not feasible if you want to wake and take measurements immediately.
@CeylordZ It's not really a loop in the traditional sense, you are just telling it to do your readings when there is activity on the accelerometer.
@railmonitor The code you posted above is actually missing the actual "deep sleep" part...
@CeylordZ If you want the actual acceleration value, I don't think you'll be able to use deep sleep with that. The accelerometer will wake up the LoPy, but by the time the LoPy has woken up and is ready to query the accelerometer, values will have changed. I don't think there's any way to tell the accelerometer to "keep" the value that caused the wake up, though you should check the chip's data sheet and application notes, there may be something in there.
IIRC the accelerometer has a buffer used to deliver data to the LoPy, but even at the lowest data rate I don't think it's large enough to last for the wake up time.
If you want the full data, you'll have to keep the LoPy awake, which will indeed use quite a bit of power.
What is your actual use case?
But if I'm using a loop where I read the acceleration, I can't sleep the board, am I wrong ?
And what is the "append" function ?
@CeylordZ You could encapsule the whole reading sequence in a loop:
if acc.activity(): Read, append etc..
@railmonitor Thanks !
But with method, I have to listen the accelerometer even if it doesn't measure any accelerations. I think it will consume a lot of current for nothing.
If it's possible I'd like to wake up only when the board is shocked (by a high acceleration) and store this acceleration in a file.
@CeylordZ Sure. There are several way to do that. An "elegant" way to do it, if you want several measurements over short intervals is to setup a timer Alarm, to call a method that takes readings from the accelerometer and stores them in a array or whatever you like. The following code, assuming you have setup the code from previous example should work, we are setting up a timer, to take readings every 10 ms, and cancelling the alarm after 5 seconds:
measurement =  #array #method for alarm callback def sample(): acc_val = acc.acceleration() measurement.append(acc_val) alarm = Timer.Alarm(lambda x: sample(), ms = 10, periodic = True) time.sleep(5) alarm.cancel()
Then you just add these lines right after the sleep, Hope it helps!
@railmonitor And is it possible to store the data from the accelerator when it wake up ? Or I have to keep it awake if I want to measure the acceleration ?
I want to know how strong is the acceleration when it wake up the board (2g, 4g or 8g for example...).
@railmonitor that's what I looked for, thank you !
Yes, that is definitly possible. Have a look a the following code, it is written for FiPy with Pysense board, so there might be some differences regarding libraries / pins but should work when corrected:
from pysense import Pysense from LIS2HH12 import LIS2HH12 import pycom import time import machine py = Pysense() #Create Pysense object acc = LIS2HH12() #Create accelerometer object if pycom.heartbeat() == True: #turn off blinking LED pycom.heartbeat(False) wakeup_reason = machine.wake_reason() #Enable interrupts from accelerometer (P13) and button (P14) machine.pin_sleep_wakeup(['P13'], mode=machine.WAKEUP_ANY_HIGH, enable_pull = False) machine.pin_sleep_wakeup(['P14'], mode=machine.WAKEUP_ALL_LOW, enable_pull = False) # set the acceleration threshold to 2000mG (2G) and the min duration to 200ms acc.enable_activity_interrupt(2000, 160) if(wakeup_reason == machine.PIN_WAKE): pycom.rgbled(0xffff00) #yellow time.sleep(2) print("Turning on Red LED for 2 sec") pycom.rgbled(0xff0000) #red time.sleep(2) print("Enter lightsleep mode for 5 sec") machine.sleep(5000) #Value in ms print("Pysense Wakeup reason is " + str(wakeup_reason)) if acc.activity(): pycom.rgbled(0x800080) #purple else: pycom.rgbled(0x0000ff) #blue