Plant Monitoring Project
Dependencies: mbed SHT21_ncleee WakeUp SSD1306 DHT Adafruit_TCS34725 DS1820
main.cpp@16:13364798fce6, 2019-10-22 (annotated)
- Committer:
- Germaint
- Date:
- Tue Oct 22 14:33:22 2019 +0000
- Revision:
- 16:13364798fce6
- Parent:
- 15:c73d0d180cc4
- Child:
- 17:7d07f5f0f033
t;
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 | 2:0bfe25431e8e | 7 | //Serial pc(SERIAL_TX, SERIAL_RX); |
Germaint | 1:3fc11a745984 | 8 | Serial nucleo(D1,D0); |
Germaint | 2:0bfe25431e8e | 9 | |
Germaint | 15:c73d0d180cc4 | 10 | I2C i2c(D4, D5); |
Germaint | 16:13364798fce6 | 11 | SSD1306 oled(D12, A6); |
jufray | 0:e030be8f0310 | 12 | |
Germaint | 1:3fc11a745984 | 13 | // capteur temperature sol |
Germaint | 1:3fc11a745984 | 14 | DS1820 DS(D3); |
jufray | 0:e030be8f0310 | 15 | |
Germaint | 1:3fc11a745984 | 16 | // capteur humidité sol |
Germaint | 1:3fc11a745984 | 17 | AnalogIn capteur_humidity_sol(A0); |
jufray | 0:e030be8f0310 | 18 | |
Germaint | 1:3fc11a745984 | 19 | // capteur humidité + température air |
jufray | 0:e030be8f0310 | 20 | SHT21 sht(&i2c); |
jufray | 0:e030be8f0310 | 21 | |
Germaint | 1:3fc11a745984 | 22 | // capteur RGB |
ludollaoo | 5:256a143ab0c2 | 23 | Adafruit_TCS34725 RGBsens = Adafruit_TCS34725(&i2c, TCS34725_INTEGRATIONTIME_154MS, TCS34725_GAIN_1X); |
jufray | 0:e030be8f0310 | 24 | |
Germaint | 1:3fc11a745984 | 25 | // capteur lumière |
Germaint | 1:3fc11a745984 | 26 | AnalogIn ain(A1); |
jufray | 0:e030be8f0310 | 27 | |
Germaint | 1:3fc11a745984 | 28 | // Définition de fonctions |
Germaint | 1:3fc11a745984 | 29 | float temp_sol(void); |
Germaint | 1:3fc11a745984 | 30 | int fct_humidity_sol(void); |
ludollaoo | 4:d7e305e06e1a | 31 | void fct_RGB(unsigned char *pr, unsigned char *pg, unsigned char *pb, unsigned short *lux); |
Germaint | 16:13364798fce6 | 32 | void sendDataSigfox(void); |
Germaint | 2:0bfe25431e8e | 33 | void oledData(void); |
Germaint | 16:13364798fce6 | 34 | void readData(void); |
jufray | 0:e030be8f0310 | 35 | |
Germaint | 1:3fc11a745984 | 36 | |
Germaint | 16:13364798fce6 | 37 | float temperature_sol; |
Germaint | 16:13364798fce6 | 38 | unsigned char humidity_sol; |
Germaint | 16:13364798fce6 | 39 | float temperature_air; |
Germaint | 16:13364798fce6 | 40 | unsigned char humidity_air; |
Germaint | 16:13364798fce6 | 41 | unsigned char pr, pg, pb; |
Germaint | 16:13364798fce6 | 42 | unsigned short lum; |
jufray | 0:e030be8f0310 | 43 | |
jufray | 0:e030be8f0310 | 44 | |
Germaint | 1:3fc11a745984 | 45 | int main() { |
Germaint | 16:13364798fce6 | 46 | oled.speed (SSD1306::Medium); |
Germaint | 16:13364798fce6 | 47 | oled.init(); |
Germaint | 16:13364798fce6 | 48 | oled.cls(0,1); |
Germaint | 2:0bfe25431e8e | 49 | oled.set_contrast(200); |
Germaint | 1:3fc11a745984 | 50 | |
jufray | 0:e030be8f0310 | 51 | while(1) { |
jufray | 0:e030be8f0310 | 52 | |
Germaint | 16:13364798fce6 | 53 | readData(); |
Germaint | 2:0bfe25431e8e | 54 | oledData(); |
Germaint | 16:13364798fce6 | 55 | wait(3); |
jufray | 0:e030be8f0310 | 56 | } |
jufray | 0:e030be8f0310 | 57 | } |
jufray | 0:e030be8f0310 | 58 | |
jufray | 0:e030be8f0310 | 59 | |
jufray | 0:e030be8f0310 | 60 | float temp_sol() |
jufray | 0:e030be8f0310 | 61 | { |
jufray | 0:e030be8f0310 | 62 | DS.convertTemperature(true, DS1820::all_devices); |
jufray | 0:e030be8f0310 | 63 | if (DS.unassignedProbe(D3)){ |
Germaint | 2:0bfe25431e8e | 64 | //pc.printf( "D3 not assigned\n\r"); |
jufray | 0:e030be8f0310 | 65 | } |
jufray | 0:e030be8f0310 | 66 | return DS.temperature(); |
jufray | 0:e030be8f0310 | 67 | } |
jufray | 0:e030be8f0310 | 68 | |
Germaint | 1:3fc11a745984 | 69 | int fct_humidity_sol(void) |
jufray | 0:e030be8f0310 | 70 | { |
jufray | 0:e030be8f0310 | 71 | float val_min = 0.377; |
jufray | 0:e030be8f0310 | 72 | float val_max = 0.772; |
jufray | 0:e030be8f0310 | 73 | float mesure, mesure_etalonnee; |
Germaint | 1:3fc11a745984 | 74 | mesure = capteur_humidity_sol.read(); |
jufray | 0:e030be8f0310 | 75 | mesure_etalonnee = (1-((mesure - val_min)/(val_max - val_min)))*100; |
jufray | 0:e030be8f0310 | 76 | return (int) mesure_etalonnee; |
jufray | 0:e030be8f0310 | 77 | } |
jufray | 0:e030be8f0310 | 78 | |
Germaint | 3:fc704c1d3087 | 79 | void fct_RGB(unsigned char *pr, unsigned char *pg, unsigned char *pb, unsigned short *lux) |
jufray | 0:e030be8f0310 | 80 | { |
jufray | 0:e030be8f0310 | 81 | int somme; |
jufray | 0:e030be8f0310 | 82 | uint16_t clear, red, green, blue; |
jufray | 0:e030be8f0310 | 83 | if (!RGBsens.begin()) |
jufray | 0:e030be8f0310 | 84 | { |
Germaint | 2:0bfe25431e8e | 85 | //pc.printf("No TCS34725 found ... check your connections"); |
Germaint | 15:c73d0d180cc4 | 86 | //while (1); // halt! |
jufray | 0:e030be8f0310 | 87 | } |
jufray | 0:e030be8f0310 | 88 | RGBsens.getRawData(&red, &green, &blue, &clear); |
jufray | 0:e030be8f0310 | 89 | somme = red + green + blue; |
jufray | 0:e030be8f0310 | 90 | *pr = red*100/somme; |
jufray | 0:e030be8f0310 | 91 | *pg = green*100/somme; |
jufray | 0:e030be8f0310 | 92 | *pb = blue*100/somme; |
Germaint | 3:fc704c1d3087 | 93 | *lux = clear; |
jufray | 0:e030be8f0310 | 94 | } |
jufray | 0:e030be8f0310 | 95 | |
Germaint | 16:13364798fce6 | 96 | void sendDataSigfox(void){ |
Germaint | 16:13364798fce6 | 97 | short tempSol_short, tempAir_short; |
Germaint | 16:13364798fce6 | 98 | tempSol_short = (short)(temperature_sol*10); |
Germaint | 16:13364798fce6 | 99 | tempAir_short = (short)(temperature_air*10); |
jufray | 0:e030be8f0310 | 100 | |
Germaint | 16:13364798fce6 | 101 | 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 | 102 | } |
Germaint | 2:0bfe25431e8e | 103 | |
Germaint | 2:0bfe25431e8e | 104 | void oledData(void){ |
Germaint | 16:13364798fce6 | 105 | oled.cls(0,1); |
Germaint | 2:0bfe25431e8e | 106 | oled.locate(0,0); |
Germaint | 2:0bfe25431e8e | 107 | oled.printf("AIR T : %.1f", temperature_air); |
Germaint | 2:0bfe25431e8e | 108 | oled.locate(1,0); |
Germaint | 2:0bfe25431e8e | 109 | oled.printf("AIR H : %d", humidity_air); |
Germaint | 2:0bfe25431e8e | 110 | oled.locate(3,0); |
Germaint | 2:0bfe25431e8e | 111 | oled.printf("FLOOR T : %.1f", temperature_sol); |
Germaint | 2:0bfe25431e8e | 112 | oled.locate(4,0); |
Germaint | 2:0bfe25431e8e | 113 | oled.printf("FLOOR H : %d", humidity_sol); |
Germaint | 2:0bfe25431e8e | 114 | oled.locate(6,0); |
Germaint | 2:0bfe25431e8e | 115 | oled.printf("Light : %d", lum); |
Germaint | 2:0bfe25431e8e | 116 | oled.locate(7,0); |
Germaint | 2:0bfe25431e8e | 117 | oled.printf("R %d G %d B %d", pr, pg, pb); |
Germaint | 2:0bfe25431e8e | 118 | |
Germaint | 2:0bfe25431e8e | 119 | oled.redraw(); |
Germaint | 2:0bfe25431e8e | 120 | } |
jufray | 0:e030be8f0310 | 121 | |
Germaint | 16:13364798fce6 | 122 | void readData(void){ |
Germaint | 16:13364798fce6 | 123 | temperature_sol = temp_sol(); |
Germaint | 16:13364798fce6 | 124 | humidity_sol = fct_humidity_sol(); |
Germaint | 16:13364798fce6 | 125 | temperature_air = sht.readTemp(); |
Germaint | 16:13364798fce6 | 126 | humidity_air = sht.readHumidity(); |
Germaint | 16:13364798fce6 | 127 | fct_RGB(&pr, &pg, &pb, &lum); |
Germaint | 16:13364798fce6 | 128 | sendDataSigfox(); |
Germaint | 16:13364798fce6 | 129 | } |
jufray | 0:e030be8f0310 | 130 | |
Germaint | 16:13364798fce6 | 131 |