BLE test with temp
Dependencies: BLE_API X_NUCLEO_IDB0XA1 X_NUCLEO_IKS01A2 mbed
Fork of BLE_HeartRate_IDB0XA1 by
main.cpp@22:ef03ce1c6c99, 2018-08-13 (annotated)
- Committer:
- tomotakaw
- Date:
- Mon Aug 13 07:09:13 2018 +0000
- Revision:
- 22:ef03ce1c6c99
- Parent:
- 21:0e7c08f5386f
BLE test with temp sensor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
screamer | 0:eb7f02ad28a7 | 1 | #include "mbed.h" |
tomotakaw | 22:ef03ce1c6c99 | 2 | #include "BLE.h" |
tomotakaw | 22:ef03ce1c6c99 | 3 | #include "XNucleoIKS01A2.h" |
screamer | 0:eb7f02ad28a7 | 4 | |
tomotakaw | 22:ef03ce1c6c99 | 5 | #if 1 //1:シリアルモニタ表示, 0:非表示 |
tomotakaw | 22:ef03ce1c6c99 | 6 | Serial pc(USBTX, USBRX); |
tomotakaw | 22:ef03ce1c6c99 | 7 | #define DEBUG(...) { pc.printf(__VA_ARGS__); } |
tomotakaw | 22:ef03ce1c6c99 | 8 | #else |
tomotakaw | 22:ef03ce1c6c99 | 9 | #define DEBUG(...) |
tomotakaw | 22:ef03ce1c6c99 | 10 | #endif |
screamer | 0:eb7f02ad28a7 | 11 | |
tomotakaw | 22:ef03ce1c6c99 | 12 | /* Instantiate the expansion board */ |
tomotakaw | 22:ef03ce1c6c99 | 13 | static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5); |
tomotakaw | 22:ef03ce1c6c99 | 14 | static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor; |
screamer | 0:eb7f02ad28a7 | 15 | |
tomotakaw | 22:ef03ce1c6c99 | 16 | float value1, value2; |
apalmieri | 13:227a0149b677 | 17 | |
tomotakaw | 22:ef03ce1c6c99 | 18 | DigitalOut led(LED1); |
tomotakaw | 22:ef03ce1c6c99 | 19 | Ticker ticker; |
tomotakaw | 22:ef03ce1c6c99 | 20 | BLE ble; |
tomotakaw | 22:ef03ce1c6c99 | 21 | const static char DEVICE_NAME[] = "CD1"; |
tomotakaw | 22:ef03ce1c6c99 | 22 | static Gap::ConnectionParams_t connectionParams; |
tomotakaw | 22:ef03ce1c6c99 | 23 | uint16_t uuid_list[] = {GattService::UUID_DEVICE_INFORMATION_SERVICE}; |
tomotakaw | 22:ef03ce1c6c99 | 24 | uint8_t msg = 0; |
tomotakaw | 22:ef03ce1c6c99 | 25 | uint16_t msg16 = 0; |
tomotakaw | 22:ef03ce1c6c99 | 26 | uint8_t msg_low = 0; |
tomotakaw | 22:ef03ce1c6c99 | 27 | uint8_t msg_high = 0; |
tomotakaw | 22:ef03ce1c6c99 | 28 | uint8_t error_flag = 0; |
screamer | 0:eb7f02ad28a7 | 29 | |
tomotakaw | 22:ef03ce1c6c99 | 30 | //ここからサービスとキャラクタリスティックの用意 |
tomotakaw | 22:ef03ce1c6c99 | 31 | static const uint8_t UUID_CHAR_DATA[] = {0xFF,0xCF,0xFC,0xCC,0xFF,0xCF,0xFC,0xCC,0xFF,0xCF,0xFC,0xCC,0xFF,0xCF,0xFC,0xCC};//なんでもいい |
tomotakaw | 22:ef03ce1c6c99 | 32 | GattCharacteristic customCharastic(UUID_CHAR_DATA, (uint8_t *)&msg16, sizeof(msg16), sizeof(msg16), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
tomotakaw | 22:ef03ce1c6c99 | 33 | GattCharacteristic *customChars[] = {&customCharastic}; |
tomotakaw | 22:ef03ce1c6c99 | 34 | GattService customService(GattService::UUID_DEVICE_INFORMATION_SERVICE, customChars, sizeof(customChars) / sizeof(GattCharacteristic *)); |
screamer | 0:eb7f02ad28a7 | 35 | |
tomotakaw | 22:ef03ce1c6c99 | 36 | //BLE接続したら呼ばれるやつ |
tomotakaw | 22:ef03ce1c6c99 | 37 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { |
tomotakaw | 22:ef03ce1c6c99 | 38 | DEBUG("Connected!\n\r"); |
tomotakaw | 22:ef03ce1c6c99 | 39 | ble.getPreferredConnectionParams(&connectionParams); |
tomotakaw | 22:ef03ce1c6c99 | 40 | connectionParams.minConnectionInterval = 7.5; |
tomotakaw | 22:ef03ce1c6c99 | 41 | connectionParams.maxConnectionInterval = 10; |
tomotakaw | 22:ef03ce1c6c99 | 42 | connectionParams.slaveLatency = 0; |
tomotakaw | 22:ef03ce1c6c99 | 43 | if (ble.gap().updateConnectionParams(params->handle, &connectionParams) != BLE_ERROR_NONE) { |
tomotakaw | 22:ef03ce1c6c99 | 44 | DEBUG("Failed to update\n\r"); |
screamer | 0:eb7f02ad28a7 | 45 | } |
screamer | 0:eb7f02ad28a7 | 46 | } |
apalmieri | 13:227a0149b677 | 47 | |
tomotakaw | 22:ef03ce1c6c99 | 48 | //BLE切断したら呼ばれるやつ |
tomotakaw | 22:ef03ce1c6c99 | 49 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { |
tomotakaw | 22:ef03ce1c6c99 | 50 | DEBUG("Disconnected!\n\r"); |
tomotakaw | 22:ef03ce1c6c99 | 51 | ticker.detach(); |
tomotakaw | 22:ef03ce1c6c99 | 52 | ble.gap().startAdvertising(); |
tomotakaw | 22:ef03ce1c6c99 | 53 | } |
apalmieri | 13:227a0149b677 | 54 | |
tomotakaw | 22:ef03ce1c6c99 | 55 | //周期的に呼ばれるやつ |
tomotakaw | 22:ef03ce1c6c99 | 56 | void tickerCallback() { |
tomotakaw | 22:ef03ce1c6c99 | 57 | DEBUG("update\n\r"); |
tomotakaw | 22:ef03ce1c6c99 | 58 | hum_temp->get_temperature(&value1); |
tomotakaw | 22:ef03ce1c6c99 | 59 | msg_high = (uint8_t)value1; |
tomotakaw | 22:ef03ce1c6c99 | 60 | msg_low = (uint8_t) ((value1 - msg_high) *255); |
tomotakaw | 22:ef03ce1c6c99 | 61 | msg16 = msg_high << 8 | msg_low ; |
tomotakaw | 22:ef03ce1c6c99 | 62 | DEBUG("msg = 0x%x\n\r", msg16); |
tomotakaw | 22:ef03ce1c6c99 | 63 | error_flag = ble.updateCharacteristicValue(customCharastic.getValueAttribute().getHandle(), (uint8_t *)&msg16 , sizeof(msg16)); |
tomotakaw | 22:ef03ce1c6c99 | 64 | DEBUG("error = %d\n\r", error_flag); |
tomotakaw | 22:ef03ce1c6c99 | 65 | } |
tomotakaw | 22:ef03ce1c6c99 | 66 | |
tomotakaw | 22:ef03ce1c6c99 | 67 | //通知を開始するやつ |
tomotakaw | 22:ef03ce1c6c99 | 68 | void updatesEnabledCallback(Gap::Handle_t handle) { |
tomotakaw | 22:ef03ce1c6c99 | 69 | led = 1; |
tomotakaw | 22:ef03ce1c6c99 | 70 | ticker.attach(&tickerCallback, 10); |
tomotakaw | 22:ef03ce1c6c99 | 71 | DEBUG("Notification is enabled\n\r"); |
tomotakaw | 22:ef03ce1c6c99 | 72 | } |
tomotakaw | 22:ef03ce1c6c99 | 73 | |
tomotakaw | 22:ef03ce1c6c99 | 74 | //通知を停止するやつ |
tomotakaw | 22:ef03ce1c6c99 | 75 | void updatesDisabledCallback(Gap::Handle_t handle) { |
tomotakaw | 22:ef03ce1c6c99 | 76 | led = 0; |
tomotakaw | 22:ef03ce1c6c99 | 77 | ticker.detach(); |
tomotakaw | 22:ef03ce1c6c99 | 78 | DEBUG("Notification is disabled\n\r"); |
apalmieri | 13:227a0149b677 | 79 | } |
apalmieri | 14:f715c13eb84f | 80 | |
tomotakaw | 22:ef03ce1c6c99 | 81 | void printMacAddress() |
tomotakaw | 22:ef03ce1c6c99 | 82 | { |
tomotakaw | 22:ef03ce1c6c99 | 83 | /* Print out device MAC address to the console*/ |
tomotakaw | 22:ef03ce1c6c99 | 84 | Gap::AddressType_t addr_type; |
tomotakaw | 22:ef03ce1c6c99 | 85 | Gap::Address_t address; |
tomotakaw | 22:ef03ce1c6c99 | 86 | BLE::Instance().gap().getAddress(&addr_type, address); |
tomotakaw | 22:ef03ce1c6c99 | 87 | printf("DEVICE MAC ADDRESS: "); |
tomotakaw | 22:ef03ce1c6c99 | 88 | for (int i = 5; i >= 1; i--){ |
tomotakaw | 22:ef03ce1c6c99 | 89 | printf("%02x:", address[i]); |
tomotakaw | 22:ef03ce1c6c99 | 90 | } |
tomotakaw | 22:ef03ce1c6c99 | 91 | printf("%02x\r\n", address[0]); |
tomotakaw | 22:ef03ce1c6c99 | 92 | } |
tomotakaw | 22:ef03ce1c6c99 | 93 | |
tomotakaw | 22:ef03ce1c6c99 | 94 | |
tomotakaw | 22:ef03ce1c6c99 | 95 | int main(void) { |
tomotakaw | 22:ef03ce1c6c99 | 96 | uint8_t id; |
tomotakaw | 22:ef03ce1c6c99 | 97 | |
tomotakaw | 22:ef03ce1c6c99 | 98 | DEBUG("start\n\r"); |
tomotakaw | 22:ef03ce1c6c99 | 99 | |
tomotakaw | 22:ef03ce1c6c99 | 100 | hum_temp->enable(); |
tomotakaw | 22:ef03ce1c6c99 | 101 | hum_temp->read_id(&id); |
tomotakaw | 22:ef03ce1c6c99 | 102 | printf("HTS221 humidity & temperature = 0x%X\r\n", id); |
tomotakaw | 22:ef03ce1c6c99 | 103 | DEBUG("Initialize\n\r"); |
tomotakaw | 22:ef03ce1c6c99 | 104 | ble.init(); |
tomotakaw | 22:ef03ce1c6c99 | 105 | |
tomotakaw | 22:ef03ce1c6c99 | 106 | DEBUG("Setup the event handlers\n\r"); |
tomotakaw | 22:ef03ce1c6c99 | 107 | ble.gap().onConnection(connectionCallback); |
tomotakaw | 22:ef03ce1c6c99 | 108 | ble.gap().onDisconnection(disconnectionCallback); |
tomotakaw | 22:ef03ce1c6c99 | 109 | ble.onUpdatesEnabled(updatesEnabledCallback); |
tomotakaw | 22:ef03ce1c6c99 | 110 | ble.onUpdatesDisabled(updatesDisabledCallback); |
tomotakaw | 22:ef03ce1c6c99 | 111 | |
tomotakaw | 22:ef03ce1c6c99 | 112 | DEBUG("Advertising payload\n\r"); |
tomotakaw | 22:ef03ce1c6c99 | 113 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
tomotakaw | 22:ef03ce1c6c99 | 114 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid_list, sizeof(uuid_list)); |
tomotakaw | 22:ef03ce1c6c99 | 115 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof((const uint8_t *)DEVICE_NAME)); |
tomotakaw | 22:ef03ce1c6c99 | 116 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
tomotakaw | 22:ef03ce1c6c99 | 117 | ble.gap().setAdvertisingInterval(160); //100ms; |
tomotakaw | 22:ef03ce1c6c99 | 118 | ble.gap().startAdvertising(); |
tomotakaw | 22:ef03ce1c6c99 | 119 | |
tomotakaw | 22:ef03ce1c6c99 | 120 | DEBUG("Add service\n\r"); |
tomotakaw | 22:ef03ce1c6c99 | 121 | ble.gattServer().addService(customService); |
tomotakaw | 22:ef03ce1c6c99 | 122 | |
tomotakaw | 22:ef03ce1c6c99 | 123 | printMacAddress(); |
tomotakaw | 22:ef03ce1c6c99 | 124 | |
tomotakaw | 22:ef03ce1c6c99 | 125 | while (true) { |
tomotakaw | 22:ef03ce1c6c99 | 126 | ble.waitForEvent(); |
tomotakaw | 22:ef03ce1c6c99 | 127 | } |
tomotakaw | 22:ef03ce1c6c99 | 128 | } |