Plant Monitoring Project
Dependencies: mbed SHT21_ncleee WakeUp SSD1306 DHT Adafruit_TCS34725 DS1820
Diff: main.cpp
- Revision:
- 23:424b3149003b
- Parent:
- 22:51aed3b3b452
- Child:
- 24:6320b46719d6
diff -r 51aed3b3b452 -r 424b3149003b main.cpp --- a/main.cpp Mon Nov 25 07:39:53 2019 +0000 +++ b/main.cpp Thu Nov 28 19:51:58 2019 +0000 @@ -1,78 +1,23 @@ -#include "mbed.h" -#include "DS1820.h" -#include "SHT21_ncleee.h" -#include "Adafruit_TCS34725.h" -#include "ssd1306.h" - -#define DUREE_MESURE 10 // Durée en seconde entre deux mesures -#define DUREE_ECRAN_ON 5 // Durée en seconde d'éveil de l'écran - -#define I2C_SDA D4 -#define I2C_SCL D5 - -Ticker timeScreen; - - -//Serial pc(SERIAL_TX, SERIAL_RX); -Serial nucleo(D1,D0); - -I2C i2c(I2C_SDA, I2C_SCL); // Pour les deux capteurs i2c -SSD1306 oled(D12, A6); - -// capteur temperature sol -DS1820 DS(D2); - -// capteur humidité sol -AnalogIn capteur_humidity_sol(A0); - -// capteur humidité + température air -SHT21 sht(&i2c); - -// capteur RGB -Adafruit_TCS34725 RGBsens = Adafruit_TCS34725(&i2c, TCS34725_INTEGRATIONTIME_154MS, TCS34725_GAIN_1X); - -//Interruption Bouton -InterruptIn bouton(D10); +#include "main.hh" -// Flag OLED state -bool oled_on = 0; - -// Définition de fonctions -float temp_sol(void); -int fct_humidity_sol(void); -void fct_RGB(void); -void sendDataSigfox(void); -void oledData(void); -void readData(void); -void interruption_bouton(void); -void turnOffScreen(void); - -float temperature_sol; -unsigned char humidity_sol; -float temperature_air; -unsigned char humidity_air; -unsigned char pr, pg, pb; -unsigned short lum; - - -int main() { - //Initialisation de l'interruption +int main(){ + #ifdef DEEPSLEEP + WakeUp::calibrate(); + #endif + #ifdef OLED bouton.rise(interruption_bouton); - - // Affichage logo pour initialisation - oled.wake(); - oled.init(); - oled.cls(0,1); - oled.locate(4,4); - oled.printf("2PA2S"); - oled.redraw(); - wait(1); - oled.cls(0,1); - oled.sleep(); + initOLED(); + #endif while(1) { readData(); - wait(DUREE_MESURE); + #ifdef DEEPSLEEP + WakeUp::set(DUREE_OFF); + WakeUp::attach(&mycallback); + #endif + #ifndef DEEPSLEEP + wait(DUREE_OFF); + #endif } } @@ -81,12 +26,16 @@ { DS.convertTemperature(true, DS1820::all_devices); if (DS.unassignedProbe(D2)){ - printf( "D3 not assigned\n\r"); + #ifdef DEBUG + pc.printf( "D3 not assigned\n\r"); + #endif } printf("temp sol: %.1f\n\r", DS.temperature()); return DS.temperature(); } + + int fct_humidity_sol(void) { float val_min = 0.377; @@ -94,17 +43,22 @@ float mesure, mesure_etalonnee; mesure = capteur_humidity_sol.read(); mesure_etalonnee = (1-((mesure - val_min)/(val_max - val_min)))*100; - printf("hum sol: %d\n\r", (int) mesure_etalonnee); + #ifdef DEBUG + pc.printf("hum sol: %d\n\r", (int) mesure_etalonnee); + #endif return (int) mesure_etalonnee; } + void fct_RGB(void) { int somme; uint16_t clear, red, green, blue; if (!RGBsens.begin()) { - //pc.printf("No TCS34725 found ... check your connections"); + #ifdef DEBUG + pc.printf("No TCS34725 found ... check your connections"); + #endif //while (1); // halt! } RGBsens.getRawData(&red, &green, &blue, &clear); @@ -113,24 +67,29 @@ pg = green*100/somme; pb = blue*100/somme; lum = clear; - printf("luminosité : %d \n\r", lum); - printf("rouge:%d vert:%d bleu:%d \n\r", pr, pg, pb); + #ifdef DEBUG + pc.printf("luminosite : %d \n\r", lum); + pc.printf("rouge:%d vert:%d bleu:%d \n\r", pr, pg, pb); + #endif } void sendDataSigfox(void){ + #ifdef DEBUG + pc.printf("Sending Data to Sigfox \n\r"); + #endif short tempSol_short, tempAir_short; tempSol_short = (short)(temperature_sol*10); tempAir_short = (short)(temperature_air*10); - nucleo.printf("AT$SF=%04x%02x%04x%02x%04x%02x%02x%02x\r\n",tempSol_short, humidity_sol, tempAir_short, humidity_air, lum, pr, pg, pb); + wisol.printf("AT$SF=%04x%02x%04x%02x%04x%02x%02x%02x\r\n",tempSol_short, humidity_sol, tempAir_short, humidity_air, lum, pr, pg, pb); } void oledData(void){ + #ifdef DEBUG + pc.printf("Displaying Data\r\n"); + #endif if(!oled_on){ oled.wake(); - //oled.speed (SSD1306::Medium); - //oled.init(); - //oled.set_contrast(200); oled_on = 1; } oled.cls(0,1); @@ -150,32 +109,66 @@ } void readData(void){ + #ifdef DEBUG + pc.printf("Reading Data\n\r"); + #endif + #ifdef FLOOR_TEMPERATURE temperature_sol = temp_sol(); + #endif + #ifdef FLOOR_HUMIDITY humidity_sol = fct_humidity_sol(); - temperature_air = sht.readTemp(); - printf("temp air: %.1f\n\r", temperature_air); + #endif + #ifdef AIR_PARAMETERS + temperature_air = sht.readTemp(); humidity_air = sht.readHumidity(); - printf("hum air: %d\n\r", humidity_air); + #endif + #ifdef DEBUG + printf("hum air: %d\n\r", humidity_air); + printf("temp air: %.1f\n\r", temperature_air); + #endif + #ifdef RGB fct_RGB(); - printf("\n\r"); + #endif + #ifdef SIGFOX sendDataSigfox(); + #endif + #ifdef OLED if(oled_on) oledData(); + #endif } void interruption_bouton(){ bouton.disable_irq(); - printf("interruption\r\n"); + #ifdef DEBUG + pc.printf("Button interrupt\r\n"); + #endif if(!oled_on){ oledData(); timeScreen.attach(&turnOffScreen,DUREE_ECRAN_ON); } - bouton.enable_irq(); } void turnOffScreen(void){ + #ifdef DEBUG + pc.printf("Turning off screen \n\r"); + #endif timeScreen.detach(); oled_on = 0; oled.sleep(); -} \ No newline at end of file +} + +void initOLED(void){ + oled.wake(); + oled.init(); + oled.cls(0,1); + oled.locate(4,4); + oled.printf("2PA2S"); + oled.redraw(); + wait(1); + oled.cls(0,1); + oled.sleep(); +} + +void mycallback(void){} \ No newline at end of file