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@14:c5578b5edabe, 2019-04-02 (annotated)
- Committer:
- loicguibert
- Date:
- Tue Apr 02 09:51:34 2019 +0000
- Revision:
- 14:c5578b5edabe
- Parent:
- 12:856286ad4cdc
- Child:
- 15:aac1b3950a9e
Added battery service;
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 | 14:c5578b5edabe | 14 | m_history(), |
| 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 | 4:bfe306335065 | 56 | |
| loicguibert | 4:bfe306335065 | 57 | void WeatherStation::performMeasurements(void) { |
| loicguibert | 6:5f56e551f84f | 58 | m_logger.log("Performing measurements:\r\n"); |
| loicguibert | 4:bfe306335065 | 59 | |
| loicguibert | 4:bfe306335065 | 60 | // get and log pressure |
| loicguibert | 12:856286ad4cdc | 61 | double pressure = m_lps25hb.getPressure(); |
| loicguibert | 12:856286ad4cdc | 62 | m_logger.log("Pressure: %.02f hPa\r\n", pressure); |
| loicguibert | 4:bfe306335065 | 63 | |
| loicguibert | 4:bfe306335065 | 64 | // get and log temperature |
| loicguibert | 12:856286ad4cdc | 65 | double temp = m_hdc1000.getTemperature(); |
| loicguibert | 12:856286ad4cdc | 66 | m_logger.log("Temperature: %.02f C\r\n", temp); |
| loicguibert | 4:bfe306335065 | 67 | |
| loicguibert | 4:bfe306335065 | 68 | // get and log humidity |
| loicguibert | 12:856286ad4cdc | 69 | double humidity = m_hdc1000.getHumidity(); |
| loicguibert | 12:856286ad4cdc | 70 | m_logger.log("Humidiy: %.02f %%\r\n", humidity); |
| loicguibert | 4:bfe306335065 | 71 | |
| loicguibert | 4:bfe306335065 | 72 | // get and log the battery level |
| loicguibert | 12:856286ad4cdc | 73 | uint8_t battery = m_iDevKit.getBatteryLevel(); |
| loicguibert | 12:856286ad4cdc | 74 | m_logger.log("Battery: %u mV\r\n", battery); |
| loicguibert | 6:5f56e551f84f | 75 | |
| loicguibert | 12:856286ad4cdc | 76 | // Cleaning the console by the addition of a blank line |
| loicguibert | 6:5f56e551f84f | 77 | m_logger.log("\r\n"); |
| loicguibert | 4:bfe306335065 | 78 | |
| loicguibert | 12:856286ad4cdc | 79 | // get and encode the system's elapsed time |
| loicguibert | 12:856286ad4cdc | 80 | time_t seconds = time(NULL); |
| loicguibert | 12:856286ad4cdc | 81 | float time = (uint32_t)seconds; |
| loicguibert | 12:856286ad4cdc | 82 | |
| loicguibert | 12:856286ad4cdc | 83 | // Saving the measured values |
| loicguibert | 12:856286ad4cdc | 84 | m_history.addMeasurement(pressure, temp, humidity, time); |
| loicguibert | 12:856286ad4cdc | 85 | |
| loicguibert | 12:856286ad4cdc | 86 | // Send the measured values for encoding |
| loicguibert | 12:856286ad4cdc | 87 | setupAdvertisementPayload(pressure, temp, humidity); |
| loicguibert | 12:856286ad4cdc | 88 | } |
| loicguibert | 12:856286ad4cdc | 89 | |
| loicguibert | 12:856286ad4cdc | 90 | void WeatherStation::setupAdvertisementPayload(double pressure, double temp, double humidity) { |
| loicguibert | 12:856286ad4cdc | 91 | |
| loicguibert | 12:856286ad4cdc | 92 | // Setting the right UUID (environmental values) to the Advertising Payload |
| loicguibert | 12:856286ad4cdc | 93 | dataPayload[0].serviceUUID = GattService::UUID_ENVIRONMENTAL_SERVICE; |
| loicguibert | 12:856286ad4cdc | 94 | |
| loicguibert | 12:856286ad4cdc | 95 | // Encode and store the measured environmental values to the Advertising Payload |
| loicguibert | 12:856286ad4cdc | 96 | uint32_encode((uint32_t) (pressure * 10), &dataPayload[0].serviceData[0]); |
| loicguibert | 12:856286ad4cdc | 97 | uint32_encode((int16_t) (temp * 100), &dataPayload[0].serviceData[4]); // Padding of 4 Bytes (= 32 bits as the pressure's type) |
| loicguibert | 12:856286ad4cdc | 98 | uint32_encode((uint16_t) (humidity * 100), &dataPayload[0].serviceData[6]); // Padding of 2 Bytes (= 16 bits as the temperature's type) |
| loicguibert | 12:856286ad4cdc | 99 | |
| loicguibert | 12:856286ad4cdc | 100 | // Setting the Advertising Payload length |
| loicguibert | 12:856286ad4cdc | 101 | dataPayload[0].serviceDataLength = 8; |
| loicguibert | 12:856286ad4cdc | 102 | |
| loicguibert | 12:856286ad4cdc | 103 | // Sending the Advertising Payload to the GAP Peripheral |
| loicguibert | 12:856286ad4cdc | 104 | // Has just one Payload |
| loicguibert | 12:856286ad4cdc | 105 | setAdvertisementServiceData(dataPayload, 1); |
| loicguibert | 14:c5578b5edabe | 106 | } |
| loicguibert | 14:c5578b5edabe | 107 | |
| loicguibert | 14:c5578b5edabe | 108 | void WeatherStation::onInitComplete(BLE::InitializationCompleteCallbackContext *event) { |
| loicguibert | 14:c5578b5edabe | 109 | GAPPeripheral::onInitComplete(event); |
| loicguibert | 14:c5578b5edabe | 110 | m_batteryService.addServiceToGattServer(); |
| loicguibert | 14:c5578b5edabe | 111 | m_batteryService.updateBatteryLevel(m_lps25hb.getPressure()); |
| loicguibert | 5:0d9e292a9d06 | 112 | } |