Strawberry forever temperature and humidity sensor controlling fan
Dependencies: mbed C12832 DHT DHT11
main.cpp@0:01f3ce729f40, 2021-10-12 (annotated)
- Committer:
- tollidal
- Date:
- Tue Oct 12 19:26:01 2021 +0000
- Revision:
- 0:01f3ce729f40
- Child:
- 1:c33171654467
mbed LPC1768 Grove Temp Sensor;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tollidal | 0:01f3ce729f40 | 1 | #include "mbed.h" |
tollidal | 0:01f3ce729f40 | 2 | #include "C12832.h" |
tollidal | 0:01f3ce729f40 | 3 | #include "DHT.h" |
tollidal | 0:01f3ce729f40 | 4 | |
tollidal | 0:01f3ce729f40 | 5 | |
tollidal | 0:01f3ce729f40 | 6 | C12832 lcd(p5, p7, p6, p8, p11); //LCD screen |
tollidal | 0:01f3ce729f40 | 7 | DHT sensor(p20,DHT11); // Use the SEN11301P sensor |
tollidal | 0:01f3ce729f40 | 8 | DigitalOut fan(p18); |
tollidal | 0:01f3ce729f40 | 9 | |
tollidal | 0:01f3ce729f40 | 10 | int main() |
tollidal | 0:01f3ce729f40 | 11 | { |
tollidal | 0:01f3ce729f40 | 12 | |
tollidal | 0:01f3ce729f40 | 13 | //welcome message on LCD |
tollidal | 0:01f3ce729f40 | 14 | |
tollidal | 0:01f3ce729f40 | 15 | lcd.cls(); |
tollidal | 0:01f3ce729f40 | 16 | lcd.locate(0,0); |
tollidal | 0:01f3ce729f40 | 17 | lcd.printf("Hello user"); |
tollidal | 0:01f3ce729f40 | 18 | lcd.locate(0,10); |
tollidal | 0:01f3ce729f40 | 19 | lcd.printf("Greenhouse Control System"); |
tollidal | 0:01f3ce729f40 | 20 | wait(2.5); |
tollidal | 0:01f3ce729f40 | 21 | |
tollidal | 0:01f3ce729f40 | 22 | lcd.cls(); //clear LCD |
tollidal | 0:01f3ce729f40 | 23 | |
tollidal | 0:01f3ce729f40 | 24 | //infinte loop to check temperature after welcome message |
tollidal | 0:01f3ce729f40 | 25 | |
tollidal | 0:01f3ce729f40 | 26 | |
tollidal | 0:01f3ce729f40 | 27 | int error = 0; |
tollidal | 0:01f3ce729f40 | 28 | float h = 0.0f, c = 0.0f, f = 0.0f, k = 0.0f, dp = 0.0f, dpf = 0.0f; |
tollidal | 0:01f3ce729f40 | 29 | lcd.cls(); |
tollidal | 0:01f3ce729f40 | 30 | char tmpString[16]; |
tollidal | 0:01f3ce729f40 | 31 | int temp_format = 0; |
tollidal | 0:01f3ce729f40 | 32 | int humid_format = 0; |
tollidal | 0:01f3ce729f40 | 33 | |
tollidal | 0:01f3ce729f40 | 34 | while(1) { |
tollidal | 0:01f3ce729f40 | 35 | |
tollidal | 0:01f3ce729f40 | 36 | wait(2.0f); |
tollidal | 0:01f3ce729f40 | 37 | error = sensor.readData(); |
tollidal | 0:01f3ce729f40 | 38 | if (0 == error) { |
tollidal | 0:01f3ce729f40 | 39 | c = sensor.ReadTemperature(CELCIUS); |
tollidal | 0:01f3ce729f40 | 40 | f = sensor.ReadTemperature(FARENHEIT); |
tollidal | 0:01f3ce729f40 | 41 | k = sensor.ReadTemperature(KELVIN); |
tollidal | 0:01f3ce729f40 | 42 | h = sensor.ReadHumidity(); |
tollidal | 0:01f3ce729f40 | 43 | dp = sensor.CalcdewPoint(c, h); |
tollidal | 0:01f3ce729f40 | 44 | dpf = sensor.CalcdewPointFast(c, h); |
tollidal | 0:01f3ce729f40 | 45 | printf("Temperature in Kelvin: %4.2f, Celcius: %4.2f, Farenheit %4.2f\n", k, c, f); |
tollidal | 0:01f3ce729f40 | 46 | printf("Humidity is %4.2f, Dewpoint: %4.2f, Dewpoint fast: %4.2f\n", h, dp, dpf); |
tollidal | 0:01f3ce729f40 | 47 | printf("Tempformat: %d HumidFormat: %d\n", temp_format, humid_format); |
tollidal | 0:01f3ce729f40 | 48 | switch(temp_format) { |
tollidal | 0:01f3ce729f40 | 49 | case 0 : |
tollidal | 0:01f3ce729f40 | 50 | sprintf(tmpString, " Temp: %4.2fC", c); |
tollidal | 0:01f3ce729f40 | 51 | break; |
tollidal | 0:01f3ce729f40 | 52 | case 1 : |
tollidal | 0:01f3ce729f40 | 53 | sprintf(tmpString, " Temp: %4.2fF", f); |
tollidal | 0:01f3ce729f40 | 54 | break; |
tollidal | 0:01f3ce729f40 | 55 | case 2 : |
tollidal | 0:01f3ce729f40 | 56 | sprintf(tmpString, " Temp: %4.2fK", k); |
tollidal | 0:01f3ce729f40 | 57 | break; |
tollidal | 0:01f3ce729f40 | 58 | } |
tollidal | 0:01f3ce729f40 | 59 | |
tollidal | 0:01f3ce729f40 | 60 | //display the results onto the LCD |
tollidal | 0:01f3ce729f40 | 61 | lcd.locate(0,0); |
tollidal | 0:01f3ce729f40 | 62 | lcd.printf("Temperature: %4.2fC", c); |
tollidal | 0:01f3ce729f40 | 63 | lcd.locate(0,10); |
tollidal | 0:01f3ce729f40 | 64 | lcd.printf("Humidity: %4.2f", h); |
tollidal | 0:01f3ce729f40 | 65 | wait(2); |
tollidal | 0:01f3ce729f40 | 66 | |
tollidal | 0:01f3ce729f40 | 67 | //turn fan on if temperature is above 25 |
tollidal | 0:01f3ce729f40 | 68 | if(c > 21) fan=1; //fan is on |
tollidal | 0:01f3ce729f40 | 69 | else fan=0; //Fan is off |
tollidal | 0:01f3ce729f40 | 70 | |
tollidal | 0:01f3ce729f40 | 71 | |
tollidal | 0:01f3ce729f40 | 72 | //wait 2 seconds and check temp again |
tollidal | 0:01f3ce729f40 | 73 | wait(2); |
tollidal | 0:01f3ce729f40 | 74 | } |
tollidal | 0:01f3ce729f40 | 75 | } |
tollidal | 0:01f3ce729f40 | 76 | } |