Arduinoble analog data send

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.

?

Please remember to use code tags when posting code

Sorry. I didn't know the code tags.

I tried changing it to const char*v =, but it doesn't work.

Sorry, it should've been

const uint8_t*

Please don't ever post those words again.

I tried changing it to const uint8_t*

error code:
error: invalid conversion from 'const uint8_t* {aka const unsigned char*}' to 'long long unsigned int' [-fpermissive]

 Serial.println(v);

                 ^

exit status 1
no matching function for call to 'println(const uint8_t*&)'

So, cast it to char* for the print.

What do you expect to see?

image

The value input to the A0 pin is 0~100.
I want to check this value as it is through BLE communication.
However, other modules cannot read this value.

Your central code has defined a BLE characteristic as unsigned char:

Your peripheral code is expecting a BLE characteristic which is defined as char:

Suggest changing the ledCharacteristic type as BLEUnsignedCharCharacteristic.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.