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

I have my esp32 very close to the switchbot button but I get only -75 rssi. Could you suggest me a reliable and powerful esp32 device I can buy?

Atm I have a WeMos D1 Mini.

Is it always reporting above 70? I am using a d1 mini and it works well but I have had a D1 mini that seemed to have a weak internal antenna compared to others. You might just have a dud

To increase range they do make esp32 with external antenna attachments. I havent purchased these but they look correct ESP32 DevKitC Core Board Development Doard ESP32 WROOM 32D ESP32 WROOM 32U for Arduino|Integrated Circuits| - AliExpress

you should also be able to use an esp32-cam with antenna, but they dont come with usb interface to flash

You can search for ESP-WROOM-32UE

Just got around to flash the wESP32, only had some issues with the LED pin, which was quickly solved. Still have to add device, but the webui already works!

Would it be hard to use ethernet instead of wifi instead for connectivity?

EDIT: Also added the curtains, they can now be controlled through HA! Awesome!

cool! editing the code for ethernet support is probably possible but would take some work. I haven’t purchased any ethernet esp32s yet to test with

Apologies for being a noob, but wish to check, is it possible that i add a PIR function such as AM312 to my ESP32 running this together so that it can also help do motion check.
I tried to piece stuff together, apparently, I’m not sure how to do it. Anyone has any working sample.

You should be able to add a binary sensor like a motion PIR sensor by adding the following code (with some modifications)

Where it says “existing code” this would be the setup and loop methods of the current ESP32 switchbot code

//EDIT
int sensorPin = 16;       // sensor pin  (PIR/Motion etc)
int publishSeconds = 10000;     // publish every 10 secs regardless of state
bool retain = true;   // retain mqtt sensor messages

//DO NOT EDIT
int sensorStat = 0;      // sensor status
int sensorLastStat = 0;
long lastSensorPublish = 0;

void setup() {

  /*


     Existing code


  */

  pinMode(sensorPin, INPUT);

}

void checkSensor()
{
sensorStat = digitalRead(sensorPin);
  if (sensorStat == HIGH) { // MOTION DETECTED
    if (((millis() - lastSensorPublish) >= publishSeconds) || (sensorStat != sensorLastStat)) {
      if (client.isConnected()) {
        client.publish("YOURTOPIC", "MESSAGE", retain);
        lastSensorPublish = millis();
      }
    }
  }
  else {
    if (((millis() - lastSensorPublish) >= publishSeconds) || (sensorStat != sensorLastStat)) {
      if (client.isConnected()) {
        client.publish("YOURTOPIC", "MESSAGE", retain);
        lastSensorPublish = millis();
      }
    }
  }
  sensorLastStat = sensorStat;
}

void loop() {

  /*


     Existing code


  */

  checkSensor();

}


class AdvertisedDeviceCallbacks: public NimBLEAdvertisedDeviceCallbacks {
    void onResult(NimBLEAdvertisedDevice* advertisedDevice) {
     
checkSensor();

  /*


     Existing code


  */

}}

It may not work while the ESP32 is busy with switchbot control actions though

Thank you so much…

I edited the code to also check the sensor while it is doing a BLE scan. the method checkSensor() is called in 2 places

let me know how it goes

This is something I wanted to try. Is there another way where these codes are in separate files so when there is an update we don’t have to paste the values back in?

Remove the client.publish messages you don’t need

I am not sure what to do with this part or what it’s doing.

lol. I wrote 95% of that on my phone and havent had a chance to test it

I’ll see what I can do about making it easy to add to main code… if it works

I edited the code again. set retain true/false instead of removing client.publish lines

I did some quick testing. Seems to work

v6.5 includes last motion and last contact for motion and contact sensor

also a fix to replace spaces in device names with underscores _

Hey guys, I came across this from another HA SwitchBot thread and just wanted to say I’m super excited for this and thanks for offering an alternative.

I ordered 2 x ESP32 D1 Minis which should arrive later this week, hopefully on time to work on it over the weekend.

I’ve been using API for a while for 2 Curtain Bots and lately it’s been an absolute nightmare. I’m fed up with it.

I’ve got 2 Hubs each very close to the bots with great Wifi signal but still get occasional (if not 50% of the time on the upstairs one) errors in the app on opening/closing.

Last night the rest api call wouldn’t even work at all for an entire evening!!! I saw that it simply wasn’t responding anymore in the HA logs. I’m aware of the 1000 max calls per day but I’ve set the 2 bots at 300 calls each to update status so it should have left me a pretty good margin considering the curtains open/close twice a day on average.

I’ve also noticed API calls take about 8 seconds to send the command to the bots which can be frustrating at times as I know Bluetooth is faster.

Anyway I’m very much looking to test this solution and will report back if there’s any issue.

Hey @notownblues . It sounds like the AWS outage affected switchbot cloud stuff yesterday

Good luck with the ESP32 setup. You should find it much more responsive and reliable. Let us know how it goes

Hi @devWaves , thank you for MQTT ESP 32 bridge which helps me a lot to control many Switchbot devices locally.
I also have a Humidifier not supported by MQTT ESP 32 bridge, so I tried the BLE check steps following your instructions while changing the mode of the Humidifier on/off/Auto/Manual.
Here is a result from nRF connect app on Android.

I’ve noticed some data changing of Service Data while checking with nRF connect on each mode.

I hope this will help you!

hey @innerzone . I’ve got the humidifier values figured out and actually have code that supports the states, but the hard part now is figuring out the control command byte array to send to it.

I can’t find those commands online and the examples I’ve seen use the same commands as the bot, which doesnt seem to work

I’ve confirmed the humidifier works with BLE only though by turning off wifi and data on my phone then controlling it and it works

basically I need the lines I would write for humidifier like this…

static byte bArrayPress[] = {0x57, 0x01};
static byte bArrayOn[] = {0x57, 0x01, 0x01};
static byte bArrayOff[] = {0x57, 0x01, 0x02};
static byte bArrayOpen[] =  {0x57, 0x0F, 0x45, 0x01, 0x05, 0xFF, 0x00};
static byte bArrayClose[] = {0x57, 0x0F, 0x45, 0x01, 0x05, 0xFF, 0x64};
static byte bArrayPause[] = {0x57, 0x0F, 0x45, 0x01, 0x00, 0xFF};
static byte bArrayPos[] =  {0x57, 0x0F, 0x45, 0x01, 0x05, 0xFF, NULL};
static byte bArrayGetSettings[] = {0x57, 0x02};
static byte bArrayHoldSecs[] = {0x57, 0x0F, 0x08, NULL };
static byte bArrayBotMode[] = {0x57, 0x03, 0x64, NULL, NULL};

Is it worth trying “nRF Sniffer for BLE” to analyze the BLE control command data of the humidifier?
I don’t know how to find that kind of data. Need more geek skills?

I don’t think so. I don’t think it provides the possible commands to send. I put in a request for humidifier BLE documentation here if you want to comment on it humidifier BLE request

1 Like

Does anyone know why my curtains will randomly open sometimes? Seems to happen about once every few days but can be more often. I have one esp32 downstairs and one upstairs, it randomly happens on both and when it does, all curtains the esp32 controls will be opened.

I don’t think it’s home assistant since whenever I catch it happening, home assistant isn’t aware of the opened state of the cover.

I’ve not been able to be quick enough to check the mosquitto broker when it happens, but even if I was, I wouldn’t really know what to look for. Maybe I could add some logging somewhere to try and catch any strange messages being sent to the broker?

make sure your mqtt ‘set’ messages are not being retained

if your control messages are retained with mqtt, then when the esp32 regains mqtt connections or HA reboots it can rerun the last commands