![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
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@1:023e1eae2048, 2017-04-18 (annotated)
- Committer:
- ledonger
- Date:
- Tue Apr 18 14:08:50 2017 +0000
- Revision:
- 1:023e1eae2048
- Parent:
- 0:d2c18f736df1
- Child:
- 2:c0bd998cb02f
Be careful to use mbed revision 125
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ledonger | 0:d2c18f736df1 | 1 | /* |
ledonger | 0:d2c18f736df1 | 2 | Tested with a STM32 L476 and the IDB05A1 BLE shield |
ledonger | 0:d2c18f736df1 | 3 | */ |
ledonger | 0:d2c18f736df1 | 4 | |
ledonger | 0:d2c18f736df1 | 5 | #include "mbed.h" |
ledonger | 0:d2c18f736df1 | 6 | #include "ble/BLE.h" |
ledonger | 0:d2c18f736df1 | 7 | #include "DHT11_BLEService.h" |
ledonger | 0:d2c18f736df1 | 8 | #include "DHT11.h" |
ledonger | 0:d2c18f736df1 | 9 | |
ledonger | 0:d2c18f736df1 | 10 | DigitalOut LED(LED1, 0); |
ledonger | 0:d2c18f736df1 | 11 | |
ledonger | 1:023e1eae2048 | 12 | const static char DEVICE_NAME[] = "LED"; |
ledonger | 0:d2c18f736df1 | 13 | static const uint16_t uuid16_list[] = {DHT11Service::DHT11_UUID}; |
ledonger | 0:d2c18f736df1 | 14 | |
ledonger | 0:d2c18f736df1 | 15 | DHT11Service *dht11ServicePtr;//Bluetooth service that manage information and ble updates |
ledonger | 1:023e1eae2048 | 16 | uint8_t tempValue; |
ledonger | 1:023e1eae2048 | 17 | uint8_t humiValue; |
ledonger | 0:d2c18f736df1 | 18 | |
ledonger | 0:d2c18f736df1 | 19 | DHT11 dht11(PC_10);//Data pin of the DTH11 sensor |
ledonger | 0:d2c18f736df1 | 20 | Ticker updateTick; |
ledonger | 0:d2c18f736df1 | 21 | |
ledonger | 0:d2c18f736df1 | 22 | void updateData(){ |
ledonger | 0:d2c18f736df1 | 23 | //LED = 1; |
ledonger | 0:d2c18f736df1 | 24 | if(!dht11.readData()){ |
ledonger | 0:d2c18f736df1 | 25 | for(int i = 0; i < 10; i++){LED = !LED; wait_ms(50);} |
ledonger | 0:d2c18f736df1 | 26 | } |
ledonger | 0:d2c18f736df1 | 27 | dht11ServicePtr->updateTemperatureValue(dht11.getTemperature()); |
ledonger | 0:d2c18f736df1 | 28 | dht11ServicePtr->updateHumidityValue(dht11.getHumidity()); |
ledonger | 0:d2c18f736df1 | 29 | //LED = 0; |
ledonger | 0:d2c18f736df1 | 30 | } |
ledonger | 0:d2c18f736df1 | 31 | |
ledonger | 0:d2c18f736df1 | 32 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
ledonger | 0:d2c18f736df1 | 33 | { |
ledonger | 0:d2c18f736df1 | 34 | (void)params; |
ledonger | 0:d2c18f736df1 | 35 | BLE::Instance().gap().startAdvertising(); // restart advertising |
ledonger | 0:d2c18f736df1 | 36 | } |
ledonger | 0:d2c18f736df1 | 37 | |
ledonger | 0:d2c18f736df1 | 38 | /** |
ledonger | 0:d2c18f736df1 | 39 | * This callback allows the LEDService to receive updates to the ledState Characteristic. |
ledonger | 0:d2c18f736df1 | 40 | * |
ledonger | 0:d2c18f736df1 | 41 | * @param[in] params |
ledonger | 0:d2c18f736df1 | 42 | * Information about the characterisitc being updated. |
ledonger | 0:d2c18f736df1 | 43 | */ |
ledonger | 0:d2c18f736df1 | 44 | /*void onDataWrittenCallback(const GattWriteCallbackParams *params) { |
ledonger | 0:d2c18f736df1 | 45 | if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) { |
ledonger | 0:d2c18f736df1 | 46 | actuatedLED = *(params->data); |
ledonger | 0:d2c18f736df1 | 47 | } |
ledonger | 0:d2c18f736df1 | 48 | }*/ |
ledonger | 0:d2c18f736df1 | 49 | |
ledonger | 0:d2c18f736df1 | 50 | /** |
ledonger | 0:d2c18f736df1 | 51 | * This function is called when the ble initialization process has failled |
ledonger | 0:d2c18f736df1 | 52 | */ |
ledonger | 0:d2c18f736df1 | 53 | void onBleInitError(BLE &ble, ble_error_t error) |
ledonger | 0:d2c18f736df1 | 54 | { |
ledonger | 0:d2c18f736df1 | 55 | /* Initialization error handling should go here */ |
ledonger | 0:d2c18f736df1 | 56 | } |
ledonger | 0:d2c18f736df1 | 57 | |
ledonger | 0:d2c18f736df1 | 58 | /** |
ledonger | 0:d2c18f736df1 | 59 | * Callback triggered when the ble initialization process has finished |
ledonger | 0:d2c18f736df1 | 60 | */ |
ledonger | 0:d2c18f736df1 | 61 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
ledonger | 0:d2c18f736df1 | 62 | { |
ledonger | 0:d2c18f736df1 | 63 | BLE& ble = params->ble; |
ledonger | 0:d2c18f736df1 | 64 | ble_error_t error = params->error; |
ledonger | 0:d2c18f736df1 | 65 | |
ledonger | 0:d2c18f736df1 | 66 | if (error != BLE_ERROR_NONE) { |
ledonger | 0:d2c18f736df1 | 67 | /* In case of error, forward the error handling to onBleInitError */ |
ledonger | 0:d2c18f736df1 | 68 | onBleInitError(ble, error); |
ledonger | 0:d2c18f736df1 | 69 | return; |
ledonger | 0:d2c18f736df1 | 70 | } |
ledonger | 0:d2c18f736df1 | 71 | |
ledonger | 0:d2c18f736df1 | 72 | /* Ensure that it is the default instance of BLE */ |
ledonger | 0:d2c18f736df1 | 73 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
ledonger | 0:d2c18f736df1 | 74 | return; |
ledonger | 0:d2c18f736df1 | 75 | } |
ledonger | 0:d2c18f736df1 | 76 | |
ledonger | 0:d2c18f736df1 | 77 | ble.gap().onDisconnection(disconnectionCallback); |
ledonger | 0:d2c18f736df1 | 78 | //ble.gattServer().onDataWritten(onDataWrittenCallback); |
ledonger | 0:d2c18f736df1 | 79 | |
ledonger | 0:d2c18f736df1 | 80 | tempValue = 20; |
ledonger | 0:d2c18f736df1 | 81 | humiValue = 10; |
ledonger | 0:d2c18f736df1 | 82 | dht11ServicePtr = new DHT11Service(ble, tempValue,humiValue); |
ledonger | 0:d2c18f736df1 | 83 | |
ledonger | 0:d2c18f736df1 | 84 | updateTick.attach(&updateData,5.0); |
ledonger | 0:d2c18f736df1 | 85 | |
ledonger | 0:d2c18f736df1 | 86 | /* setup advertising */ |
ledonger | 0:d2c18f736df1 | 87 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
ledonger | 0:d2c18f736df1 | 88 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
ledonger | 0:d2c18f736df1 | 89 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
ledonger | 0:d2c18f736df1 | 90 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
ledonger | 0:d2c18f736df1 | 91 | ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ |
ledonger | 0:d2c18f736df1 | 92 | ble.gap().startAdvertising(); |
ledonger | 0:d2c18f736df1 | 93 | |
ledonger | 0:d2c18f736df1 | 94 | while (true) { |
ledonger | 0:d2c18f736df1 | 95 | ble.waitForEvent(); |
ledonger | 0:d2c18f736df1 | 96 | } |
ledonger | 0:d2c18f736df1 | 97 | } |
ledonger | 0:d2c18f736df1 | 98 | |
ledonger | 1:023e1eae2048 | 99 | |
ledonger | 0:d2c18f736df1 | 100 | int main(void) |
ledonger | 0:d2c18f736df1 | 101 | { |
ledonger | 0:d2c18f736df1 | 102 | BLE &ble = BLE::Instance(); |
ledonger | 0:d2c18f736df1 | 103 | |
ledonger | 0:d2c18f736df1 | 104 | ble.init(bleInitComplete); |
ledonger | 0:d2c18f736df1 | 105 | } |
ledonger | 0:d2c18f736df1 | 106 |