Théo Germain / Mbed 2 deprecated 2PA2S

Dependencies:   mbed SHT21_ncleee ssd1306_library Adafruit_TCS34725 DS1820

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "DS1820.h"
00003 #include "SHT21_ncleee.h"
00004 #include "Adafruit_TCS34725.h"
00005 #include "ssd1306.h"
00006 
00007 #define DUREE_MESURE 10         // Durée en seconde entre deux mesures
00008 #define DUREE_ECRAN_ON 30       // Durée en seconde d'éveil de l'écran
00009 
00010 Serial pc(SERIAL_TX, SERIAL_RX);
00011 Serial nucleo(D1,D0);
00012 
00013 I2C i2c(D4, D5);
00014 SSD1306 oled(D12, A6);
00015 
00016 Ticker timeScreen;
00017 Ticker capture;
00018 
00019 // capteur temperature sol
00020 DS1820 DS(D3); 
00021 
00022 // capteur humidité sol
00023 AnalogIn capteur_humidity_sol(A0); 
00024 
00025 // capteur humidité + température air
00026 SHT21 sht(&i2c);
00027 
00028 // capteur RGB
00029 Adafruit_TCS34725 RGBsens = Adafruit_TCS34725(&i2c, TCS34725_INTEGRATIONTIME_154MS, TCS34725_GAIN_1X);
00030 
00031 // capteur lumière
00032 AnalogIn ain(A1); 
00033 
00034 //Interruption Bouton
00035 InterruptIn bouton(D10);
00036 
00037 // Flag OLED on
00038 bool oled_on = 0;
00039 
00040 // Définition de fonctions
00041 float   temp_sol(void);
00042 int     fct_humidity_sol(void);
00043 void    fct_RGB(void);
00044 void    sendDataSigfox(void);
00045 void    oledData(void);
00046 void    readData(void);
00047 void    interruption_bouton(void);
00048 void    turnOffScreen(void);
00049 
00050 float temperature_sol;
00051 unsigned char humidity_sol;
00052 float temperature_air;
00053 unsigned char humidity_air;
00054 unsigned char pr, pg, pb;
00055 unsigned short lum;
00056     
00057     
00058 int main() {
00059     // Affichage logo pour initialisation
00060     oled.off();
00061     oled.on();
00062     oled.init();
00063     oled.cls(0,1);
00064     oled.locate(4,4);
00065     oled.printf("2PA2S");
00066     oled.redraw();
00067     wait(1);
00068     oled.cls();
00069     oled.off();
00070     // Initialisation des mesures 
00071     capture.attach(&readData,DUREE_MESURE);    
00072     
00073     //Initialisation de l'interruption
00074     bouton.fall(interruption_bouton);
00075     readData();
00076     
00077     while(1) {
00078     }
00079 }
00080 
00081 
00082 float temp_sol()
00083 {
00084     DS.convertTemperature(true, DS1820::all_devices);
00085     if (DS.unassignedProbe(D3)){
00086         pc.printf( "D3 not assigned\n\r");
00087     }
00088     return DS.temperature();
00089 }
00090 
00091 int fct_humidity_sol(void)
00092 {
00093     float val_min = 0.377;
00094     float val_max = 0.772;
00095     float mesure, mesure_etalonnee;
00096     mesure = capteur_humidity_sol.read();
00097     mesure_etalonnee = (1-((mesure - val_min)/(val_max - val_min)))*100;
00098     return (int) mesure_etalonnee;
00099 }
00100 
00101 void fct_RGB(void)
00102 {
00103     int somme;
00104     uint16_t clear, red, green, blue;
00105     if (!RGBsens.begin())
00106     {
00107         pc.printf("No TCS34725 found ... check your connections");
00108         //while (1); // halt!
00109     }
00110     RGBsens.getRawData(&red, &green, &blue, &clear);
00111     somme = red + green + blue;
00112     pr = red*100/somme;
00113     pg = green*100/somme;
00114     pb = blue*100/somme;
00115     lum = clear;
00116 }
00117 
00118 void sendDataSigfox(void){
00119     short tempSol_short, tempAir_short;
00120     tempSol_short = (short)(temperature_sol*10);
00121     tempAir_short = (short)(temperature_air*10);
00122 
00123     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);
00124 }
00125     
00126 void oledData(void){
00127     if(!oled_on){
00128         pc.printf("Turning on screen\n\r");
00129         oled.on();
00130         oled.speed (SSD1306::Medium);
00131         oled.init();
00132         oled.set_contrast(200);
00133         oled_on = 1;
00134     }
00135     pc.printf("Displaying data\n\r");
00136     oled.cls(0,1);
00137     oled.locate(0,0);
00138     oled.printf("AIR T : %.1f", temperature_air);
00139     oled.locate(1,0);
00140     oled.printf("AIR H : %d", humidity_air);
00141     oled.locate(3,0);
00142     oled.printf("FLOOR T : %.1f", temperature_sol);
00143     oled.locate(4,0);
00144     oled.printf("FLOOR H : %d", humidity_sol);
00145     oled.locate(6,0);
00146     oled.printf("Light : %d", lum);
00147     oled.locate(7,0);
00148     oled.printf("R %d G %d B %d", pr, pg, pb);
00149     oled.redraw();
00150 }
00151 
00152 void readData(void){
00153     pc.printf("reading data\n\r");
00154     temperature_sol = temp_sol();
00155     humidity_sol = fct_humidity_sol();
00156     temperature_air = sht.readTemp();
00157     humidity_air = sht.readHumidity();
00158     fct_RGB();
00159     sendDataSigfox();
00160     if(oled_on)
00161         oledData();
00162 }
00163 
00164 void interruption_bouton(){
00165     bouton.disable_irq();
00166     if(!oled_on){
00167         pc.printf("Interruption avec ecran eteint\n\r");
00168         oledData();
00169         timeScreen.attach(&turnOffScreen,DUREE_ECRAN_ON);
00170     }
00171     else{
00172         pc.printf("Interruption avec ecran allume\n\r");
00173         readData();
00174     }
00175     bouton.enable_irq();
00176 }
00177 
00178 void turnOffScreen(void){
00179     pc.printf("Extinction ecran\n\r");
00180     timeScreen.detach();
00181     oled_on = 0;
00182     oled.off();
00183 }