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