Hi – my first post here!
I’m trying to design a setup which updates my raspberry Pi LAMP server with the orientation of my garage door. I’ve got a couple of mercury ’tilt’ switches, which would do the job of triggering the pins. I also toyed with the idea of using the hall sensor.
However, I can’t work out how to use either of these methods whilst also keeping my ESP32 in deep sleep mode? I’d like the device to run on battery power.
Ideally, the board should only wake up and post the new door orientation when it changes.
As I understand it:
- Hall sensor cannot be used to wake from deep sleep
- Using one mercury tilt switch will be ON when the door is open and OFF when it is closed, however I don’t know how to awaken from deep sleep when there is a CHANGE in pin state from both high to low OR low to high?
- I thought perhaps using two tilt switches at right angles to each other might do it, however this means that one switch is always high and the other is always low. As one switch is always HIGH then, from what I have tried, this means the board is constantly woken from deep sleep.
I’m a novice at this, and have a feeling I’m taking the wrong approach altogether – any thoughts?
Thanks in advance,
Chris
Why can’t a Hall effect Sensor be used? This article shows a positional sensor circuit.
With your tilt switches you could use both ext0 and ext1 external wakeup (Documentation).
Hi Chris.
You can use ext0 as a wake up source.
esp_sleep_enable_ext0_wakeup(GPIO_NUM_X, level)
This function accepts as first argument the pin you want to use, in this format GPIO_NUM_X, in which X represents the GPIO number of that pin.
The second argument, level, can be either 1 or 0. This represents the state of the GPIO that will trigger wake up.
You can set the level to 0 for one reed switch and to 1 for the other reed switch. This way, the ESP will wake up when one of reed switches change state.
Alternatively, you can use ext1. To use this wake up source, you use the following function:
esp_sleep_enable_ext1_wakeup(bitmask, mode)
This function accepts two arguments:
- A bitmask of the GPIO numbers that will cause the wake up;
- Mode: the logic to wake up the ESP32. It can be:
- ESP_EXT1_WAKEUP_ALL_LOW: wake up when all GPIOs go low;
- ESP_EXT1_WAKEUP_ANY_HIGH: wake up if any of the GPIOs go high.
So, I think this is not the best approach.
Use ext0 for both of your reed switches, but set a different level for each of them.
I hope this helps.
Regards,
Sara
Thanks Sara – this looks promising! I will give it a try and let you know how I get on! 🙂
@Steve Thanks for the post. I’d love to use the Hall Effect sensor with this project, but I don’t believe it can (easily) be used to wake from Deep Sleep mode? I saw a github project which tackles this problem somewhere, but it looked a bit technical for me.
Hi again (sorry for slow reply).
Ok, the problem with above is that with the reed switches they are usually in HI or LOW state for a long period – not momentarily. So, as one (or the other) of the two switches will almost ALWAYS be on then that HIGH reading will always wake the ESP32 immediately.
So, I was wondering: is it possible to switch interrupt pin on on alternate deep sleeps?
Example:
- ESP32 is in deep sleep waiting for Pin X interrupt
- Pin X wakes ESP32 because switches to ‘persistent’ HIGH (Y turns to persistent LOW)
- Code runs (sensor X status posted to server… this is my next challenge!)
- Now Pin X interrupt is turned OFF and Pin Y interrupt is turned ON
- ESP32 goes back to deep sleep, now waiting for Pin Y interrupt
… and so on.
Is it possible to do this – switching between different pins as wake source? I wonder whether a conditional function can be ran and the callback function will be able to switch the interrupt pins?
Perhaps a value can be stored in flash memory which will ensure the alternate function is called each time the ESP32 wakes?
Hi Chris.
I would suggest that: save a value permanently in the flash memory and read it every time the ESP32 wakes up and change it if necessary.
Regards,
Sara