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@12:856286ad4cdc, 2019-03-25 (annotated)
- Committer:
- loicguibert
- Date:
- Mon Mar 25 14:54:43 2019 +0000
- Revision:
- 12:856286ad4cdc
- Parent:
- 11:dbc310addbf6
- Child:
- 14:c5578b5edabe
Advertising done
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 | 12:856286ad4cdc | 9 | GAPPeripheral(ble, iDevKit, m_stationName, GapAdvertisingParams::ADV_NON_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 | 12:856286ad4cdc | 14 | m_history() { | 
| loicguibert | 12:856286ad4cdc | 15 | // Set the right timing at the Station's start | 
| loicguibert | 12:856286ad4cdc | 16 | time(NULL); | 
| loicguibert | 4:bfe306335065 | 17 | } | 
| loicguibert | 4:bfe306335065 | 18 | |
| loicguibert | 4:bfe306335065 | 19 | void WeatherStation::start() { | 
| loicguibert | 4:bfe306335065 | 20 | m_logger.log("WeatherStation is starting\r\n"); | 
| loicguibert | 5:0d9e292a9d06 | 21 | |
| loicguibert | 4:bfe306335065 | 22 | // make sure that the LPW25HB device is present | 
| loicguibert | 4:bfe306335065 | 23 | // if not log an error and return | 
| loicguibert | 5:0d9e292a9d06 | 24 | if (m_lps25hb.probe()) { | 
| loicguibert | 5:0d9e292a9d06 | 25 | m_logger.log("LPS25HB device found\r\n"); | 
| loicguibert | 5:0d9e292a9d06 | 26 | } else { | 
| loicguibert | 5:0d9e292a9d06 | 27 | m_logger.log("Device LPS25HB not found!\r\n"); | 
| loicguibert | 5:0d9e292a9d06 | 28 | return; | 
| loicguibert | 5:0d9e292a9d06 | 29 | } | 
| loicguibert | 5:0d9e292a9d06 | 30 | |
| loicguibert | 4:bfe306335065 | 31 | // make sure that the HDC1000 device is present | 
| loicguibert | 4:bfe306335065 | 32 | // if not log an error and return | 
| loicguibert | 5:0d9e292a9d06 | 33 | if (m_hdc1000.probe()) { | 
| loicguibert | 5:0d9e292a9d06 | 34 | m_logger.log("HDC1000 device found\r\n"); | 
| loicguibert | 5:0d9e292a9d06 | 35 | } else { | 
| loicguibert | 5:0d9e292a9d06 | 36 | m_logger.log("Device HDC1000 not found!\r\n"); | 
| loicguibert | 5:0d9e292a9d06 | 37 | return; | 
| loicguibert | 5:0d9e292a9d06 | 38 | } | 
| loicguibert | 6:5f56e551f84f | 39 | |
| loicguibert | 6:5f56e551f84f | 40 | //Cleaning the console by the addition of a blank line | 
| loicguibert | 6:5f56e551f84f | 41 | m_logger.log("\r\n"); | 
| loicguibert | 4:bfe306335065 | 42 | |
| loicguibert | 4:bfe306335065 | 43 | // schedule measurements every m_measurementInterval milliseconds | 
| loicguibert | 4:bfe306335065 | 44 | // apply the same syntax as in the previous call | 
| loicguibert | 4:bfe306335065 | 45 | // 1. the measurement interval is m_measurementInterval | 
| loicguibert | 4:bfe306335065 | 46 | // 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 | 47 | // 3. the method to call is performMeasurements() | 
| loicguibert | 4:bfe306335065 | 48 | // m_eventQueue.call_every(TO COMPLETED); | 
| loicguibert | 4:bfe306335065 | 49 | m_eventQueue.call_every(m_measurementInterval, this, &WeatherStation::performMeasurements); | 
| loicguibert | 11:dbc310addbf6 | 50 | |
| loicguibert | 12:856286ad4cdc | 51 | // Starting the BLE peripheral with Advertising State | 
| loicguibert | 11:dbc310addbf6 | 52 | GAPPeripheral::advertise(); | 
| loicguibert | 11:dbc310addbf6 | 53 | } | 
| loicguibert | 4:bfe306335065 | 54 | |
| loicguibert | 4:bfe306335065 | 55 | void WeatherStation::performMeasurements(void) { | 
| loicguibert | 6:5f56e551f84f | 56 | m_logger.log("Performing measurements:\r\n"); | 
| loicguibert | 4:bfe306335065 | 57 | |
| loicguibert | 4:bfe306335065 | 58 | // get and log pressure | 
| loicguibert | 12:856286ad4cdc | 59 | double pressure = m_lps25hb.getPressure(); | 
| loicguibert | 12:856286ad4cdc | 60 | m_logger.log("Pressure: %.02f hPa\r\n", pressure); | 
| loicguibert | 4:bfe306335065 | 61 | |
| loicguibert | 4:bfe306335065 | 62 | // get and log temperature | 
| loicguibert | 12:856286ad4cdc | 63 | double temp = m_hdc1000.getTemperature(); | 
| loicguibert | 12:856286ad4cdc | 64 | m_logger.log("Temperature: %.02f C\r\n", temp); | 
| loicguibert | 4:bfe306335065 | 65 | |
| loicguibert | 4:bfe306335065 | 66 | // get and log humidity | 
| loicguibert | 12:856286ad4cdc | 67 | double humidity = m_hdc1000.getHumidity(); | 
| loicguibert | 12:856286ad4cdc | 68 | m_logger.log("Humidiy: %.02f %%\r\n", humidity); | 
| loicguibert | 4:bfe306335065 | 69 | |
| loicguibert | 4:bfe306335065 | 70 | // get and log the battery level | 
| loicguibert | 12:856286ad4cdc | 71 | uint8_t battery = m_iDevKit.getBatteryLevel(); | 
| loicguibert | 12:856286ad4cdc | 72 | m_logger.log("Battery: %u mV\r\n", battery); | 
| loicguibert | 6:5f56e551f84f | 73 | |
| loicguibert | 12:856286ad4cdc | 74 | // Cleaning the console by the addition of a blank line | 
| loicguibert | 6:5f56e551f84f | 75 | m_logger.log("\r\n"); | 
| loicguibert | 4:bfe306335065 | 76 | |
| loicguibert | 12:856286ad4cdc | 77 | // get and encode the system's elapsed time | 
| loicguibert | 12:856286ad4cdc | 78 | time_t seconds = time(NULL); | 
| loicguibert | 12:856286ad4cdc | 79 | float time = (uint32_t)seconds; | 
| loicguibert | 12:856286ad4cdc | 80 | |
| loicguibert | 12:856286ad4cdc | 81 | // Saving the measured values | 
| loicguibert | 12:856286ad4cdc | 82 | m_history.addMeasurement(pressure, temp, humidity, time); | 
| loicguibert | 12:856286ad4cdc | 83 | |
| loicguibert | 12:856286ad4cdc | 84 | // Send the measured values for encoding | 
| loicguibert | 12:856286ad4cdc | 85 | setupAdvertisementPayload(pressure, temp, humidity); | 
| loicguibert | 12:856286ad4cdc | 86 | } | 
| loicguibert | 12:856286ad4cdc | 87 | |
| loicguibert | 12:856286ad4cdc | 88 | void WeatherStation::setupAdvertisementPayload(double pressure, double temp, double humidity) { | 
| loicguibert | 12:856286ad4cdc | 89 | |
| loicguibert | 12:856286ad4cdc | 90 | // Setting the right UUID (environmental values) to the Advertising Payload | 
| loicguibert | 12:856286ad4cdc | 91 | dataPayload[0].serviceUUID = GattService::UUID_ENVIRONMENTAL_SERVICE; | 
| loicguibert | 12:856286ad4cdc | 92 | |
| loicguibert | 12:856286ad4cdc | 93 | // Encode and store the measured environmental values to the Advertising Payload | 
| loicguibert | 12:856286ad4cdc | 94 | uint32_encode((uint32_t) (pressure * 10), &dataPayload[0].serviceData[0]); | 
| loicguibert | 12:856286ad4cdc | 95 | uint32_encode((int16_t) (temp * 100), &dataPayload[0].serviceData[4]); // Padding of 4 Bytes (= 32 bits as the pressure's type) | 
| loicguibert | 12:856286ad4cdc | 96 | uint32_encode((uint16_t) (humidity * 100), &dataPayload[0].serviceData[6]); // Padding of 2 Bytes (= 16 bits as the temperature's type) | 
| loicguibert | 12:856286ad4cdc | 97 | |
| loicguibert | 12:856286ad4cdc | 98 | // Setting the Advertising Payload length | 
| loicguibert | 12:856286ad4cdc | 99 | dataPayload[0].serviceDataLength = 8; | 
| loicguibert | 12:856286ad4cdc | 100 | |
| loicguibert | 12:856286ad4cdc | 101 | // Sending the Advertising Payload to the GAP Peripheral | 
| loicguibert | 12:856286ad4cdc | 102 | // Has just one Payload | 
| loicguibert | 12:856286ad4cdc | 103 | setAdvertisementServiceData(dataPayload, 1); | 
| loicguibert | 5:0d9e292a9d06 | 104 | } |