Skip to content

TIT8/shelly_button_esp32_arduino

Repository files navigation

Arduino code for Shelly's toggle button

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.


Schematich


If you have a Shelly in your wall and want to try out the MQTT connection without purchasing or wiring up the micontroller, I've saved a prototype of the project on Wokwi.

❗ The code should already handle reconnections if something goes wrong.

Prerequisities

  1. Arduino IDE to code, compile and upload the code to the ESP32 Wroom board.
  2. Remember to add the CP2102 driver to connect old ESP32 development board (with CP2102 as USB-UART bridge).
  3. ArduinJson, pubsubclient and ArduinoOta libraries.
  4. How to setup a local broker in a Docker container.

Future

  • I will make the ESP32 aware of the ligth bulb status subscribing to the rpc channel/topic where the Shelly's relay automatically publish the status (on or off).   [DONE ✔️]

  • I will make possible an initial speech recognition through a microphone and a Keras neural network (via Eddge Impulse and Colab for training it), like in this video from DigiKey.   [DONE ✔️]

  • I won't use the official ESP-IDF with the built-in MQTT library (or via Cesanta Mongoose MQTT library) because is too much power for this project.   [DONE ✔️]

  • Using Kicad, directly create a PCB with push button, microphone + opamp (considering noise constraint) and microcontroller on the same board.

Why use C++ and not Micropython or Rust?

The answer lies in the low level of the libraries used. Python will depend on low level C code, while Rust can go to bare metal, but it's by far more tedious. Try doing this on Rust, I find it better on C++.

Then the library support for microcontroller is not yet ready as in C/C++.

Take a look also at the ESP-IDF version of this project. Don't look too much at it, otherwise you won't never come back to Arduino 😍 🙈.

Shelly options

Screenshot (31)


Why don't you use Bluetooth ⁉️

About

ESP32 code for communicating with Shelly devices via MQTT

Resources

License

Stars

Watchers

Forks

Contributors 3

  •  
  •  
  •