![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Plant Monitoring Project
Dependencies: mbed SHT21_ncleee WakeUp SSD1306 DHT Adafruit_TCS34725 DS1820
main.cpp@34:fb1b9c5e5cf3, 2020-01-07 (annotated)
- Committer:
- Germaint
- Date:
- Tue Jan 07 17:57:19 2020 +0000
- Revision:
- 34:fb1b9c5e5cf3
- Parent:
- 33:b576ea9bf0e3
- Child:
- 35:d87c1391706a
Code propre
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 | 34:fb1b9c5e5cf3 | 3 | int main(){ |
Germaint | 34:fb1b9c5e5cf3 | 4 | #ifdef DEEPSLEEP |
Germaint | 34:fb1b9c5e5cf3 | 5 | WakeUp::calibrate(); |
Germaint | 34:fb1b9c5e5cf3 | 6 | #endif |
Germaint | 30:0a25c02e25d9 | 7 | |
Germaint | 32:4bb8ec535357 | 8 | #ifdef INTERRUPTEUR |
Germaint | 32:4bb8ec535357 | 9 | interrupteur = 1; |
Germaint | 32:4bb8ec535357 | 10 | #endif |
Germaint | 32:4bb8ec535357 | 11 | |
Germaint | 23:424b3149003b | 12 | #ifdef OLED |
ludollaoo | 20:79f4ef29eafd | 13 | bouton.rise(interruption_bouton); |
Germaint | 23:424b3149003b | 14 | initOLED(); |
Germaint | 23:424b3149003b | 15 | #endif |
Germaint | 1:3fc11a745984 | 16 | |
jufray | 0:e030be8f0310 | 17 | while(1) { |
Germaint | 34:fb1b9c5e5cf3 | 18 | if(flag_readData) |
Germaint | 34:fb1b9c5e5cf3 | 19 | readData(); |
Germaint | 34:fb1b9c5e5cf3 | 20 | flag_readData = true; |
Germaint | 34:fb1b9c5e5cf3 | 21 | #ifndef DEEPSLEEP |
Germaint | 34:fb1b9c5e5cf3 | 22 | wait(DUREE_OFF); |
Germaint | 34:fb1b9c5e5cf3 | 23 | #endif |
Germaint | 34:fb1b9c5e5cf3 | 24 | #ifdef DEEPSLEEP |
Germaint | 34:fb1b9c5e5cf3 | 25 | if(oled_on){ |
Germaint | 34:fb1b9c5e5cf3 | 26 | wait(DUREE_ECRAN_ON); |
Germaint | 34:fb1b9c5e5cf3 | 27 | turnOffScreen(); |
Germaint | 34:fb1b9c5e5cf3 | 28 | flag_readData = false; |
Germaint | 34:fb1b9c5e5cf3 | 29 | } |
Germaint | 34:fb1b9c5e5cf3 | 30 | else{ |
Germaint | 34:fb1b9c5e5cf3 | 31 | WakeUp::set(DUREE_OFF); |
Germaint | 34:fb1b9c5e5cf3 | 32 | deepsleep(); |
Germaint | 34:fb1b9c5e5cf3 | 33 | } |
Germaint | 34:fb1b9c5e5cf3 | 34 | #endif |
jufray | 0:e030be8f0310 | 35 | } |
jufray | 0:e030be8f0310 | 36 | } |
jufray | 0:e030be8f0310 | 37 | |
Germaint | 34:fb1b9c5e5cf3 | 38 | #ifdef AIR_PARAMETERS_DHT22 |
Germaint | 34:fb1b9c5e5cf3 | 39 | void air_temp_hum(){ |
Germaint | 34:fb1b9c5e5cf3 | 40 | int err; |
Germaint | 34:fb1b9c5e5cf3 | 41 | wait(1); |
Germaint | 34:fb1b9c5e5cf3 | 42 | do{ |
Germaint | 34:fb1b9c5e5cf3 | 43 | err = dht_sensor.readData(); |
Germaint | 34:fb1b9c5e5cf3 | 44 | wait(1); |
Germaint | 34:fb1b9c5e5cf3 | 45 | }while(err != 0); |
Germaint | 34:fb1b9c5e5cf3 | 46 | temperature_air = dht_sensor.ReadTemperature(CELCIUS); |
Germaint | 34:fb1b9c5e5cf3 | 47 | humidity_air = dht_sensor.ReadHumidity(); |
Germaint | 34:fb1b9c5e5cf3 | 48 | } |
Germaint | 34:fb1b9c5e5cf3 | 49 | #endif |
Germaint | 34:fb1b9c5e5cf3 | 50 | |
Germaint | 27:05fb631b1f6e | 51 | #ifdef FLOOR_TEMPERATURE |
Germaint | 31:45f4bfde0b9a | 52 | void temp_sol() |
jufray | 0:e030be8f0310 | 53 | { |
Germaint | 31:45f4bfde0b9a | 54 | int result = 0; |
Germaint | 31:45f4bfde0b9a | 55 | ds1820.begin(); |
Germaint | 31:45f4bfde0b9a | 56 | ds1820.startConversion(); // start temperature conversion from analog to digital |
Germaint | 31:45f4bfde0b9a | 57 | wait(1.0); // let DS1820 complete the temperature conversion |
Germaint | 31:45f4bfde0b9a | 58 | result = ds1820.read(temperature_sol); // read temperature from DS1820 and perform cyclic redundancy check (CRC) |
Germaint | 31:45f4bfde0b9a | 59 | #ifdef DEBUG |
Germaint | 31:45f4bfde0b9a | 60 | switch (result) { |
Germaint | 31:45f4bfde0b9a | 61 | case 0: // no errors -> 'temp' contains the value of measured temperature |
Germaint | 31:45f4bfde0b9a | 62 | pc.printf("temp = %3.1f C\r\n", temperature_sol); |
Germaint | 31:45f4bfde0b9a | 63 | break; |
Germaint | 31:45f4bfde0b9a | 64 | |
Germaint | 31:45f4bfde0b9a | 65 | case 1: // no sensor present -> 'temp' is not updated |
Germaint | 31:45f4bfde0b9a | 66 | pc.printf("no sensor present\n\r"); |
Germaint | 31:45f4bfde0b9a | 67 | break; |
Germaint | 31:45f4bfde0b9a | 68 | |
Germaint | 31:45f4bfde0b9a | 69 | case 2: // CRC error -> 'temp' is not updated |
Germaint | 31:45f4bfde0b9a | 70 | pc.printf("CRC error\r\n"); |
jufray | 0:e030be8f0310 | 71 | } |
Germaint | 25:77322fbe298e | 72 | #endif |
Germaint | 31:45f4bfde0b9a | 73 | |
jufray | 0:e030be8f0310 | 74 | } |
Germaint | 27:05fb631b1f6e | 75 | #endif |
Germaint | 23:424b3149003b | 76 | |
Germaint | 27:05fb631b1f6e | 77 | #ifdef FLOOR_HUMIDITY |
Germaint | 1:3fc11a745984 | 78 | int fct_humidity_sol(void) |
jufray | 0:e030be8f0310 | 79 | { |
jufray | 0:e030be8f0310 | 80 | float val_min = 0.377; |
jufray | 0:e030be8f0310 | 81 | float val_max = 0.772; |
jufray | 0:e030be8f0310 | 82 | float mesure, mesure_etalonnee; |
Germaint | 1:3fc11a745984 | 83 | mesure = capteur_humidity_sol.read(); |
jufray | 0:e030be8f0310 | 84 | mesure_etalonnee = (1-((mesure - val_min)/(val_max - val_min)))*100; |
Germaint | 23:424b3149003b | 85 | #ifdef DEBUG |
Germaint | 23:424b3149003b | 86 | pc.printf("hum sol: %d\n\r", (int) mesure_etalonnee); |
Germaint | 23:424b3149003b | 87 | #endif |
jufray | 0:e030be8f0310 | 88 | return (int) mesure_etalonnee; |
jufray | 0:e030be8f0310 | 89 | } |
Germaint | 27:05fb631b1f6e | 90 | #endif |
jufray | 0:e030be8f0310 | 91 | |
Germaint | 27:05fb631b1f6e | 92 | #ifdef RGB |
Germaint | 18:d30cd2967da4 | 93 | void fct_RGB(void) |
jufray | 0:e030be8f0310 | 94 | { |
jufray | 0:e030be8f0310 | 95 | int somme; |
jufray | 0:e030be8f0310 | 96 | uint16_t clear, red, green, blue; |
jufray | 0:e030be8f0310 | 97 | if (!RGBsens.begin()) |
jufray | 0:e030be8f0310 | 98 | { |
Germaint | 23:424b3149003b | 99 | #ifdef DEBUG |
Germaint | 23:424b3149003b | 100 | pc.printf("No TCS34725 found ... check your connections"); |
Germaint | 23:424b3149003b | 101 | #endif |
Germaint | 15:c73d0d180cc4 | 102 | //while (1); // halt! |
jufray | 0:e030be8f0310 | 103 | } |
jufray | 0:e030be8f0310 | 104 | RGBsens.getRawData(&red, &green, &blue, &clear); |
jufray | 0:e030be8f0310 | 105 | somme = red + green + blue; |
Germaint | 18:d30cd2967da4 | 106 | pr = red*100/somme; |
Germaint | 18:d30cd2967da4 | 107 | pg = green*100/somme; |
Germaint | 18:d30cd2967da4 | 108 | pb = blue*100/somme; |
Germaint | 33:b576ea9bf0e3 | 109 | lum = (unsigned short) RGBsens.calculateLux(red, green, blue); |
Germaint | 23:424b3149003b | 110 | #ifdef DEBUG |
Germaint | 23:424b3149003b | 111 | pc.printf("luminosite : %d \n\r", lum); |
Germaint | 23:424b3149003b | 112 | pc.printf("rouge:%d vert:%d bleu:%d \n\r", pr, pg, pb); |
Germaint | 23:424b3149003b | 113 | #endif |
jufray | 0:e030be8f0310 | 114 | } |
Germaint | 27:05fb631b1f6e | 115 | #endif |
jufray | 0:e030be8f0310 | 116 | |
Germaint | 27:05fb631b1f6e | 117 | #ifdef SIGFOX |
Germaint | 16:13364798fce6 | 118 | void sendDataSigfox(void){ |
Germaint | 23:424b3149003b | 119 | #ifdef DEBUG |
Germaint | 23:424b3149003b | 120 | pc.printf("Sending Data to Sigfox \n\r"); |
Germaint | 23:424b3149003b | 121 | #endif |
Germaint | 16:13364798fce6 | 122 | short tempSol_short, tempAir_short; |
Germaint | 16:13364798fce6 | 123 | tempSol_short = (short)(temperature_sol*10); |
Germaint | 16:13364798fce6 | 124 | tempAir_short = (short)(temperature_air*10); |
jufray | 0:e030be8f0310 | 125 | |
Germaint | 34:fb1b9c5e5cf3 | 126 | wisol.printf("AT$SF=%04x%02x%04x%02x%04x%02x%02x%02x%02x\r\n",tempSol_short, humidity_sol, tempAir_short, humidity_air, lum, pr, pg, pb, vBat); |
Germaint | 16:13364798fce6 | 127 | } |
Germaint | 27:05fb631b1f6e | 128 | #endif |
Germaint | 27:05fb631b1f6e | 129 | |
Germaint | 27:05fb631b1f6e | 130 | #ifdef OLED |
Germaint | 2:0bfe25431e8e | 131 | void oledData(void){ |
Germaint | 23:424b3149003b | 132 | #ifdef DEBUG |
Germaint | 23:424b3149003b | 133 | pc.printf("Displaying Data\r\n"); |
Germaint | 23:424b3149003b | 134 | #endif |
Germaint | 30:0a25c02e25d9 | 135 | |
Germaint | 18:d30cd2967da4 | 136 | if(!oled_on){ |
Germaint | 18:d30cd2967da4 | 137 | oled.wake(); |
Germaint | 30:0a25c02e25d9 | 138 | oled.clear(); |
Germaint | 18:d30cd2967da4 | 139 | oled_on = 1; |
Germaint | 18:d30cd2967da4 | 140 | } |
Germaint | 30:0a25c02e25d9 | 141 | oled.clear(); |
Germaint | 2:0bfe25431e8e | 142 | oled.printf("AIR T : %.1f", temperature_air); |
Germaint | 30:0a25c02e25d9 | 143 | oled.printf("\n\r"); |
Germaint | 2:0bfe25431e8e | 144 | oled.printf("AIR H : %d", humidity_air); |
Germaint | 30:0a25c02e25d9 | 145 | oled.printf("\n\r\n\r"); |
Germaint | 2:0bfe25431e8e | 146 | oled.printf("FLOOR T : %.1f", temperature_sol); |
Germaint | 30:0a25c02e25d9 | 147 | oled.printf("\n\r"); |
Germaint | 2:0bfe25431e8e | 148 | oled.printf("FLOOR H : %d", humidity_sol); |
Germaint | 30:0a25c02e25d9 | 149 | oled.printf("\n\r\n\r"); |
Germaint | 2:0bfe25431e8e | 150 | oled.printf("Light : %d", lum); |
Germaint | 30:0a25c02e25d9 | 151 | oled.printf("\n\r"); |
Germaint | 2:0bfe25431e8e | 152 | oled.printf("R %d G %d B %d", pr, pg, pb); |
Germaint | 30:0a25c02e25d9 | 153 | oled.update(); |
Germaint | 2:0bfe25431e8e | 154 | } |
Germaint | 27:05fb631b1f6e | 155 | #endif |
jufray | 0:e030be8f0310 | 156 | |
Germaint | 16:13364798fce6 | 157 | void readData(void){ |
Germaint | 32:4bb8ec535357 | 158 | #ifdef INTERRUPTEUR |
Germaint | 32:4bb8ec535357 | 159 | interrupteur = 1; |
Germaint | 32:4bb8ec535357 | 160 | wait_ms(100); |
Germaint | 33:b576ea9bf0e3 | 161 | RGBsens.begin(); |
Germaint | 33:b576ea9bf0e3 | 162 | wait_ms(100); |
Germaint | 32:4bb8ec535357 | 163 | #endif |
Germaint | 23:424b3149003b | 164 | #ifdef DEBUG |
Germaint | 23:424b3149003b | 165 | pc.printf("Reading Data\n\r"); |
Germaint | 23:424b3149003b | 166 | #endif |
Germaint | 23:424b3149003b | 167 | #ifdef FLOOR_TEMPERATURE |
Germaint | 31:45f4bfde0b9a | 168 | temp_sol(); |
Germaint | 23:424b3149003b | 169 | #endif |
Germaint | 23:424b3149003b | 170 | #ifdef FLOOR_HUMIDITY |
Germaint | 16:13364798fce6 | 171 | humidity_sol = fct_humidity_sol(); |
Germaint | 23:424b3149003b | 172 | #endif |
Germaint | 34:fb1b9c5e5cf3 | 173 | #ifdef AIR_PARAMETERS_DHT22 |
Germaint | 34:fb1b9c5e5cf3 | 174 | air_temp_hum(); |
Germaint | 23:424b3149003b | 175 | #endif |
Germaint | 23:424b3149003b | 176 | #ifdef DEBUG |
Germaint | 23:424b3149003b | 177 | printf("hum air: %d\n\r", humidity_air); |
Germaint | 23:424b3149003b | 178 | printf("temp air: %.1f\n\r", temperature_air); |
Germaint | 23:424b3149003b | 179 | #endif |
Germaint | 23:424b3149003b | 180 | #ifdef RGB |
Germaint | 18:d30cd2967da4 | 181 | fct_RGB(); |
Germaint | 23:424b3149003b | 182 | #endif |
Germaint | 23:424b3149003b | 183 | #ifdef SIGFOX |
Germaint | 16:13364798fce6 | 184 | sendDataSigfox(); |
Germaint | 23:424b3149003b | 185 | #endif |
Germaint | 23:424b3149003b | 186 | #ifdef OLED |
Germaint | 18:d30cd2967da4 | 187 | if(oled_on) |
Germaint | 18:d30cd2967da4 | 188 | oledData(); |
Germaint | 23:424b3149003b | 189 | #endif |
Germaint | 34:fb1b9c5e5cf3 | 190 | #ifdef BATTERY_LVL |
Germaint | 34:fb1b9c5e5cf3 | 191 | readBatteryLvl(); |
Germaint | 34:fb1b9c5e5cf3 | 192 | #endif |
Germaint | 32:4bb8ec535357 | 193 | #ifdef INTERRUPTEUR |
Germaint | 32:4bb8ec535357 | 194 | wait(8); // Il faut du temps au module SigFox pour envoyer les données |
Germaint | 32:4bb8ec535357 | 195 | interrupteur = 0; |
Germaint | 32:4bb8ec535357 | 196 | #endif |
Germaint | 16:13364798fce6 | 197 | } |
jufray | 0:e030be8f0310 | 198 | |
Germaint | 18:d30cd2967da4 | 199 | void interruption_bouton(){ |
Germaint | 18:d30cd2967da4 | 200 | bouton.disable_irq(); |
Germaint | 23:424b3149003b | 201 | #ifdef DEBUG |
Germaint | 23:424b3149003b | 202 | pc.printf("Button interrupt\r\n"); |
Germaint | 23:424b3149003b | 203 | #endif |
Germaint | 27:05fb631b1f6e | 204 | #ifdef OLED |
Germaint | 18:d30cd2967da4 | 205 | if(!oled_on){ |
Germaint | 18:d30cd2967da4 | 206 | oledData(); |
Germaint | 18:d30cd2967da4 | 207 | } |
Germaint | 27:05fb631b1f6e | 208 | #endif |
Germaint | 18:d30cd2967da4 | 209 | bouton.enable_irq(); |
Germaint | 18:d30cd2967da4 | 210 | } |
Germaint | 16:13364798fce6 | 211 | |
Germaint | 27:05fb631b1f6e | 212 | #ifdef OLED |
Germaint | 18:d30cd2967da4 | 213 | void turnOffScreen(void){ |
Germaint | 23:424b3149003b | 214 | #ifdef DEBUG |
Germaint | 23:424b3149003b | 215 | pc.printf("Turning off screen \n\r"); |
Germaint | 23:424b3149003b | 216 | #endif |
Germaint | 18:d30cd2967da4 | 217 | oled_on = 0; |
Germaint | 18:d30cd2967da4 | 218 | oled.sleep(); |
Germaint | 23:424b3149003b | 219 | } |
Germaint | 27:05fb631b1f6e | 220 | #endif |
Germaint | 23:424b3149003b | 221 | |
Germaint | 27:05fb631b1f6e | 222 | #ifdef OLED |
Germaint | 23:424b3149003b | 223 | void initOLED(void){ |
Germaint | 30:0a25c02e25d9 | 224 | oled.on(); |
Germaint | 30:0a25c02e25d9 | 225 | oled.initialise(); |
Germaint | 30:0a25c02e25d9 | 226 | oled.clear(); |
Germaint | 30:0a25c02e25d9 | 227 | oled.set_contrast(255); |
Germaint | 30:0a25c02e25d9 | 228 | oled.set_font(bold_font, 8); |
Germaint | 30:0a25c02e25d9 | 229 | oled.printf("================"); |
Germaint | 30:0a25c02e25d9 | 230 | oled.printf("\n\r"); |
Germaint | 30:0a25c02e25d9 | 231 | oled.printf(" 2PA2S"); |
Germaint | 30:0a25c02e25d9 | 232 | oled.printf("\n\r\n\r"); |
Germaint | 30:0a25c02e25d9 | 233 | oled.printf("FRAYSSE GERMAIN"); |
Germaint | 30:0a25c02e25d9 | 234 | oled.printf("\n\r\n\r"); |
Germaint | 30:0a25c02e25d9 | 235 | oled.printf(" DUPLESSIS"); |
Germaint | 30:0a25c02e25d9 | 236 | oled.printf("\n\r"); |
Germaint | 30:0a25c02e25d9 | 237 | oled.printf("================"); |
Germaint | 30:0a25c02e25d9 | 238 | oled.update(); |
Germaint | 30:0a25c02e25d9 | 239 | wait(10); |
Germaint | 30:0a25c02e25d9 | 240 | oled.clear(); |
Germaint | 30:0a25c02e25d9 | 241 | oled.update(); |
Germaint | 23:424b3149003b | 242 | oled.sleep(); |
Germaint | 27:05fb631b1f6e | 243 | } |
Germaint | 34:fb1b9c5e5cf3 | 244 | #endif |
Germaint | 34:fb1b9c5e5cf3 | 245 | |
Germaint | 34:fb1b9c5e5cf3 | 246 | void readBatteryLvl(void){ |
Germaint | 34:fb1b9c5e5cf3 | 247 | float calcVbat = 0; |
Germaint | 34:fb1b9c5e5cf3 | 248 | int batIn = 0; |
Germaint | 34:fb1b9c5e5cf3 | 249 | float ain = battery.read(); |
Germaint | 34:fb1b9c5e5cf3 | 250 | batIn = 100*ain; |
Germaint | 34:fb1b9c5e5cf3 | 251 | calcVbat = ((float)(batIn - BATTERIE_MIN)/(float)(BATTERIE_MAX - BATTERIE_MIN))*100; |
Germaint | 34:fb1b9c5e5cf3 | 252 | vBat = calcVbat; |
Germaint | 34:fb1b9c5e5cf3 | 253 | #ifdef DEBUG |
Germaint | 34:fb1b9c5e5cf3 | 254 | pc.printf("batIn = %d\n\r", batIn); |
Germaint | 34:fb1b9c5e5cf3 | 255 | pc.printf("calcVbat = %d\n\r", calcVbat); |
Germaint | 34:fb1b9c5e5cf3 | 256 | pc.printf("vBat = %d\n\r", vBat); |
Germaint | 34:fb1b9c5e5cf3 | 257 | #endif |
Germaint | 34:fb1b9c5e5cf3 | 258 | } |