Controlling Shelly “Home Automation” Relays From an ESP32 Using BLE
by Rob__S in Design > Software
43 Views, 0 Favorites, 0 Comments
Controlling Shelly “Home Automation” Relays From an ESP32 Using BLE

This project uses an ESP32 (tested with both a Standard-ESP32[NodeMCU] , and also an ESP32S3 [Waveshare ESP32S3-zero]) to directly turn a Shelly Relay on and off (ie. without needing any internet connection or registration).
Supplies


It should work with any BLE enabled Shelly Relay, but I was using the "Shelly 1 Mini Gen3” and "Shelly 1 Mini Gen4” (both with Shelly firmware version 1.62 )
(NB. This unit is 110-240Vac powered, so only connect it if your happy working with such voltages.)
In normal operation, this Shelly device itself uses about 0.6W at 240V, but also note that the unit does get warm in use (even when it’s relay is turned off).
General "Shelly 1 Mini” Unit Setup


To factory-reset the "Shelly 1 Mini Gen3 or Gen4" (resets hotspot etc):
- Press+Hold the small-button (on back of the Shelly) for 10seconds.
To update the Shelly-firmware (need to have internet-see below):
- Connect to "Shelly-hotspot",
- Go-to "http://192.168.33.1/"
- Select "Settings/Firmware/Check-For-Updates".
To Connect-Shelly-to-internet

Select "Settings/Wi-Fi”
under “Wi-Fi 1settings" click on “Enable Wi-Fi"
then select SSID/pw & "Save_settings".
(You can delete this again after the above firmware-update.)
To Add-a-password to the Aforementioned "Shelly-hotspot"

Select "Settings/Access_Point/Access_Point_settings"
click on “Password protected network”
enter a pw and click "Save_settings".
Specific "Shelly 1 Mini” Setup to Work With the ESP32

Note that for the below application we DON'T have to set any Shelly "Actions" or "Schedules" or "Scripts"
If necessary: Turn-on the Shelly’s BLE (Bluetooth) & RPC under “Setting/Bluetooth” -> enable both.
Security (or Lack Of)

Note that by default the Shelly Devices do not have any security on which BLE devices can send ON/OFF commands (ie. they will let any ESP32 connect and turn it on/off).
To enable security you need to add an Authentication- password under “Settings/Authentication”. However, although then the above "Shelly-hotspot" can be accessed by just entering the password, the BLE device needs to authenticate itself as detailed in “https://github.com/epicRE/shelly-smart-device”. But the github example only details python code, and I have not currently implemented this in the below arduino example code!
Shelly Diagnostics

To see Shelly-diagnostic-info: click on "Diagnostics" and then “Enable”
(click on "CLEAR CONSOLE" as required).
The above image shows what the Diagnostics reports for a successful Turn-On and then Turn-Off.
Arduino Code
The attached Arduino-Code (file "ESP32_Shelly_BLE_Controller_v28July25.ino") demonstrates turning the Relay on and off, which I was testing using the small Neon-Bulb (which included a resistor to enable it to work at 240Vac) – as shown in the above photos.
Note that these “Shelly Home Automation Relays do NOT send their ServiceUUIDs out in the initial BLE Advertising packet, therefore the normal arduino “advertisedDevice.haveServiceUUID()” always returns false.
For these devices the ServiceUUID is sent after the initial-Advertising-Packet, in a later "Scan Responce Packet". However, I cant currently find a way to access this in arduino (eg. with "BLESCANPARAM" or similar).
So instead the below code uses the Device's MAC address (which IS sent in the initial BLE Advertising packet) to identify the Device you want (and then just connect to it and starts looking for the CharacteristicUUIDs it wants).
The first time you run the ESP software it prints out the Advertising packets it finds, eg.
BLE *** Device found: Name: Shelly1MiniG3-CCxx, Address: cc:xx:xx:xx:xx:xx, manuf.data: a9xx, rssi: -61
To ensure you only connect to the correct Shelly, you will need to add the MAC-address or Unique-Name of your Shelly device into your ESP software in the below “const chars”:
// const char BLE_Device_Advertised_Text_Wanted[]="Shelly1MiniG";
const char BLE_Device_Advertised_Text_Wanted[]="cc:xx:xx:xx:xx:xx"; // option_1 or
// const char BLE_Device_Advertised_Text_Wanted[]="Shelly1MiniG 3-CCxxxxxxxxx"; // option_2 or…
(Note that for some reason the "cc:xx:xx:xx:xx:xx" MAC-address seems to be a better string to use, as the “Shelly1MiniG3- C C…” string appears to be corrupted more often than the MAC-address. Also the newer v1.6.2 Shelly- Firmware appears to more reliably send an uncorrupted "Shelly1MiniG…” advertising packet than the original factory-installed firmware.)
Then re-compile and re-flash your ESP software and the ESP32 will connect to your Shelly-Relay and start turning it ON and OFF.