//client #include #include #include #include #include #include #include // OLEDs without Reset of the Display U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/* clock=*/ D5, /* data=*/ D4, /* reset=*/ U8X8_PIN_NONE); BLEClient* pClient; bool doconnect = false; //BLE Server name (the other ESP32 name running the server sketch) #define bleServerName "XIAOESP32C6_BLE" //Address of the peripheral device. Address will be found during scanning... static BLEAddress *pServerAddress; BLEUUID serviceUUID("181A"); // Environmental Sensing BLEUUID charUUID("2A59"); // Analog Output char light_val[1024]; //Callback function that gets called, when another device's advertisement has been received class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks { void onResult(BLEAdvertisedDevice advertisedDevice) { if (advertisedDevice.getName() == bleServerName) { //Check if the name of the advertiser matches advertisedDevice.getScan()->stop(); //Scan can be stopped, we found what we are looking for pServerAddress = new BLEAddress(advertisedDevice.getAddress()); //Address of advertiser is the one we need Serial.println("Device found. Connecting!"); } } }; //function that prints the latest sensor readings in the OLED display void printReadings(){ u8x8.setFont(u8x8_font_chroma48medium8_r); u8x8.setCursor(0, 0); u8x8.print("Light Value:"); u8x8.drawString(0, 2, light_val); } void setup() { Serial.begin(115200); //Wire.begin(D4, D5); //OLED display setup u8x8.begin(); u8x8.setFlipMode(1); u8x8.setFont(u8x8_font_chroma48medium8_r); u8x8.drawString(0, 3, "Starting..."); Serial.println("Starting BLE client..."); BLEDevice::init("XIAOESP32C6_Client"); // Retrieve a Scanner and set the callback we want to use to be informed when we // have detected a new device. Specify that we want active scanning and start the // scan to run for 30 seconds. BLEScan* pBLEScan = BLEDevice::getScan(); pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); pBLEScan->setActiveScan(true); pBLEScan->start(30); pClient = BLEDevice::createClient(); // Connect to the remove BLE Server. pClient->connect(*pServerAddress); Serial.println(" - Connected to server"); // Obtain a reference to the service we are after in the remote BLE server. BLERemoteService* pRemoteService = pClient->getService(serviceUUID); if (pRemoteService == nullptr) { u8x8.clearDisplay(); u8x8.drawString(0, 3, "False UUID"); Serial.print("Failed to find our service UUID: "); Serial.println(serviceUUID.toString().c_str()); return; } // Obtain a reference to the characteristics in the service of the remote BLE server. BLERemoteCharacteristic* pCharacteristic = pRemoteService->getCharacteristic(charUUID); if (pCharacteristic == nullptr) { u8x8.clearDisplay(); u8x8.drawString(0, 3, "False UUID"); Serial.print("Failed to find our characteristic UUID"); return; } Serial.println(" - Found light value characteristics"); u8x8.clearDisplay(); u8x8.drawString(0, 3, "Connected!"); pCharacteristic->registerForNotify([](BLERemoteCharacteristic* pBLERemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify) { Serial.println("Notify received"); Serial.print("Value: "); Serial.println(*pData); snprintf(light_val, sizeof(light_val), "%d", *pData); }); doconnect = true; u8x8.clearDisplay(); u8x8.drawString(0, 4, "Receiving..."); } void loop() { if (doconnect) { BLERemoteService* pRemoteService = pClient->getService(serviceUUID); BLERemoteCharacteristic* pCharacteristic = pRemoteService->getCharacteristic(charUUID); pCharacteristic->registerForNotify([](BLERemoteCharacteristic* pBLERemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify) { Serial.println("Notify received"); Serial.print("Value: "); Serial.println(*pData); snprintf(light_val, sizeof(light_val), "%d", *pData); }); } printReadings(); delay(1000); u8x8.clearLine(2); }