Project aiming to do a Bluetooth Low Energy IoT devices, which measure temperature and humidity. Bluetooth achieved with the IDB05A1 shield. Temperature/humidity achieved with a DHT11 sensor. Project working, tested on an STM32 L476 board.
Dependencies: BLE_API X_NUCLEO_IDB0XA1 mbed
main.cpp
00001 /* 00002 Tested with a STM32 L476 and the IDB05A1 BLE shield 00003 */ 00004 00005 #include "mbed.h" 00006 #include "ble/BLE.h" 00007 #include "DHT11_BLEService.h" 00008 #include "DHT11.h" 00009 00010 DigitalOut LED(LED1, 0); 00011 00012 const static char DEVICE_NAME[] = "DHT11"; 00013 static const uint16_t uuid16_list[] = {DHT11Service::DHT11_UUID}; 00014 00015 DHT11Service *dht11ServicePtr;//Bluetooth service that manage information and ble updates 00016 uint8_t tempValue; 00017 uint8_t humiValue; 00018 00019 DHT11 dht11(PC_10);//Data pin of the DTH11 sensor 00020 Ticker updateTick; 00021 00022 void updateData(){ 00023 //LED = 1; 00024 if(!dht11.readData()){ 00025 for(int i = 0; i < 10; i++){LED = !LED; wait_ms(50);} 00026 } 00027 dht11ServicePtr->updateTemperatureValue(dht11.getTemperature()); 00028 dht11ServicePtr->updateHumidityValue(dht11.getHumidity()); 00029 //LED = 0; 00030 } 00031 00032 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00033 { 00034 (void)params; 00035 BLE::Instance().gap().startAdvertising(); // restart advertising 00036 } 00037 00038 /** 00039 * This callback allows the LEDService to receive updates to the ledState Characteristic. 00040 * 00041 * @param[in] params 00042 * Information about the characterisitc being updated. 00043 */ 00044 /*void onDataWrittenCallback(const GattWriteCallbackParams *params) { 00045 if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) { 00046 actuatedLED = *(params->data); 00047 } 00048 }*/ 00049 00050 /** 00051 * This function is called when the ble initialization process has failled 00052 */ 00053 void onBleInitError(BLE &ble, ble_error_t error) 00054 { 00055 /* Initialization error handling should go here */ 00056 } 00057 00058 /** 00059 * Callback triggered when the ble initialization process has finished 00060 */ 00061 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 00062 { 00063 BLE& ble = params->ble; 00064 ble_error_t error = params->error; 00065 00066 if (error != BLE_ERROR_NONE) { 00067 /* In case of error, forward the error handling to onBleInitError */ 00068 onBleInitError(ble, error); 00069 return; 00070 } 00071 00072 /* Ensure that it is the default instance of BLE */ 00073 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { 00074 return; 00075 } 00076 00077 ble.gap().onDisconnection(disconnectionCallback); 00078 //ble.gattServer().onDataWritten(onDataWrittenCallback); 00079 00080 tempValue = 20; 00081 humiValue = 10; 00082 dht11ServicePtr = new DHT11Service(ble, tempValue,humiValue); 00083 00084 updateTick.attach(&updateData,5.0); 00085 00086 /* setup advertising */ 00087 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00088 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); 00089 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00090 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00091 ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ 00092 ble.gap().startAdvertising(); 00093 00094 while (true) { 00095 ble.waitForEvent(); 00096 } 00097 } 00098 00099 00100 int main(void) 00101 { 00102 BLE &ble = BLE::Instance(); 00103 00104 ble.init(bleInitComplete); 00105 } 00106
Generated on Wed Jul 13 2022 01:51:01 by 1.7.2