BLE sensor for temperature and humidity using Si7021 and photocell.
Dependencies: BLE_API mbed nRF51822
main.cpp@3:3eda308b78e6, 2015-07-20 (annotated)
- Committer:
- sunsmile2015
- Date:
- Mon Jul 20 08:58:21 2015 +0000
- Revision:
- 3:3eda308b78e6
- Parent:
- 2:b935358da5ba
- Child:
- 4:e5fa4c8838db
1.change the coding style; 2.add struct for manufacturer data
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sunsmile2015 | 0:3dc6e424dba0 | 1 | |
sunsmile2015 | 0:3dc6e424dba0 | 2 | /* mbed Microcontroller Library |
sunsmile2015 | 0:3dc6e424dba0 | 3 | * Copyright (c) 2006-2015 ARM Limited |
sunsmile2015 | 0:3dc6e424dba0 | 4 | * |
sunsmile2015 | 0:3dc6e424dba0 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sunsmile2015 | 0:3dc6e424dba0 | 6 | * you may not use this file except in compliance with the License. |
sunsmile2015 | 0:3dc6e424dba0 | 7 | * You may obtain a copy of the License at |
sunsmile2015 | 0:3dc6e424dba0 | 8 | * |
sunsmile2015 | 0:3dc6e424dba0 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
sunsmile2015 | 0:3dc6e424dba0 | 10 | * |
sunsmile2015 | 0:3dc6e424dba0 | 11 | * Unless required by applicable law or agreed to in writing, software |
sunsmile2015 | 0:3dc6e424dba0 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
sunsmile2015 | 0:3dc6e424dba0 | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sunsmile2015 | 0:3dc6e424dba0 | 14 | * See the License for the specific language governing permissions and |
sunsmile2015 | 0:3dc6e424dba0 | 15 | * limitations under the License. |
sunsmile2015 | 0:3dc6e424dba0 | 16 | */ |
sunsmile2015 | 0:3dc6e424dba0 | 17 | |
sunsmile2015 | 0:3dc6e424dba0 | 18 | #include "mbed.h" |
sunsmile2015 | 0:3dc6e424dba0 | 19 | #include "ble/BLE.h" |
sunsmile2015 | 0:3dc6e424dba0 | 20 | #include "ble/DiscoveredCharacteristic.h" |
sunsmile2015 | 0:3dc6e424dba0 | 21 | #include "ble/DiscoveredService.h" |
sunsmile2015 | 0:3dc6e424dba0 | 22 | #include "TMP_nrf51/TMP_nrf51.h" |
sunsmile2015 | 0:3dc6e424dba0 | 23 | |
sunsmile2015 | 3:3eda308b78e6 | 24 | #define COMP_ID_TEST 0xFEFE |
sunsmile2015 | 3:3eda308b78e6 | 25 | |
sunsmile2015 | 3:3eda308b78e6 | 26 | #pragma pack(1) |
sunsmile2015 | 3:3eda308b78e6 | 27 | typedef struct manufacturerData { |
sunsmile2015 | 3:3eda308b78e6 | 28 | uint16_t companyId; |
sunsmile2015 | 3:3eda308b78e6 | 29 | /* User defined manufacture data */ |
sunsmile2015 | 3:3eda308b78e6 | 30 | TMP_nrf51::tmpSensorValue_t tmpSensorValue; |
sunsmile2015 | 3:3eda308b78e6 | 31 | } manufacturerData_t; |
sunsmile2015 | 3:3eda308b78e6 | 32 | #pragma pack() |
sunsmile2015 | 3:3eda308b78e6 | 33 | |
sunsmile2015 | 0:3dc6e424dba0 | 34 | BLE ble; |
sunsmile2015 | 2:b935358da5ba | 35 | TMP_nrf51 tempSensor; |
sunsmile2015 | 0:3dc6e424dba0 | 36 | DigitalOut alivenessLED(LED1, 1); |
sunsmile2015 | 2:b935358da5ba | 37 | static bool triggerTempValueRead = true; |
sunsmile2015 | 0:3dc6e424dba0 | 38 | |
sunsmile2015 | 2:b935358da5ba | 39 | void periodicCallback(void) |
sunsmile2015 | 2:b935358da5ba | 40 | { |
sunsmile2015 | 2:b935358da5ba | 41 | /* Do blinky on LED1 while we're waiting for BLE events */ |
sunsmile2015 | 2:b935358da5ba | 42 | alivenessLED = !alivenessLED; |
sunsmile2015 | 2:b935358da5ba | 43 | triggerTempValueRead = true; |
sunsmile2015 | 0:3dc6e424dba0 | 44 | } |
sunsmile2015 | 0:3dc6e424dba0 | 45 | |
sunsmile2015 | 2:b935358da5ba | 46 | void temperatureValueAdvertising(void) |
sunsmile2015 | 2:b935358da5ba | 47 | { |
sunsmile2015 | 3:3eda308b78e6 | 48 | manufacturerData_t manuData; |
sunsmile2015 | 3:3eda308b78e6 | 49 | manuData.companyId = COMP_ID_TEST; |
sunsmile2015 | 3:3eda308b78e6 | 50 | |
sunsmile2015 | 2:b935358da5ba | 51 | /* Read a new temperature value */ |
sunsmile2015 | 3:3eda308b78e6 | 52 | manuData.tmpSensorValue = tempSensor.get(); |
sunsmile2015 | 3:3eda308b78e6 | 53 | printf("Temp is %f\r\n", (float)manuData.tmpSensorValue); |
sunsmile2015 | 2:b935358da5ba | 54 | |
sunsmile2015 | 2:b935358da5ba | 55 | /* Stop advertising and clear the payload if in advertising state */ |
sunsmile2015 | 2:b935358da5ba | 56 | if((ble.gap().getState()).advertising == 1) { |
sunsmile2015 | 2:b935358da5ba | 57 | ble.gap().stopAdvertising(); |
sunsmile2015 | 2:b935358da5ba | 58 | ble.gap().clearAdvertisingPayload(); |
sunsmile2015 | 2:b935358da5ba | 59 | } |
sunsmile2015 | 3:3eda308b78e6 | 60 | |
sunsmile2015 | 3:3eda308b78e6 | 61 | /* Setup advertising payload */ |
sunsmile2015 | 3:3eda308b78e6 | 62 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); /* Set flag */ |
sunsmile2015 | 3:3eda308b78e6 | 63 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER); /* Set appearance */ |
sunsmile2015 | 3:3eda308b78e6 | 64 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&manuData, sizeof(manufacturerData_t)); /* Set data */ |
sunsmile2015 | 3:3eda308b78e6 | 65 | /* Setup advertising parameters */ |
sunsmile2015 | 2:b935358da5ba | 66 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); |
sunsmile2015 | 2:b935358da5ba | 67 | ble.gap().setAdvertisingInterval(500); |
sunsmile2015 | 3:3eda308b78e6 | 68 | /* Start advertising */ |
sunsmile2015 | 2:b935358da5ba | 69 | ble.gap().startAdvertising(); |
sunsmile2015 | 2:b935358da5ba | 70 | } |
sunsmile2015 | 2:b935358da5ba | 71 | |
sunsmile2015 | 2:b935358da5ba | 72 | int main(void) |
sunsmile2015 | 2:b935358da5ba | 73 | { |
sunsmile2015 | 0:3dc6e424dba0 | 74 | Ticker ticker; |
sunsmile2015 | 0:3dc6e424dba0 | 75 | /* Refresh temperature value every 2 seconds */ |
sunsmile2015 | 0:3dc6e424dba0 | 76 | ticker.attach(periodicCallback, 2); |
sunsmile2015 | 0:3dc6e424dba0 | 77 | |
sunsmile2015 | 0:3dc6e424dba0 | 78 | ble.init(); |
sunsmile2015 | 0:3dc6e424dba0 | 79 | |
sunsmile2015 | 0:3dc6e424dba0 | 80 | while (true) { |
sunsmile2015 | 2:b935358da5ba | 81 | if (triggerTempValueRead) { |
sunsmile2015 | 2:b935358da5ba | 82 | temperatureValueAdvertising(); |
sunsmile2015 | 2:b935358da5ba | 83 | triggerTempValueRead = false; |
sunsmile2015 | 0:3dc6e424dba0 | 84 | } |
sunsmile2015 | 0:3dc6e424dba0 | 85 | ble.waitForEvent(); |
sunsmile2015 | 0:3dc6e424dba0 | 86 | } |
sunsmile2015 | 0:3dc6e424dba0 | 87 | } |