Hello everyone I've never posted before so I may not know how a couple of things work but I've made a fairly basic BLE sketch where a central connects consecutively with 2 peripherals, checks the rssi with each of them and then writes a value to the characteristic of the one with the most powerful rssi. My problem is that the bleCharacteristic.writeValue() function is not going through at all and to confirm this I made a while loop at the point where the value should be updated on the peripheral and the program stops there which means that the value is never written in the first place. I have tried to change the value with the BLE app from my phone and it works as it should but it doesn't with the central.
Here's the codes for the central.
`
#include <ArduinoBLE.h>
int rssiState1, rssiState2 ;
BLEDevice peripheral1;
BLEDevice peripheral2;
void setup() {
Serial.begin(9600);
while (!Serial);
rssiState1 = -13;
rssiState2 = -10;
// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);
}
Serial.println("Bluetooth® Low Energy Central - Peripheral Explorer");
}
void loop() {
BLE.scanForUuid("19B10010-E8F2-537E-4F6C-D104768A1214");
Serial.print("Scanning for Rover 1 ");
Serial.println();
delay(3000);
// check if a peripheral has been discovered
BLEDevice peripheral1 = BLE.available();
if (peripheral1) {
// discovered a peripheral, print out address, local name, and advertised service
Serial.print("Found ");
Serial.print(peripheral1.address());
Serial.print(" '");
Serial.print(peripheral1.localName());
Serial.print("' ");
Serial.print(peripheral1.advertisedServiceUuid());
Serial.println();
// see if peripheral is a Rover
if (peripheral1.localName() == "Rover 1") {
// stop scanning
BLE.stopScan();
// connect to the peripheral
Serial.println("Connecting ...");
if (peripheral1.connect()) {
Serial.println("Connected");
} else {
Serial.println("Failed to connect!");
}
//rssiState1 = peripheral1.rssi();
// Print RSSI
Serial.print("RSSI=");
Serial.println(rssiState1);
// // retrieve the moror characteristic
BLECharacteristic motorCharacteristic = peripheral1.characteristic("19b10002-e8f2-537e-4f6c-d104768a1214");
//if (rssiState1 > rssiState2 || rssiState1 == rssiState2) {
delay(3000);
while(motorCharacteristic.written()== false){
motorCharacteristic.writeValue((byte(0x01))); //τιμη 1 για το 1 και 0 μηδεν για το άλλο
// }else if (rssiState1 < rssiState2){
// motorCharacteristic.writeValue((byte)0x00);
}
// }
Serial.println("Moving Rover 1");
}
}
BLE.disconnect();
BLE.scanForUuid("19B10011-E8F2-537E-4F6C-D104768A1214");
Serial.println("Scanning for Rover 2 ");
delay(3000);
BLEDevice peripheral2 = BLE.available();
if (peripheral2) {
// discovered a peripheral, print out address, local name, and advertised service
Serial.print("Found ");
Serial.print(peripheral2.address());
Serial.print(" '");
Serial.print(peripheral2.localName());
Serial.print("' ");
Serial.print(peripheral2.advertisedServiceUuid());
Serial.println();
// see if peripheral is a Rover
if (peripheral2.localName() == "Rover 2") {
// stop scanning
BLE.stopScan();
// connect to the peripheral
Serial.println("Connecting ...");
if (peripheral2.connect()) {
Serial.println("Connected");
} else {
Serial.println("Failed to connect!");
return;
}
// rssiState2 = peripheral2.rssi();
// Print RSSI
Serial.print("RSSI=");
Serial.println(rssiState2);
BLECharacteristic motorCharacteristic = peripheral2.characteristic("19b10001-e8f2-537e-4f6c-d104768a1214");
// if (rssiState1 < rssiState2) {
motorCharacteristic.writeValue((byte(0x01))); //τιμη 1 για το 1 και 0 μηδεν για το άλλο
// }else if (rssiState1 > rssiState2){
delay(3000);
// peripheral2.characteristic("19b10001-e8f2-537e-4f6c-d104768a1214").writeValue((byte)0x00);
Serial.println("Moving Rover 2");
// }
BLE.disconnect();
}
delay(3000);
}
}
`
And here's the code of the peripheral
`
#include <ArduinoBLE.h>
//const int motorPin = 4; // set buttonPin to digital pin 4
BLEService motorService("19B10010-E8F2-537E-4F6C-D104768A1214"); // create service
// create motor characteristic and allow remote device to read and write
BLEByteCharacteristic motorCharacteristic("19b10002-e8f2-537e-4f6c-d104768a1214", BLERead | BLEWrite);
void setup() {
Serial.begin(9600);
while (!Serial);
pinMode(LED_BUILTIN, OUTPUT); // use the Motor as an output
// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
//while (1);
}
// set the local name peripheral advertises
BLE.setLocalName("Rover 1");
// set the UUID for the service this peripheral advertises:
BLE.setAdvertisedService(motorService);
// add the characteristics to the service
motorService.addCharacteristic(motorCharacteristic);
// add the service
BLE.addService(motorService);
motorCharacteristic.writeValue((byte)0x00);
// start advertising
BLE.advertise();
Serial.println("advertising ...");
Serial.println("Bluetooth® device active, waiting for connections...");
Serial.println("motorchar");
Serial.println(motorCharacteristic.value()) ;
}
void loop() {
// listen for Bluetooth® Low Energy peripherals to connect:
BLE.poll();
BLEDevice central = BLE.central();
//if a central is connected to peripheral:
if (central) {
Serial.print("Connected to central: ");
// print the central's MAC address:
Serial.println(central.address());
while (central.connected()) {
Serial.println("it is connetcted");
// if the remote device wrote to the characteristic,
if (motorCharacteristic.written()) {
Serial.println("it is written");
// use the value to is written()) {
if (motorCharacteristic.value()>0) { // any value other than 0
Serial.println("Movement active");
digitalWrite(LED_BUILTIN, HIGH); // will turn the LED on
delay(5000);
digitalWrite(LED_BUILTIN, LOW);
//motorCharacteristic.writeValue((byte)0x00);
Serial.println("Movement inactive");
}
}
Serial.println("motorchar");
Serial.println(motorCharacteristic.value()) ;
}
}
}
`
Also in the serial of the peripheral the value is always 0 just to cross check that it is never written
I have also tried to send a different value type other than a byte but the problem is that it never goes through