St-connect

Dependencies:   HP206C mbed WakeUp QMC5883L DHT22 DS1820

  1. ST Conect Project Look how we did it : [Hackster](https://www.hackster.io/monginjulien/st-connect-dike-monitoring-534a32)
Revision:
6:865aa63f2106
Parent:
5:d805b1c2dc1e
Child:
7:036d9a2accff
--- a/main.cpp	Mon Oct 15 07:36:13 2018 +0000
+++ b/main.cpp	Mon Oct 22 09:22:05 2018 +0000
@@ -4,15 +4,9 @@
 #include "HP20x_dev.h"
 #include "DS1820.h"
 #include "QMC5883L.h"
+#define MESSAGE_SIZE 32
+#define TRAME_SIZE 25
 
-#define X_MSB 0x03
-#define X_LSB 0x04
-#define Z_MSB 0x05
-#define Z_LSB 0x06
-#define Y_MSB 0x07
-#define Y_LSB 0x08
-#define ADDR_MAGN_W 0x3C
-#define ADDR_MAGN_R 0x3D
 
 #define DEBUG 1
 // #define SEND_SIGFOX 1
@@ -28,7 +22,7 @@
 DHT22 capt_thermo_air_humidity(D3);     // DHT22
 Serial sigfox(D1, D0);
 
-char message[20] = {};
+char message[MESSAGE_SIZE] = "AT$SF=";
 
 long pressure = 10000;
 float air_temperature = 20.0;
@@ -41,6 +35,75 @@
 {
 }
 
+int16_t round(float f)
+{
+    int16_t res = (int16_t)f;
+    if (f - res > 1/2) 
+        res++;
+        
+    return res;
+}
+
+void format(float v_ground_temperature, float v_air_temperature, float v_ground_humidity, float v_air_humidity, long v_pressure, int16_t* v_magnetic_field, char* v_trame)
+{
+    int16_t i_ground_temperature = (int16_t) round(v_ground_temperature * 10);
+    int16_t i_air_temperature = (int16_t) round(v_air_temperature * 10);
+    int16_t i_ground_humidity = (int16_t) round(v_ground_humidity);
+    int16_t i_air_humidity = (int16_t) round(v_air_humidity);
+    int32_t i_pressure = (int32_t) v_pressure;
+    bool err[3] = {false, false, false};    // x,y,z
+    
+    // Checking if the values are in the range
+    if(!(i_ground_temperature >= -512 && i_ground_temperature < 511))
+        i_ground_temperature = -512;
+        
+    if(!(i_air_temperature >= -512 && i_air_temperature < 511))
+        i_air_temperature = -512;
+    
+    if(!(i_ground_humidity <= 100))
+        i_ground_humidity = 127;
+    
+    if(!(i_air_humidity <= 100))
+        i_air_humidity = 127;
+    
+    if(!(i_pressure <= 131071))
+        i_pressure = 0;
+    
+    unsigned int i;
+    for(i = 0; i < 3; i++)
+    {
+        if(!(v_magnetic_field[i] > -65536 && v_magnetic_field[i] < 65535))
+            err[i] = true;
+    }
+    
+    snprintf(v_trame, TRAME_SIZE, "%02x", (char)(i_ground_temperature >> 2));
+    snprintf(v_trame, TRAME_SIZE, "%s%02x", v_trame, (char)((i_ground_temperature << 6) | ((i_air_temperature >> 4) & 0x3f)));
+    snprintf(v_trame, TRAME_SIZE, "%s%02x", v_trame, (char)((i_air_temperature << 4) | ((i_ground_humidity >> 3) & 0x0f)));
+    snprintf(v_trame, TRAME_SIZE, "%s%02x", v_trame, (char)((i_ground_humidity << 5) | ((i_air_humidity >> 2) & 0x1f)));
+    snprintf(v_trame, TRAME_SIZE, "%s%02x", v_trame, (char)((i_air_humidity << 6) | ((i_pressure >> 11) & 0x3f)));
+    snprintf(v_trame, TRAME_SIZE, "%s%02x", v_trame, (char)(i_pressure >> 3));
+    snprintf(v_trame, TRAME_SIZE, "%s%02x", v_trame, (char)((i_pressure << 5) | ((v_magnetic_field[0] >> 9) & 0x1f)));
+    snprintf(v_trame, TRAME_SIZE, "%s%02x", v_trame, (char)(v_magnetic_field[0] >> 1));
+    snprintf(v_trame, TRAME_SIZE, "%s%02x", v_trame, (char)((v_magnetic_field[0] << 7) | ((v_magnetic_field[1] >> 7) & 0x7f)));
+    snprintf(v_trame, TRAME_SIZE, "%s%02x", v_trame, (char)((v_magnetic_field[1] << 1) | ((v_magnetic_field[2] >> 13) & 0x01)));
+    snprintf(v_trame, TRAME_SIZE, "%s%02x", v_trame, (char)(v_magnetic_field[2] >> 5));
+    snprintf(v_trame, TRAME_SIZE, "%s%02x", v_trame, (char)((v_magnetic_field[2] << 3) | (err[0] << 2) | (err[1] << 1) | (err[2])));
+    /*
+    v_trame[0] = i_ground_temperature >> 2;
+    v_trame[1] = (i_ground_temperature << 6) | ((i_air_temperature >> 4) & 0x3f);
+    v_trame[2] = (i_air_temperature << 4) | ((i_ground_humidity >> 3) & 0x0f);
+    v_trame[3] = (i_ground_humidity << 5) | ((i_air_humidity >> 2) & 0x1f);
+    v_trame[4] = (i_air_humidity << 6) | ((i_pressure >> 11) & 0x3f);
+    v_trame[5] = (i_pressure >> 3);
+    v_trame[6] = (i_pressure << 5) | ((v_magnetic_field[0] >> 9) & 0x1f);
+    v_trame[7] = (v_magnetic_field[0] >> 1);
+    v_trame[8] = (v_magnetic_field[0] << 7) | ((v_magnetic_field[1] >> 7) & 0x7f);
+    v_trame[9] = (v_magnetic_field[1] << 1) | ((v_magnetic_field[2] >> 13) & 0x01);
+    v_trame[10] = (v_magnetic_field[2] >> 5);
+    v_trame[11] = (v_magnetic_field[2] << 3) | (err[0] << 2) | (err[1] << 1) | (err[2]);
+    */
+}
+
 int main()
 {
     sigfox.printf("\r\n");
@@ -55,7 +118,7 @@
         ground_temperature = capt_ground_temperature.temperature();
         
         // Humidite Sol
-        ground_humidity = capt_ground_humidity.read();
+        ground_humidity = capt_ground_humidity.read() * 100;
         
         // Temperature et Humidite Air
         if(capt_thermo_air_humidity.sample())
@@ -78,20 +141,23 @@
             pc.printf("\r\n");
             pc.printf("Pressure: %f hPa\r\n", pressure/100.0);
             pc.printf("Ground Temperature: %f\t|\t", ground_temperature);
-            pc.printf("Ground Humidity: %.1f\r\n", ground_humidity*100);
+            pc.printf("Ground Humidity: %.1f\r\n", ground_humidity);
             pc.printf("Air Temperature: %.1f\t|\tAir Humidity: %.1f\r\n", air_temperature, air_humidity);
             pc.printf("Magnetic field: x: %hd, y: %hd, z: %hd\r\n", magnetic_field[0], magnetic_field[1], magnetic_field[2]);
         #endif
         
         // Envoie sigfox
-        sprintf(message, "AT$SF=%04x%02x\r\n", (int)(air_temperature*10), (int)(air_humidity));
+        format(ground_temperature, air_temperature, ground_humidity, air_humidity, pressure, magnetic_field, &(message[6]));
+        message[MESSAGE_SIZE-2] = '\r';
+        message[MESSAGE_SIZE-1] = '\n';
         #ifdef DEBUG
-            pc.printf("%s", message);
+            pc.printf("msg=%s", message);
         #endif
         
         #ifdef SEND_SIGFOX
             sigfox.printf("%s", message);
         #endif
+        
         // DEEPSLEEP
         /*
         WakeUp::set_ms(10000);
@@ -100,4 +166,4 @@
         */
         wait(5);
     }
-}
+}
\ No newline at end of file