/* modifications by (c)2014-2021 Forward Computing and Control Pty. Ltd. NSW Australia, www.forward.com.au This code is not warranted to be fit for any purpose. You may only use it at your own risk. This generated code may be freely used for both private and commercial use provided this copyright is maintained. */ // This sketch also needs the SafeString library installed from the Arduino Library manager. // The Client_MAC below must be set to match the BLE Power Switch module you are trying to control /********************************************************************* This is an example for our nRF52 based Bluefruit LE modules Pick one up today in the adafruit shop! Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit! MIT license, check LICENSE for more information All text above, and the splash screen below must be included in any redistribution *********************************************************************/ /* This sketch demonstrate the central API(). A additional bluefruit that has bleuart as peripheral is required for the demo. */ #include #include const char Client_MAC[] = "F5:FA:14:1C:85:B7"; // <<<<<<<<< this must match the MAC of your Power Switch BLE bool cmdSent = false; size_t printHexBufferReverse(Print *prt, uint8_t const buffer[], int len, char delim = ' ', int byteline = 0); void printHexBufferReverse(SafeString &sf, uint8_t const buffer[], int len, char delim) { if (buffer == NULL || len == 0) return; for (int i = 0; i < len; i++) { if (i != 0) sf.print(delim); if (buffer[len - 1 - i] < 16) { sf.print('0'); } sf.print(buffer[len - 1 - i], HEX); } } BLEClientUart clientUart; // bleuart client void setup() { Serial.begin(115200); Serial.println("BLE Power Switch Remote"); Serial.println("-----------------------------------\n"); // Initialize Bluefruit with maximum connections as Peripheral = 0, Central = 1 // SRAM usage required by SoftDevice will increase dramatically with number of connections Bluefruit.begin(0, 1); Bluefruit.setName("Power Switch Remote"); // Init BLE Central Uart Serivce clientUart.begin(); clientUart.setRxCallback(bleuart_rx_callback); // Increase Blink rate to different from PrPh advertising mode Bluefruit.setConnLedInterval(250); // Callbacks for Central Bluefruit.Central.setConnectCallback(connect_callback); Bluefruit.Central.setDisconnectCallback(disconnect_callback); /* Start Central Scanning - Enable auto scan if disconnected - Interval = 100 ms, window = 80 ms - Don't use active scan - Start(timeout) with timeout = 0 will scan forever (until connected) */ Bluefruit.Scanner.setRxCallback(scan_callback); Bluefruit.Scanner.restartOnDisconnect(true); Bluefruit.Scanner.setInterval(80, 60); // in unit of 0.625 ms Bluefruit.Scanner.useActiveScan(false); Bluefruit.Scanner.start(0); // // 0 = Don't stop scanning after n seconds } /** Callback invoked when scanner pick up an advertising data @param report Structural advertising data */ void scan_callback(ble_gap_evt_adv_report_t* report) { uint8_t len = 0; uint8_t buffer[32]; memset(buffer, 0, sizeof(buffer)); bool foundClient = false; cSF(sfMAC, 20); printHexBufferReverse(sfMAC, report->peer_addr.addr, 6, ':'); Serial.println(sfMAC); if ((sfMAC != Client_MAC) || cmdSent) { // do not connect more than once per power cycle Bluefruit.Scanner.resume(); return; } // else connect // Check if advertising contain BleUart service if ( Bluefruit.Scanner.checkReportForService(report, clientUart) ) { Serial.print("BLE UART service detected. Connecting ... "); Bluefruit.Central.connect(report); } else { // For Softdevice v6: after received a report, scanner will be paused // We need to call Scanner resume() to continue scanning Bluefruit.Scanner.resume(); } } /** Callback invoked when an connection is established @param conn_handle */ void connect_callback(uint16_t conn_handle) { Serial.println("Connected"); Serial.print("Discovering BLE Uart Service ... "); if ( clientUart.discover(conn_handle) ) { Serial.println("Found it"); Serial.println("Enable TXD's notify"); clientUart.enableTXD(); Serial.println("Ready to receive from peripheral"); } else { Serial.println("Found NONE"); // disconnect since we couldn't find bleuart service Bluefruit.disconnect(conn_handle); } } /** Callback invoked when a connection is dropped @param conn_handle @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h */ void disconnect_callback(uint16_t conn_handle, uint8_t reason) { (void) conn_handle; (void) reason; Serial.print("Disconnected, reason = 0x"); Serial.println(reason, HEX); } /** Callback invoked when uart received data @param uart_svc Reference object to the service where the data arrived. In this example it is clientUart */ void bleuart_rx_callback(BLEClientUart& uart_svc) { Serial.print("[RX]: "); while ( uart_svc.available() ) { Serial.print( (char) uart_svc.read() ); } Serial.println(); Serial.println("Disconnect"); Bluefruit.disconnect(clientUart.connHandle()); } void loop() { if ( Bluefruit.Central.connected() ) { // Not discovered yet if ( clientUart.discovered() ) { if (!cmdSent) { // only send the toggle cmd once cmdSent = true; char str[20 + 1] = { 0 }; cSFA(sfStr, str); sfStr = "{T}"; Serial.print("Sending : ");Serial.println(sfStr); clientUart.print(str ); } } } }