![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Plant Monitoring Project
Dependencies: mbed SHT21_ncleee WakeUp SSD1306 DHT Adafruit_TCS34725 DS1820
main.cpp@22:51aed3b3b452, 2019-11-25 (annotated)
- Committer:
- ludollaoo
- Date:
- Mon Nov 25 07:39:53 2019 +0000
- Revision:
- 22:51aed3b3b452
- Parent:
- 21:4a18c9f1eb5e
- Child:
- 23:424b3149003b
version de demo;
Who changed what in which revision?
User | Revision | Line number | New 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 | |
ludollaoo | 22:51aed3b3b452 | 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 |
ludollaoo | 22:51aed3b3b452 | 20 | SSD1306 oled(D12, A6); |
jufray | 0:e030be8f0310 | 21 | |
Germaint | 1:3fc11a745984 | 22 | // capteur temperature sol |
ludollaoo | 22:51aed3b3b452 | 23 | DS1820 DS(D2); |
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); |
Germaint | 1:3fc11a745984 | 49 | |
Germaint | 16:13364798fce6 | 50 | float temperature_sol; |
Germaint | 18:d30cd2967da4 | 51 | unsigned char humidity_sol; |
Germaint | 16:13364798fce6 | 52 | float temperature_air; |
Germaint | 16:13364798fce6 | 53 | unsigned char humidity_air; |
Germaint | 16:13364798fce6 | 54 | unsigned char pr, pg, pb; |
Germaint | 16:13364798fce6 | 55 | unsigned short lum; |
jufray | 0:e030be8f0310 | 56 | |
jufray | 0:e030be8f0310 | 57 | |
Germaint | 1:3fc11a745984 | 58 | int main() { |
Germaint | 18:d30cd2967da4 | 59 | //Initialisation de l'interruption |
ludollaoo | 20:79f4ef29eafd | 60 | bouton.rise(interruption_bouton); |
Germaint | 18:d30cd2967da4 | 61 | |
Germaint | 19:d6236253bf5f | 62 | // Affichage logo pour initialisation |
Germaint | 19:d6236253bf5f | 63 | oled.wake(); |
Germaint | 16:13364798fce6 | 64 | oled.init(); |
Germaint | 16:13364798fce6 | 65 | oled.cls(0,1); |
Germaint | 19:d6236253bf5f | 66 | oled.locate(4,4); |
Germaint | 19:d6236253bf5f | 67 | oled.printf("2PA2S"); |
Germaint | 19:d6236253bf5f | 68 | oled.redraw(); |
Germaint | 19:d6236253bf5f | 69 | wait(1); |
Germaint | 19:d6236253bf5f | 70 | oled.cls(0,1); |
Germaint | 18:d30cd2967da4 | 71 | oled.sleep(); |
Germaint | 1:3fc11a745984 | 72 | |
jufray | 0:e030be8f0310 | 73 | while(1) { |
Germaint | 16:13364798fce6 | 74 | readData(); |
ludollaoo | 21:4a18c9f1eb5e | 75 | wait(DUREE_MESURE); |
jufray | 0:e030be8f0310 | 76 | } |
jufray | 0:e030be8f0310 | 77 | } |
jufray | 0:e030be8f0310 | 78 | |
jufray | 0:e030be8f0310 | 79 | |
jufray | 0:e030be8f0310 | 80 | float temp_sol() |
jufray | 0:e030be8f0310 | 81 | { |
jufray | 0:e030be8f0310 | 82 | DS.convertTemperature(true, DS1820::all_devices); |
ludollaoo | 22:51aed3b3b452 | 83 | if (DS.unassignedProbe(D2)){ |
ludollaoo | 20:79f4ef29eafd | 84 | printf( "D3 not assigned\n\r"); |
jufray | 0:e030be8f0310 | 85 | } |
ludollaoo | 20:79f4ef29eafd | 86 | printf("temp sol: %.1f\n\r", DS.temperature()); |
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; |
ludollaoo | 20:79f4ef29eafd | 97 | printf("hum sol: %d\n\r", (int) mesure_etalonnee); |
jufray | 0:e030be8f0310 | 98 | return (int) mesure_etalonnee; |
jufray | 0:e030be8f0310 | 99 | } |
jufray | 0:e030be8f0310 | 100 | |
Germaint | 18:d30cd2967da4 | 101 | void fct_RGB(void) |
jufray | 0:e030be8f0310 | 102 | { |
jufray | 0:e030be8f0310 | 103 | int somme; |
jufray | 0:e030be8f0310 | 104 | uint16_t clear, red, green, blue; |
jufray | 0:e030be8f0310 | 105 | if (!RGBsens.begin()) |
jufray | 0:e030be8f0310 | 106 | { |
Germaint | 2:0bfe25431e8e | 107 | //pc.printf("No TCS34725 found ... check your connections"); |
Germaint | 15:c73d0d180cc4 | 108 | //while (1); // halt! |
jufray | 0:e030be8f0310 | 109 | } |
jufray | 0:e030be8f0310 | 110 | RGBsens.getRawData(&red, &green, &blue, &clear); |
jufray | 0:e030be8f0310 | 111 | somme = red + green + blue; |
Germaint | 18:d30cd2967da4 | 112 | pr = red*100/somme; |
Germaint | 18:d30cd2967da4 | 113 | pg = green*100/somme; |
Germaint | 18:d30cd2967da4 | 114 | pb = blue*100/somme; |
Germaint | 18:d30cd2967da4 | 115 | lum = clear; |
ludollaoo | 20:79f4ef29eafd | 116 | printf("luminosité : %d \n\r", lum); |
ludollaoo | 20:79f4ef29eafd | 117 | printf("rouge:%d vert:%d bleu:%d \n\r", pr, pg, pb); |
jufray | 0:e030be8f0310 | 118 | } |
jufray | 0:e030be8f0310 | 119 | |
Germaint | 16:13364798fce6 | 120 | void sendDataSigfox(void){ |
Germaint | 16:13364798fce6 | 121 | short tempSol_short, tempAir_short; |
Germaint | 16:13364798fce6 | 122 | tempSol_short = (short)(temperature_sol*10); |
Germaint | 16:13364798fce6 | 123 | tempAir_short = (short)(temperature_air*10); |
jufray | 0:e030be8f0310 | 124 | |
Germaint | 16:13364798fce6 | 125 | 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 | 126 | } |
Germaint | 2:0bfe25431e8e | 127 | |
Germaint | 2:0bfe25431e8e | 128 | void oledData(void){ |
Germaint | 18:d30cd2967da4 | 129 | if(!oled_on){ |
Germaint | 18:d30cd2967da4 | 130 | oled.wake(); |
Germaint | 18:d30cd2967da4 | 131 | //oled.speed (SSD1306::Medium); |
Germaint | 18:d30cd2967da4 | 132 | //oled.init(); |
Germaint | 18:d30cd2967da4 | 133 | //oled.set_contrast(200); |
Germaint | 18:d30cd2967da4 | 134 | oled_on = 1; |
Germaint | 18:d30cd2967da4 | 135 | } |
Germaint | 16:13364798fce6 | 136 | oled.cls(0,1); |
Germaint | 2:0bfe25431e8e | 137 | oled.locate(0,0); |
Germaint | 2:0bfe25431e8e | 138 | oled.printf("AIR T : %.1f", temperature_air); |
Germaint | 2:0bfe25431e8e | 139 | oled.locate(1,0); |
Germaint | 2:0bfe25431e8e | 140 | oled.printf("AIR H : %d", humidity_air); |
Germaint | 2:0bfe25431e8e | 141 | oled.locate(3,0); |
Germaint | 2:0bfe25431e8e | 142 | oled.printf("FLOOR T : %.1f", temperature_sol); |
Germaint | 2:0bfe25431e8e | 143 | oled.locate(4,0); |
Germaint | 2:0bfe25431e8e | 144 | oled.printf("FLOOR H : %d", humidity_sol); |
Germaint | 2:0bfe25431e8e | 145 | oled.locate(6,0); |
Germaint | 2:0bfe25431e8e | 146 | oled.printf("Light : %d", lum); |
Germaint | 2:0bfe25431e8e | 147 | oled.locate(7,0); |
Germaint | 2:0bfe25431e8e | 148 | oled.printf("R %d G %d B %d", pr, pg, pb); |
Germaint | 2:0bfe25431e8e | 149 | oled.redraw(); |
Germaint | 2:0bfe25431e8e | 150 | } |
jufray | 0:e030be8f0310 | 151 | |
Germaint | 16:13364798fce6 | 152 | void readData(void){ |
Germaint | 16:13364798fce6 | 153 | temperature_sol = temp_sol(); |
Germaint | 16:13364798fce6 | 154 | humidity_sol = fct_humidity_sol(); |
ludollaoo | 20:79f4ef29eafd | 155 | temperature_air = sht.readTemp(); |
ludollaoo | 20:79f4ef29eafd | 156 | printf("temp air: %.1f\n\r", temperature_air); |
ludollaoo | 20:79f4ef29eafd | 157 | humidity_air = sht.readHumidity(); |
ludollaoo | 20:79f4ef29eafd | 158 | printf("hum air: %d\n\r", humidity_air); |
Germaint | 18:d30cd2967da4 | 159 | fct_RGB(); |
ludollaoo | 20:79f4ef29eafd | 160 | printf("\n\r"); |
Germaint | 16:13364798fce6 | 161 | sendDataSigfox(); |
Germaint | 18:d30cd2967da4 | 162 | if(oled_on) |
Germaint | 18:d30cd2967da4 | 163 | oledData(); |
Germaint | 16:13364798fce6 | 164 | } |
jufray | 0:e030be8f0310 | 165 | |
Germaint | 18:d30cd2967da4 | 166 | void interruption_bouton(){ |
Germaint | 18:d30cd2967da4 | 167 | bouton.disable_irq(); |
ludollaoo | 22:51aed3b3b452 | 168 | printf("interruption\r\n"); |
Germaint | 18:d30cd2967da4 | 169 | if(!oled_on){ |
Germaint | 18:d30cd2967da4 | 170 | oledData(); |
Germaint | 18:d30cd2967da4 | 171 | timeScreen.attach(&turnOffScreen,DUREE_ECRAN_ON); |
Germaint | 18:d30cd2967da4 | 172 | } |
Germaint | 19:d6236253bf5f | 173 | |
Germaint | 18:d30cd2967da4 | 174 | bouton.enable_irq(); |
Germaint | 18:d30cd2967da4 | 175 | } |
Germaint | 16:13364798fce6 | 176 | |
Germaint | 18:d30cd2967da4 | 177 | void turnOffScreen(void){ |
Germaint | 18:d30cd2967da4 | 178 | timeScreen.detach(); |
Germaint | 18:d30cd2967da4 | 179 | oled_on = 0; |
Germaint | 18:d30cd2967da4 | 180 | oled.sleep(); |
ludollaoo | 20:79f4ef29eafd | 181 | } |