Plant Monitoring Project

Dependencies:   mbed SHT21_ncleee WakeUp SSD1306 DHT Adafruit_TCS34725 DS1820

Committer:
Germaint
Date:
Mon Nov 04 09:00:13 2019 +0000
Revision:
19:d6236253bf5f
Parent:
18:d30cd2967da4
Child:
20:79f4ef29eafd
version fonctionnelle

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jufray 0:e030be8f0310 1 #include "mbed.h"
Germaint 1:3fc11a745984 2 #include "DS1820.h"
Germaint 1:3fc11a745984 3 #include "SHT21_ncleee.h"
Germaint 1:3fc11a745984 4 #include "Adafruit_TCS34725.h"
Germaint 1:3fc11a745984 5 #include "ssd1306.h"
jufray 0:e030be8f0310 6
Germaint 19:d6236253bf5f 7 #define DUREE_MESURE 10 // Durée en seconde entre deux mesures
Germaint 19:d6236253bf5f 8 #define DUREE_ECRAN_ON 5 // Durée en seconde d'éveil de l'écran
Germaint 19:d6236253bf5f 9
Germaint 19:d6236253bf5f 10 #define I2C_SDA D4
Germaint 19:d6236253bf5f 11 #define I2C_SCL D5
Germaint 18:d30cd2967da4 12
Germaint 18:d30cd2967da4 13 Ticker timeScreen;
Germaint 18:d30cd2967da4 14
Germaint 19:d6236253bf5f 15
Germaint 2:0bfe25431e8e 16 //Serial pc(SERIAL_TX, SERIAL_RX);
Germaint 1:3fc11a745984 17 Serial nucleo(D1,D0);
Germaint 2:0bfe25431e8e 18
Germaint 19:d6236253bf5f 19 I2C i2c(I2C_SDA, I2C_SCL); // Pour les deux capteurs i2c
Germaint 19:d6236253bf5f 20 SSD1306 oled(I2C_SDA, I2C_SCL);
jufray 0:e030be8f0310 21
Germaint 1:3fc11a745984 22 // capteur temperature sol
Germaint 1:3fc11a745984 23 DS1820 DS(D3);
jufray 0:e030be8f0310 24
Germaint 1:3fc11a745984 25 // capteur humidité sol
Germaint 1:3fc11a745984 26 AnalogIn capteur_humidity_sol(A0);
jufray 0:e030be8f0310 27
Germaint 1:3fc11a745984 28 // capteur humidité + température air
jufray 0:e030be8f0310 29 SHT21 sht(&i2c);
jufray 0:e030be8f0310 30
Germaint 1:3fc11a745984 31 // capteur RGB
ludollaoo 5:256a143ab0c2 32 Adafruit_TCS34725 RGBsens = Adafruit_TCS34725(&i2c, TCS34725_INTEGRATIONTIME_154MS, TCS34725_GAIN_1X);
jufray 0:e030be8f0310 33
Germaint 18:d30cd2967da4 34 //Interruption Bouton
Germaint 18:d30cd2967da4 35 InterruptIn bouton(D10);
Germaint 18:d30cd2967da4 36
Germaint 18:d30cd2967da4 37 // Flag OLED state
Germaint 18:d30cd2967da4 38 bool oled_on = 0;
jufray 0:e030be8f0310 39
Germaint 1:3fc11a745984 40 // Définition de fonctions
Germaint 1:3fc11a745984 41 float temp_sol(void);
Germaint 1:3fc11a745984 42 int fct_humidity_sol(void);
Germaint 18:d30cd2967da4 43 void fct_RGB(void);
Germaint 16:13364798fce6 44 void sendDataSigfox(void);
Germaint 2:0bfe25431e8e 45 void oledData(void);
Germaint 16:13364798fce6 46 void readData(void);
Germaint 18:d30cd2967da4 47 void interruption_bouton(void);
Germaint 18:d30cd2967da4 48 void turnOffScreen(void);
jufray 0:e030be8f0310 49
Germaint 1:3fc11a745984 50
Germaint 16:13364798fce6 51 float temperature_sol;
Germaint 18:d30cd2967da4 52 unsigned char humidity_sol;
Germaint 16:13364798fce6 53 float temperature_air;
Germaint 16:13364798fce6 54 unsigned char humidity_air;
Germaint 16:13364798fce6 55 unsigned char pr, pg, pb;
Germaint 16:13364798fce6 56 unsigned short lum;
jufray 0:e030be8f0310 57
jufray 0:e030be8f0310 58
Germaint 1:3fc11a745984 59 int main() {
Germaint 18:d30cd2967da4 60 //Initialisation de l'interruption
Germaint 18:d30cd2967da4 61 bouton.fall(interruption_bouton);
Germaint 18:d30cd2967da4 62
Germaint 19:d6236253bf5f 63 // Affichage logo pour initialisation
Germaint 19:d6236253bf5f 64 oled.wake();
Germaint 16:13364798fce6 65 oled.init();
Germaint 16:13364798fce6 66 oled.cls(0,1);
Germaint 19:d6236253bf5f 67 oled.locate(4,4);
Germaint 19:d6236253bf5f 68 oled.printf("2PA2S");
Germaint 19:d6236253bf5f 69 oled.redraw();
Germaint 19:d6236253bf5f 70 wait(1);
Germaint 19:d6236253bf5f 71 oled.cls(0,1);
Germaint 18:d30cd2967da4 72 oled.sleep();
Germaint 1:3fc11a745984 73
jufray 0:e030be8f0310 74 while(1) {
Germaint 16:13364798fce6 75 readData();
Germaint 18:d30cd2967da4 76 wait(DUREE_MESURE);
jufray 0:e030be8f0310 77 }
jufray 0:e030be8f0310 78 }
jufray 0:e030be8f0310 79
jufray 0:e030be8f0310 80
jufray 0:e030be8f0310 81 float temp_sol()
jufray 0:e030be8f0310 82 {
jufray 0:e030be8f0310 83 DS.convertTemperature(true, DS1820::all_devices);
jufray 0:e030be8f0310 84 if (DS.unassignedProbe(D3)){
Germaint 2:0bfe25431e8e 85 //pc.printf( "D3 not assigned\n\r");
jufray 0:e030be8f0310 86 }
jufray 0:e030be8f0310 87 return DS.temperature();
jufray 0:e030be8f0310 88 }
jufray 0:e030be8f0310 89
Germaint 1:3fc11a745984 90 int fct_humidity_sol(void)
jufray 0:e030be8f0310 91 {
jufray 0:e030be8f0310 92 float val_min = 0.377;
jufray 0:e030be8f0310 93 float val_max = 0.772;
jufray 0:e030be8f0310 94 float mesure, mesure_etalonnee;
Germaint 1:3fc11a745984 95 mesure = capteur_humidity_sol.read();
jufray 0:e030be8f0310 96 mesure_etalonnee = (1-((mesure - val_min)/(val_max - val_min)))*100;
jufray 0:e030be8f0310 97 return (int) mesure_etalonnee;
jufray 0:e030be8f0310 98 }
jufray 0:e030be8f0310 99
Germaint 18:d30cd2967da4 100 void fct_RGB(void)
jufray 0:e030be8f0310 101 {
jufray 0:e030be8f0310 102 int somme;
jufray 0:e030be8f0310 103 uint16_t clear, red, green, blue;
jufray 0:e030be8f0310 104 if (!RGBsens.begin())
jufray 0:e030be8f0310 105 {
Germaint 2:0bfe25431e8e 106 //pc.printf("No TCS34725 found ... check your connections");
Germaint 15:c73d0d180cc4 107 //while (1); // halt!
jufray 0:e030be8f0310 108 }
jufray 0:e030be8f0310 109 RGBsens.getRawData(&red, &green, &blue, &clear);
jufray 0:e030be8f0310 110 somme = red + green + blue;
Germaint 18:d30cd2967da4 111 pr = red*100/somme;
Germaint 18:d30cd2967da4 112 pg = green*100/somme;
Germaint 18:d30cd2967da4 113 pb = blue*100/somme;
Germaint 18:d30cd2967da4 114 lum = clear;
jufray 0:e030be8f0310 115 }
jufray 0:e030be8f0310 116
Germaint 16:13364798fce6 117 void sendDataSigfox(void){
Germaint 16:13364798fce6 118 short tempSol_short, tempAir_short;
Germaint 16:13364798fce6 119 tempSol_short = (short)(temperature_sol*10);
Germaint 16:13364798fce6 120 tempAir_short = (short)(temperature_air*10);
jufray 0:e030be8f0310 121
Germaint 16:13364798fce6 122 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);
Germaint 16:13364798fce6 123 }
Germaint 2:0bfe25431e8e 124
Germaint 2:0bfe25431e8e 125 void oledData(void){
Germaint 18:d30cd2967da4 126 if(!oled_on){
Germaint 18:d30cd2967da4 127 oled.wake();
Germaint 18:d30cd2967da4 128 //oled.speed (SSD1306::Medium);
Germaint 18:d30cd2967da4 129 //oled.init();
Germaint 18:d30cd2967da4 130 //oled.set_contrast(200);
Germaint 18:d30cd2967da4 131 oled_on = 1;
Germaint 18:d30cd2967da4 132 }
Germaint 16:13364798fce6 133 oled.cls(0,1);
Germaint 2:0bfe25431e8e 134 oled.locate(0,0);
Germaint 2:0bfe25431e8e 135 oled.printf("AIR T : %.1f", temperature_air);
Germaint 2:0bfe25431e8e 136 oled.locate(1,0);
Germaint 2:0bfe25431e8e 137 oled.printf("AIR H : %d", humidity_air);
Germaint 2:0bfe25431e8e 138 oled.locate(3,0);
Germaint 2:0bfe25431e8e 139 oled.printf("FLOOR T : %.1f", temperature_sol);
Germaint 2:0bfe25431e8e 140 oled.locate(4,0);
Germaint 2:0bfe25431e8e 141 oled.printf("FLOOR H : %d", humidity_sol);
Germaint 2:0bfe25431e8e 142 oled.locate(6,0);
Germaint 2:0bfe25431e8e 143 oled.printf("Light : %d", lum);
Germaint 2:0bfe25431e8e 144 oled.locate(7,0);
Germaint 2:0bfe25431e8e 145 oled.printf("R %d G %d B %d", pr, pg, pb);
Germaint 2:0bfe25431e8e 146 oled.redraw();
Germaint 2:0bfe25431e8e 147 }
jufray 0:e030be8f0310 148
Germaint 16:13364798fce6 149 void readData(void){
Germaint 16:13364798fce6 150 temperature_sol = temp_sol();
Germaint 16:13364798fce6 151 humidity_sol = fct_humidity_sol();
Germaint 16:13364798fce6 152 temperature_air = sht.readTemp();
Germaint 16:13364798fce6 153 humidity_air = sht.readHumidity();
Germaint 18:d30cd2967da4 154 fct_RGB();
Germaint 16:13364798fce6 155 sendDataSigfox();
Germaint 18:d30cd2967da4 156 if(oled_on)
Germaint 18:d30cd2967da4 157 oledData();
Germaint 16:13364798fce6 158 }
jufray 0:e030be8f0310 159
Germaint 18:d30cd2967da4 160 void interruption_bouton(){
Germaint 18:d30cd2967da4 161 bouton.disable_irq();
Germaint 18:d30cd2967da4 162 if(!oled_on){
Germaint 18:d30cd2967da4 163 oledData();
Germaint 18:d30cd2967da4 164 timeScreen.attach(&turnOffScreen,DUREE_ECRAN_ON);
Germaint 18:d30cd2967da4 165 }
Germaint 19:d6236253bf5f 166
Germaint 18:d30cd2967da4 167 bouton.enable_irq();
Germaint 18:d30cd2967da4 168 }
Germaint 16:13364798fce6 169
Germaint 18:d30cd2967da4 170 void turnOffScreen(void){
Germaint 18:d30cd2967da4 171 timeScreen.detach();
Germaint 18:d30cd2967da4 172 oled_on = 0;
Germaint 18:d30cd2967da4 173 oled.sleep();
Germaint 18:d30cd2967da4 174 }