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.
Dependencies: CCS811
Fork of MtConnect04S_MtSense02 by
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2014 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include <events/mbed_events.h> 00018 #include <mbed.h> 00019 #include "ble/BLE.h" 00020 #include "ble/Gap.h" 00021 #include "CCS811.h" 00022 #include "AirQualityService.h" 00023 00024 //initial function pin 00025 #ifdef NRF52 00026 Serial pc(p20, p24); 00027 #else 00028 Serial pc(p5, p4); 00029 #endif 00030 00031 I2C i2c(p3, p2); 00032 DigitalOut ledR(p16, 1); 00033 CCS811 ccs811(i2c, pc); 00034 00035 static AirQualityService *AQSPtr; 00036 static uint16_t eco2, tvoc; 00037 00038 const static char DEVICE_NAME[] = "Mt5MtSense02"; 00039 static const uint16_t uuid16_list[] = {GattService::UUID_BATTERY_SERVICE}; 00040 00041 00042 static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE); 00043 00044 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00045 { 00046 BLE::Instance().gap().startAdvertising(); 00047 } 00048 00049 void updateSensorValue() { 00050 00051 ledR = 0; 00052 00053 uint16_t revertECO2 = (eco2>>8) | (eco2<<8); 00054 uint16_t revertTVOC = (tvoc>>8) | (tvoc<<8); 00055 00056 AQSPtr->updateCO2(revertECO2); 00057 AQSPtr->updateTVOC(revertTVOC); 00058 00059 } 00060 00061 void blinkCallback(void) 00062 { 00063 ledR = !ledR; /* Do blinky on LED1 while we're waiting for BLE events */ 00064 } 00065 00066 void CCS811Callback(void) { 00067 00068 ccs811.readData(&eco2, &tvoc); 00069 00070 BLE &ble = BLE::Instance(); 00071 if (ble.gap().getState().connected) { 00072 eventQueue.call(updateSensorValue); 00073 } 00074 00075 pc.printf("eco2:%8d, tvoc:%8d\r\n", eco2, tvoc); 00076 00077 } 00078 00079 /** 00080 * This function is called when the ble initialization process has failled 00081 */ 00082 void onBleInitError(BLE &ble, ble_error_t error) 00083 { 00084 /* Initialization error handling should go here */ 00085 } 00086 00087 /** 00088 * Callback triggered when the ble initialization process has finished 00089 */ 00090 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 00091 { 00092 BLE& ble = params->ble; 00093 ble_error_t error = params->error; 00094 00095 00096 uint8_t co2CID[16] = { 00097 0x00, 'M', 'T', 'M', 00098 'C' , 'O', '2', 0xFF, 00099 0xFF, 0xFF, 0xFF, 0xFF, 00100 0xFF, 0xFF, 0xFF, 0xFF 00101 }; 00102 00103 uint8_t tvocCID[16] = { 00104 0x00, 'M', 'T', 'M' , 00105 'T' , 'V', 'O', 'C' , 00106 0xFF, 0xFF, 0xFF, 0xFF, 00107 0xFF, 0xFF, 0xFF, 0xFF 00108 }; 00109 00110 UUID CO2CID (co2CID); 00111 UUID TVOCCID(tvocCID); 00112 00113 AQSPtr = new AirQualityService(ble, CO2CID, TVOCCID); 00114 00115 00116 if (error != BLE_ERROR_NONE) { 00117 /* In case of error, forward the error handling to onBleInitError */ 00118 onBleInitError(ble, error); 00119 return; 00120 } 00121 00122 /* Ensure that it is the default instance of BLE */ 00123 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { 00124 return; 00125 } 00126 00127 ble.gap().onDisconnection(disconnectionCallback); 00128 00129 /* Setup advertising */ 00130 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00131 // ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *) uuid16_list, sizeof(uuid16_list)); 00132 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *) DEVICE_NAME, sizeof(DEVICE_NAME)); 00133 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00134 ble.gap().setAdvertisingInterval(100); /* 1000ms */ 00135 ble.gap().startAdvertising(); 00136 } 00137 00138 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { 00139 BLE &ble = BLE::Instance(); 00140 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); 00141 } 00142 00143 int main() 00144 { 00145 pc.set_flow_control(SerialBase::Disabled); 00146 pc.baud(115200); 00147 pc.printf("\r\n"); 00148 pc.printf("Welcome MTM Node !\r\n"); 00149 00150 eventQueue.call_every(500, blinkCallback); 00151 00152 ccs811.init(); 00153 // ccs811.softRest(); 00154 eventQueue.call_every(2000, CCS811Callback); 00155 00156 BLE &ble = BLE::Instance(); 00157 ble.onEventsToProcess(scheduleBleEventsProcessing); 00158 ble.init(bleInitComplete); 00159 00160 eventQueue.dispatch_forever(); 00161 00162 return 0; 00163 }
Generated on Fri Jul 15 2022 13:30:15 by
1.7.2
