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