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: BLE_API mbed nRF51822 SHT21_ncleee
main.cpp
00001 #define I2C_SDA p3 00002 #define I2C_SCL p4 00003 00004 #include "mbed.h" 00005 #include "BLE.h" 00006 #include "battery.h" 00007 #include "WeatherService.h" 00008 #include "BatteryService.h" 00009 #include "BMP180.h" 00010 #include "SHT21_ncleee.h" 00011 00012 00013 float temperature1; 00014 float temperature2; 00015 float pressure; 00016 float humidity; 00017 00018 BLE ble; 00019 00020 DigitalOut okLED(LED1); 00021 DigitalOut errLED(LED2); 00022 DigitalOut instrumentsPower(p30); 00023 00024 BMP180 bmp180; 00025 SHT21 sht21(&i2c); 00026 00027 00028 const static char DEVICE_NAME[] = "Weather Station"; 00029 00030 static const uint16_t serviceList[] = { 00031 GattService::UUID_ENVIRONMENTAL_SERVICE, 00032 GattService::UUID_BATTERY_SERVICE 00033 }; 00034 00035 static volatile bool triggerSensorPolling = false; 00036 00037 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00038 { 00039 /* Restart Advertising on disconnection*/ 00040 ble.gap().startAdvertising(); 00041 } 00042 00043 void blink(void) 00044 { 00045 // statusLED = !statusLED; 00046 triggerSensorPolling = true; 00047 } 00048 00049 void updateFromBMP180() { 00050 uint8_t c = bmp180.readByte(BMP180_ADDRESS, BMP180_WHO_AM_I); 00051 00052 printf("BMP-180 is 0x%x\n\r", c); 00053 printf("BMP-180 should be 0x55\n"); 00054 00055 if(c == 0x55) { 00056 printf("BMP-180 online\n"); 00057 00058 printf("Calibrating BMP-180..."); 00059 bmp180.BMP180Calibration(); 00060 printf("done\n"); 00061 } 00062 else 00063 { 00064 printf("BMP-180 unreachable\n"); 00065 return; 00066 } 00067 00068 temperature1 = (float)bmp180.BMP180GetTemperature()/10.0f; 00069 pressure = (float)bmp180.BMP180GetPressure(); 00070 } 00071 00072 void updateFromSHT21() { 00073 temperature2 = sht21.readTemp(); 00074 humidity = sht21.readHumidity(); 00075 } 00076 00077 int main(void) 00078 { 00079 printf("Start\n"); 00080 00081 okLED = 1; 00082 errLED = 1; 00083 00084 Ticker ticker; 00085 ticker.attach(blink, 5); 00086 00087 ble.init(); 00088 ble.gap().onDisconnection(disconnectionCallback); 00089 00090 /* Setup weather service. */ 00091 WeatherService weatherService(ble); 00092 BatteryService batteryService(ble, 0); 00093 Battery battery(BATTERY_PIN); 00094 00095 /* setup advertising */ 00096 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00097 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)serviceList, sizeof(serviceList)); 00098 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::UNKNOWN); 00099 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00100 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00101 ble.gap().setAdvertisingInterval(1000); /* 1000ms */ 00102 ble.gap().startAdvertising(); 00103 00104 while (true) { 00105 if (triggerSensorPolling && ble.getGapState().connected) { 00106 okLED = 1; 00107 triggerSensorPolling = false; 00108 00109 instrumentsPower = 1; 00110 wait(1); 00111 updateFromBMP180(); 00112 updateFromSHT21(); 00113 instrumentsPower = 0; 00114 00115 float temperature = (temperature1 + temperature2) / 2; 00116 00117 printf("Temp1: %.1f ºC \n", temperature1); 00118 printf("Temp2: %.1f ºC \n", temperature2); 00119 printf("Temp Avg.: %.1f ºC \n", temperature); 00120 printf("Pressure: %.3f Pa \n", pressure); 00121 printf("Humidity: %.1f%% \n", humidity); 00122 printf("---\n"); 00123 00124 00125 weatherService.updateTemperature(temperature); 00126 weatherService.updatePressure(pressure); 00127 weatherService.updateHumidity(humidity); 00128 00129 00130 batteryService.updateBatteryLevel(battery.read()); 00131 } else { 00132 ble.waitForEvent(); 00133 } 00134 } 00135 }
Generated on Sun Jul 17 2022 20:00:16 by
1.7.2