Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:ef0f188a6fdd, 2015-06-03 (annotated)
- Committer:
- Marcomissyou
- Date:
- Wed Jun 03 03:13:27 2015 +0000
- Revision:
- 0:ef0f188a6fdd
- Child:
- 1:a7ecbb0ccc61
BLE HTS Demo
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Marcomissyou | 0:ef0f188a6fdd | 1 | |
| Marcomissyou | 0:ef0f188a6fdd | 2 | |
| Marcomissyou | 0:ef0f188a6fdd | 3 | /* mbed Microcontroller Library |
| Marcomissyou | 0:ef0f188a6fdd | 4 | * Copyright (c) 2006-2013 ARM Limited |
| Marcomissyou | 0:ef0f188a6fdd | 5 | * |
| Marcomissyou | 0:ef0f188a6fdd | 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| Marcomissyou | 0:ef0f188a6fdd | 7 | * you may not use this file except in compliance with the License. |
| Marcomissyou | 0:ef0f188a6fdd | 8 | * You may obtain a copy of the License at |
| Marcomissyou | 0:ef0f188a6fdd | 9 | * |
| Marcomissyou | 0:ef0f188a6fdd | 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| Marcomissyou | 0:ef0f188a6fdd | 11 | * |
| Marcomissyou | 0:ef0f188a6fdd | 12 | * Unless required by applicable law or agreed to in writing, software |
| Marcomissyou | 0:ef0f188a6fdd | 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| Marcomissyou | 0:ef0f188a6fdd | 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| Marcomissyou | 0:ef0f188a6fdd | 15 | * See the License for the specific language governing permissions and |
| Marcomissyou | 0:ef0f188a6fdd | 16 | * limitations under the License. |
| Marcomissyou | 0:ef0f188a6fdd | 17 | */ |
| Marcomissyou | 0:ef0f188a6fdd | 18 | |
| Marcomissyou | 0:ef0f188a6fdd | 19 | #include "mbed.h" |
| Marcomissyou | 0:ef0f188a6fdd | 20 | #include "BatteryService.h" |
| Marcomissyou | 0:ef0f188a6fdd | 21 | #include "DeviceInformationService.h" |
| Marcomissyou | 0:ef0f188a6fdd | 22 | #include "BLEDevice.h" |
| Marcomissyou | 0:ef0f188a6fdd | 23 | |
| Marcomissyou | 0:ef0f188a6fdd | 24 | #include "hts221.h" |
| Marcomissyou | 0:ef0f188a6fdd | 25 | #include "uvis25.h" |
| Marcomissyou | 0:ef0f188a6fdd | 26 | #include "nrf_gpio.h" |
| Marcomissyou | 0:ef0f188a6fdd | 27 | |
| Marcomissyou | 0:ef0f188a6fdd | 28 | #include "nrf51_bitfields.h" |
| Marcomissyou | 0:ef0f188a6fdd | 29 | |
| Marcomissyou | 0:ef0f188a6fdd | 30 | #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console; |
| Marcomissyou | 0:ef0f188a6fdd | 31 | * it will have an impact on code-size and power consumption. */ |
| Marcomissyou | 0:ef0f188a6fdd | 32 | |
| Marcomissyou | 0:ef0f188a6fdd | 33 | #if NEED_CONSOLE_OUTPUT |
| Marcomissyou | 0:ef0f188a6fdd | 34 | Serial pc(p25, p23); |
| Marcomissyou | 0:ef0f188a6fdd | 35 | #define DEBUG(...) { pc.printf(__VA_ARGS__); } |
| Marcomissyou | 0:ef0f188a6fdd | 36 | #else |
| Marcomissyou | 0:ef0f188a6fdd | 37 | #define DEBUG(...) /* nothing */ |
| Marcomissyou | 0:ef0f188a6fdd | 38 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
| Marcomissyou | 0:ef0f188a6fdd | 39 | |
| Marcomissyou | 0:ef0f188a6fdd | 40 | BLEDevice ble; |
| Marcomissyou | 0:ef0f188a6fdd | 41 | |
| Marcomissyou | 0:ef0f188a6fdd | 42 | InterruptIn Button0_LowInt(p7); |
| Marcomissyou | 0:ef0f188a6fdd | 43 | |
| Marcomissyou | 0:ef0f188a6fdd | 44 | DigitalOut led0(p4); |
| Marcomissyou | 0:ef0f188a6fdd | 45 | DigitalOut led1(p5); |
| Marcomissyou | 0:ef0f188a6fdd | 46 | DigitalOut led2(p6); |
| Marcomissyou | 0:ef0f188a6fdd | 47 | |
| Marcomissyou | 0:ef0f188a6fdd | 48 | static const uint8_t UUID_HUMI_AND_UVI[] = {0xf5, 0x59, 0xa2, 0x49, 0xbe, 0xb1, 0x4c, 0x54, 0xa1, 0x0a, 0xc7, 0x95, 0x7e, 0x17, 0xf8, 0x67}; |
| Marcomissyou | 0:ef0f188a6fdd | 49 | uint8_t wrs_HumiUVI_payload[11] = {0, }; |
| Marcomissyou | 0:ef0f188a6fdd | 50 | uint8_t htsTempPayload[7] = {0,}; |
| Marcomissyou | 0:ef0f188a6fdd | 51 | /* Health Thermometer Service */ |
| Marcomissyou | 0:ef0f188a6fdd | 52 | /* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.heart_rate.xml */ |
| Marcomissyou | 0:ef0f188a6fdd | 53 | /* Temperature Measurement: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml */ |
| Marcomissyou | 0:ef0f188a6fdd | 54 | GattCharacteristic htsTemp ( GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR, htsTempPayload, 6, 13, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE); |
| Marcomissyou | 0:ef0f188a6fdd | 55 | GattCharacteristic wrs_HumiUVI ( UUID_HUMI_AND_UVI, wrs_HumiUVI_payload, 1, 7, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE); |
| Marcomissyou | 0:ef0f188a6fdd | 56 | GattCharacteristic *htsChars[] = {&htsTemp, &wrs_HumiUVI}; |
| Marcomissyou | 0:ef0f188a6fdd | 57 | GattService htsService ( GattService::UUID_HEALTH_THERMOMETER_SERVICE, htsChars, sizeof(htsChars) / sizeof(GattCharacteristic *)); |
| Marcomissyou | 0:ef0f188a6fdd | 58 | |
| Marcomissyou | 0:ef0f188a6fdd | 59 | float tempCelsius = 25.50; |
| Marcomissyou | 0:ef0f188a6fdd | 60 | int32_t tempCelsius_ix100; |
| Marcomissyou | 0:ef0f188a6fdd | 61 | int8_t exponent = -2; |
| Marcomissyou | 0:ef0f188a6fdd | 62 | uint8_t UTC[7] = { 222, 7, 9, 11, 13, 42, 59 }; |
| Marcomissyou | 0:ef0f188a6fdd | 63 | float humi = 55; |
| Marcomissyou | 0:ef0f188a6fdd | 64 | int32_t humi_ix100; |
| Marcomissyou | 0:ef0f188a6fdd | 65 | uint8_t uvi = 8; |
| Marcomissyou | 0:ef0f188a6fdd | 66 | uint32_t counter = 0; |
| Marcomissyou | 0:ef0f188a6fdd | 67 | |
| Marcomissyou | 0:ef0f188a6fdd | 68 | const static char DEVICE_NAME[] = "WRS_d7"; |
| Marcomissyou | 0:ef0f188a6fdd | 69 | static const uint16_t uuid16_list[] = {GattService::UUID_BATTERY_SERVICE, |
| Marcomissyou | 0:ef0f188a6fdd | 70 | GattService::UUID_DEVICE_INFORMATION_SERVICE}; |
| Marcomissyou | 0:ef0f188a6fdd | 71 | static volatile bool triggerSensorPolling = false; |
| Marcomissyou | 0:ef0f188a6fdd | 72 | |
| Marcomissyou | 0:ef0f188a6fdd | 73 | /**************************************************************************/ |
| Marcomissyou | 0:ef0f188a6fdd | 74 | /*! |
| Marcomissyou | 0:ef0f188a6fdd | 75 | @brief This custom class can be used to override any GattServerEvents |
| Marcomissyou | 0:ef0f188a6fdd | 76 | that you are interested in handling on an application level. |
| Marcomissyou | 0:ef0f188a6fdd | 77 | */ |
| Marcomissyou | 0:ef0f188a6fdd | 78 | /**************************************************************************/ |
| Marcomissyou | 0:ef0f188a6fdd | 79 | // |
| Marcomissyou | 0:ef0f188a6fdd | 80 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
| Marcomissyou | 0:ef0f188a6fdd | 81 | { |
| Marcomissyou | 0:ef0f188a6fdd | 82 | DEBUG("Disconnected!\n\r"); |
| Marcomissyou | 0:ef0f188a6fdd | 83 | DEBUG("Restarting the advertising process\n\r"); |
| Marcomissyou | 0:ef0f188a6fdd | 84 | ble.startAdvertising(); |
| Marcomissyou | 0:ef0f188a6fdd | 85 | } |
| Marcomissyou | 0:ef0f188a6fdd | 86 | |
| Marcomissyou | 0:ef0f188a6fdd | 87 | void periodicCallback(void) |
| Marcomissyou | 0:ef0f188a6fdd | 88 | { |
| Marcomissyou | 0:ef0f188a6fdd | 89 | led0 = !led0; /* Do blinky on LED1 while we're waiting for BLE events */ |
| Marcomissyou | 0:ef0f188a6fdd | 90 | |
| Marcomissyou | 0:ef0f188a6fdd | 91 | /* Note that the periodicCallback() executes in interrupt context, so it is safer to do |
| Marcomissyou | 0:ef0f188a6fdd | 92 | * heavy-weight sensor polling from the main thread. */ |
| Marcomissyou | 0:ef0f188a6fdd | 93 | triggerSensorPolling = true; |
| Marcomissyou | 0:ef0f188a6fdd | 94 | } |
| Marcomissyou | 0:ef0f188a6fdd | 95 | |
| Marcomissyou | 0:ef0f188a6fdd | 96 | void LowInt() { |
| Marcomissyou | 0:ef0f188a6fdd | 97 | led0 = !led0; |
| Marcomissyou | 0:ef0f188a6fdd | 98 | } |
| Marcomissyou | 0:ef0f188a6fdd | 99 | /**************************************************************************/ |
| Marcomissyou | 0:ef0f188a6fdd | 100 | /*! |
| Marcomissyou | 0:ef0f188a6fdd | 101 | @brief Program entry point |
| Marcomissyou | 0:ef0f188a6fdd | 102 | */ |
| Marcomissyou | 0:ef0f188a6fdd | 103 | /**************************************************************************/ |
| Marcomissyou | 0:ef0f188a6fdd | 104 | |
| Marcomissyou | 0:ef0f188a6fdd | 105 | int main(void) |
| Marcomissyou | 0:ef0f188a6fdd | 106 | { |
| Marcomissyou | 0:ef0f188a6fdd | 107 | nrf_gpio_cfg_output(19); // SWIO |
| Marcomissyou | 0:ef0f188a6fdd | 108 | nrf_gpio_pin_clear(19); |
| Marcomissyou | 0:ef0f188a6fdd | 109 | |
| Marcomissyou | 0:ef0f188a6fdd | 110 | Button0_LowInt.fall(&LowInt); |
| Marcomissyou | 0:ef0f188a6fdd | 111 | |
| Marcomissyou | 0:ef0f188a6fdd | 112 | if (hts221_init() ) //led2=1; |
| Marcomissyou | 0:ef0f188a6fdd | 113 | // else led2=0; |
| Marcomissyou | 0:ef0f188a6fdd | 114 | HTS221_Calib(); |
| Marcomissyou | 0:ef0f188a6fdd | 115 | /* Setup blinky: led1 is toggled in main, led2 is toggled via Ticker */ |
| Marcomissyou | 0:ef0f188a6fdd | 116 | // led1=1; |
| Marcomissyou | 0:ef0f188a6fdd | 117 | // led2=0; |
| Marcomissyou | 0:ef0f188a6fdd | 118 | |
| Marcomissyou | 0:ef0f188a6fdd | 119 | Ticker ticker; |
| Marcomissyou | 0:ef0f188a6fdd | 120 | ticker.attach(periodicCallback, 2); |
| Marcomissyou | 0:ef0f188a6fdd | 121 | |
| Marcomissyou | 0:ef0f188a6fdd | 122 | ble.init(); |
| Marcomissyou | 0:ef0f188a6fdd | 123 | ble.onDisconnection(disconnectionCallback); |
| Marcomissyou | 0:ef0f188a6fdd | 124 | |
| Marcomissyou | 0:ef0f188a6fdd | 125 | /* Setup auxiliary services. */ |
| Marcomissyou | 0:ef0f188a6fdd | 126 | BatteryService battery(ble); |
| Marcomissyou | 0:ef0f188a6fdd | 127 | DeviceInformationService deviceInfo(ble, "Delta", "NNN40_1.0", "SN", "EVB_1.0", "B230-n80-m92", "T_0128b"); |
| Marcomissyou | 0:ef0f188a6fdd | 128 | |
| Marcomissyou | 0:ef0f188a6fdd | 129 | /* Setup advertising. */ |
| Marcomissyou | 0:ef0f188a6fdd | 130 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
| Marcomissyou | 0:ef0f188a6fdd | 131 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
| Marcomissyou | 0:ef0f188a6fdd | 132 | ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR); |
| Marcomissyou | 0:ef0f188a6fdd | 133 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
| Marcomissyou | 0:ef0f188a6fdd | 134 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
| Marcomissyou | 0:ef0f188a6fdd | 135 | ble.setAdvertisingInterval(1600); |
| Marcomissyou | 0:ef0f188a6fdd | 136 | ble.startAdvertising(); |
| Marcomissyou | 0:ef0f188a6fdd | 137 | |
| Marcomissyou | 0:ef0f188a6fdd | 138 | ble.addService(htsService); |
| Marcomissyou | 0:ef0f188a6fdd | 139 | |
| Marcomissyou | 0:ef0f188a6fdd | 140 | while (true) { |
| Marcomissyou | 0:ef0f188a6fdd | 141 | // if (triggerSensorPolling ) { |
| Marcomissyou | 0:ef0f188a6fdd | 142 | // triggerSensorPolling = false; |
| Marcomissyou | 0:ef0f188a6fdd | 143 | |
| Marcomissyou | 0:ef0f188a6fdd | 144 | HTS221_ReadTempHumi(&tempCelsius, &humi); |
| Marcomissyou | 0:ef0f188a6fdd | 145 | uvi = UVIS25_ReadUVI(); |
| Marcomissyou | 0:ef0f188a6fdd | 146 | /* Update the Temperature measurement */ |
| Marcomissyou | 0:ef0f188a6fdd | 147 | /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml */ |
| Marcomissyou | 0:ef0f188a6fdd | 148 | tempCelsius_ix100 = tempCelsius * 100; |
| Marcomissyou | 0:ef0f188a6fdd | 149 | htsTempPayload[0] = 0x02; // flag |
| Marcomissyou | 0:ef0f188a6fdd | 150 | htsTempPayload[1] = tempCelsius_ix100%0xff; |
| Marcomissyou | 0:ef0f188a6fdd | 151 | htsTempPayload[2] = tempCelsius_ix100/0xff; |
| Marcomissyou | 0:ef0f188a6fdd | 152 | htsTempPayload[3] = tempCelsius_ix100/0xffff; |
| Marcomissyou | 0:ef0f188a6fdd | 153 | htsTempPayload[4] = exponent; |
| Marcomissyou | 0:ef0f188a6fdd | 154 | for (int i = 0; i < 7; i++) htsTempPayload[i+5] = UTC[i]; |
| Marcomissyou | 0:ef0f188a6fdd | 155 | ble.updateCharacteristicValue(htsTemp.getValueAttribute().getHandle(), htsTempPayload, 12); |
| Marcomissyou | 0:ef0f188a6fdd | 156 | |
| Marcomissyou | 0:ef0f188a6fdd | 157 | ble.waitForEvent(); |
| Marcomissyou | 0:ef0f188a6fdd | 158 | wait(1); |
| Marcomissyou | 0:ef0f188a6fdd | 159 | //humi_ix100 = humi * 100; |
| Marcomissyou | 0:ef0f188a6fdd | 160 | wrs_HumiUVI_payload[0] = 0x03; // flag |
| Marcomissyou | 0:ef0f188a6fdd | 161 | memcpy(&wrs_HumiUVI_payload[1],&humi,4); |
| Marcomissyou | 0:ef0f188a6fdd | 162 | //wrs_HumiUVI_payload[1] = humi_ix100%0xff; |
| Marcomissyou | 0:ef0f188a6fdd | 163 | // wrs_HumiUVI_payload[2] = humi_ix100/0xff; |
| Marcomissyou | 0:ef0f188a6fdd | 164 | // wrs_HumiUVI_payload[3] = humi_ix100/0xffff; |
| Marcomissyou | 0:ef0f188a6fdd | 165 | // wrs_HumiUVI_payload[4] = exponent; |
| Marcomissyou | 0:ef0f188a6fdd | 166 | wrs_HumiUVI_payload[5] = uvi; |
| Marcomissyou | 0:ef0f188a6fdd | 167 | |
| Marcomissyou | 0:ef0f188a6fdd | 168 | ble.updateCharacteristicValue(wrs_HumiUVI.getValueAttribute().getHandle(), wrs_HumiUVI_payload, 6); |
| Marcomissyou | 0:ef0f188a6fdd | 169 | ble.waitForEvent(); |
| Marcomissyou | 0:ef0f188a6fdd | 170 | DEBUG("%f,%f,%d\r\n", tempCelsius, humi, counter++); |
| Marcomissyou | 0:ef0f188a6fdd | 171 | wait(1); |
| Marcomissyou | 0:ef0f188a6fdd | 172 | |
| Marcomissyou | 0:ef0f188a6fdd | 173 | //} else { |
| Marcomissyou | 0:ef0f188a6fdd | 174 | // ble.waitForEvent(); |
| Marcomissyou | 0:ef0f188a6fdd | 175 | // } |
| Marcomissyou | 0:ef0f188a6fdd | 176 | } |
| Marcomissyou | 0:ef0f188a6fdd | 177 | |
| Marcomissyou | 0:ef0f188a6fdd | 178 | } |
| Marcomissyou | 0:ef0f188a6fdd | 179 | |
| Marcomissyou | 0:ef0f188a6fdd | 180 | // Gill modify |
| Marcomissyou | 0:ef0f188a6fdd | 181 | // 20150331 change humi data format to float |
| Marcomissyou | 0:ef0f188a6fdd | 182 | // 20150408 diable triggerSensorPolling function, using while loop and wait |
| Marcomissyou | 0:ef0f188a6fdd | 183 | // 20150428 change external clock to internal RC |