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