Hi all,
I'm writing bluetooth application that transmits accelerometer data from a peripheral to a central. The peripheral is a xiao nRF52840 sense, and the central a xiao ESP32C3. In general, thanks to code from Klaus_K that I found, the communication works, i.e. the central receives data from the peripheral. However, I'm having troubles with valueUpdated() that either doesn't work, or maybe I didn't understand it correctly.
On the peripheral side, I set up the BLECharacteristic like this:
BLECharacteristic accCharacteristic(BLE_UUID_ACC_CHAR, BLERead | BLENotify, sizeof accData.bytes);
With the definition of a union data structure, the data is sent with
accCharacteristic.writeValue(accData.bytes, sizeof accData.bytes);
On the central side, I create the BLECharacteristic
BLECharacteristic accCharacteristic = peripheral.characteristic(BLE_UUID_ACC_CHAR);
Since the peripheral side is a notification service, I understand that the central has to subscribe to it with
bool sub = accCharacteristic.subscribe();
This returns true on the central, and the peripheral also prints that something has subscribed to the characteristic, so I must conclude that the subscription is working. The peripheral is writing new accelerometer values every second. Now, I thought if I use valueUpdated(), the central will automatically read the new values also every second:
while (peripheral.connected()) {
Serial.println(accCharacteristic.valueUpdated());
if (accCharacteristic.valueUpdated()) {
accCharacteristic.readValue(accData.bytes, sizeof accData.bytes);
Serial.println(accData.values[0]);
}
}
However, valueUpdated() always prints 0, so new values are never read. If I remove the if-clause, then the central reads correct accelerometer values every loop, i.e. it reads/prints the same value for 1 second, then another value for 1 second (since the peripheral has written new values).
So my question is, if I'm either missing something and this doesn't work as I imagine, or if this could be caused by some incompatibility between the nRF52840 sense and the ESP32C3?
Thanks for any insight anybody might have!