8 years, 9 months ago.

How to send temperature and humidity read from a sensor via BLE?

Hi everyone! I'm trying to write a code to read temperature and humidity from a sensor and transmit them via BLE, using the sensor HTS221 of the expansion board ST- X-NUCLEO-IKS01A1 and the BLE expansion board ST- X-NUCLEO-IDB04A1. Here below I reported my code. The two codes for transmitting a data via BLE and reading the temperature and humidity from the sensor have been tested separately and they work. Running this code, only the "printf" line 57 is displayed, then I think It stucks in "updateApplicationData" and, in particular, the function "HTS221_ReadTempHumi" (line 42) does not end correctly because the next prinft line 45 is not executed. Someone can find the error?

Thank you very much!

PS. The library used for the sensor: https://developer.mbed.org/teams/Delta/code/hts221/

#include "mbed.h"
#include "toolchain.h"
#include "ble/BLE.h"
#include "hts221.h"

BLE ble;

static Ticker ticker;
static bool triggerTempValueUpdate = false;
static DigitalOut alivenessLED(LED1, 1);
Serial pc(USBTX, USBRX);

struct ApplicationData_t {
    uint16_t applicationSpecificId; /* An ID used to identify temperature value in the manufacturer specific AD data field */
    float tmpSensorValue; /* User defined application data */
    float humiditySensorValue; /* User defined application data */
} PACKED;

void periodicCallback(void)
{
    alivenessLED = !alivenessLED; /* Do blinky on LED1 while we're waiting for BLE events. */
    /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
     heavy-weight sensor polling from the main thread (where we should be able to block safely, if
    needed). */
    triggerTempValueUpdate = true;
}

void setupApplicationData(ApplicationData_t &appData)
{
    static const uint16_t APP_SPECIFIC_ID_TEST = 0xFEFE;
    appData.applicationSpecificId = APP_SPECIFIC_ID_TEST;
    appData.tmpSensorValue= 0;
    appData.humiditySensorValue = 0;
    pc.printf("setupApplicationData: T=%f | H=%f\r\n ",appData.tmpSensorValue,appData.humiditySensorValue);
}

void updateApplicationData(ApplicationData_t &appData)
{
    static const uint16_t APP_SPECIFIC_ID_TEST = 0xFEFE;
    appData.applicationSpecificId = APP_SPECIFIC_ID_TEST;
    pc.printf("Reading...\r\n");
    HTS221_ReadTempHumi(&appData.tmpSensorValue, &appData.humiditySensorValue);
    //appData.tmpSensorValue += 1;
    //appData.humiditySensorValue += 1;
    pc.printf("updateApplicationData: T=%f | H=%f\r\n ",appData.tmpSensorValue,appData.humiditySensorValue);
}


void startAdvertisingTemperature(void)
{
    /* Setup advertising payload */
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED |GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER);
    ApplicationData_t appData;
    setupApplicationData(appData);
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&appData, sizeof(ApplicationData_t));
    pc.printf("1.startAdvertisingTemperature: T=%f  | H=%f\r\n ",appData.tmpSensorValue,appData.humiditySensorValue);
    
    /* Setup advertising parameters */
    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
    ble.gap().setAdvertisingInterval(500);
    ble.gap().startAdvertising();
}

int main(void)

{
    ticker.attach(periodicCallback, 2); /* trigger sensor polling every 10 seconds */
    ble.init();
    hts221_init();
    HTS221_Calib();
    startAdvertisingTemperature();
        
    while (true) {
        if (triggerTempValueUpdate) {
            
            // Do blocking calls or whatever hardware-specific action is necessary to poll the sensor.
            ApplicationData_t appData;
            
            updateApplicationData(appData);
            ble.gap().updateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA,(uint8_t *)&appData, sizeof(ApplicationData_t));
            pc.printf("3.updateAdvertisingPayload: T=%f  | H=%f\r\n ",appData.tmpSensorValue,appData.humiditySensorValue);
                         
            triggerTempValueUpdate = false;
            
            }
            
    ble.waitForEvent();
    
    }
}
Be the first to answer this question.