![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Plant Monitoring Project
Dependencies: mbed SHT21_ncleee WakeUp SSD1306 DHT Adafruit_TCS34725 DS1820
main.cpp@32:4bb8ec535357, 2019-12-12 (annotated)
- Committer:
- Germaint
- Date:
- Thu Dec 12 16:25:33 2019 +0000
- Revision:
- 32:4bb8ec535357
- Parent:
- 31:45f4bfde0b9a
- Child:
- 33:b576ea9bf0e3
Ajout interrupteur (probleme RGB);
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Germaint | 23:424b3149003b | 1 | #include "main.hh" |
Germaint | 18:d30cd2967da4 | 2 | |
Germaint | 30:0a25c02e25d9 | 3 | |
Germaint | 23:424b3149003b | 4 | int main(){ |
Germaint | 32:4bb8ec535357 | 5 | #ifdef INTERRUPTEUR |
Germaint | 32:4bb8ec535357 | 6 | interrupteur = 1; |
Germaint | 32:4bb8ec535357 | 7 | #endif |
Germaint | 32:4bb8ec535357 | 8 | |
Germaint | 23:424b3149003b | 9 | #ifdef OLED |
ludollaoo | 20:79f4ef29eafd | 10 | bouton.rise(interruption_bouton); |
Germaint | 23:424b3149003b | 11 | initOLED(); |
Germaint | 23:424b3149003b | 12 | #endif |
Germaint | 1:3fc11a745984 | 13 | |
jufray | 0:e030be8f0310 | 14 | while(1) { |
Germaint | 16:13364798fce6 | 15 | readData(); |
Germaint | 23:424b3149003b | 16 | wait(DUREE_OFF); |
jufray | 0:e030be8f0310 | 17 | } |
jufray | 0:e030be8f0310 | 18 | } |
jufray | 0:e030be8f0310 | 19 | |
Germaint | 27:05fb631b1f6e | 20 | #ifdef FLOOR_TEMPERATURE |
Germaint | 31:45f4bfde0b9a | 21 | void temp_sol() |
jufray | 0:e030be8f0310 | 22 | { |
Germaint | 31:45f4bfde0b9a | 23 | int result = 0; |
Germaint | 31:45f4bfde0b9a | 24 | ds1820.begin(); |
Germaint | 31:45f4bfde0b9a | 25 | ds1820.startConversion(); // start temperature conversion from analog to digital |
Germaint | 31:45f4bfde0b9a | 26 | wait(1.0); // let DS1820 complete the temperature conversion |
Germaint | 31:45f4bfde0b9a | 27 | result = ds1820.read(temperature_sol); // read temperature from DS1820 and perform cyclic redundancy check (CRC) |
Germaint | 31:45f4bfde0b9a | 28 | #ifdef DEBUG |
Germaint | 31:45f4bfde0b9a | 29 | switch (result) { |
Germaint | 31:45f4bfde0b9a | 30 | case 0: // no errors -> 'temp' contains the value of measured temperature |
Germaint | 31:45f4bfde0b9a | 31 | pc.printf("temp = %3.1f C\r\n", temperature_sol); |
Germaint | 31:45f4bfde0b9a | 32 | break; |
Germaint | 31:45f4bfde0b9a | 33 | |
Germaint | 31:45f4bfde0b9a | 34 | case 1: // no sensor present -> 'temp' is not updated |
Germaint | 31:45f4bfde0b9a | 35 | pc.printf("no sensor present\n\r"); |
Germaint | 31:45f4bfde0b9a | 36 | break; |
Germaint | 31:45f4bfde0b9a | 37 | |
Germaint | 31:45f4bfde0b9a | 38 | case 2: // CRC error -> 'temp' is not updated |
Germaint | 31:45f4bfde0b9a | 39 | pc.printf("CRC error\r\n"); |
jufray | 0:e030be8f0310 | 40 | } |
Germaint | 25:77322fbe298e | 41 | #endif |
Germaint | 31:45f4bfde0b9a | 42 | |
jufray | 0:e030be8f0310 | 43 | } |
Germaint | 27:05fb631b1f6e | 44 | #endif |
Germaint | 23:424b3149003b | 45 | |
Germaint | 27:05fb631b1f6e | 46 | #ifdef FLOOR_HUMIDITY |
Germaint | 1:3fc11a745984 | 47 | int fct_humidity_sol(void) |
jufray | 0:e030be8f0310 | 48 | { |
jufray | 0:e030be8f0310 | 49 | float val_min = 0.377; |
jufray | 0:e030be8f0310 | 50 | float val_max = 0.772; |
jufray | 0:e030be8f0310 | 51 | float mesure, mesure_etalonnee; |
Germaint | 1:3fc11a745984 | 52 | mesure = capteur_humidity_sol.read(); |
jufray | 0:e030be8f0310 | 53 | mesure_etalonnee = (1-((mesure - val_min)/(val_max - val_min)))*100; |
Germaint | 23:424b3149003b | 54 | #ifdef DEBUG |
Germaint | 23:424b3149003b | 55 | pc.printf("hum sol: %d\n\r", (int) mesure_etalonnee); |
Germaint | 23:424b3149003b | 56 | #endif |
jufray | 0:e030be8f0310 | 57 | return (int) mesure_etalonnee; |
jufray | 0:e030be8f0310 | 58 | } |
Germaint | 27:05fb631b1f6e | 59 | #endif |
jufray | 0:e030be8f0310 | 60 | |
Germaint | 27:05fb631b1f6e | 61 | #ifdef RGB |
Germaint | 18:d30cd2967da4 | 62 | void fct_RGB(void) |
jufray | 0:e030be8f0310 | 63 | { |
jufray | 0:e030be8f0310 | 64 | int somme; |
jufray | 0:e030be8f0310 | 65 | uint16_t clear, red, green, blue; |
jufray | 0:e030be8f0310 | 66 | if (!RGBsens.begin()) |
jufray | 0:e030be8f0310 | 67 | { |
Germaint | 23:424b3149003b | 68 | #ifdef DEBUG |
Germaint | 23:424b3149003b | 69 | pc.printf("No TCS34725 found ... check your connections"); |
Germaint | 23:424b3149003b | 70 | #endif |
Germaint | 15:c73d0d180cc4 | 71 | //while (1); // halt! |
jufray | 0:e030be8f0310 | 72 | } |
jufray | 0:e030be8f0310 | 73 | RGBsens.getRawData(&red, &green, &blue, &clear); |
jufray | 0:e030be8f0310 | 74 | somme = red + green + blue; |
Germaint | 18:d30cd2967da4 | 75 | pr = red*100/somme; |
Germaint | 18:d30cd2967da4 | 76 | pg = green*100/somme; |
Germaint | 18:d30cd2967da4 | 77 | pb = blue*100/somme; |
Germaint | 18:d30cd2967da4 | 78 | lum = clear; |
Germaint | 23:424b3149003b | 79 | #ifdef DEBUG |
Germaint | 23:424b3149003b | 80 | pc.printf("luminosite : %d \n\r", lum); |
Germaint | 23:424b3149003b | 81 | pc.printf("rouge:%d vert:%d bleu:%d \n\r", pr, pg, pb); |
Germaint | 23:424b3149003b | 82 | #endif |
jufray | 0:e030be8f0310 | 83 | } |
Germaint | 27:05fb631b1f6e | 84 | #endif |
jufray | 0:e030be8f0310 | 85 | |
Germaint | 27:05fb631b1f6e | 86 | #ifdef SIGFOX |
Germaint | 16:13364798fce6 | 87 | void sendDataSigfox(void){ |
Germaint | 23:424b3149003b | 88 | #ifdef DEBUG |
Germaint | 23:424b3149003b | 89 | pc.printf("Sending Data to Sigfox \n\r"); |
Germaint | 23:424b3149003b | 90 | #endif |
Germaint | 16:13364798fce6 | 91 | short tempSol_short, tempAir_short; |
Germaint | 16:13364798fce6 | 92 | tempSol_short = (short)(temperature_sol*10); |
Germaint | 16:13364798fce6 | 93 | tempAir_short = (short)(temperature_air*10); |
jufray | 0:e030be8f0310 | 94 | |
Germaint | 23:424b3149003b | 95 | 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); |
Germaint | 16:13364798fce6 | 96 | } |
Germaint | 27:05fb631b1f6e | 97 | #endif |
Germaint | 27:05fb631b1f6e | 98 | |
Germaint | 27:05fb631b1f6e | 99 | #ifdef OLED |
Germaint | 2:0bfe25431e8e | 100 | void oledData(void){ |
Germaint | 23:424b3149003b | 101 | #ifdef DEBUG |
Germaint | 23:424b3149003b | 102 | pc.printf("Displaying Data\r\n"); |
Germaint | 23:424b3149003b | 103 | #endif |
Germaint | 30:0a25c02e25d9 | 104 | |
Germaint | 18:d30cd2967da4 | 105 | if(!oled_on){ |
Germaint | 18:d30cd2967da4 | 106 | oled.wake(); |
Germaint | 30:0a25c02e25d9 | 107 | oled.clear(); |
Germaint | 18:d30cd2967da4 | 108 | oled_on = 1; |
Germaint | 18:d30cd2967da4 | 109 | } |
Germaint | 30:0a25c02e25d9 | 110 | oled.clear(); |
Germaint | 2:0bfe25431e8e | 111 | oled.printf("AIR T : %.1f", temperature_air); |
Germaint | 30:0a25c02e25d9 | 112 | oled.printf("\n\r"); |
Germaint | 2:0bfe25431e8e | 113 | oled.printf("AIR H : %d", humidity_air); |
Germaint | 30:0a25c02e25d9 | 114 | oled.printf("\n\r\n\r"); |
Germaint | 2:0bfe25431e8e | 115 | oled.printf("FLOOR T : %.1f", temperature_sol); |
Germaint | 30:0a25c02e25d9 | 116 | oled.printf("\n\r"); |
Germaint | 2:0bfe25431e8e | 117 | oled.printf("FLOOR H : %d", humidity_sol); |
Germaint | 30:0a25c02e25d9 | 118 | oled.printf("\n\r\n\r"); |
Germaint | 2:0bfe25431e8e | 119 | oled.printf("Light : %d", lum); |
Germaint | 30:0a25c02e25d9 | 120 | oled.printf("\n\r"); |
Germaint | 2:0bfe25431e8e | 121 | oled.printf("R %d G %d B %d", pr, pg, pb); |
Germaint | 30:0a25c02e25d9 | 122 | oled.update(); |
Germaint | 2:0bfe25431e8e | 123 | } |
Germaint | 27:05fb631b1f6e | 124 | #endif |
jufray | 0:e030be8f0310 | 125 | |
Germaint | 16:13364798fce6 | 126 | void readData(void){ |
Germaint | 32:4bb8ec535357 | 127 | #ifdef INTERRUPTEUR |
Germaint | 32:4bb8ec535357 | 128 | interrupteur = 1; |
Germaint | 32:4bb8ec535357 | 129 | wait_ms(100); |
Germaint | 32:4bb8ec535357 | 130 | #endif |
Germaint | 23:424b3149003b | 131 | #ifdef DEBUG |
Germaint | 23:424b3149003b | 132 | pc.printf("Reading Data\n\r"); |
Germaint | 23:424b3149003b | 133 | #endif |
Germaint | 23:424b3149003b | 134 | #ifdef FLOOR_TEMPERATURE |
Germaint | 31:45f4bfde0b9a | 135 | temp_sol(); |
Germaint | 23:424b3149003b | 136 | #endif |
Germaint | 23:424b3149003b | 137 | #ifdef FLOOR_HUMIDITY |
Germaint | 16:13364798fce6 | 138 | humidity_sol = fct_humidity_sol(); |
Germaint | 23:424b3149003b | 139 | #endif |
Germaint | 23:424b3149003b | 140 | #ifdef AIR_PARAMETERS |
Germaint | 23:424b3149003b | 141 | temperature_air = sht.readTemp(); |
ludollaoo | 20:79f4ef29eafd | 142 | humidity_air = sht.readHumidity(); |
Germaint | 23:424b3149003b | 143 | #endif |
Germaint | 23:424b3149003b | 144 | #ifdef DEBUG |
Germaint | 23:424b3149003b | 145 | printf("hum air: %d\n\r", humidity_air); |
Germaint | 23:424b3149003b | 146 | printf("temp air: %.1f\n\r", temperature_air); |
Germaint | 23:424b3149003b | 147 | #endif |
Germaint | 23:424b3149003b | 148 | #ifdef RGB |
Germaint | 18:d30cd2967da4 | 149 | fct_RGB(); |
Germaint | 23:424b3149003b | 150 | #endif |
Germaint | 23:424b3149003b | 151 | #ifdef SIGFOX |
Germaint | 16:13364798fce6 | 152 | sendDataSigfox(); |
Germaint | 23:424b3149003b | 153 | #endif |
Germaint | 23:424b3149003b | 154 | #ifdef OLED |
Germaint | 18:d30cd2967da4 | 155 | if(oled_on) |
Germaint | 18:d30cd2967da4 | 156 | oledData(); |
Germaint | 23:424b3149003b | 157 | #endif |
Germaint | 32:4bb8ec535357 | 158 | #ifdef INTERRUPTEUR |
Germaint | 32:4bb8ec535357 | 159 | wait(8); // Il faut du temps au module SigFox pour envoyer les données |
Germaint | 32:4bb8ec535357 | 160 | interrupteur = 0; |
Germaint | 32:4bb8ec535357 | 161 | #endif |
Germaint | 16:13364798fce6 | 162 | } |
jufray | 0:e030be8f0310 | 163 | |
Germaint | 18:d30cd2967da4 | 164 | void interruption_bouton(){ |
Germaint | 18:d30cd2967da4 | 165 | bouton.disable_irq(); |
Germaint | 23:424b3149003b | 166 | #ifdef DEBUG |
Germaint | 23:424b3149003b | 167 | pc.printf("Button interrupt\r\n"); |
Germaint | 23:424b3149003b | 168 | #endif |
Germaint | 27:05fb631b1f6e | 169 | #ifdef OLED |
Germaint | 18:d30cd2967da4 | 170 | if(!oled_on){ |
Germaint | 18:d30cd2967da4 | 171 | oledData(); |
Germaint | 18:d30cd2967da4 | 172 | timeScreen.attach(&turnOffScreen,DUREE_ECRAN_ON); |
Germaint | 18:d30cd2967da4 | 173 | } |
Germaint | 27:05fb631b1f6e | 174 | #endif |
Germaint | 18:d30cd2967da4 | 175 | bouton.enable_irq(); |
Germaint | 18:d30cd2967da4 | 176 | } |
Germaint | 16:13364798fce6 | 177 | |
Germaint | 27:05fb631b1f6e | 178 | #ifdef OLED |
Germaint | 18:d30cd2967da4 | 179 | void turnOffScreen(void){ |
Germaint | 23:424b3149003b | 180 | #ifdef DEBUG |
Germaint | 23:424b3149003b | 181 | pc.printf("Turning off screen \n\r"); |
Germaint | 23:424b3149003b | 182 | #endif |
Germaint | 18:d30cd2967da4 | 183 | timeScreen.detach(); |
Germaint | 18:d30cd2967da4 | 184 | oled_on = 0; |
Germaint | 18:d30cd2967da4 | 185 | oled.sleep(); |
Germaint | 23:424b3149003b | 186 | } |
Germaint | 27:05fb631b1f6e | 187 | #endif |
Germaint | 23:424b3149003b | 188 | |
Germaint | 27:05fb631b1f6e | 189 | #ifdef OLED |
Germaint | 23:424b3149003b | 190 | void initOLED(void){ |
Germaint | 30:0a25c02e25d9 | 191 | oled.on(); |
Germaint | 30:0a25c02e25d9 | 192 | oled.initialise(); |
Germaint | 30:0a25c02e25d9 | 193 | oled.clear(); |
Germaint | 30:0a25c02e25d9 | 194 | oled.set_contrast(255); |
Germaint | 30:0a25c02e25d9 | 195 | oled.set_font(bold_font, 8); |
Germaint | 30:0a25c02e25d9 | 196 | oled.printf("================"); |
Germaint | 30:0a25c02e25d9 | 197 | oled.printf("\n\r"); |
Germaint | 30:0a25c02e25d9 | 198 | oled.printf(" 2PA2S"); |
Germaint | 30:0a25c02e25d9 | 199 | oled.printf("\n\r\n\r"); |
Germaint | 30:0a25c02e25d9 | 200 | oled.printf("FRAYSSE GERMAIN"); |
Germaint | 30:0a25c02e25d9 | 201 | oled.printf("\n\r\n\r"); |
Germaint | 30:0a25c02e25d9 | 202 | oled.printf(" DUPLESSIS"); |
Germaint | 30:0a25c02e25d9 | 203 | oled.printf("\n\r"); |
Germaint | 30:0a25c02e25d9 | 204 | oled.printf("================"); |
Germaint | 30:0a25c02e25d9 | 205 | oled.update(); |
Germaint | 30:0a25c02e25d9 | 206 | wait(10); |
Germaint | 30:0a25c02e25d9 | 207 | oled.clear(); |
Germaint | 30:0a25c02e25d9 | 208 | oled.update(); |
Germaint | 23:424b3149003b | 209 | oled.sleep(); |
Germaint | 27:05fb631b1f6e | 210 | } |
Germaint | 27:05fb631b1f6e | 211 | #endif |