- #include "rpcWiFi.h"
- #include <rpcBLEDevice.h>
- #include <BLEScan.h>
- #include <BLEAdvertisedDevice.h>
- #define SWITCHBOT_1 "C8:39:2E:90:32:30" //DEDO
- //#define SWITCHBOT_1 "30:32:90:2E:39:C8" //reversed
- #include"TFT_eSPI.h" //include TFT LCD library
- TFT_eSPI tft; //initialize TFT LCD
- bool go ;
- static BLEUUID serviceUUID("CBA20D00-224D-11E6-9FB8-0002A5D5C51B");
- static BLEUUID characteristicUUID("CBA20002-224D-11E6-9FB8-0002A5D5C51B");
- static BLEAdvertisedDevice* myDevice = NULL;
- static uint8_t cmdSwi[6] = {0x57, 0x11, 0x3E, 0x2B, 0x26, 0x2D};
- //https://github.com/RoButton/switchbotpy
- //unencripted, ojo, 0x57 01 // static uint8_t cmdPress[3] = {0x57, 0x01, 0x00};
- //static uint8_t cmdOn[3] = {0x57, 0x01, 0x01};
- //static uint8_t cmdOff[3] = {0x57, 0x01, 0x02};
- //static uint8_t cmdDown[3] = {0x57, 0x01, 0x03};
- //static uint8_t cmdUp[3] = {0x57, 0x01, 0x04};
- //https://www.lammertbies.nl/comm/info/crc-calculation 0x3E 2B 26 2D (SB711)
- //with passw ojo , 0x57 11 //press 0x 57 11 & crc32 checksum of pass in 4 bytes
- //with passw on 0x 57 11 & crc32 checksum of pass in 4 bytes & 01
- //with passw off 0x 57 11 & crc32 checksum of pass in 4 bytes & 02
- bool pressSwitchBot() {
- bool result;
- BLEClient* pClient = BLEDevice::createClient();
- result = pClient->connect(myDevice);
- if (!result) {
- tft.drawString("****here*****", 3, 5);
- delay(1000);
- return (false);
- }
- if (go == false) {
- tft.drawString("NOT FOUND", 3, 5);
- delay(1000);
- pClient->disconnect();
- return (false);
- }
- BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
- if (pRemoteService == nullptr) {
- pClient->disconnect();
- tft.drawString("NOT SERVICE", 3, 5);
- delay(1000);
- return (false);
- }
- BLERemoteCharacteristic* pCharacteristic = pRemoteService->getCharacteristic(characteristicUUID);
- if (pCharacteristic == nullptr) {
- pClient->disconnect(); //if no characteristics matched
- tft.drawString("NOT CHAR", 3, 5);
- delay(1000);
- return (false);
- }
- if (go == true) {
- pCharacteristic->writeValue(cmdSwi, sizeof(cmdSwi), false);
- }
- delay(100);
- pClient->disconnect();
- return (true);
- delay(100);
- }
- class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
- void onResult(BLEAdvertisedDevice advertisedDevice) {
- if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(serviceUUID) && advertisedDevice.getAddress().equals(BLEAddress(SWITCHBOT_1))) {
- tft.printf(advertisedDevice.getAddress().toString().c_str()); // if specified device found, print its MAC address
- delay(100);
- BLEDevice::getScan()->stop();
- myDevice = new BLEAdvertisedDevice(advertisedDevice);
- go = true;
- }
- }
- };
- void setup() {
- tft.begin(); //start TFT LCD
- tft.setRotation(3); //set screen rotation
- tft.fillScreen(TFT_RED); //fill background
- tft.setTextColor(TFT_BLACK); //set text color
- tft.setTextSize(4); //set text size
- pinMode(WIO_KEY_A, INPUT); //set button A pin as input
- tft.drawString("SETUP", 3, 5); //draw text string
- delay(500);
- }
- void loop() {
- go = false;
- if (digitalRead(WIO_KEY_A) == LOW) {//check whether button A is pressed
- tft.fillScreen(TFT_NAVY);
- delay(10);
- BLEDevice::init("");
- BLEScan* pBLEScan = BLEDevice::getScan();
- pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
- pBLEScan->setInterval(100);
- pBLEScan->setWindow(99);
- pBLEScan->setActiveScan(true);
- pBLEScan->start(5, false);
- delay(200);
- pressSwitchBot();
- }
- delay(2000);
- tft.fillScreen(TFT_BLACK);
- }