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.
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 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 "mbed.h" 00018 #include "ble/services/BatteryService.h" 00019 #include "ble/services/DeviceInformationService.h" 00020 #include "ble/BLE.h" 00021 00022 #include "hts221.h" 00023 #include "uvis25.h" 00024 00025 DigitalOut led1(LED1); 00026 00027 static const uint8_t UUID_HUMI_AND_UVI[] = {0xf5, 0x59, 0xa2, 0x49, 0xbe, 0xb1, 0x4c, 0x54, 0xa1, 0x0a, 0xc7, 0x95, 0x7e, 0x17, 0xf8, 0x67}; 00028 uint8_t wrs_HumiUVI_payload[11] = {0, }; 00029 uint8_t htsTempPayload[7] = {0,}; 00030 /* Health Thermometer Service */ 00031 /* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.heart_rate.xml */ 00032 /* Temperature Measurement: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml */ 00033 GattCharacteristic htsTemp ( GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR, htsTempPayload, 6, 13, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE); 00034 GattCharacteristic wrs_HumiUVI ( UUID_HUMI_AND_UVI, wrs_HumiUVI_payload, 1, 7, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE); 00035 GattCharacteristic *htsChars[] = {&htsTemp, &wrs_HumiUVI}; 00036 GattService htsService ( GattService::UUID_HEALTH_THERMOMETER_SERVICE, htsChars, sizeof(htsChars) / sizeof(GattCharacteristic *)); 00037 00038 float tempCelsius = 25.50; 00039 int32_t tempCelsius_ix100; 00040 int8_t exponent = -2; 00041 uint8_t UTC[7] = { 222, 7, 9, 11, 13, 42, 59 }; 00042 float humi = 55; 00043 int32_t humi_ix100; 00044 uint8_t uvi = 8; 00045 uint32_t counter = 0; 00046 00047 const static char DEVICE_NAME[] = "WRS_d7"; 00048 static const uint16_t uuid16_list[] = {GattService::UUID_BATTERY_SERVICE, 00049 GattService::UUID_DEVICE_INFORMATION_SERVICE}; 00050 static volatile bool triggerSensorPolling = false; 00051 00052 /**************************************************************************/ 00053 /*! 00054 @brief This custom class can be used to override any GattServerEvents 00055 that you are interested in handling on an application level. 00056 */ 00057 /**************************************************************************/ 00058 // 00059 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00060 { 00061 BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising 00062 } 00063 00064 void periodicCallback(void) 00065 { 00066 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ 00067 00068 /* Note that the periodicCallback() executes in interrupt context, so it is safer to do 00069 * heavy-weight sensor polling from the main thread. */ 00070 triggerSensorPolling = true; 00071 } 00072 00073 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 00074 { 00075 BLE &ble = params->ble; 00076 ble_error_t error = params->error; 00077 00078 if (error != BLE_ERROR_NONE) { 00079 return; 00080 } 00081 00082 ble.gap().onDisconnection(disconnectionCallback); 00083 00084 /* Setup auxiliary services. */ 00085 BatteryService battery(ble); 00086 DeviceInformationService deviceInfo(ble, "Delta", "NQ620", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); 00087 00088 /* Setup advertising. */ 00089 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00090 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); 00091 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR); 00092 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00093 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00094 ble.gap().setAdvertisingInterval(1000); /* 1000ms */ 00095 ble.gap().startAdvertising(); 00096 00097 ble.addService(htsService); 00098 } 00099 00100 /**************************************************************************/ 00101 /*! 00102 @brief Program entry point 00103 */ 00104 /**************************************************************************/ 00105 00106 int main(void) 00107 { 00108 00109 hts221_init(); 00110 uvis25_init(); 00111 00112 HTS221_Calib(); 00113 00114 Ticker ticker; 00115 ticker.attach(periodicCallback, 2); 00116 00117 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); 00118 ble.init(bleInitComplete); 00119 00120 //ble.init(); 00121 // ble.onDisconnection(disconnectionCallback); 00122 00123 /* SpinWait for initialization to complete. This is necessary because the 00124 * BLE object is used in the main loop below. */ 00125 while (ble.hasInitialized() == false) { /* spin loop */ } 00126 00127 while (1) { 00128 00129 if(hts221_verify_product_id()) 00130 HTS221_ReadTempHumi(&tempCelsius, &humi); 00131 else { 00132 //show dummy value 00133 tempCelsius = 100; 00134 humi = 100; 00135 } 00136 00137 if(uvis25_verify_product_id()) 00138 uvi = UVIS25_ReadUVI(); 00139 else 00140 uvi = 100; //Environment Sensor APP will show NO UV DEVICE. 00141 00142 /* Update the Temperature measurement */ 00143 /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml */ 00144 tempCelsius_ix100 = tempCelsius * 100; 00145 htsTempPayload[0] = 0x02; // flag 00146 htsTempPayload[1] = tempCelsius_ix100%0xff; 00147 htsTempPayload[2] = tempCelsius_ix100/0xff; 00148 htsTempPayload[3] = tempCelsius_ix100/0xffff; 00149 htsTempPayload[4] = exponent; 00150 for (int i = 0; i < 7; i++) htsTempPayload[i+5] = UTC[i]; 00151 ble.updateCharacteristicValue(htsTemp.getValueAttribute().getHandle(), htsTempPayload, 12); 00152 00153 ble.waitForEvent(); 00154 wait(1); 00155 wrs_HumiUVI_payload[0] = 0x03; // flag 00156 memcpy(&wrs_HumiUVI_payload[1],&humi,4); 00157 wrs_HumiUVI_payload[5] = uvi; 00158 00159 ble.updateCharacteristicValue(wrs_HumiUVI.getValueAttribute().getHandle(), wrs_HumiUVI_payload, 6); 00160 ble.waitForEvent(); 00161 wait(1); 00162 00163 } 00164 00165 }
Generated on Wed Jul 13 2022 14:30:06 by
1.7.2