/* Peripheral 2 sending data to central */ #include unsigned long DATA; unsigned int i; BLEService dataService("19B10000-E8F2-537E-4F6C-D104768A1214"); BLEUnsignedLongCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); const int ledPin = LED_BUILTIN; void setup() { // Serial.begin(9600); //Serial.begin(19200); Serial.begin(115200); while (!Serial); pinMode(ledPin, OUTPUT); if (!BLE.begin()) { Serial.println("starting BLE failed!"); while (1); } BLE.setLocalName("PERIPHERAL-2"); BLE.setAdvertisedService(dataService); dataService.addCharacteristic(switchCharacteristic); BLE.addService(dataService); switchCharacteristic.writeValue(0); BLE.advertise(); Serial.println("PERIPHERAL-2"); } void loop() { BLEDevice central = BLE.central(); for(i=0; i<3; i++) { DATA+= random(5000,8000); } DATA=DATA/3; if (central) { // Serial.print("Connected to central: "); // Serial.println(central.address()); while (central.connected()) { if (switchCharacteristic.written()) { if (switchCharacteristic.value()) { digitalWrite(ledPin, HIGH); // Serial.print("Connected to central: "); switchCharacteristic.writeValue(DATA); Serial.print("["); Serial.print(switchCharacteristic.value()); Serial.print("]"); Serial.print("\n"); } else { digitalWrite(ledPin, LOW); } } } // Serial.print(F("Disconnected PERIPHERAL-2 from central: ")); // Serial.println(central.address()); } }