Switchbot and Wio Terminal

In case anyone can use it this is the sketch. I will upload the final and a video if I can make it work…

#include "BLEDevice.h"
#include "Seeed_erpcUnified.h"

// Address of your SwitchBot reversed
//static String addrSwitchBot = "c8:39:2E:90:32:30";
static String addrSwitchBot = "30:32:90:2e:39:c8";
// SwitchBot user-defined service
static BLEUUID serviceUUID("cba20d00-224d-11e6-9fb8-0002a5d5c51b");
// Target characteristic in the above service
static BLEUUID    charUUID("cba20002-224d-11e6-9fb8-0002a5d5c51b");
// SwitchBot Press command
static uint8_t cmdPress[3] = {0x57, 0x01, 0x00};

static BLEAddress *pGattServerAddress;
static BLERemoteCharacteristic* pRemoteCharacteristic;
static BLEClient*  pClient = NULL;
static boolean doSendCommand = false;

void dbg(const char *format, ...) {
  char b[512];
  va_list va;
  va_start(va, format);
  vsnprintf(b, sizeof(b), format, va);
  va_end(va);
  Serial.print(b);;
}

// Callback when advertisement is detected
class advdCallback: public BLEAdvertisedDeviceCallbacks {
    void onResult(BLEAdvertisedDevice advertisedDevice) {
      dbg("BLE device found: ");
      String addr = advertisedDevice.getAddress().toString().c_str();
      dbg("addr=[%s]\r\n", addr.c_str());
       // Discover SwitchBot
      if (addr.equalsIgnoreCase(addrSwitchBot)) {
        dbg("found SwitchBot\r\n");
        advertisedDevice.getScan()->stop();
        pGattServerAddress = new BLEAddress(advertisedDevice.getAddress());        
        doSendCommand = true;
      }
    }
};

void setup() {
  
  Serial.begin(115200);
  pinMode(WIO_KEY_A, INPUT); //set button A pin as input
  dbg("start");
  // BLE initialization
  BLEDevice::init("");
  // Scan advertisements from devices
  BLEScan* pBLEScan = BLEDevice::getScan();
  pBLEScan->setAdvertisedDeviceCallbacks(new advdCallback());
  pBLEScan->setActiveScan(true);
  pBLEScan->start(30);
}

void loop() {
    if (digitalRead(WIO_KEY_A) == LOW) { //check whether button A is pressed
    Serial.println("Press Swichtbot"); 
    if (connectAndSendCommand(*pGattServerAddress)) {
    } else {
      dbg("connectAndSendCommand failed");
    }
    doSendCommand = false;
    dbg("done");
  }
  delay(1000);
}

// Connect to SwitchBot's GATT server-Send Press command
static bool connectAndSendCommand(BLEAddress pAddress) {
  dbg("start connectAndSendCommand\r\n");
  pClient  = BLEDevice::createClient();

  pClient->connect(pAddress);
  dbg("connected\r\n");

// Get the target service 
 BLERemoteService* pRemoteService = pClient->getService(serviceUUID);//>>>>>>>>>>>>>>>>>STUCKED HERE 
 dbg("aqui\r\n");
 
  if (pRemoteService == nullptr) {
    dbg("target service not found\r\n");
    return false;
  }
  dbg("found target service\r\n");

  // Get the target characteristic
  pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
  if (pRemoteCharacteristic == nullptr) {
    dbg("target characteristic not found");
    return false;
  }
  dbg("found target characteristic\r\n");
  
  // Write the Press command characteristically
  pRemoteCharacteristic->writeValue(cmdPress, sizeof(cmdPress), false);

  delay(3000);
  if (pClient) {
    pClient->disconnect();
    pClient = NULL;
  }
  return true;
}
1 Like