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.
WeatherStation.cpp@22:79c64511d34d, 2019-05-29 (annotated)
- Committer:
- loicguibert
- Date:
- Wed May 29 06:36:54 2019 +0000
- Revision:
- 22:79c64511d34d
- Parent:
- 19:e056f98192f2
- Child:
- 23:f45027ac625c
EnvironmentalService modified: each characteristic splitted
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| loicguibert | 4:bfe306335065 | 1 | #include "WeatherStation.h" |
| loicguibert | 4:bfe306335065 | 2 | |
| loicguibert | 4:bfe306335065 | 3 | // initialization of static data members |
| loicguibert | 12:856286ad4cdc | 4 | const int WeatherStation::m_measurementInterval = 9000; |
| loicguibert | 12:856286ad4cdc | 5 | const string WeatherStation::m_stationName = "GuiSpa"; |
| loicguibert | 4:bfe306335065 | 6 | |
| loicguibert | 4:bfe306335065 | 7 | // constructor |
| loicguibert | 11:dbc310addbf6 | 8 | WeatherStation::WeatherStation(IDevKit& iDevKit, Logger& logger, BLE& ble) : |
| loicguibert | 14:c5578b5edabe | 9 | GAPPeripheral(ble, iDevKit, m_stationName, GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED, m_eventQueue, logger), |
| loicguibert | 12:856286ad4cdc | 10 | m_iDevKit(iDevKit), |
| loicguibert | 12:856286ad4cdc | 11 | m_logger(logger), |
| loicguibert | 12:856286ad4cdc | 12 | m_lps25hb(m_iDevKit.getSDA(), m_iDevKit.getSCL(), m_logger), |
| loicguibert | 12:856286ad4cdc | 13 | m_hdc1000(m_iDevKit.getSDA(), m_iDevKit.getSCL(), m_iDevKit.getDataRdy(), m_logger), |
| loicguibert | 16:eed9a9ba319c | 14 | m_history(logger), |
| loicguibert | 14:c5578b5edabe | 15 | m_batteryService(ble, m_logger), |
| loicguibert | 14:c5578b5edabe | 16 | m_environmentalService(ble, m_logger) { |
| loicguibert | 12:856286ad4cdc | 17 | // Set the right timing at the Station's start |
| loicguibert | 12:856286ad4cdc | 18 | time(NULL); |
| loicguibert | 4:bfe306335065 | 19 | } |
| loicguibert | 4:bfe306335065 | 20 | |
| loicguibert | 4:bfe306335065 | 21 | void WeatherStation::start() { |
| loicguibert | 4:bfe306335065 | 22 | m_logger.log("WeatherStation is starting\r\n"); |
| loicguibert | 5:0d9e292a9d06 | 23 | |
| loicguibert | 4:bfe306335065 | 24 | // make sure that the LPW25HB device is present |
| loicguibert | 4:bfe306335065 | 25 | // if not log an error and return |
| loicguibert | 5:0d9e292a9d06 | 26 | if (m_lps25hb.probe()) { |
| loicguibert | 5:0d9e292a9d06 | 27 | m_logger.log("LPS25HB device found\r\n"); |
| loicguibert | 5:0d9e292a9d06 | 28 | } else { |
| loicguibert | 5:0d9e292a9d06 | 29 | m_logger.log("Device LPS25HB not found!\r\n"); |
| loicguibert | 5:0d9e292a9d06 | 30 | return; |
| loicguibert | 5:0d9e292a9d06 | 31 | } |
| loicguibert | 5:0d9e292a9d06 | 32 | |
| loicguibert | 4:bfe306335065 | 33 | // make sure that the HDC1000 device is present |
| loicguibert | 4:bfe306335065 | 34 | // if not log an error and return |
| loicguibert | 5:0d9e292a9d06 | 35 | if (m_hdc1000.probe()) { |
| loicguibert | 5:0d9e292a9d06 | 36 | m_logger.log("HDC1000 device found\r\n"); |
| loicguibert | 5:0d9e292a9d06 | 37 | } else { |
| loicguibert | 5:0d9e292a9d06 | 38 | m_logger.log("Device HDC1000 not found!\r\n"); |
| loicguibert | 5:0d9e292a9d06 | 39 | return; |
| loicguibert | 5:0d9e292a9d06 | 40 | } |
| loicguibert | 6:5f56e551f84f | 41 | |
| loicguibert | 6:5f56e551f84f | 42 | //Cleaning the console by the addition of a blank line |
| loicguibert | 6:5f56e551f84f | 43 | m_logger.log("\r\n"); |
| loicguibert | 4:bfe306335065 | 44 | |
| loicguibert | 4:bfe306335065 | 45 | // schedule measurements every m_measurementInterval milliseconds |
| loicguibert | 4:bfe306335065 | 46 | // apply the same syntax as in the previous call |
| loicguibert | 4:bfe306335065 | 47 | // 1. the measurement interval is m_measurementInterval |
| loicguibert | 4:bfe306335065 | 48 | // 2. the object is the WeatherStation itself (recall how you refer to the current object in Java - the same applies in c++) |
| loicguibert | 4:bfe306335065 | 49 | // 3. the method to call is performMeasurements() |
| loicguibert | 4:bfe306335065 | 50 | // m_eventQueue.call_every(TO COMPLETED); |
| loicguibert | 4:bfe306335065 | 51 | m_eventQueue.call_every(m_measurementInterval, this, &WeatherStation::performMeasurements); |
| loicguibert | 11:dbc310addbf6 | 52 | |
| loicguibert | 12:856286ad4cdc | 53 | // Starting the BLE peripheral with Advertising State |
| loicguibert | 14:c5578b5edabe | 54 | GAPPeripheral::advertise(); |
| loicguibert | 11:dbc310addbf6 | 55 | } |
| loicguibert | 15:aac1b3950a9e | 56 | |
| loicguibert | 15:aac1b3950a9e | 57 | |
| loicguibert | 15:aac1b3950a9e | 58 | void WeatherStation::onInitComplete(BLE::InitializationCompleteCallbackContext *event) { |
| loicguibert | 15:aac1b3950a9e | 59 | GAPPeripheral::onInitComplete(event); |
| loicguibert | 15:aac1b3950a9e | 60 | m_batteryService.addServiceToGattServer(); |
| loicguibert | 15:aac1b3950a9e | 61 | m_environmentalService.addServiceToGattServer(); |
| loicguibert | 15:aac1b3950a9e | 62 | performMeasurements(); |
| loicguibert | 15:aac1b3950a9e | 63 | } |
| loicguibert | 15:aac1b3950a9e | 64 | |
| loicguibert | 4:bfe306335065 | 65 | |
| loicguibert | 4:bfe306335065 | 66 | void WeatherStation::performMeasurements(void) { |
| loicguibert | 6:5f56e551f84f | 67 | m_logger.log("Performing measurements:\r\n"); |
| loicguibert | 4:bfe306335065 | 68 | |
| loicguibert | 4:bfe306335065 | 69 | // get and log pressure |
| loicguibert | 12:856286ad4cdc | 70 | double pressure = m_lps25hb.getPressure(); |
| loicguibert | 12:856286ad4cdc | 71 | m_logger.log("Pressure: %.02f hPa\r\n", pressure); |
| loicguibert | 4:bfe306335065 | 72 | |
| loicguibert | 4:bfe306335065 | 73 | // get and log temperature |
| loicguibert | 12:856286ad4cdc | 74 | double temp = m_hdc1000.getTemperature(); |
| loicguibert | 12:856286ad4cdc | 75 | m_logger.log("Temperature: %.02f C\r\n", temp); |
| loicguibert | 4:bfe306335065 | 76 | |
| loicguibert | 4:bfe306335065 | 77 | // get and log humidity |
| loicguibert | 12:856286ad4cdc | 78 | double humidity = m_hdc1000.getHumidity(); |
| loicguibert | 12:856286ad4cdc | 79 | m_logger.log("Humidiy: %.02f %%\r\n", humidity); |
| loicguibert | 4:bfe306335065 | 80 | |
| loicguibert | 4:bfe306335065 | 81 | // get and log the battery level |
| loicguibert | 12:856286ad4cdc | 82 | uint8_t battery = m_iDevKit.getBatteryLevel(); |
| loicguibert | 15:aac1b3950a9e | 83 | m_logger.log("Battery: %u %%\r\n", battery); |
| loicguibert | 6:5f56e551f84f | 84 | |
| loicguibert | 12:856286ad4cdc | 85 | // Cleaning the console by the addition of a blank line |
| loicguibert | 6:5f56e551f84f | 86 | m_logger.log("\r\n"); |
| loicguibert | 4:bfe306335065 | 87 | |
| loicguibert | 12:856286ad4cdc | 88 | // get and encode the system's elapsed time |
| loicguibert | 12:856286ad4cdc | 89 | time_t seconds = time(NULL); |
| loicguibert | 12:856286ad4cdc | 90 | float time = (uint32_t)seconds; |
| loicguibert | 12:856286ad4cdc | 91 | |
| loicguibert | 12:856286ad4cdc | 92 | // Saving the measured values |
| loicguibert | 17:76787f5a334f | 93 | m_history.addMeasurement(((uint32_t)(pressure*10)), ((int16_t)(temp*100)), ((uint16_t)(humidity*100)), ((uint32_t)time)); |
| loicguibert | 12:856286ad4cdc | 94 | |
| loicguibert | 22:79c64511d34d | 95 | // Send the measured values to GATT's services |
| loicguibert | 22:79c64511d34d | 96 | m_environmentalService.updatePressureLevel ((uint32_t)(pressure*10)); |
| loicguibert | 22:79c64511d34d | 97 | m_environmentalService.updateTemperatureLevel ((int16_t)(temp*100)); |
| loicguibert | 22:79c64511d34d | 98 | m_environmentalService.updateHumidityLevel ((uint16_t)(humidity*100)); |
| loicguibert | 22:79c64511d34d | 99 | // get pressure trend for the environmental service |
| spadaaa | 19:e056f98192f2 | 100 | uint32_t averagePressure = m_history.getPressureTrend(); |
| loicguibert | 22:79c64511d34d | 101 | m_environmentalService.updatePressureLevel (averagePressure); |
| loicguibert | 15:aac1b3950a9e | 102 | m_batteryService.updateBatteryLevel(battery); |
| loicguibert | 5:0d9e292a9d06 | 103 | } |