nrf52 nrf51 with same code
Dependencies: CCS811
Fork of MtConnect04S_MtSense02 by
source/main.cpp@0:4d76c0bc0f78, 2017-06-27 (annotated)
- Committer:
- johnathanlyu
- Date:
- Tue Jun 27 05:18:38 2017 +0000
- Revision:
- 0:4d76c0bc0f78
- Child:
- 2:f3768a226561
Initial MtConnect04S with MtSense02 first version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
johnathanlyu | 0:4d76c0bc0f78 | 1 | /* mbed Microcontroller Library |
johnathanlyu | 0:4d76c0bc0f78 | 2 | * Copyright (c) 2006-2014 ARM Limited |
johnathanlyu | 0:4d76c0bc0f78 | 3 | * |
johnathanlyu | 0:4d76c0bc0f78 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
johnathanlyu | 0:4d76c0bc0f78 | 5 | * you may not use this file except in compliance with the License. |
johnathanlyu | 0:4d76c0bc0f78 | 6 | * You may obtain a copy of the License at |
johnathanlyu | 0:4d76c0bc0f78 | 7 | * |
johnathanlyu | 0:4d76c0bc0f78 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
johnathanlyu | 0:4d76c0bc0f78 | 9 | * |
johnathanlyu | 0:4d76c0bc0f78 | 10 | * Unless required by applicable law or agreed to in writing, software |
johnathanlyu | 0:4d76c0bc0f78 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
johnathanlyu | 0:4d76c0bc0f78 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
johnathanlyu | 0:4d76c0bc0f78 | 13 | * See the License for the specific language governing permissions and |
johnathanlyu | 0:4d76c0bc0f78 | 14 | * limitations under the License. |
johnathanlyu | 0:4d76c0bc0f78 | 15 | */ |
johnathanlyu | 0:4d76c0bc0f78 | 16 | |
johnathanlyu | 0:4d76c0bc0f78 | 17 | #include <events/mbed_events.h> |
johnathanlyu | 0:4d76c0bc0f78 | 18 | #include <mbed.h> |
johnathanlyu | 0:4d76c0bc0f78 | 19 | #include "ble/BLE.h" |
johnathanlyu | 0:4d76c0bc0f78 | 20 | #include "ble/Gap.h" |
johnathanlyu | 0:4d76c0bc0f78 | 21 | #include "CCS811.h" |
johnathanlyu | 0:4d76c0bc0f78 | 22 | #include "AirQualityService.h" |
johnathanlyu | 0:4d76c0bc0f78 | 23 | |
johnathanlyu | 0:4d76c0bc0f78 | 24 | //initial function pin |
johnathanlyu | 0:4d76c0bc0f78 | 25 | Serial pc(p5, p4); |
johnathanlyu | 0:4d76c0bc0f78 | 26 | I2C i2c(p3, p2); |
johnathanlyu | 0:4d76c0bc0f78 | 27 | DigitalOut ledR(p16, 1); |
johnathanlyu | 0:4d76c0bc0f78 | 28 | CCS811 ccs811(i2c); |
johnathanlyu | 0:4d76c0bc0f78 | 29 | |
johnathanlyu | 0:4d76c0bc0f78 | 30 | static AirQualityService *AQSPtr; |
johnathanlyu | 0:4d76c0bc0f78 | 31 | static uint16_t eco2, tvoc; |
johnathanlyu | 0:4d76c0bc0f78 | 32 | |
johnathanlyu | 0:4d76c0bc0f78 | 33 | const static char DEVICE_NAME[] = "MtConnect04sMtSense02"; |
johnathanlyu | 0:4d76c0bc0f78 | 34 | static const uint16_t uuid16_list[] = {GattService::UUID_BATTERY_SERVICE}; |
johnathanlyu | 0:4d76c0bc0f78 | 35 | |
johnathanlyu | 0:4d76c0bc0f78 | 36 | |
johnathanlyu | 0:4d76c0bc0f78 | 37 | static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE); |
johnathanlyu | 0:4d76c0bc0f78 | 38 | |
johnathanlyu | 0:4d76c0bc0f78 | 39 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
johnathanlyu | 0:4d76c0bc0f78 | 40 | { |
johnathanlyu | 0:4d76c0bc0f78 | 41 | BLE::Instance().gap().startAdvertising(); |
johnathanlyu | 0:4d76c0bc0f78 | 42 | } |
johnathanlyu | 0:4d76c0bc0f78 | 43 | |
johnathanlyu | 0:4d76c0bc0f78 | 44 | void updateSensorValue() { |
johnathanlyu | 0:4d76c0bc0f78 | 45 | |
johnathanlyu | 0:4d76c0bc0f78 | 46 | ledR = 0; |
johnathanlyu | 0:4d76c0bc0f78 | 47 | |
johnathanlyu | 0:4d76c0bc0f78 | 48 | uint16_t revertECO2 = (eco2>>8) | (eco2<<8); |
johnathanlyu | 0:4d76c0bc0f78 | 49 | uint16_t revertTVOC = (tvoc>>8) | (tvoc<<8); |
johnathanlyu | 0:4d76c0bc0f78 | 50 | |
johnathanlyu | 0:4d76c0bc0f78 | 51 | AQSPtr->updateCO2(revertECO2); |
johnathanlyu | 0:4d76c0bc0f78 | 52 | AQSPtr->updateTVOC(revertTVOC); |
johnathanlyu | 0:4d76c0bc0f78 | 53 | |
johnathanlyu | 0:4d76c0bc0f78 | 54 | } |
johnathanlyu | 0:4d76c0bc0f78 | 55 | |
johnathanlyu | 0:4d76c0bc0f78 | 56 | void blinkCallback(void) |
johnathanlyu | 0:4d76c0bc0f78 | 57 | { |
johnathanlyu | 0:4d76c0bc0f78 | 58 | ledR = !ledR; /* Do blinky on LED1 while we're waiting for BLE events */ |
johnathanlyu | 0:4d76c0bc0f78 | 59 | } |
johnathanlyu | 0:4d76c0bc0f78 | 60 | |
johnathanlyu | 0:4d76c0bc0f78 | 61 | void CCS811Callback(void) { |
johnathanlyu | 0:4d76c0bc0f78 | 62 | |
johnathanlyu | 0:4d76c0bc0f78 | 63 | ccs811.readData(&eco2, &tvoc); |
johnathanlyu | 0:4d76c0bc0f78 | 64 | |
johnathanlyu | 0:4d76c0bc0f78 | 65 | BLE &ble = BLE::Instance(); |
johnathanlyu | 0:4d76c0bc0f78 | 66 | if (ble.gap().getState().connected) { |
johnathanlyu | 0:4d76c0bc0f78 | 67 | eventQueue.call(updateSensorValue); |
johnathanlyu | 0:4d76c0bc0f78 | 68 | } |
johnathanlyu | 0:4d76c0bc0f78 | 69 | |
johnathanlyu | 0:4d76c0bc0f78 | 70 | pc.printf("eco2:%8d, tvoc:%8d\r\n", eco2, tvoc); |
johnathanlyu | 0:4d76c0bc0f78 | 71 | |
johnathanlyu | 0:4d76c0bc0f78 | 72 | } |
johnathanlyu | 0:4d76c0bc0f78 | 73 | |
johnathanlyu | 0:4d76c0bc0f78 | 74 | /** |
johnathanlyu | 0:4d76c0bc0f78 | 75 | * This function is called when the ble initialization process has failled |
johnathanlyu | 0:4d76c0bc0f78 | 76 | */ |
johnathanlyu | 0:4d76c0bc0f78 | 77 | void onBleInitError(BLE &ble, ble_error_t error) |
johnathanlyu | 0:4d76c0bc0f78 | 78 | { |
johnathanlyu | 0:4d76c0bc0f78 | 79 | /* Initialization error handling should go here */ |
johnathanlyu | 0:4d76c0bc0f78 | 80 | } |
johnathanlyu | 0:4d76c0bc0f78 | 81 | |
johnathanlyu | 0:4d76c0bc0f78 | 82 | /** |
johnathanlyu | 0:4d76c0bc0f78 | 83 | * Callback triggered when the ble initialization process has finished |
johnathanlyu | 0:4d76c0bc0f78 | 84 | */ |
johnathanlyu | 0:4d76c0bc0f78 | 85 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
johnathanlyu | 0:4d76c0bc0f78 | 86 | { |
johnathanlyu | 0:4d76c0bc0f78 | 87 | BLE& ble = params->ble; |
johnathanlyu | 0:4d76c0bc0f78 | 88 | ble_error_t error = params->error; |
johnathanlyu | 0:4d76c0bc0f78 | 89 | |
johnathanlyu | 0:4d76c0bc0f78 | 90 | |
johnathanlyu | 0:4d76c0bc0f78 | 91 | uint8_t co2CID[16] = { |
johnathanlyu | 0:4d76c0bc0f78 | 92 | 0x00, 'M', 'T', 'M', |
johnathanlyu | 0:4d76c0bc0f78 | 93 | 'C' , 'O', '2', 0xFF, |
johnathanlyu | 0:4d76c0bc0f78 | 94 | 0xFF, 0xFF, 0xFF, 0xFF, |
johnathanlyu | 0:4d76c0bc0f78 | 95 | 0xFF, 0xFF, 0xFF, 0xFF |
johnathanlyu | 0:4d76c0bc0f78 | 96 | }; |
johnathanlyu | 0:4d76c0bc0f78 | 97 | |
johnathanlyu | 0:4d76c0bc0f78 | 98 | uint8_t tvocCID[16] = { |
johnathanlyu | 0:4d76c0bc0f78 | 99 | 0x00, 'M', 'T', 'M' , |
johnathanlyu | 0:4d76c0bc0f78 | 100 | 'T' , 'V', 'O', 'C' , |
johnathanlyu | 0:4d76c0bc0f78 | 101 | 0xFF, 0xFF, 0xFF, 0xFF, |
johnathanlyu | 0:4d76c0bc0f78 | 102 | 0xFF, 0xFF, 0xFF, 0xFF |
johnathanlyu | 0:4d76c0bc0f78 | 103 | }; |
johnathanlyu | 0:4d76c0bc0f78 | 104 | |
johnathanlyu | 0:4d76c0bc0f78 | 105 | UUID CO2CID (co2CID); |
johnathanlyu | 0:4d76c0bc0f78 | 106 | UUID TVOCCID(tvocCID); |
johnathanlyu | 0:4d76c0bc0f78 | 107 | |
johnathanlyu | 0:4d76c0bc0f78 | 108 | AQSPtr = new AirQualityService(ble, CO2CID, TVOCCID); |
johnathanlyu | 0:4d76c0bc0f78 | 109 | |
johnathanlyu | 0:4d76c0bc0f78 | 110 | |
johnathanlyu | 0:4d76c0bc0f78 | 111 | if (error != BLE_ERROR_NONE) { |
johnathanlyu | 0:4d76c0bc0f78 | 112 | /* In case of error, forward the error handling to onBleInitError */ |
johnathanlyu | 0:4d76c0bc0f78 | 113 | onBleInitError(ble, error); |
johnathanlyu | 0:4d76c0bc0f78 | 114 | return; |
johnathanlyu | 0:4d76c0bc0f78 | 115 | } |
johnathanlyu | 0:4d76c0bc0f78 | 116 | |
johnathanlyu | 0:4d76c0bc0f78 | 117 | /* Ensure that it is the default instance of BLE */ |
johnathanlyu | 0:4d76c0bc0f78 | 118 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
johnathanlyu | 0:4d76c0bc0f78 | 119 | return; |
johnathanlyu | 0:4d76c0bc0f78 | 120 | } |
johnathanlyu | 0:4d76c0bc0f78 | 121 | |
johnathanlyu | 0:4d76c0bc0f78 | 122 | ble.gap().onDisconnection(disconnectionCallback); |
johnathanlyu | 0:4d76c0bc0f78 | 123 | |
johnathanlyu | 0:4d76c0bc0f78 | 124 | /* Setup advertising */ |
johnathanlyu | 0:4d76c0bc0f78 | 125 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
johnathanlyu | 0:4d76c0bc0f78 | 126 | // ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *) uuid16_list, sizeof(uuid16_list)); |
johnathanlyu | 0:4d76c0bc0f78 | 127 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *) DEVICE_NAME, sizeof(DEVICE_NAME)); |
johnathanlyu | 0:4d76c0bc0f78 | 128 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
johnathanlyu | 0:4d76c0bc0f78 | 129 | ble.gap().setAdvertisingInterval(100); /* 1000ms */ |
johnathanlyu | 0:4d76c0bc0f78 | 130 | ble.gap().startAdvertising(); |
johnathanlyu | 0:4d76c0bc0f78 | 131 | } |
johnathanlyu | 0:4d76c0bc0f78 | 132 | |
johnathanlyu | 0:4d76c0bc0f78 | 133 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { |
johnathanlyu | 0:4d76c0bc0f78 | 134 | BLE &ble = BLE::Instance(); |
johnathanlyu | 0:4d76c0bc0f78 | 135 | eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); |
johnathanlyu | 0:4d76c0bc0f78 | 136 | } |
johnathanlyu | 0:4d76c0bc0f78 | 137 | |
johnathanlyu | 0:4d76c0bc0f78 | 138 | int main() |
johnathanlyu | 0:4d76c0bc0f78 | 139 | { |
johnathanlyu | 0:4d76c0bc0f78 | 140 | pc.set_flow_control(SerialBase::Disabled); |
johnathanlyu | 0:4d76c0bc0f78 | 141 | pc.baud(115200); |
johnathanlyu | 0:4d76c0bc0f78 | 142 | pc.printf("\r\n"); |
johnathanlyu | 0:4d76c0bc0f78 | 143 | pc.printf("Welcome MTM Node !\r\n"); |
johnathanlyu | 0:4d76c0bc0f78 | 144 | |
johnathanlyu | 0:4d76c0bc0f78 | 145 | eventQueue.call_every(500, blinkCallback); |
johnathanlyu | 0:4d76c0bc0f78 | 146 | |
johnathanlyu | 0:4d76c0bc0f78 | 147 | |
johnathanlyu | 0:4d76c0bc0f78 | 148 | ccs811.init(); |
johnathanlyu | 0:4d76c0bc0f78 | 149 | // ccs811.softRest(); |
johnathanlyu | 0:4d76c0bc0f78 | 150 | eventQueue.call_every(1000, CCS811Callback); |
johnathanlyu | 0:4d76c0bc0f78 | 151 | |
johnathanlyu | 0:4d76c0bc0f78 | 152 | BLE &ble = BLE::Instance(); |
johnathanlyu | 0:4d76c0bc0f78 | 153 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
johnathanlyu | 0:4d76c0bc0f78 | 154 | ble.init(bleInitComplete); |
johnathanlyu | 0:4d76c0bc0f78 | 155 | |
johnathanlyu | 0:4d76c0bc0f78 | 156 | eventQueue.dispatch_forever(); |
johnathanlyu | 0:4d76c0bc0f78 | 157 | |
johnathanlyu | 0:4d76c0bc0f78 | 158 | return 0; |
johnathanlyu | 0:4d76c0bc0f78 | 159 | } |