Strawberry forever temperature and humidity sensor controlling fan
Dependencies: mbed C12832 DHT DHT11
Diff: main.cpp
- Revision:
- 0:01f3ce729f40
- Child:
- 1:c33171654467
diff -r 000000000000 -r 01f3ce729f40 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Oct 12 19:26:01 2021 +0000 @@ -0,0 +1,76 @@ +#include "mbed.h" +#include "C12832.h" +#include "DHT.h" + + +C12832 lcd(p5, p7, p6, p8, p11); //LCD screen +DHT sensor(p20,DHT11); // Use the SEN11301P sensor +DigitalOut fan(p18); + +int main() +{ + +//welcome message on LCD + + lcd.cls(); + lcd.locate(0,0); + lcd.printf("Hello user"); + lcd.locate(0,10); + lcd.printf("Greenhouse Control System"); + wait(2.5); + + lcd.cls(); //clear LCD + + //infinte loop to check temperature after welcome message + + + int error = 0; + float h = 0.0f, c = 0.0f, f = 0.0f, k = 0.0f, dp = 0.0f, dpf = 0.0f; + lcd.cls(); + char tmpString[16]; + int temp_format = 0; + int humid_format = 0; + + while(1) { + + wait(2.0f); + error = sensor.readData(); + if (0 == error) { + c = sensor.ReadTemperature(CELCIUS); + f = sensor.ReadTemperature(FARENHEIT); + k = sensor.ReadTemperature(KELVIN); + h = sensor.ReadHumidity(); + dp = sensor.CalcdewPoint(c, h); + dpf = sensor.CalcdewPointFast(c, h); + printf("Temperature in Kelvin: %4.2f, Celcius: %4.2f, Farenheit %4.2f\n", k, c, f); + printf("Humidity is %4.2f, Dewpoint: %4.2f, Dewpoint fast: %4.2f\n", h, dp, dpf); + printf("Tempformat: %d HumidFormat: %d\n", temp_format, humid_format); + switch(temp_format) { + case 0 : + sprintf(tmpString, " Temp: %4.2fC", c); + break; + case 1 : + sprintf(tmpString, " Temp: %4.2fF", f); + break; + case 2 : + sprintf(tmpString, " Temp: %4.2fK", k); + break; + } + + //display the results onto the LCD + lcd.locate(0,0); + lcd.printf("Temperature: %4.2fC", c); + lcd.locate(0,10); + lcd.printf("Humidity: %4.2f", h); + wait(2); + + //turn fan on if temperature is above 25 + if(c > 21) fan=1; //fan is on + else fan=0; //Fan is off + + + //wait 2 seconds and check temp again + wait(2); + } + } +}