52 51 with same code

Dependencies:   BMP280 MtSense01 SHT2X

Fork of MtConnect04S_MtSense01 by MtM+

Committer:
johnathanlyu
Date:
Fri Apr 27 09:45:55 2018 +0000
Revision:
5:3eb7ff57d6f4
Parent:
4:0556535dbf82
initial commit

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 5:3eb7ff57d6f4 25 #ifdef NRF52
johnathanlyu 5:3eb7ff57d6f4 26 Serial pc(p20, p24);
johnathanlyu 5:3eb7ff57d6f4 27 #else
johnathanlyu 0:6eacca6939a9 28 Serial pc(p5, p4);
johnathanlyu 5:3eb7ff57d6f4 29 #endif
johnathanlyu 5:3eb7ff57d6f4 30
johnathanlyu 1:67bf70088281 31 I2C i2c(p3, p2);
johnathanlyu 0:6eacca6939a9 32 DigitalOut led1(p16, 1);
johnathanlyu 5:3eb7ff57d6f4 33 MtSense01 mtsense1(i2c, pc);
johnathanlyu 0:6eacca6939a9 34
johnathanlyu 5:3eb7ff57d6f4 35 const static char DEVICE_NAME[] = "Mt5MtSense01";
johnathanlyu 1:67bf70088281 36 static const uint16_t uuid16_list[] = {0x1822, 0x181A};
johnathanlyu 0:6eacca6939a9 37
johnathanlyu 1:67bf70088281 38 static EnvironmentalService *environmentalServicePtr;
johnathanlyu 0:6eacca6939a9 39
johnathanlyu 0:6eacca6939a9 40 static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE);
johnathanlyu 0:6eacca6939a9 41
johnathanlyu 0:6eacca6939a9 42 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
johnathanlyu 0:6eacca6939a9 43 {
johnathanlyu 0:6eacca6939a9 44 BLE::Instance().gap().startAdvertising();
johnathanlyu 0:6eacca6939a9 45 }
johnathanlyu 0:6eacca6939a9 46
johnathanlyu 1:67bf70088281 47 void initMtSense01() {
johnathanlyu 1:67bf70088281 48 mtsense1.Initial();
johnathanlyu 1:67bf70088281 49 }
johnathanlyu 1:67bf70088281 50
johnathanlyu 1:67bf70088281 51 void readMtSense01() {
johnathanlyu 1:67bf70088281 52 int pressPa;
johnathanlyu 1:67bf70088281 53 float tempC, humidity;
johnathanlyu 1:67bf70088281 54
johnathanlyu 1:67bf70088281 55 mtsense1.readData(&tempC, &pressPa, &humidity);
johnathanlyu 1:67bf70088281 56 BLE &ble = BLE::Instance();
johnathanlyu 1:67bf70088281 57 if (ble.gap().getState().connected) {
johnathanlyu 1:67bf70088281 58 environmentalServicePtr->updateHumidity(humidity);
johnathanlyu 1:67bf70088281 59 environmentalServicePtr->updatePressure(pressPa);
johnathanlyu 1:67bf70088281 60 environmentalServicePtr->updateTemperature(tempC);
johnathanlyu 0:6eacca6939a9 61 }
johnathanlyu 1:67bf70088281 62
johnathanlyu 1:67bf70088281 63 // pc.printf("tempC: %8f, pressPa: %8d, humidity: %8f \r\n", tempC, pressPa, humidity);
johnathanlyu 1:67bf70088281 64
johnathanlyu 0:6eacca6939a9 65 }
johnathanlyu 0:6eacca6939a9 66
johnathanlyu 0:6eacca6939a9 67 void blinkCallback(void)
johnathanlyu 0:6eacca6939a9 68 {
johnathanlyu 0:6eacca6939a9 69 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
johnathanlyu 0:6eacca6939a9 70
johnathanlyu 0:6eacca6939a9 71 BLE &ble = BLE::Instance();
johnathanlyu 0:6eacca6939a9 72 }
johnathanlyu 0:6eacca6939a9 73
johnathanlyu 0:6eacca6939a9 74 /**
johnathanlyu 0:6eacca6939a9 75 * This function is called when the ble initialization process has failled
johnathanlyu 0:6eacca6939a9 76 */
johnathanlyu 0:6eacca6939a9 77 void onBleInitError(BLE &ble, ble_error_t error)
johnathanlyu 0:6eacca6939a9 78 {
johnathanlyu 0:6eacca6939a9 79 /* Initialization error handling should go here */
johnathanlyu 0:6eacca6939a9 80 }
johnathanlyu 0:6eacca6939a9 81
johnathanlyu 0:6eacca6939a9 82 /**
johnathanlyu 0:6eacca6939a9 83 * Callback triggered when the ble initialization process has finished
johnathanlyu 0:6eacca6939a9 84 */
johnathanlyu 0:6eacca6939a9 85 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
johnathanlyu 0:6eacca6939a9 86 {
johnathanlyu 0:6eacca6939a9 87 BLE& ble = params->ble;
johnathanlyu 0:6eacca6939a9 88 ble_error_t error = params->error;
johnathanlyu 0:6eacca6939a9 89
johnathanlyu 0:6eacca6939a9 90 if (error != BLE_ERROR_NONE) {
johnathanlyu 0:6eacca6939a9 91 /* In case of error, forward the error handling to onBleInitError */
johnathanlyu 0:6eacca6939a9 92 onBleInitError(ble, error);
johnathanlyu 0:6eacca6939a9 93 return;
johnathanlyu 0:6eacca6939a9 94 }
johnathanlyu 0:6eacca6939a9 95
johnathanlyu 0:6eacca6939a9 96 /* Ensure that it is the default instance of BLE */
johnathanlyu 0:6eacca6939a9 97 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
johnathanlyu 0:6eacca6939a9 98 return;
johnathanlyu 0:6eacca6939a9 99 }
johnathanlyu 0:6eacca6939a9 100
johnathanlyu 0:6eacca6939a9 101 ble.gap().onDisconnection(disconnectionCallback);
johnathanlyu 0:6eacca6939a9 102
johnathanlyu 0:6eacca6939a9 103 /* Setup primary service */
johnathanlyu 1:67bf70088281 104 environmentalServicePtr = new EnvironmentalService(ble);
johnathanlyu 0:6eacca6939a9 105
johnathanlyu 0:6eacca6939a9 106 /* Setup advertising */
johnathanlyu 0:6eacca6939a9 107 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
johnathanlyu 0:6eacca6939a9 108 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *) uuid16_list, sizeof(uuid16_list));
johnathanlyu 0:6eacca6939a9 109 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *) DEVICE_NAME, sizeof(DEVICE_NAME));
johnathanlyu 0:6eacca6939a9 110 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
johnathanlyu 1:67bf70088281 111 ble.gap().setAdvertisingInterval(100); /* 100ms */
johnathanlyu 0:6eacca6939a9 112 ble.gap().startAdvertising();
johnathanlyu 0:6eacca6939a9 113 }
johnathanlyu 0:6eacca6939a9 114
johnathanlyu 0:6eacca6939a9 115 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
johnathanlyu 0:6eacca6939a9 116 BLE &ble = BLE::Instance();
johnathanlyu 0:6eacca6939a9 117 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
johnathanlyu 0:6eacca6939a9 118 }
johnathanlyu 0:6eacca6939a9 119
johnathanlyu 0:6eacca6939a9 120 int main()
johnathanlyu 0:6eacca6939a9 121 {
johnathanlyu 0:6eacca6939a9 122 pc.set_flow_control(SerialBase::Disabled);
johnathanlyu 0:6eacca6939a9 123 pc.baud(115200);
johnathanlyu 0:6eacca6939a9 124 pc.printf("\r\n");
johnathanlyu 0:6eacca6939a9 125 pc.printf("Welcome MTM Node !\r\n");
johnathanlyu 0:6eacca6939a9 126
johnathanlyu 1:67bf70088281 127
johnathanlyu 0:6eacca6939a9 128 eventQueue.call_every(500, blinkCallback);
johnathanlyu 1:67bf70088281 129
johnathanlyu 1:67bf70088281 130 initMtSense01();
johnathanlyu 1:67bf70088281 131 eventQueue.call_every(1000, readMtSense01); // 1000 ms repeat
johnathanlyu 0:6eacca6939a9 132
johnathanlyu 0:6eacca6939a9 133 BLE &ble = BLE::Instance();
johnathanlyu 0:6eacca6939a9 134 ble.onEventsToProcess(scheduleBleEventsProcessing);
johnathanlyu 0:6eacca6939a9 135 ble.init(bleInitComplete);
johnathanlyu 0:6eacca6939a9 136
johnathanlyu 0:6eacca6939a9 137 eventQueue.dispatch_forever();
johnathanlyu 0:6eacca6939a9 138
johnathanlyu 0:6eacca6939a9 139 return 0;
johnathanlyu 0:6eacca6939a9 140 }