yes Spada / Mbed OS programme
Revision:
12:856286ad4cdc
Parent:
11:dbc310addbf6
Child:
14:c5578b5edabe
--- a/WeatherStation.cpp	Tue Mar 19 20:48:48 2019 +0000
+++ b/WeatherStation.cpp	Mon Mar 25 14:54:43 2019 +0000
@@ -1,23 +1,24 @@
 #include "WeatherStation.h"
 
 // initialization of static data members
-    //const int WeatherStation::m_blinkInterval = 500;
-    const int WeatherStation::m_measurementInterval = 10000;
+    const int WeatherStation::m_measurementInterval = 9000;
+    const string WeatherStation::m_stationName = "GuiSpa";
 
 // constructor
 WeatherStation::WeatherStation(IDevKit& iDevKit, Logger& logger, BLE& ble) :
-    GAPPeripheral(ble, iDevKit, "Spada-Guibert", GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED, m_eventQueue, logger),
-    m_iDevKit(iDevKit),
-    m_logger(logger),
-    //m_peripheral(ble, iDevKit, "Spada-Guibert", GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED,m_eventQueue, logger),
-    m_lps25hb(m_iDevKit.getSDA(), m_iDevKit.getSCL(), m_logger),
-    m_hdc1000(m_iDevKit.getSDA(), m_iDevKit.getSCL(), m_iDevKit.getDataRdy(), m_logger) {
+  GAPPeripheral(ble, iDevKit, m_stationName, GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED, m_eventQueue, logger),
+  m_iDevKit(iDevKit),
+  m_logger(logger),
+  m_lps25hb(m_iDevKit.getSDA(), m_iDevKit.getSCL(), m_logger),
+  m_hdc1000(m_iDevKit.getSDA(), m_iDevKit.getSCL(), m_iDevKit.getDataRdy(), m_logger),
+  m_history() {
+    //  Set the right timing at the Station's start
+    time(NULL);
 }
 
 void WeatherStation::start() {
   m_logger.log("WeatherStation is starting\r\n");
     
-    
   // make sure that the LPW25HB device is present
   // if not log an error and return
   if (m_lps25hb.probe()) {
@@ -27,7 +28,6 @@
       return;
   }
     
-    
   // make sure that the HDC1000 device is present
   // if not log an error and return
   if (m_hdc1000.probe()) {
@@ -39,14 +39,6 @@
   
   //Cleaning the console by the addition of a blank line
   m_logger.log("\r\n");
-    
-  
-  // to show we're running we'll blink every m_blinkInterval milliseconds
-  // the syntax used here is the following:
-  //   1. the first argument represents the interval in milliseconds at which the event is executed
-  //   2. the second argument represents  the address of instance of the object on which the method passed in argument 3 will be applied
-  //  3. the third argument is the address of the method to be called (applied to the object passed as argument 2)
-  //m_eventQueue.call_every(m_blinkInterval, &m_iDevKit, &IDevKit::toggleLed1);
 
   // schedule measurements every m_measurementInterval milliseconds
   // apply the same syntax as in the previous call
@@ -54,20 +46,9 @@
   // 2. the object is the WeatherStation itself (recall how you refer to the current object in Java - the same applies in c++)
   // 3. the method to call is performMeasurements()
   // m_eventQueue.call_every(TO COMPLETED);
-  
   m_eventQueue.call_every(m_measurementInterval, this, &WeatherStation::performMeasurements);
   
-  // Starting the BLE peripheral
-  // the syntax used here is the following:
-  //  1. the first argument represents  the address of instance of the object on which the method passed in argument 3 will be applied
-  //  2. the second argument is the address of the method to be called (applied to the object passed as argument 2)
-    m_eventQueue.call(this, &WeatherStation::advertise);
-    
-  // this will not return until shutdown
-  m_eventQueue.dispatch_forever(); 
-}
-
-void WeatherStation::advertise(void){
+  // Starting the BLE peripheral with Advertising State
     GAPPeripheral::advertise();
 }
  
@@ -75,18 +56,49 @@
   m_logger.log("Performing measurements:\r\n");
   
   // get and log pressure
-  m_logger.log("Pressure:      %.02f hPa\r\n", m_lps25hb.getPressure());
+  double pressure = m_lps25hb.getPressure();
+  m_logger.log("Pressure:      %.02f hPa\r\n", pressure);
   
   // get and log temperature
-  m_logger.log("Temperature:    %.02f C\r\n", m_hdc1000.getTemperature());
+  double temp = m_hdc1000.getTemperature();
+  m_logger.log("Temperature:    %.02f C\r\n", temp);
   
   // get and log humidity
-  m_logger.log("Humidiy:        %.02f %%\r\n", m_hdc1000.getHumidity());
+  double humidity = m_hdc1000.getHumidity();
+  m_logger.log("Humidiy:        %.02f %%\r\n", humidity);
   
   // get and log the battery level
-  m_logger.log("Battery:        %u mV\r\n", m_iDevKit.getBatteryLevel());
+  uint8_t battery = m_iDevKit.getBatteryLevel();
+  m_logger.log("Battery:        %u mV\r\n", battery);
   
-  //Cleaning the console by the addition of a blank line
+  //  Cleaning the console by the addition of a blank line
   m_logger.log("\r\n");
   
+  //  get and encode the system's elapsed time
+  time_t seconds = time(NULL);
+  float time = (uint32_t)seconds;
+  
+  //  Saving the measured values
+  m_history.addMeasurement(pressure, temp, humidity, time);
+  
+  //  Send the measured values for encoding
+  setupAdvertisementPayload(pressure, temp, humidity);
+}
+
+void WeatherStation::setupAdvertisementPayload(double pressure, double temp, double humidity) {
+    
+  //  Setting the right UUID (environmental values) to the Advertising Payload
+  dataPayload[0].serviceUUID = GattService::UUID_ENVIRONMENTAL_SERVICE;
+
+  //  Encode and store the measured environmental values to the Advertising Payload
+  uint32_encode((uint32_t) (pressure * 10), &dataPayload[0].serviceData[0]);
+  uint32_encode((int16_t) (temp * 100), &dataPayload[0].serviceData[4]);        // Padding of 4 Bytes (= 32 bits as the pressure's type)
+  uint32_encode((uint16_t) (humidity * 100), &dataPayload[0].serviceData[6]);   // Padding of 2 Bytes (= 16 bits as the temperature's type)
+    
+  //  Setting the Advertising Payload length   
+  dataPayload[0].serviceDataLength = 8; 
+    
+  //  Sending the Advertising Payload to the GAP Peripheral
+  //  Has just one Payload
+  setAdvertisementServiceData(dataPayload, 1);
 }
\ No newline at end of file