❗ ESP-IDF version of the Arduino repo: https://github.com/TIT8/shelly_button_esp32_arduino
A push button is connected to the ESP32 microcontroller. When its state changes, it will trigger an MQTT publish which will toggle the light controlled by a Shelly plus 1 relay
.
❗Keep in mind that you should either use a capacitor (better) or providing some delay in the code to debounce the push button and filter out spurious changes. Pull down the push button via a 10k resistor, if you don't set the pull-up mode on the input pin.
- For testing I use the Hive MQ public broker.
- For production I use Mosquitto from a Docker container inside my local environment (see the Docker compose file).
❗ The software and the hardware (watchdog timer) can already handle reconnections if something goes wrong.
- Look at the PlatformIO documentation to start.
- If you already have the ESP-IDF installed (via Platformio or not), all the dependencies come with it, so you won't import anything. Simply build and upload 💪.
- An ESP32 board (with IDF, also other versions like S2/S3/C3).
- Remember to add the CP2102 driver to connect old ESP32 development board (with CP2102 as USB-UART bridge).
- How to setup a local broker in a Docker container.
Features | ESP-IDF | Arduino |
---|---|---|
Dependencies | All included in the official SDK |
|
Clear advantage | Extreme control on execution contexts | Portability of code to other Arduino compatible board |
JSON response | Handled via cJSON (well maintained) | Handled via ArduinoJson (wrapper of cJSON) |
MQTT features |
|
|
GPIO pin handling | Same as Arduino, but more control on interrupt ISR and FreeRTOS queue | Easieast to start, you know... ❤️ |
OTA updates | Great flexibility, but difficult to start without strong motivation | Less flexibility, but easy to get the job done |
Memory footprint |
|
|
RTOS | Yes, you see clearly how it works 3 | Yes, but hidden |
Hardware configurations | Easy via the menu config (like STM32CubeMX, less graphic though) | You have to explicitly write the code to configure the peripherals/timers you need |
Feelings | Faster 🚀 | Just works ⭐ |
On the master branch, I'm using task notifications from FreeRTOS to unblock a high-priority (relative to core 1) task responsible for debouncing in the software, despite the use of interrupts (which are disabled and enabled within that task). Therefore, when the button is not in use, the task remains blocked on a type of semaphore, conserving CPU resources. This allows the idle task to run uninterrupted until the button is pressed, ensuring the watchdog timer is consistently reset.
The main thread is more efficient than the one in the V2 branch (which employs busy wait for monitoring button status instead of being notified by interrupts).
In the main task, interrupts are enabled and disabled only on the GPIO pin used and on the core where the task is pinned, akin to a critical section, as outlined in the official documentation. 4
The code handles the scenario where the button is continuously pressed; after a while, the main thread will enter a blocked state. You can refer to the relevant part of the firmware for more details. In this way, I can add other tasks in the future to the same board. Meanwhile, with busy waiting, the time consumed by the main task could be critical for other tasks to execute.
Obviously, putting the ESP32 to sleep until it's awakened by the GPIO would be much more efficient. However, the latency from button press to waking the CPU, connecting to WiFi and MQTT, and sending commands will decrease the responsiveness of the application (on the ESP32, you cannot put the CPU into deep sleep without turning off the radio).
- Useful installation video for Shelly's relays.
- How Shelly handle MQTT connections in the official doc.
Yea, it will be far less power consuming (two devices that talk via BLE, instead of two + broker + router via WiFi). But Shelly cannot receive command via Bluetooth, it sends only informations.
You can make it with I2S MIC + ESP-EYE on Edge Impulse for example. I tried with different boards, take a look here for offline recognition and here for online.