SwitchBot bot/curtain/meter/contact/motion MQTT ESP32 bridge - Local control


I wrote this MQTT bridge for SwitchBot on ESP32

It works with
bot press/on/off
curtain open/close/pause/position%
meter temp sensor
contact sensor
motion sensor

ESP32 cost like $5 Canadian on aliexpress. I am using Wemos D1 Mini ESP32 and powering it by simple usb. No soldering of pins needed.

If you have a big house place a couple ESP32s around the house = Full “bluetooth” switchbot coverage far away from your smart hub.

I am using only one ESP32 atm and it covers my whole house (these things have decent signal strength). It is plugged in close to the coffee (the important part) and works almost instantly. The other is my printer which is across the house. It still works, but takes a few more tries. The esp32 code tries like 60 times until full failure. It will send MQTT messages on error and success (pushed)

I only own bots. I don’t own curtain, meter, contact, or motion sensors so others are helping with testing. If you have an issue with the latest code let me know or you can request specific updates or contribute in github

  • works with curtain open/close/pause/position%

  • works with button on/off/press

  • works with meter temp sensor, contact sensor, motion sensor

  • and works with “unlimited” devices now

  • get single device status

  • rescan to get all device statuses

  • OTA update

  • bot passwords

  • HA MQTT Discovery

  • set bot hold seconds

  • set bot mode

  • define delays between commands per device

  • get responses from bot/curtain. Retry on “busy” response

  • Simulate ON/OFF when bot is in "PRESS"mode. For devices that only toggle

Thanks for your contribution.
I tried to compile it with Arduino 1.8.13, but I am getting some errors while compiling the scketch:
:\Users\Dragos\Documents\Arduino\libraries\EspMQTTClient\src\EspMQTTClient.cpp: In member function 'bool EspMQTTClient::setMaxPacketSize(uint16_t):
C:\Users\Dragos\Documents\Arduino\libraries\EspMQTTClient\src\EspMQTTClient.cpp:384:30: error: ‘class PubSubClient’ has no member named ‘setBufferSize’
bool success = _mqttClient.setBufferSize(size);

C:\Users\Dragos\Documents\Arduino\libraries\EspMQTTClient\src\EspMQTTClient.cpp: In member function ‘void EspMQTTClient::setKeepAlive(uint16_t)’:
C:\Users\Dragos\Documents\Arduino\libraries\EspMQTTClient\src\EspMQTTClient.cpp:499:15: error: ‘class PubSubClient’ has no member named ‘setKeepAlive’

C:\Users\Dragos\Documents\Arduino\libraries\EspMQTTClient\src\EspMQTTClient.cpp: In member function ‘void EspMQTTClient::mqttMessageReceivedCallback(char*, byte*, unsigned int)’:
C:\Users\Dragos\Documents\Arduino\libraries\EspMQTTClient\src\EspMQTTClient.cpp:648:49: error: ‘class PubSubClient’ has no member named ‘getBufferSize’
if (strlen(topic) + length + 9 >= _mqttClient.getBufferSize())
Can you let me know what libraries version are you using?

Best regards,

1 Like

SOLVED with compile:
need to update the PubSubClient library to his latest version to make it work :wink:

ok cool. I started from a fresh arduino IDE 1.8.13 and didn’t have to do that

There are still some minor issue with the curtain/meter values. I’m hoping to get to that this weekend

latest version v0.12 should work for bots/curtain/tempSensor controlling and get current status using a rescan of all devices

sorry @devWaves . can you give me some basic notions to use it in ESPHome?

this does not use ESPHome. All that is needed is to compile+upload the code using the arduino IDE to the esp32. since v0.14 you can update OTA

then unplug it from your PC and plug it in to any 5v usb (no data needed). You can then send it MQTT messages over wifi to control or get status from the bots

1 Like

new version released v0.13. requestInfo should work now for individual devices. It does not need to do a full rescan anymore. It only scans for one device so should be faster

new version released v0.14 Now includes OTA update code for future updates and easier to add new devices etc

v0.15 released. fix for meter if it wasnt getting values

Thank you very much, I have followed your instructions (Arduino IDE) and now the “message” I get is


I also get this message

{“status”: “idle”}

When you first power up the esp32 or when it finished uploading the code, it will preform a full switchbotMQTT/rescan of any bots you a have added to the code. In mqtt explorer, do you see data values (id, rssi, mode, batt, etc) for that bot it has error locating? Maybe the MAC address is not correct??

1 Like

The syntax of the device name is important? Upper / lower case, blank spaces, etc …

{ “Curtain Izda”, “XX:XX:XX:XX:XX:XX” },
{ “Curtain Drcha”, “XX:XX:XX:XX:XX:XX” }

No … I only see these messages

{“sec”: “30”}
{“status”: “scanning”}
{“status”: “idle”}

For the naming, use camelCase or underscore. No space. This is mine.

static std::map<std::string, std::string> allBots = {
  { "24hr_fan", "fa:67:52:87:50:r4" },
  { "floor_heat", "e7:p4:41:cc:k8:ea" },
  { "jcom_tv_box", "f4:8f:09:cd:e2:c2" }

The bot is within range to the esp32?

1 Like

The problem was that I had put the MAC address in lowercase!
Thank you very much to everyone for your help

didnt even realise that issue as i always typed in lowercase. ill put a fix in at some point. thanks

v0.16 you can use uppercase MAC address now

According to https://community.home-assistant.io/t/re-switchbot-curtain/292493?u=marith

Finally I added to MQTT.
So it will not show in MQTT Integration as a device?
Have I control it from HA via MQTT request?

MQTT is its own topic and can be done multiple ways.

I believe mqtt discovery in ha or mqtt explorer is what most people use with HA? then create mqtt devices https://www.home-assistant.io/docs/configuration/devices/

I use node red for my mqtt stuff but it doesnt create HA devices automatically