St-connect
Dependencies: HP206C mbed WakeUp QMC5883L DHT22 DS1820
- ST Conect Project Look how we did it : [Hackster](https://www.hackster.io/monginjulien/st-connect-dike-monitoring-534a32)
Diff: main.cpp
- Revision:
- 11:e48bb5d44808
- Parent:
- 10:c645f569b7ce
- Child:
- 12:41fa5a145a22
diff -r c645f569b7ce -r e48bb5d44808 main.cpp --- a/main.cpp Tue Dec 04 15:02:02 2018 +0000 +++ b/main.cpp Tue Jan 08 15:03:27 2019 +0000 @@ -5,20 +5,30 @@ #include "DS1820.h" #include "QMC5883L.h" -// #define ENABLE_BAROMETER 1 -// #define ENABLE_MAGNETOMETER 1 -// #define ENABLE_GROUND_TEMPERATURE 1 -// #define ENABLE_GROUND_HUMIDITY 1 -// #define ENABLE_DHT22 1 +// Time during the deepsleep in ms +#define DEEPSLEEP_TIME (12*60000) +// Enabling components +#define ENABLE_BAROMETER 1 +#define ENABLE_MAGNETOMETER 1 +#define ENABLE_GROUND_TEMPERATURE 1 +#define ENABLE_GROUND_HUMIDITY 1 +#define ENABLE_DHT22 1 + +// Sigfox constants #define MESSAGE_SIZE 32 #define TRAME_SIZE 25 -#define DEBUG +// Enable the debug +// #define DEBUG + +// Enable the sending over Sigfox #define SEND_SIGFOX 1 +// Object initializations #ifdef DEBUG Serial pc(USBTX, USBRX); + DigitalOut myled(LED1); #endif #ifdef ENABLE_BAROMETER @@ -42,12 +52,18 @@ DHT22 capt_thermo_air_humidity(D3); // DHT22 #endif +// Pin to disable deepsleep +DigitalIn disable_deep(D6); + Serial sigfox(D1, D0); + +// Pin to control the transistor DigitalInOut reset_sigfox(D9); -DigitalOut myled(LED1); + char message[MESSAGE_SIZE] = "AT$SF="; +// Default values at the init long pressure = 10000; float air_temperature = 20.0; float air_humidity = 50.0; @@ -59,6 +75,9 @@ { } +/* + * Round the float f + */ int16_t round(float f) { int16_t res = (int16_t)f; @@ -68,14 +87,19 @@ return res; } +/* + * Format the trame to send over Sigfox + * Put the trame in *v_trame + */ 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) { + // Converting our data to use as few bits as possible 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 + bool err[3] = {false, false, false}; // magnetic field error x,y,z // Checking if the values are in the range if(!(i_ground_temperature >= -512 && i_ground_temperature < 511)) @@ -100,6 +124,7 @@ err[i] = true; } + // Putting them in the trame 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))); @@ -112,20 +137,6 @@ 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() @@ -136,11 +147,18 @@ sigfox.printf("\r\n"); + #ifdef ENABLE_BAROMETER + if(capt_barometer.isAvailable()) + pressure = capt_barometer.ReadPressure(); + #endif + #ifdef ENABLE_MAGNETOMETER capt_magnetometer.init(); - wait(5); #endif + wait(5); + + // Main loop while(1) { #ifdef DEBUG myled = 1; @@ -162,19 +180,19 @@ reset_sigfox.input(); #ifdef ENABLE_GROUND_TEMPERATURE - // Temperature Sol + // Ground Temperature capt_ground_temperature.convertTemperature(true, DS1820::all_devices); ground_temperature = capt_ground_temperature.temperature(); #endif #ifdef ENABLE_GROUND_HUMIDITY - // Humidite Sol + // Ground Humidity ground_humidity = capt_ground_humidity.read() * 100; transistor_humidity = 0; #endif #ifdef ENABLE_DHT22 - // Temperature et Humidite Air + // Air temperature and humidity if(capt_thermo_air_humidity.sample()) { air_temperature = capt_thermo_air_humidity.getTemperature() / 10.0; @@ -183,15 +201,13 @@ #endif #ifdef ENABLE_BAROMETER - // Pression + // Pressure if(capt_barometer.isAvailable()) - { pressure = capt_barometer.ReadPressure(); - } #endif #ifdef ENABLE_MAGNETOMETER - // Magnetometre + // Magnetometer capt_magnetometer.init(); magnetic_field[0] = capt_magnetometer.getMagXvalue(); magnetic_field[1] = capt_magnetometer.getMagYvalue(); @@ -199,8 +215,8 @@ capt_magnetometer.standby(); #endif - // Affichage pour debug #ifdef DEBUG + // Display to check your values pc.printf("\r\n"); pc.printf("Pressure: %f hPa\r\n", pressure/100.0); pc.printf("Ground Temperature: %f\t|\t", ground_temperature); @@ -209,7 +225,7 @@ pc.printf("Magnetic field: x: %hd, y: %hd, z: %hd\r\n", magnetic_field[0], magnetic_field[1], magnetic_field[2]); #endif - // Envoie sigfox + // Creating your sigfox trame format(ground_temperature, air_temperature, ground_humidity, air_humidity, pressure, magnetic_field, &(message[6])); message[MESSAGE_SIZE-2] = '\r'; message[MESSAGE_SIZE-1] = '\n'; @@ -217,17 +233,17 @@ pc.printf("msg=%s", message); #endif + // Sending over sigfox #ifdef SEND_SIGFOX - #ifdef DEBUG - pc.printf("ENVOIE\r\n", message); - #endif sigfox.printf("%s", message); - wait(8); + wait(8); // Time during the sigfox module is sending, do not stop the sigfox before ! #endif // Sleep Sigfox sigfox.printf("AT$P=1\r\n"); + #ifdef DEBUG + // Detect that the system is going in sleep mode pc.printf("Sleep"); myled = 1; wait(0.3); @@ -242,9 +258,12 @@ myled = 0; #endif - // DEEPSLEEP - WakeUp::set_ms(30000); - WakeUp::attach(&mycallback); - deepsleep(); + // Deepsleep + if(disable_deep == 0) + { + WakeUp::set_ms(DEEPSLEEP_TIME); + WakeUp::attach(&mycallback); + deepsleep(); + } } } \ No newline at end of file