Hi, I love the Wio Terminal! I´m strugglin with this sketch, adapting, working from 0… no good luck, I am doing something wrong with BLE.
I have several of them so the blue address must be involved.
The manufacturer gave the code for python in: https://github.com/OpenWonderLabs/python-host/blob/master/switchbot_py3.py
It works with iOS-android nRF connect.
I would like to activate de Press Command in the switchbot in a similar way than in this gif.
If anyone could help me I would be really happy.
This is the sketch and some notes:
#include "BLEDevice.h"
#include "Seeed_erpcUnified.h" //not sure if needed
#define SWITCHBOT_MAC "C8:39:2E:90:32:31"
//#define SWITCHBOT_MAC "31:32:90:2E:39:C8" //REVERSE MAC ADRESS
static BLEUUID serviceUUID("CBA20D00-224D-11E6-9FB8-0002A5D5C51B"); //same for all switchbots
static BLEUUID characteristicUUID("CBA20002-224D-11E6-9FB8-0002A5D5C51B"); //same for all switchbots
static BLEAdvertisedDevice* myDevice;
//commands = {
// 'press' : '\x57\x01\x00',
// 'on' : '\x57\x01\x01',
//'off' : '\x57\x01\x02',
static uint8_t cmdPress[3] = {0x57, 0x01, 0x00}; //Command to send. Works in iOs-Android with nRF Connect APP
uint8_t bd_addr[6] = {0x31, 0x32, 0x90, 0x2e, 0x39, 0xc8}; //REVERSE MAC ADRESS HEX
BLEAddress BattServer(bd_addr); //not sure if needed
bool pressSwitchBot() {
bool result;
BLEClient* pClient = BLEDevice::createClient();
result = pClient->connect(myDevice);
Serial.println(myDevice->getAddress().toString().c_str()); //wrong bl address returns here
if (!result) {
delay(2000);
return (false);
}
BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
if (pRemoteService == nullptr) {
pClient->disconnect();
delay(2000);
return (false);
}
BLERemoteCharacteristic* pCharacteristic = pRemoteService->getCharacteristic(characteristicUUID);
if (pCharacteristic == nullptr) {
pClient->disconnect();
delay(2000);
return (false);
}
pCharacteristic->writeValue(cmdPress, sizeof(cmdPress), false);
pClient->disconnect();
delay(2000);
return (true);
}
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(serviceUUID)
&& advertisedDevice.getAddress().equals(BLEAddress(SWITCHBOT_MAC))) {
Serial.println(advertisedDevice.getAddress().toString().c_str()); // if specified device found, print its MAC address to serial console.
BLEDevice::getScan()->stop();
myDevice = new BLEAdvertisedDevice(advertisedDevice);
}
}
};
void setup() {
Serial.begin(115200);
pinMode(WIO_KEY_A, INPUT); //set button A pin as input
delay(10);
Serial.println("Starting Arduino BLE Client application...");
BLEDevice::init("");
BLEScan* pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setInterval(1349);
pBLEScan->setWindow(449);
pBLEScan->setActiveScan(true);
pBLEScan->start(5, false);
}
static uint32_t count = 0;
void loop() {
if (digitalRead(WIO_KEY_A) == LOW) { //check whether button A is pressed
Serial.println("Press Swichtbot");
pressSwitchBot();
delay(2000);
}
}