Simple greenhouse heating system
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

greenhouseheating.ino 6.7KB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Greenhouse heating system
  2. #include <Arduino.h>
  3. #include <Thread.h> // ArduinoThread
  4. #include <DHT.h> // DHT Sensor Library by Adafruit 1.2.3 (!!! 1.3 not working)
  5. #include <DS1307RTC.h> // A basic DS1307 library that returns time as a time_t
  6. #include <SevenSegmentExtended.h> // Extended TM1637 library https://github.com/bremme/arduino-tm1637
  7. const byte PIN_CLK = 2; // Define CLK pin (for 4-Digit Display)
  8. const byte PIN_DIO = 3; // Define DIO pin (for 4-Digit Display)
  9. const byte button = 4;
  10. const byte relay = 5; // Relay PIN
  11. #define DHTPIN 6 // PIN DHT
  12. DHT dht(DHTPIN, DHT11); // DHT-22 - AM2302 init
  13. SevenSegmentExtended display(PIN_CLK, PIN_DIO);
  14. int minTemp = 11; // Minimum temperature for relay (on)
  15. int maxTemp = 14; // Maximum temperature for relay (off)
  16. long relayWorkingTime = 600000; // 10min - minimun working time
  17. int tempThreadMs = 10000; // checkTemp thread interval
  18. int buttonThreadMs = 200; // pressButton thread interval
  19. int buttonState = 0; // Button state
  20. int buttonView = 0; // Button viewing state
  21. int relayState = 0; // Relay state
  22. long previousRelayChanged = 0; // Last time relay state changed
  23. long buttonPressed = 0; // Button ms pressed counter
  24. long previousButtonMillis = 0; // Last time button pressing
  25. long buttonInterval = 10000; // Interval for highlighting 4-Digit Display after pressing button
  26. // Threads:
  27. Thread pressButtonThread = Thread(); // Create thread for button state checking
  28. Thread checkTempThread = Thread(); // Create thread for temperature state checking
  29. void setup() {
  30. Serial.begin(9600); // Initializes the Serial connection @ 9600 baud for debug
  31. serStr("starting setup...");
  32. display.begin(); // Initializes the display
  33. display.setBacklight(100); // Set the brightness to 100 %
  34. display.print("INIT"); // Display INIT on the display
  35. while (!Serial); // Wait until Arduino Serial Monitor opens
  36. setSyncProvider(RTC.get); // The function to get the time from the RTC
  37. if(timeStatus()!= timeSet)
  38. Serial.println("Unable to sync with the RTC");
  39. else
  40. Serial.println("RTC has set the system time");
  41. dht.begin();
  42. pinMode(button, INPUT);
  43. pinMode(relay, OUTPUT);
  44. digitalWrite(relay, LOW);
  45. // Button state cheking thread:
  46. checkTempThread.onRun(checkTemp);
  47. checkTempThread.setInterval(tempThreadMs); // Interval for checking temperature
  48. pressButtonThread.onRun(pressButton);
  49. pressButtonThread.setInterval(buttonThreadMs); // Interval for checking button pressing
  50. delay(5000); // 5 second delay before display off and setup complete
  51. display.off();
  52. serStr("...setup finished");
  53. }
  54. void loop() {
  55. // Threads init:
  56. if (pressButtonThread.shouldRun())
  57. pressButtonThread.run();
  58. if (checkTempThread.shouldRun())
  59. checkTempThread.run();
  60. // Check RTC.
  61. if (!(timeStatus() == timeSet)) {
  62. display.on();
  63. display.print("SET TIME");
  64. delay (10000);
  65. }
  66. }
  67. // Check temperature thread
  68. void checkTemp() {
  69. unsigned long currentMillis = millis();
  70. if (((currentMillis - previousRelayChanged) > relayWorkingTime) || (previousRelayChanged == 0)) {
  71. float tempFloat;
  72. readDHTtemp(&tempFloat);
  73. int tempInt = (int)tempFloat;
  74. if ((tempInt < minTemp) && (relayState == 0)) {
  75. serStr("low temperature - heating on");
  76. digitalWrite(relay, HIGH);
  77. previousRelayChanged = currentMillis;
  78. relayState = 1;
  79. }
  80. if ((tempInt > maxTemp) && (relayState == 1)) {
  81. serStr("high temperature - heating off");
  82. digitalWrite(relay, LOW);
  83. previousRelayChanged = currentMillis;
  84. relayState = 0;
  85. }
  86. }
  87. }
  88. // Check button pressing thread
  89. void pressButton() {
  90. unsigned long currentMillis = millis();
  91. if (currentMillis - previousButtonMillis > buttonInterval) {
  92. previousButtonMillis = currentMillis;
  93. display.off();
  94. }
  95. if (digitalRead(button) == HIGH) buttonState = 1;
  96. else buttonState = 0;
  97. if (buttonState == 1) {
  98. buttonPressed=buttonPressed+200;
  99. if (buttonPressed > 5000) {
  100. if (relayState == 0) {
  101. serStr("long press button - 5 sec - heating on");
  102. digitalWrite(relay, HIGH);
  103. relayState = 1;
  104. previousRelayChanged = currentMillis;
  105. buttonPressed = 0;
  106. } else {
  107. serStr("long press button - 5 sec - heating off");
  108. digitalWrite(relay, LOW);
  109. previousRelayChanged = currentMillis;
  110. relayState = 0;
  111. buttonPressed = 0;
  112. }
  113. }
  114. previousButtonMillis = currentMillis;
  115. display.on();
  116. if (buttonView == 0) {
  117. display.printTime(hour(), minute(), true);
  118. } else if (buttonView == 1) {
  119. float tempFloat;
  120. readDHTtemp(&tempFloat);
  121. int tempInt = (int)tempFloat;
  122. byte rawData[4];
  123. if (tempInt == 0) {
  124. rawData[0] = B01000000; // dash
  125. rawData[1] = B01000000; // dash
  126. } else {
  127. rawData[0] = display.encode(tempInt / 10);
  128. rawData[1] = display.encode(tempInt % 10);
  129. }
  130. rawData[2] = B01100011; // degree
  131. rawData[3] = display.encode('A') ;
  132. display.printRaw(rawData);
  133. } else if (buttonView == 2) {
  134. float humFloat;
  135. readDHThum(&humFloat);
  136. int humInt = (int)humFloat;
  137. byte rawData[4];
  138. rawData[0] = display.encode('h');
  139. rawData[1] = display.encode('u');
  140. if (humInt == 0) {
  141. rawData[2] = B01000000; // dash
  142. rawData[3] = B01000000; // dash
  143. } else {
  144. rawData[2] = display.encode(humInt / 10);
  145. rawData[3] = display.encode(humInt % 10);
  146. }
  147. display.printRaw(rawData);
  148. }
  149. if (buttonView < 2) buttonView += 1;
  150. else buttonView = 0;
  151. } else {
  152. buttonPressed = 0;
  153. }
  154. }
  155. // DHT Temperature
  156. void readDHTtemp(float *a) {
  157. float t = dht.readTemperature();
  158. *a = t;
  159. }
  160. // DHT Humidity
  161. void readDHThum(float *a) {
  162. float h = dht.readHumidity();
  163. *a = h;
  164. }
  165. // Send string to serial monitor with millis() counter and date/time
  166. void serStr(const char* serString) {
  167. long currentTime = millis();
  168. String space = " ";
  169. String stringToPrint = currentTime + space + serString;
  170. Serial.println(stringToPrint);
  171. // RTC mark
  172. Serial.print("RTC time = ");
  173. Serial.print(hour());
  174. Serial.write(':');
  175. Serial.print(minute());
  176. Serial.write(':');
  177. Serial.print(second());
  178. Serial.print(", date (D/M/Y) = ");
  179. Serial.print(day());
  180. Serial.write('/');
  181. Serial.print(month());
  182. Serial.write('/');
  183. Serial.print(year());
  184. Serial.println();
  185. }