Grreenhouse project
Dependencies: DHT DS1820 publishhhhh SSD1306 TSL2561_I2C mbed
Fork of Seeed_Grove_Moisture_Sensor_Example by
main.cpp@3:201cb66d7bd5, 2018-01-30 (annotated)
- Committer:
- cricbis
- Date:
- Tue Jan 30 00:43:25 2018 +0000
- Revision:
- 3:201cb66d7bd5
- Parent:
- 2:72e1b9689366
greenhouse_project
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cricbis | 1:280672cfee6b | 1 | #include "mbed.h" |
cricbis | 1:280672cfee6b | 2 | #include <stdint.h> |
sam_grove | 0:780321a3f63a | 3 | |
cricbis | 2:72e1b9689366 | 4 | #include "DHT.h" //1.Temp & Hum Sensor White Block -- DHT22 --> A3 |
cricbis | 2:72e1b9689366 | 5 | #include "DS1820.h" //2.One Wire Temperature Sensor -- DS18B20 --> A1 |
cricbis | 2:72e1b9689366 | 6 | //3.Grove Moisture :: no library --> A0 |
cricbis | 2:72e1b9689366 | 7 | #include "TSL2561_I2C.h" //4.Flat Light Sensor -- TSL2561 --> SDA : D4, SCL : D5 |
cricbis | 2:72e1b9689366 | 8 | |
cricbis | 2:72e1b9689366 | 9 | #include "bold_font.h" // 6. lcd OLDE Display 128x64 |
cricbis | 2:72e1b9689366 | 10 | #include "standard_font.h" // 6. lcd OLDE Display 128x64 |
cricbis | 2:72e1b9689366 | 11 | #include "ssd1306.h" // 6. lcd OLDE Display 128x64 |
cricbis | 2:72e1b9689366 | 12 | |
cricbis | 2:72e1b9689366 | 13 | Serial pc(SERIAL_TX, SERIAL_RX); |
cricbis | 1:280672cfee6b | 14 | |
cricbis | 3:201cb66d7bd5 | 15 | //1. White block DHT22 |
cricbis | 1:280672cfee6b | 16 | DHT sensor(A3,DHT22); |
cricbis | 1:280672cfee6b | 17 | void task_DHT(); |
cricbis | 2:72e1b9689366 | 18 | int air_hum, air_temp,farenheit; |
cricbis | 2:72e1b9689366 | 19 | float dp = 0.0f; |
cricbis | 1:280672cfee6b | 20 | |
cricbis | 2:72e1b9689366 | 21 | //6.OLED |
cricbis | 3:201cb66d7bd5 | 22 | SSD1306 ecran(D3 /* cs */, D6/* reset */, A5 /* dc */, A4 /* clock */, D2 /* data */); // original |
cricbis | 3:201cb66d7bd5 | 23 | //SSD1306 ecran(D7 /* cs */, D4/* reset */, A5 /* dc */, A4 /* clock */, D8 /* data */); |
cricbis | 1:280672cfee6b | 24 | |
cricbis | 1:280672cfee6b | 25 | //2. One Wire Temperature DS18B20 |
cricbis | 1:280672cfee6b | 26 | #define DATA_PIN A1 |
cricbis | 1:280672cfee6b | 27 | #define MAX_PROBES 16 |
cricbis | 1:280672cfee6b | 28 | DS1820* probe[MAX_PROBES]; |
cricbis | 2:72e1b9689366 | 29 | float gnd_temp; |
sam_grove | 0:780321a3f63a | 30 | |
cricbis | 1:280672cfee6b | 31 | //3. Grove Moisture |
sam_grove | 0:780321a3f63a | 32 | AnalogIn moisture(A0); |
cricbis | 2:72e1b9689366 | 33 | float gnd_hum = 0.0f; |
sam_grove | 0:780321a3f63a | 34 | |
cricbis | 3:201cb66d7bd5 | 35 | //4. Flat Light Sensor TSL2561 |
cricbis | 3:201cb66d7bd5 | 36 | TSL2561_I2C lum_sensor( D0, D1 );//SDA, SCL |
cricbis | 3:201cb66d7bd5 | 37 | //TSL2561_I2C lum_sensor( D3, D2 );//SDA, SCL |
cricbis | 2:72e1b9689366 | 38 | float float_air_light; |
cricbis | 2:72e1b9689366 | 39 | int air_light ; |
cricbis | 2:72e1b9689366 | 40 | |
cricbis | 2:72e1b9689366 | 41 | //5. Sigfox |
cricbis | 3:201cb66d7bd5 | 42 | Serial sigfox(D1, D0); //Rx Sigfox, Tx Sigfox |
cricbis | 2:72e1b9689366 | 43 | void txData(int a ,int b,int c,int d,int e) |
cricbis | 2:72e1b9689366 | 44 | { |
cricbis | 2:72e1b9689366 | 45 | //air_light ; air_temp ; air_hum ; gnd_temp ; gnd_hum |
cricbis | 2:72e1b9689366 | 46 | pc.printf("Envoie sur le Sigfox : AT$SS:%d %d %d %d %d\r\n",a,b,c,d,e); |
cricbis | 2:72e1b9689366 | 47 | //sigfox.printf("AT$SS=%d",valeur); |
cricbis | 2:72e1b9689366 | 48 | sigfox.printf("AT$SS=%02x %02x %02x %02x %02x\r\n", a,b,c,d,e); |
cricbis | 2:72e1b9689366 | 49 | } |
cricbis | 1:280672cfee6b | 50 | |
cricbis | 1:280672cfee6b | 51 | |
sam_grove | 0:780321a3f63a | 52 | int main(void) |
sam_grove | 0:780321a3f63a | 53 | { |
cricbis | 2:72e1b9689366 | 54 | printf("\r\n\nHello World!\r\n"); |
cricbis | 1:280672cfee6b | 55 | |
cricbis | 1:280672cfee6b | 56 | //1. Temp & Humidity sensor -- White block |
cricbis | 1:280672cfee6b | 57 | //No more init |
cricbis | 1:280672cfee6b | 58 | |
cricbis | 1:280672cfee6b | 59 | //2. Temperature Probe -- One Wire Temperature Sensor |
cricbis | 1:280672cfee6b | 60 | int num_devices = 0; |
cricbis | 1:280672cfee6b | 61 | while(DS1820::unassignedProbe(DATA_PIN)) { |
cricbis | 1:280672cfee6b | 62 | probe[num_devices] = new DS1820(DATA_PIN); |
cricbis | 1:280672cfee6b | 63 | num_devices++; |
cricbis | 1:280672cfee6b | 64 | if (num_devices == MAX_PROBES) break; |
cricbis | 1:280672cfee6b | 65 | } |
cricbis | 2:72e1b9689366 | 66 | printf("%d probe found\r\n", num_devices); |
cricbis | 1:280672cfee6b | 67 | |
cricbis | 1:280672cfee6b | 68 | //3. Grove Moisture |
cricbis | 1:280672cfee6b | 69 | |
cricbis | 1:280672cfee6b | 70 | //4. |
cricbis | 1:280672cfee6b | 71 | lum_sensor.enablePower(); |
cricbis | 2:72e1b9689366 | 72 | |
cricbis | 2:72e1b9689366 | 73 | //6. |
cricbis | 2:72e1b9689366 | 74 | /* lcd.speed (SSD1306::Medium); // set working frequency |
cricbis | 2:72e1b9689366 | 75 | lcd.init(); // initialize SSD1306 |
cricbis | 2:72e1b9689366 | 76 | lcd.cls(); // clear frame buffer |
cricbis | 2:72e1b9689366 | 77 | lcd.locate (3,1); // set text cursor to line 3, column 1 |
cricbis | 2:72e1b9689366 | 78 | lcd.printf ("Hello, world!"); // print to frame buffer |
cricbis | 2:72e1b9689366 | 79 | lcd.line ( 6, 22, 114, 22, SSD1306::Normal); // |
cricbis | 2:72e1b9689366 | 80 | lcd.line (114, 22, 114, 33, SSD1306::Normal); // Surrounds text with |
cricbis | 2:72e1b9689366 | 81 | lcd.line (114, 33, 6, 33, SSD1306::Normal); // a rectangle |
cricbis | 2:72e1b9689366 | 82 | lcd.line ( 6, 33, 6, 22, SSD1306::Normal); // |
cricbis | 2:72e1b9689366 | 83 | lcd.fill (0, 0); // fills screen outside rectangle |
cricbis | 2:72e1b9689366 | 84 | lcd.redraw(); // updates actual display transferring frame buffer over I2C bus |
cricbis | 2:72e1b9689366 | 85 | */ |
cricbis | 2:72e1b9689366 | 86 | |
cricbis | 2:72e1b9689366 | 87 | |
cricbis | 2:72e1b9689366 | 88 | ecran.initialise(); |
cricbis | 2:72e1b9689366 | 89 | ecran.clear(); |
cricbis | 2:72e1b9689366 | 90 | ecran.set_contrast(255); |
cricbis | 2:72e1b9689366 | 91 | ecran.set_font(bold_font, 8); |
cricbis | 2:72e1b9689366 | 92 | |
cricbis | 1:280672cfee6b | 93 | // Nothing else |
cricbis | 1:280672cfee6b | 94 | |
cricbis | 1:280672cfee6b | 95 | |
cricbis | 2:72e1b9689366 | 96 | printf("Inits ended"); |
sam_grove | 0:780321a3f63a | 97 | while(1) { |
cricbis | 1:280672cfee6b | 98 | printf("\r\n\n\nNew loop\r\n\n"); |
cricbis | 1:280672cfee6b | 99 | |
cricbis | 1:280672cfee6b | 100 | //1. Temp & Humidity sensor -- White block |
cricbis | 1:280672cfee6b | 101 | printf("DHT22 White Block : \r\n"); |
cricbis | 1:280672cfee6b | 102 | task_DHT(); |
cricbis | 1:280672cfee6b | 103 | |
cricbis | 1:280672cfee6b | 104 | //2. Temperature Probe -- One Wire Temperature Sensor |
cricbis | 1:280672cfee6b | 105 | printf("DS18B20 Probe : \r\n"); |
cricbis | 1:280672cfee6b | 106 | probe[0]->convertTemperature(true, DS1820::all_devices); //Start temperature conversion, wait until ready |
cricbis | 1:280672cfee6b | 107 | for (int i = 0; i<num_devices; i++){ |
cricbis | 2:72e1b9689366 | 108 | gnd_temp = probe[i]->temperature(); |
cricbis | 2:72e1b9689366 | 109 | printf("Temperature : %3.1f oC\r\n\n", gnd_temp); |
cricbis | 1:280672cfee6b | 110 | } |
cricbis | 1:280672cfee6b | 111 | |
cricbis | 1:280672cfee6b | 112 | //3. Grove Moisture |
cricbis | 2:72e1b9689366 | 113 | gnd_hum = moisture; |
cricbis | 2:72e1b9689366 | 114 | gnd_hum *= 100 ; // resultat |
cricbis | 2:72e1b9689366 | 115 | printf("Grove Moisture : \r\nMoisture reading is %2.2f\r\n\n", gnd_hum); |
cricbis | 1:280672cfee6b | 116 | |
cricbis | 1:280672cfee6b | 117 | //4. TSL2561 Flat Light Sensor |
cricbis | 2:72e1b9689366 | 118 | float_air_light = lum_sensor.getLux(); |
cricbis | 2:72e1b9689366 | 119 | air_light = (int)float_air_light ; |
cricbis | 3:201cb66d7bd5 | 120 | printf( "TSL2561 :\r\nLuminosity: %4.2f float\r\n\n", float_air_light ); |
cricbis | 2:72e1b9689366 | 121 | printf( "TSL2561 :\r\nLuminosity: %d int\r\n\n", air_light ); |
cricbis | 2:72e1b9689366 | 122 | //printf( "TSL2561 :\r\nLuminosity: %d\r\n\n", air_light ); |
cricbis | 3:201cb66d7bd5 | 123 | //int air_light = 1 ; |
cricbis | 1:280672cfee6b | 124 | |
cricbis | 1:280672cfee6b | 125 | |
cricbis | 2:72e1b9689366 | 126 | //5.Transmission data par Sigfox |
cricbis | 2:72e1b9689366 | 127 | txData((int)air_light,(int)air_temp,(int)air_hum,(int)gnd_temp,(int)gnd_hum); |
cricbis | 1:280672cfee6b | 128 | |
cricbis | 2:72e1b9689366 | 129 | //6.Ecriture sur lcd OLED |
cricbis | 2:72e1b9689366 | 130 | /* unsigned char level = 255*moisture.read(); // reads pot. Scales to 0-255 |
cricbis | 2:72e1b9689366 | 131 | lcd.set_contrast (level); // set contrast |
cricbis | 2:72e1b9689366 | 132 | printf("\r\nWriting on lcd done!\r\n"); |
cricbis | 2:72e1b9689366 | 133 | */ |
cricbis | 2:72e1b9689366 | 134 | |
cricbis | 2:72e1b9689366 | 135 | ecran.set_font(bold_font, 8); |
cricbis | 2:72e1b9689366 | 136 | ecran.printf("Valeurs"); |
cricbis | 2:72e1b9689366 | 137 | ecran.printf("\r\n"); |
cricbis | 2:72e1b9689366 | 138 | ecran.update(); |
cricbis | 2:72e1b9689366 | 139 | ecran.set_font(standard_font, 6); |
cricbis | 2:72e1b9689366 | 140 | ecran.printf("Air Lum = %d", (int)air_light); |
cricbis | 2:72e1b9689366 | 141 | ecran.printf("\r\n"); |
cricbis | 2:72e1b9689366 | 142 | ecran.printf("Air Temp = %d", (int)air_temp); |
cricbis | 2:72e1b9689366 | 143 | ecran.printf("\r\n"); |
cricbis | 2:72e1b9689366 | 144 | ecran.printf("Ait Hum = %d", (int)air_hum); |
cricbis | 2:72e1b9689366 | 145 | ecran.printf("\r\n"); |
cricbis | 2:72e1b9689366 | 146 | ecran.printf("Gnd Temp = %d", (int)gnd_temp); |
cricbis | 2:72e1b9689366 | 147 | ecran.printf("\r\n"); |
cricbis | 2:72e1b9689366 | 148 | ecran.printf("Gnd Hum = %d", (int)gnd_hum); |
cricbis | 2:72e1b9689366 | 149 | ecran.printf("\r\n"); |
cricbis | 2:72e1b9689366 | 150 | ecran.update(); |
cricbis | 2:72e1b9689366 | 151 | ecran.clear(); |
cricbis | 2:72e1b9689366 | 152 | |
cricbis | 2:72e1b9689366 | 153 | wait(10000); // 1 lecture toutes les 30 secondes |
cricbis | 1:280672cfee6b | 154 | |
sam_grove | 0:780321a3f63a | 155 | } |
cricbis | 1:280672cfee6b | 156 | |
cricbis | 1:280672cfee6b | 157 | |
sam_grove | 0:780321a3f63a | 158 | } |
cricbis | 1:280672cfee6b | 159 | |
cricbis | 2:72e1b9689366 | 160 | /********************************************** |
cricbis | 2:72e1b9689366 | 161 | Transmission max de la sigfox : 240 octets/jour |
cricbis | 2:72e1b9689366 | 162 | ***********************************************/ |
cricbis | 1:280672cfee6b | 163 | |
cricbis | 1:280672cfee6b | 164 | |
cricbis | 1:280672cfee6b | 165 | |
cricbis | 1:280672cfee6b | 166 | |
cricbis | 1:280672cfee6b | 167 | //1. |
cricbis | 1:280672cfee6b | 168 | void task_DHT(){ |
cricbis | 1:280672cfee6b | 169 | int error = 0; |
cricbis | 1:280672cfee6b | 170 | |
cricbis | 1:280672cfee6b | 171 | wait(2.0f); |
cricbis | 1:280672cfee6b | 172 | error = sensor.readData(); |
cricbis | 1:280672cfee6b | 173 | if (0 == error) { |
cricbis | 2:72e1b9689366 | 174 | air_temp = sensor.ReadTemperature(CELCIUS); |
cricbis | 2:72e1b9689366 | 175 | farenheit = sensor.ReadTemperature(FARENHEIT); |
cricbis | 1:280672cfee6b | 176 | air_hum = sensor.ReadHumidity(); |
cricbis | 2:72e1b9689366 | 177 | dp = sensor.CalcdewPoint(air_temp, air_hum); |
cricbis | 1:280672cfee6b | 178 | |
cricbis | 2:72e1b9689366 | 179 | printf("Air Temperature : %d oC\r\n", air_temp); |
cricbis | 2:72e1b9689366 | 180 | printf("Air Humidity %d /100\r\nDewpoint: %4.2f\r\n\n", air_hum, dp); |
cricbis | 1:280672cfee6b | 181 | } else { |
cricbis | 1:280672cfee6b | 182 | printf("Error: %d\r\n", error); |
cricbis | 1:280672cfee6b | 183 | } |
cricbis | 1:280672cfee6b | 184 | } |
cricbis | 1:280672cfee6b | 185 | |
cricbis | 1:280672cfee6b | 186 |