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