This is a simple program to work with RedBearLab BLE Controller App. Type something from the Terminal to send to the BLEController App or vice verse. Characteristics received from App will print on Terminal.

Dependencies:   BLE_API BME280 DHT mbed nRF51822

Fork of nRF51822_SimpleChat by RedBearLab

Committer:
orhanhenrik
Date:
Mon Jan 29 10:58:48 2018 +0000
Revision:
4:86bf080e3453
Parent:
3:b3f6c612b603
hello

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RedBearLab 0:cffe8ac1bdf0 1 #include "mbed.h"
RedBearLab 2:4b66b69c7ecb 2 #include "ble/BLE.h"
orhanhenrik 4:86bf080e3453 3 #include "BME280.h"
RedBearLab 0:cffe8ac1bdf0 4
RedBearLab 2:4b66b69c7ecb 5 BLE ble;
RedBearLab 0:cffe8ac1bdf0 6
orhanhenrik 4:86bf080e3453 7 // Serial pc(USBTX, USBRX);
orhanhenrik 4:86bf080e3453 8
orhanhenrik 4:86bf080e3453 9 BME280 bme(P0_3, P0_4);
orhanhenrik 4:86bf080e3453 10
orhanhenrik 4:86bf080e3453 11 const static uint16_t environment_ser_uuid = 0xA000;
RedBearLab 0:cffe8ac1bdf0 12
orhanhenrik 4:86bf080e3453 13 const static uint16_t temperature_chr_uuid = 0xA001;
orhanhenrik 4:86bf080e3453 14 uint8_t tempPayload[3] = { 0, 0, 0 };
orhanhenrik 4:86bf080e3453 15 GattCharacteristic temp_chr ( temperature_chr_uuid, tempPayload, sizeof(tempPayload), sizeof(tempPayload),
orhanhenrik 4:86bf080e3453 16 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY |
orhanhenrik 4:86bf080e3453 17 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
RedBearLab 0:cffe8ac1bdf0 18
orhanhenrik 4:86bf080e3453 19 const static uint16_t humidity_chr_uuid = 0xA002;
orhanhenrik 4:86bf080e3453 20 uint8_t humidityPayload[3] = { 0, 0, 0 };
orhanhenrik 4:86bf080e3453 21 GattCharacteristic humidity_chr ( humidity_chr_uuid, humidityPayload, sizeof(humidityPayload), sizeof(humidityPayload),
orhanhenrik 4:86bf080e3453 22 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY |
orhanhenrik 4:86bf080e3453 23 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
orhanhenrik 4:86bf080e3453 24
orhanhenrik 4:86bf080e3453 25 const static uint16_t pressure_chr_uuid = 0xA003;
orhanhenrik 4:86bf080e3453 26 uint8_t pressurePayload[3] = { 0, 0, 0 };
orhanhenrik 4:86bf080e3453 27 GattCharacteristic pressure_chr ( pressure_chr_uuid, pressurePayload, sizeof(pressurePayload), sizeof(pressurePayload),
orhanhenrik 4:86bf080e3453 28 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY |
orhanhenrik 4:86bf080e3453 29 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
RedBearLab 0:cffe8ac1bdf0 30
RedBearLab 0:cffe8ac1bdf0 31
orhanhenrik 4:86bf080e3453 32 GattCharacteristic *envChars[] = { &temp_chr, &humidity_chr, &pressure_chr };
orhanhenrik 4:86bf080e3453 33 GattService envService(environment_ser_uuid, envChars, sizeof(envChars) / sizeof(GattCharacteristic *));
RedBearLab 0:cffe8ac1bdf0 34
RedBearLab 0:cffe8ac1bdf0 35
RedBearLab 0:cffe8ac1bdf0 36
orhanhenrik 4:86bf080e3453 37 const static uint16_t count_ser_uuid = 0xB000;
orhanhenrik 4:86bf080e3453 38 const static uint16_t count_chr_uuid = 0xB001;
orhanhenrik 4:86bf080e3453 39 uint8_t counter[1] = { 0 };
orhanhenrik 4:86bf080e3453 40 GattCharacteristic count_chr ( count_chr_uuid, counter, sizeof(counter), sizeof(counter),
orhanhenrik 4:86bf080e3453 41 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY |
orhanhenrik 4:86bf080e3453 42 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
orhanhenrik 4:86bf080e3453 43 GattCharacteristic *count_chrs[] = {&count_chr, };
orhanhenrik 4:86bf080e3453 44 GattService countService(count_ser_uuid, count_chrs, sizeof(count_chrs) / sizeof(GattCharacteristic *));
RedBearLab 0:cffe8ac1bdf0 45
orhanhenrik 4:86bf080e3453 46
RedBearLab 0:cffe8ac1bdf0 47
orhanhenrik 4:86bf080e3453 48 static const uint16_t uuid16_list[] = {environment_ser_uuid, count_ser_uuid};
RedBearLab 0:cffe8ac1bdf0 49
RedBearLab 0:cffe8ac1bdf0 50
RedBearLab 0:cffe8ac1bdf0 51
RedBearLab 0:cffe8ac1bdf0 52
RedBearLab 0:cffe8ac1bdf0 53
RedBearLab 0:cffe8ac1bdf0 54
orhanhenrik 4:86bf080e3453 55 static volatile bool triggerSensorPolling = false;
RedBearLab 0:cffe8ac1bdf0 56
orhanhenrik 4:86bf080e3453 57 void periodicCallback(void);
orhanhenrik 4:86bf080e3453 58 void update_values(void);
RedBearLab 0:cffe8ac1bdf0 59
RedBearLab 0:cffe8ac1bdf0 60
orhanhenrik 4:86bf080e3453 61 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
orhanhenrik 4:86bf080e3453 62 {
orhanhenrik 4:86bf080e3453 63 // pc.printf("Disconnected \r\n");
orhanhenrik 4:86bf080e3453 64 // pc.printf("Restart advertising \r\n");
orhanhenrik 4:86bf080e3453 65 ble.startAdvertising();
orhanhenrik 4:86bf080e3453 66 }
orhanhenrik 4:86bf080e3453 67 void tare(void);
orhanhenrik 4:86bf080e3453 68 int main(void)
orhanhenrik 4:86bf080e3453 69 {
orhanhenrik 4:86bf080e3453 70 Ticker ticker;
orhanhenrik 4:86bf080e3453 71 ticker.attach(periodicCallback, 2.0 );
RedBearLab 0:cffe8ac1bdf0 72
orhanhenrik 4:86bf080e3453 73 ble.init();
orhanhenrik 4:86bf080e3453 74 ble.onDisconnection(disconnectionCallback);
orhanhenrik 4:86bf080e3453 75 // pc.printf("hello\r\n");
orhanhenrik 4:86bf080e3453 76 // pc.printf("hi\r\n");
orhanhenrik 4:86bf080e3453 77 // pc.baud(9600);
orhanhenrik 4:86bf080e3453 78 // pc.printf("SimpleChat Init \r\n");
orhanhenrik 4:86bf080e3453 79
orhanhenrik 4:86bf080e3453 80 // setup advertising
orhanhenrik 4:86bf080e3453 81 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
orhanhenrik 4:86bf080e3453 82 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
orhanhenrik 4:86bf080e3453 83 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
orhanhenrik 4:86bf080e3453 84 (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1);
orhanhenrik 4:86bf080e3453 85 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
orhanhenrik 4:86bf080e3453 86
orhanhenrik 4:86bf080e3453 87 // 100ms; in multiples of 0.625ms.
orhanhenrik 4:86bf080e3453 88 ble.setAdvertisingInterval(160);
orhanhenrik 4:86bf080e3453 89
orhanhenrik 4:86bf080e3453 90 ble.addService(countService);
orhanhenrik 4:86bf080e3453 91 ble.addService(envService);
orhanhenrik 4:86bf080e3453 92
orhanhenrik 4:86bf080e3453 93 ble.startAdvertising();
orhanhenrik 4:86bf080e3453 94 // pc.printf("Advertising Start \r\n");
orhanhenrik 4:86bf080e3453 95
orhanhenrik 4:86bf080e3453 96 while(1) {
orhanhenrik 4:86bf080e3453 97 if (triggerSensorPolling) {
orhanhenrik 4:86bf080e3453 98 triggerSensorPolling = false;
orhanhenrik 4:86bf080e3453 99 update_values();
orhanhenrik 4:86bf080e3453 100 } else {
orhanhenrik 4:86bf080e3453 101 ble.waitForEvent();
orhanhenrik 4:86bf080e3453 102 }
orhanhenrik 4:86bf080e3453 103 }
orhanhenrik 4:86bf080e3453 104 }
orhanhenrik 4:86bf080e3453 105
orhanhenrik 4:86bf080e3453 106 void periodicCallback(void)
orhanhenrik 4:86bf080e3453 107 {
orhanhenrik 4:86bf080e3453 108 triggerSensorPolling = true;
orhanhenrik 4:86bf080e3453 109 }
RedBearLab 0:cffe8ac1bdf0 110
RedBearLab 0:cffe8ac1bdf0 111
orhanhenrik 4:86bf080e3453 112 void update_values(void) {
orhanhenrik 4:86bf080e3453 113 float temp = bme.getTemperature();
orhanhenrik 4:86bf080e3453 114 float humidity = bme.getHumidity();
orhanhenrik 4:86bf080e3453 115 float pressure = bme.getPressure();
orhanhenrik 4:86bf080e3453 116
orhanhenrik 4:86bf080e3453 117 if (ble.getGapState().connected ) {
orhanhenrik 4:86bf080e3453 118 uint32_t temp_ = temp * 10000;
orhanhenrik 4:86bf080e3453 119 tempPayload[0] = (uint8_t)((temp_ & 0xFF0000) >> 16);
orhanhenrik 4:86bf080e3453 120 tempPayload[1] = (uint8_t)((temp_ & 0x00FF00) >> 8);
orhanhenrik 4:86bf080e3453 121 tempPayload[2] = (uint8_t)(temp_ & 0x0000FF);
orhanhenrik 4:86bf080e3453 122 ble.updateCharacteristicValue(temp_chr.getValueAttribute().getHandle(), tempPayload, sizeof(tempPayload));
orhanhenrik 4:86bf080e3453 123
orhanhenrik 4:86bf080e3453 124 uint32_t humidity_ = humidity * 10000;
orhanhenrik 4:86bf080e3453 125 humidityPayload[0] = (uint8_t)((humidity_ & 0xFF0000) >> 16);
orhanhenrik 4:86bf080e3453 126 humidityPayload[1] = (uint8_t)((humidity_ & 0x00FF00) >> 8);
orhanhenrik 4:86bf080e3453 127 humidityPayload[2] = (uint8_t)(humidity_ & 0x0000FF);
orhanhenrik 4:86bf080e3453 128 ble.updateCharacteristicValue(humidity_chr.getValueAttribute().getHandle(), humidityPayload, sizeof(humidityPayload));
orhanhenrik 4:86bf080e3453 129
orhanhenrik 4:86bf080e3453 130 uint32_t pressure_ = pressure * 10000;
orhanhenrik 4:86bf080e3453 131 pressurePayload[0] = (uint8_t)((pressure_ & 0xFF0000) >> 16);
orhanhenrik 4:86bf080e3453 132 pressurePayload[1] = (uint8_t)((pressure_ & 0x00FF00) >> 8);
orhanhenrik 4:86bf080e3453 133 pressurePayload[2] = (uint8_t)(pressure_ & 0x0000FF);
orhanhenrik 4:86bf080e3453 134 ble.updateCharacteristicValue(pressure_chr.getValueAttribute().getHandle(), pressurePayload, sizeof(pressurePayload));
orhanhenrik 4:86bf080e3453 135 }
orhanhenrik 4:86bf080e3453 136
orhanhenrik 4:86bf080e3453 137 }
RedBearLab 0:cffe8ac1bdf0 138