/* Antero Metso the 6th of November 2022 */ /* Code for the Final Project */ /* (C) Copyright 2022 Antero Metso */ /* Distributed under Fab licence */ #include // Required by LiquidCrystal #include // For communicating with the LCD #include LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display #define RELAYPIN 0 // A pin between LedPin and DHTPin (2nd from top, left connector) #define DHTPIN 4 // DHTpin is the 3rd from top, left connector. #define DHTTYPE DHT22 // DHT 22 (AM2302) DHTStable DHT; // initialize the DHT library // Initialize the humidity and temperature variables int humidity =0; int temperature =0; void setup() { lcd.init(); pinMode(RELAYPIN, OUTPUT); pinMode(DHTPIN, INPUT); // Print the initialization message to the LCD. lcd.backlight(); // Turn backlight on lcd.setCursor(0,0); // Set cursor to top left corner and type the text // lcd.print("Keyes AM2302 DHT-22"); lcd.setCursor(2,1); lcd.print("Test"); } /* The Main Loop */ void loop() { lcd.init(); // Initialize LCD. int chk = DHT.read22(DHTPIN); // Read DHT22 data. lcd.setCursor(0,0); // Set cursor to upper left corner // for printing the status information // Print the status of the DHT22 sensor based on the information switch (chk) { case DHTLIB_OK: lcd.print("Read Status OK"); break; case DHTLIB_ERROR_CHECKSUM: lcd.print("Checksum err"); break; case DHTLIB_ERROR_TIMEOUT: lcd.print("Time out err"); break; default: lcd.print("Unknown err"); break; } // Reading takes about 250 milliseconds // Data may be 2 seconds old // Read temperature. temperature = DHT.getTemperature(); // Read humidity humidity = DHT.getHumidity(); /* Print Humidity on LCD */ lcd.setCursor(0,1); lcd.print("Humidity:"); lcd.setCursor(14,1); lcd.print(humidity); lcd.setCursor(18,1); lcd.print("%"); /* Print Temperature on LCD */ lcd.setCursor(0,2); lcd.print("Temp:"); lcd.setCursor(14,2); lcd.print(temperature); lcd.setCursor(18,2); lcd.print("C"); /* See if temperature is over limit and control relay */ if (temperature > 35) { // Turn relay OFF, print OFF lcd.setCursor(0,3); lcd.print("OFF"); digitalWrite(RELAYPIN,0); } else { // Turn relay ON, print ON lcd.setCursor(0,3); lcd.print("ON"); digitalWrite(RELAYPIN,1); }; delay(3000); /* Add a 3 second delay in the loop */ }