Plant Monitoring Project

Dependencies:   mbed SHT21_ncleee WakeUp SSD1306 DHT Adafruit_TCS34725 DS1820

Committer:
Germaint
Date:
Mon Oct 21 08:38:11 2019 +0000
Revision:
15:c73d0d180cc4
Parent:
5:256a143ab0c2
Child:
16:13364798fce6
Programme qui fonctionne

Who changed what in which revision?

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