ADT7410 sample
Dependencies: ADT7410 BLE_API mbed nRF51822
Fork of BLENano_SimpleTemplate_temp_170802 by
main.cpp@7:a1e3e531e518, 2017-08-02 (annotated)
- Committer:
- Jun_adi
- Date:
- Wed Aug 02 13:31:52 2017 +0000
- Revision:
- 7:a1e3e531e518
- Parent:
- 6:84aa3914b00d
- Child:
- 8:236994820e39
Update ADT7410 temp sensor value
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RedBearLab | 1:81a97eb70d3d | 1 | /* |
RedBearLab | 1:81a97eb70d3d | 2 | |
RedBearLab | 1:81a97eb70d3d | 3 | Copyright (c) 2012-2014 RedBearLab |
RedBearLab | 1:81a97eb70d3d | 4 | |
RedBearLab | 1:81a97eb70d3d | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
RedBearLab | 1:81a97eb70d3d | 6 | and associated documentation files (the "Software"), to deal in the Software without restriction, |
RedBearLab | 1:81a97eb70d3d | 7 | including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
RedBearLab | 1:81a97eb70d3d | 8 | and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, |
RedBearLab | 1:81a97eb70d3d | 9 | subject to the following conditions: |
RedBearLab | 1:81a97eb70d3d | 10 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
RedBearLab | 1:81a97eb70d3d | 11 | |
RedBearLab | 1:81a97eb70d3d | 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
RedBearLab | 1:81a97eb70d3d | 13 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
RedBearLab | 1:81a97eb70d3d | 14 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
RedBearLab | 1:81a97eb70d3d | 15 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
RedBearLab | 1:81a97eb70d3d | 16 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
RedBearLab | 1:81a97eb70d3d | 17 | |
RedBearLab | 1:81a97eb70d3d | 18 | */ |
RedBearLab | 0:be2e4095513a | 19 | |
electricbaka | 4:7e5d4a18cd22 | 20 | /* |
electricbaka | 5:95238da08cee | 21 | * The application works with the BlueJelly.js |
electricbaka | 5:95238da08cee | 22 | * |
electricbaka | 5:95238da08cee | 23 | * http://jellyware.jp/ |
electricbaka | 5:95238da08cee | 24 | * |
electricbaka | 4:7e5d4a18cd22 | 25 | */ |
electricbaka | 4:7e5d4a18cd22 | 26 | |
electricbaka | 4:7e5d4a18cd22 | 27 | //====================================================================== |
electricbaka | 4:7e5d4a18cd22 | 28 | //Grobal |
electricbaka | 4:7e5d4a18cd22 | 29 | //====================================================================== |
electricbaka | 4:7e5d4a18cd22 | 30 | //------------------------------------------------------------ |
electricbaka | 4:7e5d4a18cd22 | 31 | //Include Header Files |
electricbaka | 4:7e5d4a18cd22 | 32 | //------------------------------------------------------------ |
RedBearLab | 0:be2e4095513a | 33 | #include "mbed.h" |
RedBearLab | 2:3cd654f42efa | 34 | #include "ble/BLE.h" |
Jun_adi | 6:84aa3914b00d | 35 | #include "ADT7410.h" |
RedBearLab | 0:be2e4095513a | 36 | |
electricbaka | 4:7e5d4a18cd22 | 37 | //------------------------------------------------------------ |
electricbaka | 4:7e5d4a18cd22 | 38 | //Definition |
electricbaka | 4:7e5d4a18cd22 | 39 | //------------------------------------------------------------ |
electricbaka | 5:95238da08cee | 40 | #define TXRX_BUF_LEN 20 //max 20[byte] |
Jun_adi | 7:a1e3e531e518 | 41 | #define DEVICE_LOCAL_NAME "ADT7410 Temperature" //Change Device name |
electricbaka | 4:7e5d4a18cd22 | 42 | #define ADVERTISING_INTERVAL 160 //160 * 0.625[ms] = 100[ms] |
electricbaka | 5:95238da08cee | 43 | #define TICKER_TIME 200000 //200000[us] = 200[ms] |
electricbaka | 5:95238da08cee | 44 | #define DIGITAL_OUT_PIN P0_9 |
Jun_adi | 6:84aa3914b00d | 45 | //#define ANALOG_IN_PIN P0_4 |
RedBearLab | 0:be2e4095513a | 46 | |
RedBearLab | 0:be2e4095513a | 47 | |
electricbaka | 4:7e5d4a18cd22 | 48 | //------------------------------------------------------------ |
electricbaka | 4:7e5d4a18cd22 | 49 | //Object generation |
electricbaka | 4:7e5d4a18cd22 | 50 | //------------------------------------------------------------ |
electricbaka | 4:7e5d4a18cd22 | 51 | BLE ble; |
electricbaka | 5:95238da08cee | 52 | DigitalOut LED_SET(DIGITAL_OUT_PIN); |
Jun_adi | 6:84aa3914b00d | 53 | //AnalogIn ANALOG(ANALOG_IN_PIN); |
electricbaka | 4:7e5d4a18cd22 | 54 | |
Jun_adi | 6:84aa3914b00d | 55 | //I2C Pin setting P0_4=SDA, P0_5=SCL |
Jun_adi | 6:84aa3914b00d | 56 | ADT7410 temp(P0_4, P0_5, 0x90, 10000); |
electricbaka | 4:7e5d4a18cd22 | 57 | |
electricbaka | 4:7e5d4a18cd22 | 58 | //------------------------------------------------------------ |
electricbaka | 4:7e5d4a18cd22 | 59 | //Service & Characteristic Setting |
electricbaka | 4:7e5d4a18cd22 | 60 | //------------------------------------------------------------ |
electricbaka | 4:7e5d4a18cd22 | 61 | //Service UUID |
electricbaka | 4:7e5d4a18cd22 | 62 | static const uint8_t base_uuid[] = { 0x71, 0x3D, 0x00, 0x00, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E } ; |
electricbaka | 4:7e5d4a18cd22 | 63 | |
electricbaka | 4:7e5d4a18cd22 | 64 | //Characteristic UUID |
electricbaka | 4:7e5d4a18cd22 | 65 | static const uint8_t tx_uuid[] = { 0x71, 0x3D, 0x00, 0x03, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E } ; |
electricbaka | 4:7e5d4a18cd22 | 66 | static const uint8_t rx_uuid[] = { 0x71, 0x3D, 0x00, 0x02, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E } ; |
electricbaka | 4:7e5d4a18cd22 | 67 | |
electricbaka | 4:7e5d4a18cd22 | 68 | //Characteristic Value |
RedBearLab | 0:be2e4095513a | 69 | uint8_t txPayload[TXRX_BUF_LEN] = {0,}; |
RedBearLab | 0:be2e4095513a | 70 | uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; |
RedBearLab | 0:be2e4095513a | 71 | |
electricbaka | 4:7e5d4a18cd22 | 72 | //Characteristic Property Setting etc |
electricbaka | 5:95238da08cee | 73 | GattCharacteristic txCharacteristic (tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
electricbaka | 4:7e5d4a18cd22 | 74 | GattCharacteristic rxCharacteristic (rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY| GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
electricbaka | 4:7e5d4a18cd22 | 75 | GattCharacteristic *myChars[] = {&txCharacteristic, &rxCharacteristic}; |
electricbaka | 4:7e5d4a18cd22 | 76 | |
electricbaka | 4:7e5d4a18cd22 | 77 | //Service Setting |
electricbaka | 4:7e5d4a18cd22 | 78 | GattService myService(base_uuid, myChars, sizeof(myChars) / sizeof(GattCharacteristic *)); |
RedBearLab | 0:be2e4095513a | 79 | |
RedBearLab | 0:be2e4095513a | 80 | |
electricbaka | 4:7e5d4a18cd22 | 81 | //====================================================================== |
electricbaka | 4:7e5d4a18cd22 | 82 | //onDisconnection |
electricbaka | 4:7e5d4a18cd22 | 83 | //====================================================================== |
electricbaka | 4:7e5d4a18cd22 | 84 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
electricbaka | 4:7e5d4a18cd22 | 85 | { |
electricbaka | 4:7e5d4a18cd22 | 86 | ble.startAdvertising(); |
electricbaka | 4:7e5d4a18cd22 | 87 | } |
RedBearLab | 0:be2e4095513a | 88 | |
RedBearLab | 0:be2e4095513a | 89 | |
electricbaka | 4:7e5d4a18cd22 | 90 | //====================================================================== |
electricbaka | 4:7e5d4a18cd22 | 91 | //onDataWritten |
electricbaka | 4:7e5d4a18cd22 | 92 | //====================================================================== |
RedBearLab | 2:3cd654f42efa | 93 | void WrittenHandler(const GattWriteCallbackParams *Handler) |
RedBearLab | 0:be2e4095513a | 94 | { |
RedBearLab | 0:be2e4095513a | 95 | uint8_t buf[TXRX_BUF_LEN]; |
RedBearLab | 0:be2e4095513a | 96 | uint16_t bytesRead; |
RedBearLab | 0:be2e4095513a | 97 | |
RedBearLab | 2:3cd654f42efa | 98 | if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) |
RedBearLab | 0:be2e4095513a | 99 | { |
RedBearLab | 0:be2e4095513a | 100 | ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead); |
electricbaka | 5:95238da08cee | 101 | memset(txPayload, 0, TXRX_BUF_LEN); |
electricbaka | 5:95238da08cee | 102 | memcpy(txPayload, buf, TXRX_BUF_LEN); |
electricbaka | 5:95238da08cee | 103 | |
electricbaka | 5:95238da08cee | 104 | if(buf[0] == 1) |
electricbaka | 5:95238da08cee | 105 | LED_SET = 1; |
electricbaka | 5:95238da08cee | 106 | else |
electricbaka | 5:95238da08cee | 107 | LED_SET = 0; |
RedBearLab | 0:be2e4095513a | 108 | } |
RedBearLab | 0:be2e4095513a | 109 | } |
RedBearLab | 0:be2e4095513a | 110 | |
RedBearLab | 0:be2e4095513a | 111 | |
electricbaka | 4:7e5d4a18cd22 | 112 | //====================================================================== |
electricbaka | 4:7e5d4a18cd22 | 113 | //onTimeout |
electricbaka | 4:7e5d4a18cd22 | 114 | //====================================================================== |
Jun_adi | 6:84aa3914b00d | 115 | |
Jun_adi | 7:a1e3e531e518 | 116 | /* |
electricbaka | 4:7e5d4a18cd22 | 117 | void m_status_check_handle(void) |
electricbaka | 4:7e5d4a18cd22 | 118 | { |
electricbaka | 4:7e5d4a18cd22 | 119 | uint8_t buf[2]; |
electricbaka | 4:7e5d4a18cd22 | 120 | //Read Analog port |
electricbaka | 4:7e5d4a18cd22 | 121 | float s = ANALOG; |
electricbaka | 4:7e5d4a18cd22 | 122 | uint16_t value = s*1024; |
electricbaka | 4:7e5d4a18cd22 | 123 | buf[0] = (value >> 8); |
electricbaka | 5:95238da08cee | 124 | buf[1] = value; |
Jun_adi | 6:84aa3914b00d | 125 | |
electricbaka | 4:7e5d4a18cd22 | 126 | } |
Jun_adi | 7:a1e3e531e518 | 127 | */ |
electricbaka | 4:7e5d4a18cd22 | 128 | |
electricbaka | 4:7e5d4a18cd22 | 129 | //====================================================================== |
electricbaka | 4:7e5d4a18cd22 | 130 | //convert reverse UUID |
electricbaka | 4:7e5d4a18cd22 | 131 | //====================================================================== |
electricbaka | 4:7e5d4a18cd22 | 132 | void reverseUUID(const uint8_t* src, uint8_t* dst) |
electricbaka | 4:7e5d4a18cd22 | 133 | { |
electricbaka | 4:7e5d4a18cd22 | 134 | int i; |
electricbaka | 4:7e5d4a18cd22 | 135 | |
electricbaka | 4:7e5d4a18cd22 | 136 | for(i=0;i<16;i++) |
electricbaka | 4:7e5d4a18cd22 | 137 | dst[i] = src[15 - i]; |
electricbaka | 4:7e5d4a18cd22 | 138 | } |
electricbaka | 4:7e5d4a18cd22 | 139 | |
electricbaka | 4:7e5d4a18cd22 | 140 | |
electricbaka | 4:7e5d4a18cd22 | 141 | //====================================================================== |
electricbaka | 4:7e5d4a18cd22 | 142 | //main |
electricbaka | 4:7e5d4a18cd22 | 143 | //====================================================================== |
RedBearLab | 0:be2e4095513a | 144 | int main(void) |
electricbaka | 4:7e5d4a18cd22 | 145 | { |
electricbaka | 4:7e5d4a18cd22 | 146 | uint8_t base_uuid_rev[16]; |
electricbaka | 4:7e5d4a18cd22 | 147 | |
electricbaka | 4:7e5d4a18cd22 | 148 | //Timer Setting [us] |
RedBearLab | 0:be2e4095513a | 149 | Ticker ticker; |
Jun_adi | 7:a1e3e531e518 | 150 | //ticker.attach_us(m_status_check_handle, TICKER_TIME); |
RedBearLab | 0:be2e4095513a | 151 | |
electricbaka | 4:7e5d4a18cd22 | 152 | //BLE init |
RedBearLab | 0:be2e4095513a | 153 | ble.init(); |
electricbaka | 4:7e5d4a18cd22 | 154 | |
electricbaka | 4:7e5d4a18cd22 | 155 | //EventListener |
RedBearLab | 0:be2e4095513a | 156 | ble.onDisconnection(disconnectionCallback); |
RedBearLab | 0:be2e4095513a | 157 | ble.onDataWritten(WrittenHandler); |
electricbaka | 4:7e5d4a18cd22 | 158 | |
electricbaka | 4:7e5d4a18cd22 | 159 | //------------------------------------------------------------ |
electricbaka | 4:7e5d4a18cd22 | 160 | //setup advertising |
electricbaka | 4:7e5d4a18cd22 | 161 | //------------------------------------------------------------ |
electricbaka | 4:7e5d4a18cd22 | 162 | //Classic BT not support |
electricbaka | 4:7e5d4a18cd22 | 163 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
RedBearLab | 0:be2e4095513a | 164 | |
electricbaka | 4:7e5d4a18cd22 | 165 | //Connectable to Central |
RedBearLab | 0:be2e4095513a | 166 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
electricbaka | 4:7e5d4a18cd22 | 167 | |
electricbaka | 4:7e5d4a18cd22 | 168 | //Local Name |
electricbaka | 4:7e5d4a18cd22 | 169 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, |
electricbaka | 4:7e5d4a18cd22 | 170 | (const uint8_t *)DEVICE_LOCAL_NAME, sizeof(DEVICE_LOCAL_NAME) - 1); |
electricbaka | 4:7e5d4a18cd22 | 171 | |
electricbaka | 4:7e5d4a18cd22 | 172 | //GAP AdvertisingData |
electricbaka | 4:7e5d4a18cd22 | 173 | reverseUUID(base_uuid, base_uuid_rev); |
RedBearLab | 0:be2e4095513a | 174 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
electricbaka | 4:7e5d4a18cd22 | 175 | (uint8_t *)base_uuid_rev, sizeof(base_uuid)); |
electricbaka | 4:7e5d4a18cd22 | 176 | |
electricbaka | 4:7e5d4a18cd22 | 177 | //Advertising Interval |
electricbaka | 4:7e5d4a18cd22 | 178 | ble.setAdvertisingInterval(ADVERTISING_INTERVAL); |
RedBearLab | 0:be2e4095513a | 179 | |
electricbaka | 4:7e5d4a18cd22 | 180 | //Add Service |
electricbaka | 4:7e5d4a18cd22 | 181 | ble.addService(myService); |
RedBearLab | 0:be2e4095513a | 182 | |
electricbaka | 4:7e5d4a18cd22 | 183 | //Start Advertising |
RedBearLab | 0:be2e4095513a | 184 | ble.startAdvertising(); |
RedBearLab | 0:be2e4095513a | 185 | |
Jun_adi | 6:84aa3914b00d | 186 | |
Jun_adi | 6:84aa3914b00d | 187 | uint8_t buf[2]; |
Jun_adi | 6:84aa3914b00d | 188 | float tempadt; |
Jun_adi | 6:84aa3914b00d | 189 | |
Jun_adi | 6:84aa3914b00d | 190 | // reset sensor to default values |
Jun_adi | 6:84aa3914b00d | 191 | temp.reset(); |
Jun_adi | 6:84aa3914b00d | 192 | |
Jun_adi | 6:84aa3914b00d | 193 | // reduce sample rate to save power |
Jun_adi | 6:84aa3914b00d | 194 | temp.setConfig(ONE_SPS_MODE); |
Jun_adi | 6:84aa3914b00d | 195 | |
Jun_adi | 6:84aa3914b00d | 196 | |
electricbaka | 4:7e5d4a18cd22 | 197 | //------------------------------------------------------------ |
electricbaka | 4:7e5d4a18cd22 | 198 | //Loop |
electricbaka | 4:7e5d4a18cd22 | 199 | //------------------------------------------------------------ |
RedBearLab | 0:be2e4095513a | 200 | while(1) |
RedBearLab | 0:be2e4095513a | 201 | { |
Jun_adi | 6:84aa3914b00d | 202 | |
Jun_adi | 6:84aa3914b00d | 203 | // get temperature every two seconds |
Jun_adi | 6:84aa3914b00d | 204 | tempadt = temp.getTemp(); |
Jun_adi | 6:84aa3914b00d | 205 | |
Jun_adi | 7:a1e3e531e518 | 206 | int16_t value0 = tempadt; //Get integer value |
Jun_adi | 7:a1e3e531e518 | 207 | int16_t value1 = (tempadt - value0)*100; //Get decimal value |
Jun_adi | 7:a1e3e531e518 | 208 | buf[0] = value0; |
Jun_adi | 7:a1e3e531e518 | 209 | buf[1] = value1; |
Jun_adi | 6:84aa3914b00d | 210 | |
Jun_adi | 6:84aa3914b00d | 211 | //Send out |
Jun_adi | 6:84aa3914b00d | 212 | ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 2); |
Jun_adi | 6:84aa3914b00d | 213 | wait(0.5); |
Jun_adi | 6:84aa3914b00d | 214 | |
RedBearLab | 0:be2e4095513a | 215 | ble.waitForEvent(); |
RedBearLab | 0:be2e4095513a | 216 | } |
electricbaka | 4:7e5d4a18cd22 | 217 | } |