Advertisement
safwan092

Untitled

Jun 7th, 2022
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h>
  3. #include "DHT.h"
  4. #include "RTClib.h"
  5.  
  6.  
  7.  
  8. #define RELAYPIN 9
  9. #define DHTPIN 8
  10. #define DHTTYPE DHT22
  11. DHT dht(DHTPIN, DHTTYPE);
  12. LiquidCrystal_I2C lcd(0x27, 16, 2);
  13. RTC_DS3231 rtc;
  14.  
  15. void setup() {
  16. Serial.begin(9600);
  17. lcd.init();
  18. lcd.init();
  19. lcd.backlight();
  20. lcd.setCursor(0, 0);
  21. lcd.print("Hello, world!");
  22. dht.begin();
  23. rtc.begin();
  24. pinMode(RELAYPIN, OUTPUT);
  25. digitalWrite(RELAYPIN, LOW);
  26. if (rtc.lostPower()) {
  27. rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  28. }
  29. }
  30.  
  31. void loop() {
  32. DateTime now = rtc.now();
  33. delay(1000);
  34. float h = dht.readHumidity();
  35. float t = dht.readTemperature();
  36. Serial.print(F("Humidity: "));
  37. Serial.print(h);
  38. Serial.print(F("% Temperature: "));
  39. Serial.print(t);
  40. Serial.println(F("°C "));
  41. Serial.print(now.year(), DEC);
  42. Serial.print('/');
  43. Serial.print(now.month(), DEC);
  44. Serial.print('/');
  45. Serial.print(now.day(), DEC);
  46. Serial.print(" ");
  47. Serial.print(now.hour(), DEC);
  48. Serial.print(':');
  49. Serial.print(now.minute(), DEC);
  50. Serial.println();
  51. lcd.setCursor(0, 0);
  52. lcd.print(now.year());
  53. lcd.print("/");
  54. lcd.print(now.month());
  55. lcd.print("/");
  56. lcd.print(now.day());
  57. lcd.print(" ");
  58. lcd.print(now.hour());
  59. lcd.print(":");
  60. lcd.print(now.minute());
  61. lcd.setCursor(0, 1);
  62. lcd.print("T:");
  63. lcd.print(t);
  64. lcd.print(" L:");
  65. lcd.print("OFF");
  66. //8AM-3PM for 10 minutes
  67. if (now.hour() == 8 || now.hour() == 9 || now.hour() == 10 || now.hour() == 11 || now.hour() == 12 || now.hour() == 13 || now.hour() == 14 || now.hour() == 15) {
  68. if (now.minute() <= 10) {
  69. if (now.second() <= 20) {
  70. digitalWrite(RELAYPIN, HIGH);
  71. lcd.setCursor(10, 1);
  72. lcd.print("ON ");
  73. delay(1000);
  74. }
  75. else {
  76. digitalWrite(RELAYPIN, LOW);
  77. lcd.setCursor(10, 1);
  78. lcd.print("OFF");
  79. }
  80. }
  81. else {
  82. digitalWrite(RELAYPIN, LOW);
  83. lcd.setCursor(10, 1);
  84. lcd.print("OFF");
  85. }
  86. }
  87. else {
  88. digitalWrite(RELAYPIN, LOW);
  89. lcd.setCursor(10, 1);
  90. lcd.print("OFF");
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement