MtM+ / Mbed OS Mt05_MtSense01

Dependencies:   BMP280 MtSense01 SHT2X

Fork of MtConnect04S_MtSense01 by MtM+

Committer:
johnathanlyu
Date:
Sat Jun 03 02:12:36 2017 +0000
Revision:
3:3957bdf33fb7
Parent:
1:67bf70088281
Child:
4:0556535dbf82
remove battery_service.h

Who changed what in which revision?

UserRevisionLine numberNew contents of line
johnathanlyu 0:6eacca6939a9 1 /* mbed Microcontroller Library
johnathanlyu 0:6eacca6939a9 2 * Copyright (c) 2006-2014 ARM Limited
johnathanlyu 0:6eacca6939a9 3 *
johnathanlyu 0:6eacca6939a9 4 * Licensed under the Apache License, Version 2.0 (the "License");
johnathanlyu 0:6eacca6939a9 5 * you may not use this file except in compliance with the License.
johnathanlyu 0:6eacca6939a9 6 * You may obtain a copy of the License at
johnathanlyu 0:6eacca6939a9 7 *
johnathanlyu 0:6eacca6939a9 8 * http://www.apache.org/licenses/LICENSE-2.0
johnathanlyu 0:6eacca6939a9 9 *
johnathanlyu 0:6eacca6939a9 10 * Unless required by applicable law or agreed to in writing, software
johnathanlyu 0:6eacca6939a9 11 * distributed under the License is distributed on an "AS IS" BASIS,
johnathanlyu 0:6eacca6939a9 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
johnathanlyu 0:6eacca6939a9 13 * See the License for the specific language governing permissions and
johnathanlyu 0:6eacca6939a9 14 * limitations under the License.
johnathanlyu 0:6eacca6939a9 15 */
johnathanlyu 0:6eacca6939a9 16
johnathanlyu 0:6eacca6939a9 17 #include <events/mbed_events.h>
johnathanlyu 0:6eacca6939a9 18 #include <mbed.h>
johnathanlyu 0:6eacca6939a9 19 #include "ble/BLE.h"
johnathanlyu 0:6eacca6939a9 20 #include "ble/Gap.h"
johnathanlyu 1:67bf70088281 21 #include "ble/services/EnvironmentalService.h"
johnathanlyu 1:67bf70088281 22 #include "MtSense01.h"
johnathanlyu 0:6eacca6939a9 23
johnathanlyu 0:6eacca6939a9 24 /* UART printf */
johnathanlyu 0:6eacca6939a9 25 Serial pc(p5, p4);
johnathanlyu 1:67bf70088281 26 I2C i2c(p3, p2);
johnathanlyu 0:6eacca6939a9 27 DigitalOut led1(p16, 1);
johnathanlyu 1:67bf70088281 28 MtSense01 mtsense1(i2c);
johnathanlyu 0:6eacca6939a9 29
johnathanlyu 1:67bf70088281 30 const static char DEVICE_NAME[] = "MtSense01_Test";
johnathanlyu 1:67bf70088281 31 static const uint16_t uuid16_list[] = {0x1822, 0x181A};
johnathanlyu 0:6eacca6939a9 32
johnathanlyu 1:67bf70088281 33 static EnvironmentalService *environmentalServicePtr;
johnathanlyu 0:6eacca6939a9 34
johnathanlyu 0:6eacca6939a9 35 static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE);
johnathanlyu 0:6eacca6939a9 36
johnathanlyu 0:6eacca6939a9 37 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
johnathanlyu 0:6eacca6939a9 38 {
johnathanlyu 0:6eacca6939a9 39 BLE::Instance().gap().startAdvertising();
johnathanlyu 0:6eacca6939a9 40 }
johnathanlyu 0:6eacca6939a9 41
johnathanlyu 1:67bf70088281 42 void initMtSense01() {
johnathanlyu 1:67bf70088281 43 mtsense1.Initial();
johnathanlyu 1:67bf70088281 44 }
johnathanlyu 1:67bf70088281 45
johnathanlyu 1:67bf70088281 46 void readMtSense01() {
johnathanlyu 1:67bf70088281 47 int pressPa;
johnathanlyu 1:67bf70088281 48 float tempC, humidity;
johnathanlyu 1:67bf70088281 49
johnathanlyu 1:67bf70088281 50 mtsense1.readData(&tempC, &pressPa, &humidity);
johnathanlyu 1:67bf70088281 51 BLE &ble = BLE::Instance();
johnathanlyu 1:67bf70088281 52 if (ble.gap().getState().connected) {
johnathanlyu 1:67bf70088281 53 environmentalServicePtr->updateHumidity(humidity);
johnathanlyu 1:67bf70088281 54 environmentalServicePtr->updatePressure(pressPa);
johnathanlyu 1:67bf70088281 55 environmentalServicePtr->updateTemperature(tempC);
johnathanlyu 0:6eacca6939a9 56 }
johnathanlyu 1:67bf70088281 57
johnathanlyu 1:67bf70088281 58 // pc.printf("tempC: %8f, pressPa: %8d, humidity: %8f \r\n", tempC, pressPa, humidity);
johnathanlyu 1:67bf70088281 59
johnathanlyu 0:6eacca6939a9 60 }
johnathanlyu 0:6eacca6939a9 61
johnathanlyu 0:6eacca6939a9 62 void blinkCallback(void)
johnathanlyu 0:6eacca6939a9 63 {
johnathanlyu 0:6eacca6939a9 64 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
johnathanlyu 0:6eacca6939a9 65
johnathanlyu 0:6eacca6939a9 66 BLE &ble = BLE::Instance();
johnathanlyu 0:6eacca6939a9 67 }
johnathanlyu 0:6eacca6939a9 68
johnathanlyu 0:6eacca6939a9 69 /**
johnathanlyu 0:6eacca6939a9 70 * This function is called when the ble initialization process has failled
johnathanlyu 0:6eacca6939a9 71 */
johnathanlyu 0:6eacca6939a9 72 void onBleInitError(BLE &ble, ble_error_t error)
johnathanlyu 0:6eacca6939a9 73 {
johnathanlyu 0:6eacca6939a9 74 /* Initialization error handling should go here */
johnathanlyu 0:6eacca6939a9 75 }
johnathanlyu 0:6eacca6939a9 76
johnathanlyu 0:6eacca6939a9 77 /**
johnathanlyu 0:6eacca6939a9 78 * Callback triggered when the ble initialization process has finished
johnathanlyu 0:6eacca6939a9 79 */
johnathanlyu 0:6eacca6939a9 80 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
johnathanlyu 0:6eacca6939a9 81 {
johnathanlyu 0:6eacca6939a9 82 BLE& ble = params->ble;
johnathanlyu 0:6eacca6939a9 83 ble_error_t error = params->error;
johnathanlyu 0:6eacca6939a9 84
johnathanlyu 0:6eacca6939a9 85 if (error != BLE_ERROR_NONE) {
johnathanlyu 0:6eacca6939a9 86 /* In case of error, forward the error handling to onBleInitError */
johnathanlyu 0:6eacca6939a9 87 onBleInitError(ble, error);
johnathanlyu 0:6eacca6939a9 88 return;
johnathanlyu 0:6eacca6939a9 89 }
johnathanlyu 0:6eacca6939a9 90
johnathanlyu 0:6eacca6939a9 91 /* Ensure that it is the default instance of BLE */
johnathanlyu 0:6eacca6939a9 92 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
johnathanlyu 0:6eacca6939a9 93 return;
johnathanlyu 0:6eacca6939a9 94 }
johnathanlyu 0:6eacca6939a9 95
johnathanlyu 0:6eacca6939a9 96 ble.gap().onDisconnection(disconnectionCallback);
johnathanlyu 0:6eacca6939a9 97
johnathanlyu 0:6eacca6939a9 98 /* Setup primary service */
johnathanlyu 1:67bf70088281 99 environmentalServicePtr = new EnvironmentalService(ble);
johnathanlyu 0:6eacca6939a9 100
johnathanlyu 0:6eacca6939a9 101 /* Setup advertising */
johnathanlyu 0:6eacca6939a9 102 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
johnathanlyu 0:6eacca6939a9 103 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *) uuid16_list, sizeof(uuid16_list));
johnathanlyu 0:6eacca6939a9 104 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *) DEVICE_NAME, sizeof(DEVICE_NAME));
johnathanlyu 0:6eacca6939a9 105 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
johnathanlyu 1:67bf70088281 106 ble.gap().setAdvertisingInterval(100); /* 100ms */
johnathanlyu 0:6eacca6939a9 107 ble.gap().startAdvertising();
johnathanlyu 0:6eacca6939a9 108 }
johnathanlyu 0:6eacca6939a9 109
johnathanlyu 0:6eacca6939a9 110 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
johnathanlyu 0:6eacca6939a9 111 BLE &ble = BLE::Instance();
johnathanlyu 0:6eacca6939a9 112 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
johnathanlyu 0:6eacca6939a9 113 }
johnathanlyu 0:6eacca6939a9 114
johnathanlyu 0:6eacca6939a9 115 int main()
johnathanlyu 0:6eacca6939a9 116 {
johnathanlyu 0:6eacca6939a9 117 pc.set_flow_control(SerialBase::Disabled);
johnathanlyu 0:6eacca6939a9 118 pc.baud(115200);
johnathanlyu 0:6eacca6939a9 119 pc.printf("\r\n");
johnathanlyu 0:6eacca6939a9 120 pc.printf("Welcome MTM Node !\r\n");
johnathanlyu 0:6eacca6939a9 121
johnathanlyu 1:67bf70088281 122
johnathanlyu 0:6eacca6939a9 123 eventQueue.call_every(500, blinkCallback);
johnathanlyu 1:67bf70088281 124
johnathanlyu 1:67bf70088281 125 initMtSense01();
johnathanlyu 1:67bf70088281 126 eventQueue.call_every(1000, readMtSense01); // 1000 ms repeat
johnathanlyu 0:6eacca6939a9 127
johnathanlyu 0:6eacca6939a9 128 BLE &ble = BLE::Instance();
johnathanlyu 0:6eacca6939a9 129 ble.onEventsToProcess(scheduleBleEventsProcessing);
johnathanlyu 0:6eacca6939a9 130 ble.init(bleInitComplete);
johnathanlyu 0:6eacca6939a9 131
johnathanlyu 0:6eacca6939a9 132 eventQueue.dispatch_forever();
johnathanlyu 0:6eacca6939a9 133
johnathanlyu 0:6eacca6939a9 134 return 0;
johnathanlyu 0:6eacca6939a9 135 }