Page 1 of 1

machine missing functions?

Posted: Sun Nov 08, 2020 10:25 pm
by webbhm
I have spent the last week working on a pyboard project to monitor stream environments. So far things are going well with I2C and analog sensors. I got to the point of working on the sleep cycles, and have discovered that there is no machine.wake_reason function in the machine module. I am trying to be sure I don't lock up the board, since after it does data collection is is putting things into a deep sleep, so I want to make sure it only runs this code when woken up via the RTC (if machine.wake_state() == machine.RTC_WAKE)
I had reflashed the board (D series) the other day to make sure it was updated (PYBD-SF2-20200902-v1.13.dfu).

This is what appears in the machine module:

>>> dir(machine)
['__class__', '__name__', 'ADC', 'RTC', 'DEEPSLEEP_RESET', 'HARD_RESET', 'I2C', 'PWRON_RESET', 'Pin', 'SOFT_RESET', 'SPI', 'Signal', 'Timer', 'UART', 'WDT', 'WDT_RESET', 'bootloader', 'deepsleep', 'disable_irq', 'enable_irq', 'freq', 'idle', 'info', 'lightsleep', 'mem16', 'mem32', 'mem8', 'reset', 'reset_cause', 'rng', 'sleep', 'soft_reset', 'time_pulse_us', 'unique_id']

Has the library been change recently, and if so, is there a comparable function? Or, do I need a different update? There doesn't seem to be an equivalent function in pyb.


Re: machine missing functions?

Posted: Mon Nov 09, 2020 7:06 am
by Roberthh
The function you are looking for is machine.reset_cause()

Re: machine missing functions?

Posted: Mon Nov 09, 2020 11:39 am
by pythoncoder
This repo has an alternative, enhanced version of machine.reset_cause() which can identify all possible wakeup reasons on a Pyboard D.

Re: machine missing functions?

Posted: Mon Nov 09, 2020 4:00 pm
by webbhm
I re-read the fine print and noticed that machine.wake_reason is "Availability: ESP32, WiPy." Hence not for the 'D'!
reset_cause is working just fine for what I need.

I like what Peter has for upower, I need to look into it in the future. I am considering an option where I have a magnetic reed switch which would trigger a wifi connection for data download. I also like the ability to set a specific time for the alarm. I want to collect data readings every 20 minutes [currently using RTC().wakeup()]. It looks like with upower I would need to reset the alarm (or at least one) to log on 00, 20 and 40 minutes.
Have you considered an Alarm function to read the current setting? To cycle the alarm I would need to either read the current setting or cache a copy of the value in the BkpRAM so I could read it on the next cycle.

My project is a fork of Oregon University's SmartRock ( ... ckmartRock). I think a pyboard will work better than an Arduino. I need to do some power testing and code clean-up, but so far it is looking good.

Re: machine missing functions?

Posted: Mon Nov 09, 2020 4:58 pm
by pythoncoder
Cacheing the value in bkpram or an RTC register seems a good solution. The registers are specially easy to use if you have no other need for bkpram.