Bluefruit Library Always Reads Zero When Removing Characteristcs

Hello!

I am currently working on a project where I am using the Bluefruit library in combination with the Seeed Studio XIAO nRF52840 to read some values from an ESP32-S3 via BLE.

Now I run into a bug I do not know how to resolve. Previously I emitted three characteristics (inside temperature, outside temperature and time), however now I removed the time characteristics on the ESP32 emitting those values.

I adapted my BLE code on the Seeed XIAO as well and now i am always getting 0 when calling bleInsideTempCharacteristic.read(tempStr, 4)

When I put the time characteristic back in everything works again. I can also read the BLE characteristics using my phone and the two temperatures are still correctly emitted and not just 0.

Here is the relevant code BEFORE removing the time characeristics:

BLEClientService bleService(SERVICE_UUID);
BLEClientCharacteristic bleInsideTempCharacteristic(INSIDE_TEMP_CHARAC_UUID);
BLEClientCharacteristic bleOutsideTempCharacteristic(OUTSIDE_TEMP_CHARAC_UUID);
BLEClientCharacteristic bleTimeCharacteristic(TIME_CHARAC_UUID);

void bleClientConnectedCallback(uint16_t connectionHandle)
{
    if (bleInsideTempCharacteristic.discover() && bleOutsideTempCharacteristic.discover() && bleTimeCharacteristic.discover())
    {
        Serial.println("Found it: inside, outside temperature and time characteristics");

        float insideTemp, outsideTemp;
        char tempStr[5];
        tempStr[4] = '\0'; // set the last character directly to null terminator as we only write to the four bytes before
        bleInsideTempCharacteristic.read(tempStr, 4);
        insideTemp = String(tempStr).toFloat();
        bleOutsideTempCharacteristic.read(tempStr, 4);
        outsideTemp = String(tempStr).toFloat();
        Serial.println("Temps:");
        Serial.println(insideTemp);
        Serial.println(outsideTemp);

        char dateTimeStr[17];
        bleTimeCharacteristic.read(dateTimeStr, 17);
        dateTimeStr[16] = '\0';
        Serial.print("Time: ");
        Serial.println(dateTimeStr);
    }
    else
    {
        Serial.println("Did not find the required characeristics");
    }
    Bluefruit.Scanner.stop();
    Bluefruit.disconnect(connectionHandle);
#if DEBUG_SERIAL == true
    Serial.print("Update took: ");
    Serial.println((millis() - lastUpdateBeginMillis) / 1000.0);
#endif
    sd_app_evt_wait();
}

void startBleScannerForTemperatureCharacteristics(TimerHandle_t xTimerID)
{
    (void)xTimerID;
#if DEBUG_SERIAL == true
    lastUpdateBeginMillis = millis();
#endif
    Bluefruit.autoConnLed(false);
    Bluefruit.setName("BLE Weather Display Client / Central");
    // Initialize the BLE Service client and characeristics
    bleService.begin();
    bleInsideTempCharacteristic.begin();
    bleOutsideTempCharacteristic.begin();
    bleTimeCharacteristic.begin();

    // Increase Blink rate to different from PrPh advertising mode
    Bluefruit.setConnLedInterval(250); // TODO needed?
    // Callbacks for Central
    Bluefruit.Central.setConnectCallback(bleClientConnectedCallback);
    Bluefruit.Central.setDisconnectCallback(bleClientDisconnectedCallback);
    /* Start Central Scanning
     * - Enable auto scan if disconnected
     * - Interval = 100 ms, window = 80 ms
     * - Don't use active scan
     * - Filter only accept HRM service
     * - Start(timeout) with timeout = 0 will scan forever (until connected), 10 will scan for 10 seconds
     */
    Bluefruit.Scanner.setRxCallback(bleServiceFoundCallback);
    Bluefruit.Scanner.restartOnDisconnect(false);
    Bluefruit.Scanner.setInterval(160, 80); // in unit of 0.625 ms
    Bluefruit.Scanner.filterUuid(bleService.uuid);
    Bluefruit.Scanner.useActiveScan(false);
    Bluefruit.Scanner.start(10);
}

And this is the code AFTER removing the timme charcteristics where insideTemp as well as outsideTemp are always zero:

BLEClientService bleService(SERVICE_UUID);
BLEClientCharacteristic bleInsideTempCharacteristic(INSIDE_TEMP_CHARAC_UUID);
BLEClientCharacteristic bleOutsideTempCharacteristic(OUTSIDE_TEMP_CHARAC_UUID);

void bleClientConnectedCallback(uint16_t connectionHandle)
{
    Serial.println("Connected");
    Serial.println("Discovering Temperature Service ... ");
    if (!bleService.discover(connectionHandle))
    {
        Serial.println("Service not found!");
        Bluefruit.disconnect(connectionHandle);
        sd_app_evt_wait();
        return;
    }

    if (bleInsideTempCharacteristic.discover() && bleOutsideTempCharacteristic.discover())
    {
        Serial.println("Found it: inside, outside temperature characteristics");

        float insideTemp, outsideTemp;
        char tempStr[5];
        tempStr[4] = '\0'; // set the last character directly to null terminator as we only write to the four bytes before
        bleInsideTempCharacteristic.read(tempStr, 4);
        insideTemp = String(tempStr).toFloat();
        bleOutsideTempCharacteristic.read(tempStr, 4);
        outsideTemp = String(tempStr).toFloat();
        Serial.println("Temps:");
        Serial.println(insideTemp); // NOW ALWAYS 0 - WHY?
        Serial.println(outsideTemp); // NOW ALWAYS 0 - WHY?
    }
    else
    {
        Serial.println("Did not find the required characeristics");
    }
    Bluefruit.Scanner.stop();
    Bluefruit.disconnect(connectionHandle);
#if DEBUG_SERIAL == true
    Serial.print("Update took: ");
    Serial.println((millis() - lastUpdateBeginMillis) / 1000.0);
#endif
    sd_app_evt_wait();
}

void startBleScannerForTemperatureCharacteristics(TimerHandle_t xTimerID)
{
    (void)xTimerID;
#if DEBUG_SERIAL == true
    lastUpdateBeginMillis = millis();
#endif
    Bluefruit.autoConnLed(false);
    Bluefruit.setName("BLE Weather Display Client / Central");
    // Initialize the BLE Service client and characeristics
    bleService.begin();
    bleInsideTempCharacteristic.begin();
    bleOutsideTempCharacteristic.begin();

    // Increase Blink rate to different from PrPh advertising mode
    Bluefruit.setConnLedInterval(250); // TODO needed?
    // Callbacks for Central
    Bluefruit.Central.setConnectCallback(bleClientConnectedCallback);
    Bluefruit.Central.setDisconnectCallback(bleClientDisconnectedCallback);
    /* Start Central Scanning
     * - Enable auto scan if disconnected
     * - Interval = 100 ms, window = 80 ms
     * - Don't use active scan
     * - Filter only accept HRM service
     * - Start(timeout) with timeout = 0 will scan forever (until connected), 10 will scan for 10 seconds
     */
    Bluefruit.Scanner.setRxCallback(bleServiceFoundCallback);
    Bluefruit.Scanner.restartOnDisconnect(false);
    Bluefruit.Scanner.setInterval(160, 80); // in unit of 0.625 ms
    Bluefruit.Scanner.filterUuid(bleService.uuid);
    Bluefruit.Scanner.useActiveScan(false);
    Bluefruit.Scanner.start(10);
}

Can someone help me find out what is going on? Thanks

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