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

Hey

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-$10 Canadian on aliexpress. There are MANY options for ESP32. I can only confirm the ESP32-WROOM-32 variations work as that is what I own . Most can be powered by 5v USB. They make ESP32 with onboard antennas (Wemos D1 Mini ESP32, ESP32-WROOM-32) and/or external antennas (example: ESP32-WROOM-32U, QuinLED-ESP32-AE). No soldering of pins needed for this project

I am currently using a Wemos D1 Mini ESP32 while testing but use a QuinLED-ESP32-AE as my primary unit because it has the external antenna and I have multiple switchbot devices now.

ESP32 models that I can confirm work

  • Wemos D1 Mini ESP32
  • ESP32-WROOM-32U
  • QuinLED-ESP32-AE

ESP32 models that may not work

  • ESP32-S2 = No Bluetooth so definitely won’t work
  • ESP32-C3 = Less RAM/ROM that the WROOM models
  • ESP32s = Ai-Thinker model. Some users have said they had issues with this model
  • ESP32-solo = Only has one core instead of 2 cores

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

If you have an issue with the latest code let me know or you can request specific updates or contribute in github

if you like my work please feel free to donate. I’ve got a kid on the way and it would help a lot

Buy Me A Coffee

24 Likes
  • 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’
_mqttClient.setKeepAlive(keepAliveSeconds);

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,
Dragos

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

{“id”:“curtainone”,“status”:“errorLocatingDevice”}

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

Solved!
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.
{"id":"curtain","status":"info","rssi":-59,"calib":true,"batt":96,"pos":99,"light":4}
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