I am trying to read analog data through BLE communication with two Nano 33 BLE units.
My central code is:
<# include <ArduinoBLE.h>
BLEService batteryService("1101");
BLEUnsignedCharCharacteristic batteryLevelChar("1101", BLERead | BLEWrite);
void setup() {
Serial.begin(9600);
while(!Serial);
pinMode(LED_BUILTIN, OUTPUT);
if(!BLE.begin()) {
Serial.println("Starting BLE Failed!");
while(1);
}
BLE.setLocalName("BatteryMonitor");
BLE.setAdvertisedService(batteryService);
batteryService.addCharacteristic(batteryLevelChar);
BLE.addService(batteryService);
BLE.advertise();
Serial.println("Bluetooth Device Active, Waiting for Connections...");
}
void loop() {
BLEDevice central = BLE.central();
if(central) {
Serial.print("Connected to Central: ");
Serial.println(central.address());
digitalWrite(LED_BUILTIN, HIGH);
while(central.connected()) {
int battery = analogRead(A0);
int batteryLevel = map(battery, 0, 1023, 0, 100);
Serial.print("Battery Level % is now : ");
Serial.println(batteryLevel);
batteryLevelChar.writeValue(batteryLevel);
delay(200);
}
}
digitalWrite(LED_BUILTIN, LOW);
Serial.print("Disconnected from Central: ");
Serial.println(central.address());
}
My peripheral code is:
#include <ArduinoBLE.h>
void setup() {
Serial.begin(9600);
while (!Serial);
BLE.begin();
Serial.println("BLE Central - BatteryMonitor");
BLE.scanForUuid("1101");
}
void loop() {
BLEDevice peripheral = BLE.available();
if (peripheral) {
if (peripheral.localName() != "BatteryMonitor") {
return;
}
BLE.stopScan();
controlLed(peripheral);
}
}
void controlLed(BLEDevice peripheral) {
Serial.println("Connecting...");
if (peripheral.connect()) {
Serial.println("Connected");
} else {
Serial.println("Failed to connect!");
return;
}
Serial.println("Discovering attributes ...");
if (peripheral.discoverAttributes()) {
Serial.println("Attributes discovered");
} else {
Serial.println("Attribute discovery failed!");
peripheral.disconnect();
return;
}
BLECharacteristic ledCharacteristic = peripheral.characteristic("1101");
if (!ledCharacteristic) {
Serial.println("Peripheral does not have LED characteristic!");
peripheral.disconnect();
return;
} else if (!ledCharacteristic.canWrite()) {
Serial.println("Peripheral does not have a writable LED characteristic!");
peripheral.disconnect();
return;
}
while (peripheral.connected()) {
char v = ledCharacteristic.value();
Serial.println(v);
Serial.println("LED on");
delay(200);
}
}
The error in the peripheral code is:
Scan_2:61:37: error: invalid conversion from 'const uint8_t* {aka const unsigned char*}' to 'char' [-fpermissive]
char v = ledCharacteristic.value();
~~~~~~~~~~~~~~~~~~~~~~~^~
exit status 1
invalid conversion from 'const uint8_t* {aka const unsigned char*}' to 'char' [-fpermissive]
The error cannot be resolved.
Please fix the error and tell me if there is anything wrong in the code other than the error.