Facebook
From Sisx, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 73
  1. #include "rpcWiFi.h"
  2. #include <rpcBLEDevice.h>
  3. #include <BLEScan.h>
  4. #include <BLEAdvertisedDevice.h>
  5.  
  6. #define SWITCHBOT_1 "C8:39:2E:90:32:30" //DEDO
  7. //#define SWITCHBOT_1 "30:32:90:2E:39:C8" //reversed
  8.  
  9. #include"TFT_eSPI.h" //include TFT LCD library
  10. TFT_eSPI tft; //initialize TFT LCD
  11.  
  12. bool go ;
  13.  
  14. static BLEUUID serviceUUID("CBA20D00-224D-11E6-9FB8-0002A5D5C51B");
  15. static BLEUUID characteristicUUID("CBA20002-224D-11E6-9FB8-0002A5D5C51B");
  16. static BLEAdvertisedDevice* myDevice = NULL;
  17. static uint8_t cmdSwi[6] = {0x57, 0x11, 0x3E, 0x2B, 0x26, 0x2D};
  18. //https://github.com/RoButton/switchbotpy
  19. //unencripted, ojo, 0x57 01 // static uint8_t cmdPress[3] = {0x57, 0x01, 0x00};
  20. //static uint8_t cmdOn[3] = {0x57, 0x01, 0x01};
  21. //static uint8_t cmdOff[3] = {0x57, 0x01, 0x02};
  22. //static uint8_t cmdDown[3] = {0x57, 0x01, 0x03};
  23. //static uint8_t cmdUp[3] = {0x57, 0x01, 0x04};
  24. //https://www.lammertbies.nl/comm/info/crc-calculation  0x3E 2B 26 2D (SB711)
  25. //with passw ojo , 0x57 11 //press 0x 57 11 & crc32 checksum of pass in 4 bytes
  26. //with passw                 on    0x 57 11 & crc32 checksum of pass in 4 bytes & 01
  27. //with passw                 off   0x 57 11 & crc32 checksum of pass in 4 bytes & 02
  28.  
  29. bool pressSwitchBot() {
  30.   bool result;
  31.  
  32.   BLEClient*  pClient  = BLEDevice::createClient();
  33.  
  34.   result = pClient->connect(myDevice);
  35.   if (!result) {
  36.     tft.drawString("****here*****", 3, 5);
  37.     delay(1000);
  38.     return (false);
  39.   }
  40.  
  41.   if (go == false) {
  42.  
  43.     tft.drawString("NOT FOUND", 3, 5);
  44.     delay(1000);
  45.     pClient->disconnect();
  46.     return (false);
  47.   }
  48.  
  49.   BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
  50.   if (pRemoteService == nullptr) {
  51.     pClient->disconnect();
  52.     tft.drawString("NOT SERVICE", 3, 5);
  53.     delay(1000);
  54.     return (false);
  55.   }
  56.  
  57.   BLERemoteCharacteristic* pCharacteristic = pRemoteService->getCharacteristic(characteristicUUID);
  58.   if (pCharacteristic == nullptr) {
  59.     pClient->disconnect();            //if no characteristics matched
  60.     tft.drawString("NOT CHAR", 3, 5);
  61.     delay(1000);
  62.     return (false);
  63.   }
  64.  
  65.   if (go == true) {
  66.     pCharacteristic->writeValue(cmdSwi, sizeof(cmdSwi), false);
  67.   }
  68.   delay(100);
  69.   pClient->disconnect();
  70.   return (true);
  71.   delay(100);
  72. }
  73.  
  74. class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
  75.     void onResult(BLEAdvertisedDevice advertisedDevice) {
  76.       if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(serviceUUID) && advertisedDevice.getAddress().equals(BLEAddress(SWITCHBOT_1))) {
  77.  
  78.         tft.printf(advertisedDevice.getAddress().toString().c_str());    // if specified device found, print its MAC address
  79.         delay(100);
  80.         BLEDevice::getScan()->stop();
  81.         myDevice = new BLEAdvertisedDevice(advertisedDevice);
  82.         go = true;
  83.       }
  84.     }
  85. };
  86.  
  87.  
  88. void setup() {
  89.   tft.begin(); //start TFT LCD
  90.   tft.setRotation(3); //set screen rotation
  91.   tft.fillScreen(TFT_RED); //fill background
  92.   tft.setTextColor(TFT_BLACK); //set text color
  93.   tft.setTextSize(4); //set text size
  94.   pinMode(WIO_KEY_A, INPUT); //set button A pin as input
  95.   tft.drawString("SETUP", 3, 5); //draw text string
  96.   delay(500);
  97. }
  98.  
  99. void loop() {
  100.   go = false;
  101.   if (digitalRead(WIO_KEY_A) == LOW)  {//check whether button A is pressed
  102.     tft.fillScreen(TFT_NAVY);
  103.     delay(10);
  104.     BLEDevice::init("");
  105.     BLEScan* pBLEScan = BLEDevice::getScan();
  106.     pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  107.     pBLEScan->setInterval(100);
  108.     pBLEScan->setWindow(99);
  109.     pBLEScan->setActiveScan(true);
  110.     pBLEScan->start(5, false);
  111.     delay(200);
  112.     pressSwitchBot();
  113.   }
  114.   delay(2000);
  115.   tft.fillScreen(TFT_BLACK);
  116. }