Plant Monitoring Project

Dependencies:   mbed SHT21_ncleee WakeUp SSD1306 DHT Adafruit_TCS34725 DS1820

Committer:
Germaint
Date:
Tue Oct 08 14:38:36 2019 +0000
Revision:
3:fc704c1d3087
Parent:
2:0bfe25431e8e
Child:
4:d7e305e06e1a
Changement capteur lux; ;

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 2:0bfe25431e8e 7 #define I2C_SDA D4
Germaint 2:0bfe25431e8e 8 #define I2C_SCL D5
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 2:0bfe25431e8e 13 I2C i2c(I2C_SDA, I2C_SCL);
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
jufray 0:e030be8f0310 26 Adafruit_TCS34725 RGBsens = Adafruit_TCS34725(&i2c, TCS34725_INTEGRATIONTIME_2_4MS, TCS34725_GAIN_16X);
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);
Germaint 3:fc704c1d3087 34 void fct_RGB(unsigned char *pr, unsigned char *pg, unsigned char *pb, unsigned short *lux);
Germaint 1:3fc11a745984 35 float fct_lumiere(void);
Germaint 1:3fc11a745984 36 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 37 void oledData(void);
jufray 0:e030be8f0310 38
Germaint 1:3fc11a745984 39
Germaint 1:3fc11a745984 40 float temperature_sol;
Germaint 1:3fc11a745984 41 unsigned char humidity_sol;
Germaint 1:3fc11a745984 42 float temperature_air;
Germaint 1:3fc11a745984 43 unsigned char humidity_air;
jufray 0:e030be8f0310 44 unsigned char pr, pg, pb;
jufray 0:e030be8f0310 45 unsigned short lum;
jufray 0:e030be8f0310 46
jufray 0:e030be8f0310 47
Germaint 1:3fc11a745984 48 int main() {
Germaint 2:0bfe25431e8e 49 oled.speed (SSD1306::Medium); // set working frequency
Germaint 2:0bfe25431e8e 50 oled.init(); // initialize SSD1306
Germaint 2:0bfe25431e8e 51 oled.cls(); // clear frame buffer
Germaint 2:0bfe25431e8e 52 oled.set_contrast(200);
Germaint 1:3fc11a745984 53
jufray 0:e030be8f0310 54 while(1) {
jufray 0:e030be8f0310 55
Germaint 1:3fc11a745984 56 temperature_sol = temp_sol();
Germaint 2:0bfe25431e8e 57 //pc.printf("T sol = %.2f\n\r", temperature_sol);
jufray 0:e030be8f0310 58
Germaint 1:3fc11a745984 59 humidity_sol = fct_humidity_sol();
Germaint 2:0bfe25431e8e 60 //pc.printf("H sol = %d\n\r", humidity_sol);
jufray 0:e030be8f0310 61
Germaint 1:3fc11a745984 62 temperature_air = sht.readTemp();
Germaint 2:0bfe25431e8e 63 //pc.printf("temperature_air = %.2f\n\r", temperature_air);
Germaint 2:0bfe25431e8e 64
Germaint 1:3fc11a745984 65 humidity_air = sht.readHumidity();
Germaint 2:0bfe25431e8e 66 //pc.printf("humidity_air = %d\n\r", humidity_air);
jufray 0:e030be8f0310 67
Germaint 3:fc704c1d3087 68 fct_RGB(&pr, &pg, &pb, &lum);
Germaint 2:0bfe25431e8e 69 //pc.printf("red=%d green=%d blue=%d\n\r", pr, pg, pb);
jufray 0:e030be8f0310 70
Germaint 3:fc704c1d3087 71 //lum = fct_lumiere();
Germaint 2:0bfe25431e8e 72 //pc.printf("lumiere = %d\n\r", lum);
jufray 0:e030be8f0310 73
Germaint 2:0bfe25431e8e 74 oledData();
Germaint 1:3fc11a745984 75 sendDataSigfox(temperature_sol, &humidity_sol, temperature_air, &humidity_air, &lum, &pr, &pg, &pb);
jufray 0:e030be8f0310 76
Germaint 2:0bfe25431e8e 77 //pc.printf("\n\r");
Germaint 2:0bfe25431e8e 78 wait(10);
jufray 0:e030be8f0310 79 }
jufray 0:e030be8f0310 80 }
jufray 0:e030be8f0310 81
jufray 0:e030be8f0310 82
jufray 0:e030be8f0310 83 float temp_sol()
jufray 0:e030be8f0310 84 {
jufray 0:e030be8f0310 85 DS.convertTemperature(true, DS1820::all_devices);
jufray 0:e030be8f0310 86 if (DS.unassignedProbe(D3)){
Germaint 2:0bfe25431e8e 87 //pc.printf( "D3 not assigned\n\r");
jufray 0:e030be8f0310 88 }
jufray 0:e030be8f0310 89 return DS.temperature();
jufray 0:e030be8f0310 90 }
jufray 0:e030be8f0310 91
Germaint 1:3fc11a745984 92 int fct_humidity_sol(void)
jufray 0:e030be8f0310 93 {
jufray 0:e030be8f0310 94 float val_min = 0.377;
jufray 0:e030be8f0310 95 float val_max = 0.772;
jufray 0:e030be8f0310 96 float mesure, mesure_etalonnee;
Germaint 1:3fc11a745984 97 mesure = capteur_humidity_sol.read();
jufray 0:e030be8f0310 98 mesure_etalonnee = (1-((mesure - val_min)/(val_max - val_min)))*100;
jufray 0:e030be8f0310 99 return (int) mesure_etalonnee;
jufray 0:e030be8f0310 100 }
jufray 0:e030be8f0310 101
Germaint 3:fc704c1d3087 102 void fct_RGB(unsigned char *pr, unsigned char *pg, unsigned char *pb, unsigned short *lux)
jufray 0:e030be8f0310 103 {
jufray 0:e030be8f0310 104 int somme;
jufray 0:e030be8f0310 105 uint16_t clear, red, green, blue;
jufray 0:e030be8f0310 106 if (!RGBsens.begin())
jufray 0:e030be8f0310 107 {
Germaint 2:0bfe25431e8e 108 //pc.printf("No TCS34725 found ... check your connections");
jufray 0:e030be8f0310 109 while (1); // halt!
jufray 0:e030be8f0310 110 }
jufray 0:e030be8f0310 111 RGBsens.getRawData(&red, &green, &blue, &clear);
jufray 0:e030be8f0310 112 somme = red + green + blue;
jufray 0:e030be8f0310 113 *pr = red*100/somme;
jufray 0:e030be8f0310 114 *pg = green*100/somme;
jufray 0:e030be8f0310 115 *pb = blue*100/somme;
Germaint 3:fc704c1d3087 116 *lux = clear;
jufray 0:e030be8f0310 117 }
jufray 0:e030be8f0310 118
jufray 0:e030be8f0310 119 float fct_lumiere()
jufray 0:e030be8f0310 120 {
Germaint 3:fc704c1d3087 121 float valueIn, logLux;
jufray 0:e030be8f0310 122 float logRange = 5.0; // 3.3v = 10^5 lux
Germaint 3:fc704c1d3087 123
Germaint 3:fc704c1d3087 124 valueIn = ain.read();
Germaint 3:fc704c1d3087 125 logLux = valueIn * logRange;
Germaint 3:fc704c1d3087 126 return pow(10, logLux);
jufray 0:e030be8f0310 127 }
jufray 0:e030be8f0310 128
jufray 0:e030be8f0310 129
jufray 0:e030be8f0310 130 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 131 short tempSol_short, tempAir_short;
jufray 0:e030be8f0310 132 tempSol *= 10;
jufray 0:e030be8f0310 133 tempAir *= 10;
jufray 0:e030be8f0310 134 tempSol_short = (short) tempSol;
jufray 0:e030be8f0310 135 tempAir_short = (short) tempAir;
jufray 0:e030be8f0310 136
jufray 0:e030be8f0310 137 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 138 }
Germaint 2:0bfe25431e8e 139
Germaint 2:0bfe25431e8e 140 void oledData(void){
Germaint 2:0bfe25431e8e 141 oled.cls();
Germaint 2:0bfe25431e8e 142 oled.locate(0,0);
Germaint 2:0bfe25431e8e 143 oled.printf("AIR T : %.1f", temperature_air);
Germaint 2:0bfe25431e8e 144 oled.locate(1,0);
Germaint 2:0bfe25431e8e 145 oled.printf("AIR H : %d", humidity_air);
Germaint 2:0bfe25431e8e 146 oled.locate(3,0);
Germaint 2:0bfe25431e8e 147 oled.printf("FLOOR T : %.1f", temperature_sol);
Germaint 2:0bfe25431e8e 148 oled.locate(4,0);
Germaint 2:0bfe25431e8e 149 oled.printf("FLOOR H : %d", humidity_sol);
Germaint 2:0bfe25431e8e 150 oled.locate(6,0);
Germaint 2:0bfe25431e8e 151 oled.printf("Light : %d", lum);
Germaint 2:0bfe25431e8e 152 oled.locate(7,0);
Germaint 2:0bfe25431e8e 153 oled.printf("R %d G %d B %d", pr, pg, pb);
Germaint 2:0bfe25431e8e 154
Germaint 2:0bfe25431e8e 155 oled.redraw();
Germaint 2:0bfe25431e8e 156 }
jufray 0:e030be8f0310 157
jufray 0:e030be8f0310 158