ESP32 module wake on touch, wake on ext1
Hi there,
I'm running MicroPython v1.9.4-409-g434975def and have successfully used a Pin irq to wake from deep sleep and can confirm the wake reason:
import machine
from machine import Pin, TouchPad, deepsleep
import time
wake = Pin(14, mode = Pin.IN, pull = Pin.PULL_DOWN)
wake.irq(trigger=Pin.WAKE_HIGH, wake = machine.DEEPSLEEP)
time.sleep(5)
deepsleep(5*1000)
after bringing Pin 14 line up to 3V3
Reset cause: DEEPSLEEP_RESET
Wake reason: PIN_WAKE/EXT0_WAKE
Now I'm trying to do the same but with a TouchPad input:
from machine import Pin, TouchPad, deepsleep
import time
import esp32
wake = Pin(14, mode = Pin.IN)
touch = TouchPad(wake)
touch.config(500)
esp32.wake_on_touch(True)
time.sleep(5)
deepsleep(5*1000)
(after 5 seconds and touching/untouching wire to Pin 14)
Reset cause: DEEPSLEEP_RESET
Wake reason: TIMER_WAKE
The range of my touch.read() values are around 700 when untouched, and down to around 100 when touched. I've tried adjusting the touch.config to various settings but I cannot get my ESP32 to wake from touch.. only from timer in this mode.
Lastly,
I've tried using wake_on_ext1
import machine
from machine import Pin, TouchPad
import time
import esp32
wake = Pin(14, mode = Pin.IN, pull = Pin.PULL_DOWN)
esp32.wake_on_ext1(pins = [wake], level = Pin.WAKE_HIGH)
time.sleep(5)
deepsleep(5*1000)
But this resets immediately regardless of whether I bring Pin 14 high or not (not in touch mode, mind you).
Reset cause: DEEPSLEEP_RESET
Wake reason: EXT1_WAKE
I've browsed through the esp32 module code, but I'm not sure if any of the wake modes work?
Any ideas?
ports/esp32:* Implementation of deep sleep, esp32 module, machine.RTC
Note 1: Copied over from the old micropython-esp32 repo.
Note 2: Fixed the 1970/2000 bug in RTC.datetime()
Current API:
Basic sleep
import machine
machine.deepsleep() # Sleep until hard reboot
machine.deepsleep(5000) # Deep sleep for 5 seconds
machine.sleep() is implemented, but currently throws an error. If/when the IDF "light sleep" works, this can be removed.
EXT0 wakeups, using the common Micropython idiom
EXT0 wakes are for a single pin, either high or low level.
This is separate from the IRQ handler; the WAKE_LOW and WAKE_HIGH only setup the EXT0 type wake. Using a value other than WAKE_LOW or WAKE_HIGH will clear the EXT0 wake from that pin.
Additionally, any EXT0 wake will fail if esp32.wake_from_touch(True) has been called, since wake from touch and EXT0 are mutually exclusive in the IDF.
>>> import machine
>>> def h(*args): print(args)
...
>>> p1 = machine.Pin(26)
>>> p2 = machine.Pin(27)
>>> p1.irq(trigger = machine.Pin.WAKE_LOW, wake = machine.DEEPSLEEP)
<IRQ>
>>> p2.irq(trigger = machine.Pin.WAKE_LOW, wake = machine.DEEPSLEEP)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: no resources
>>> p1.irq(trigger=machine.Pin.IRQ_RISING, handler = h) # Clears EXT0 wake
>>> p2.irq(trigger = machine.Pin.WAKE_LOW, wake = machine.DEEPSLEEP) # EXT0 now assigned to p2
<IRQ>
Other changes to machine
machine.reset_cause() has been implemented, though usually returns machine.WDT_RESET since most of the time the IDF doesn't reset properly. Does properly return machine.DEEPSLEEP_RESET when waking from deep sleep.
machine.wake_reason() has been implemented.
The new top-level esp32 module
esp32.wake_on_touch(<True|False>) Configures waking on touch. Throws ValueError('no resources') in the event that EXT0 is already configured, since they are not compatible. For this to work, you need to use machine.TouchPad().config() to configure the threshold; an interrupt handler is not required.
esp32.wake_on_ext0(<Pin>, level = <0 | 1>). Configures the EXT0 wake. This will raise ValueError('no resources') if esp32.wake_on_touch(True) has been called. It will not raise this error if another pin has already been configured, it will simply configure the new pin for EXT0 and deconfigure the old pin. If level is not specified, then it is unchanged, and defaults to 0 (low).
esp32.wake_on_ext1(pins = <sequence of Pin object>, level = <esp32.WAKEUP_ALL_LOW | esp32.WAKEUP_ANY_HIGH> ): As described above, wake on multiple pins, either "all low" or "any high".
The new machine.RTC module
machine.RTC.datetime() and machine.RTC.init() work as documented in mainline MicroPython.
machine.RTC.memory() Without arguments returns the RTC nonvolatile memory, as bytes. To write memory, pass a bytes, bytearray, or str object.