Advertisement
Lukashk

Terarium LL

Apr 2nd, 2021
770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.96 KB | None | 0 0
  1. #include <DHT.h>
  2. #include <ESP8266WiFi.h>
  3. #include <NTPClient.h>
  4. #include <WiFiUdp.h>
  5. #include <LiquidCrystal_I2C.h>
  6.  
  7.  
  8. String apiKey = "C4IIJ674M262G2YZ"; // Enter your Write API key here
  9. const char* ssid = "Lorencovi"; // Enter your WiFi Name
  10. const char* pass = "LIKB29,ole.51783"; // Enter your WiFi Password
  11. const char* server = "api.thingspeak.com";
  12.  
  13.  
  14. // set the LCD number of columns and rows
  15. int lcdColumns = 20;
  16. int lcdRows = 4;
  17.  
  18. // set LCD address, number of columns and rows
  19. // if you don't know your display address, run an I2C scanner sketch
  20. LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
  21.  
  22.  
  23.  
  24. #define pinDHT D6 // GPIO Pin where the dht11 is connected
  25. #define typDHT22 DHT22
  26. DHT dht(pinDHT, DHT22);
  27. WiFiClient client;
  28.  
  29. #define RELE1 10 // světlo 1 sd3 // pin SD3
  30. #define RELE2 4 // světlo 2 sd2 // pin SD2
  31. #define RELE3 5 // Hl.světlo // pin D5
  32. #define RELE4 7 // rosa // pin D7
  33.  
  34. unsigned long lastMillis = 0;
  35. unsigned long zobrMillis = 0;
  36. unsigned long serMillis = 0;
  37. unsigned long lcdMillis = 0;
  38. WiFiUDP ntpUDP;
  39. NTPClient timeClient(ntpUDP, "cz.pool.ntp.org", 7200, 60000);
  40.  
  41. //==================================================================
  42. //=============Časové spínání=======================================
  43. //============= Halogen 1 =========================================
  44. void svetlo1() {
  45. // Časové spínání
  46. int Hod = timeClient.getHours();
  47. int Min = timeClient.getMinutes();
  48. int Sec = timeClient.getSeconds();
  49.  
  50. if (Hod == 6 && Min >= 50 && Hod < 7 ) {
  51. digitalWrite(10, LOW);
  52. Serial.println("Světlo 1 svítí");
  53. lcd.setCursor(0, 2);
  54. lcd.print("SV1 _ON");
  55. }
  56. if (Hod == 20 && Min >= 0 && Min < 5) {
  57. digitalWrite(10, LOW);
  58. Serial.println("Světlo 1 svítí");
  59. lcd.setCursor(0, 2);
  60. lcd.print("SV1 _ON");
  61. }
  62. /*if (Hod == 19 && Min == 32) {
  63. digitalWrite(10,LOW);
  64. Serial.println("Test");
  65. }*/
  66.  
  67. else {
  68. //Serial.println("Světlo 1 nesvítí");
  69. digitalWrite(10, HIGH);
  70. lcd.setCursor(0, 2);
  71. lcd.print("SV1 OFF");
  72. }
  73. }
  74. //===================================================================
  75. //============= Halogen 2 ==========================================
  76. void svetlo2() {
  77. // Časové spínání
  78. int Hod = timeClient.getHours();
  79. int Min = timeClient.getMinutes();
  80. int Sec = timeClient.getSeconds();
  81.  
  82. if (Hod == 6 && Min >= 55 && Hod < 7 ) {
  83. digitalWrite(D4, LOW);
  84. Serial.println("Světlo 2 svítí");
  85. lcd.setCursor(10, 2);
  86. lcd.print("SV2 _ON");
  87. }
  88. if (Hod == 20 && Min >= 0 && Min < 10) {
  89. digitalWrite(D4, LOW);
  90. Serial.println("Světlo 2 svítí");
  91. lcd.setCursor(10, 2);
  92. lcd.print("SV2 _ON");
  93. }
  94. else {
  95. //Serial.println("Světlo 2 nesvítí");
  96. digitalWrite(D4, HIGH);
  97. lcd.setCursor(10, 2);
  98. lcd.print("SV2 OFF");
  99. }
  100. }
  101. //====================================================================
  102. //============= Hlavní světlo =======================================
  103. void hlsvetlo() {
  104. // Časové spínání
  105. int Hod = timeClient.getHours();
  106. int Min = timeClient.getMinutes();
  107. int Sec = timeClient.getSeconds();
  108.  
  109.  
  110. if (Hod >= 7 && Hod < 20 ) {
  111. digitalWrite(D5, LOW);
  112. Serial.println("Hlavní světlo svítí");
  113. lcd.setCursor(0, 3);
  114. lcd.print("HLSV _ON");
  115. }
  116. /*
  117. if (Hod >= 7 && Min >= 30) {
  118. digitalWrite(D5,LOW);
  119. Serial.println("Hlavní světlo svítí");
  120. }
  121. if (Hod >= 18 && Min >= 20) {
  122. digitalWrite(D5,HIGH);
  123. Serial.println("Hlavní světlo nesvítí");
  124. }*/
  125. else {
  126. digitalWrite(D5, HIGH);
  127. //Serial.println("Hlavní světlo nesvítí");
  128. lcd.setCursor(0, 3);
  129. lcd.print("HLSV OFF");
  130.  
  131. }
  132. }
  133. //======================================================================
  134. //================== Rošení ==========================================
  135.  
  136. void rosa() {
  137. // Časové spínání
  138. int Hod = timeClient.getHours();
  139. int Min = timeClient.getMinutes();
  140. int Sec = timeClient.getSeconds();
  141.  
  142. if (Hod == 8 && Min == 0 && Sec >= 40) {
  143. digitalWrite(D7, LOW);
  144. Serial.println("Rošení");
  145. lcd.setCursor(10, 3);
  146. lcd.print("ROSENI");
  147. }
  148. if (Hod == 13 && Min == 0 && Sec >= 40) {
  149. digitalWrite(D7, LOW);
  150. Serial.println("Rošení");
  151. lcd.setCursor(10, 3);
  152. lcd.print("ROSENI");
  153. }
  154. if (Hod == 18 && Min == 0 && Sec >= 40) {
  155. digitalWrite(D7, LOW);
  156. Serial.println("Rošení");
  157. lcd.setCursor(10, 3);
  158. lcd.print("ROSENI");
  159. }
  160. else {
  161. digitalWrite(D7, HIGH);
  162. lcd.setCursor(10, 3);
  163. lcd.print("_________");
  164.  
  165. }
  166. }
  167. //=========================================================================
  168. //===================== Výpis teplot na displey ===========================
  169. void lcdtep() {
  170. if (millis() - zobrMillis > 6000) {
  171. float a = dht.readHumidity();
  172. float b = dht.readTemperature();
  173.  
  174. if (isnan(b) || isnan(a))
  175. {
  176. //Serial.println("Chyba čtení z DHT sensoru!");
  177.  
  178. return;
  179. }
  180.  
  181.  
  182. lcd.setCursor(1, 1);
  183. lcd.print("T:");
  184. lcd.print(b);
  185. lcd.print("C");
  186. lcd.setCursor(11, 1);
  187. lcd.print("V:");
  188. lcd.print(a);
  189. lcd.print("%");
  190. zobrMillis = millis();
  191. }
  192. }
  193. //========================================================================
  194. //=================== Výpis teplot na serial print ========================
  195. void sertep() {
  196. if (millis() - serMillis > 1000) {
  197. float c = dht.readHumidity();
  198. float d = dht.readTemperature();
  199.  
  200. if (isnan(c) || isnan(d))
  201. {
  202. Serial.println("Chyba čtení z DHT sensoru!");
  203. return;
  204. }
  205.  
  206. Serial.println(timeClient.getFormattedTime());
  207. Serial.print("Teplota: ");
  208. Serial.print(d);
  209. Serial.print(" °C, Vlhkost: ");
  210. Serial.print(c);
  211. Serial.println("%");
  212. Serial.println("Waiting...");
  213. serMillis = millis();
  214. }
  215. }
  216. //=========================================================================
  217. //=================== Odesilani na Thinkspeak ============================
  218. void thinkspeak() {
  219. if (millis() - lastMillis > 60000) {
  220. float h = dht.readHumidity();
  221. float t = dht.readTemperature();
  222.  
  223. if (isnan(h) || isnan(t))
  224. {
  225. Serial.println("Chyba čtení z DHT sensoru!");
  226. return;
  227. }
  228. else {
  229. if (client.connect(server, 80))
  230. {
  231. String postStr = apiKey;
  232. postStr += "&field1=";
  233. postStr += String(t);
  234. postStr += "&field2=";
  235. postStr += String(h);
  236. postStr += "\r\n\r\n";
  237.  
  238.  
  239. client.print("POST /update HTTP/1.1\n");
  240. client.print("Host: api.thingspeak.com\n");
  241. client.print("Connection: close\n");
  242. client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
  243. client.print("Content-Type: application/x-www-form-urlencoded\n");
  244. client.print("Content-Length: ");
  245. client.print(postStr.length());
  246. client.print("\n\n");
  247. client.print(postStr);
  248. client.stop();
  249. Serial.println("xxxxxxxxxxxxxxxxx");
  250. Serial.print("Send to Thingspeak");
  251. Serial.println(timeClient.getFormattedTime());
  252. Serial.println("xxxxxxxxxxxxxxxxx");
  253. lastMillis = millis();
  254. }
  255. }
  256. }
  257. }
  258. //===================================================================================================
  259. void lcdreset() {
  260. if (millis() - lcdMillis > 600000) {
  261. lcd.backlight();
  262. lcd.clear();
  263. lastMillis = millis();
  264. }
  265. }
  266.  
  267. void setup() {
  268.  
  269. Serial.begin(115200);
  270. delay(10);
  271. Serial.println("Connecting to ");
  272. Serial.println(ssid);
  273. WiFi.mode(WIFI_STA);
  274. WiFi.begin(ssid, pass);
  275. while (WiFi.status() != WL_CONNECTED)
  276. {
  277. delay(550);
  278. Serial.print(".");
  279. }
  280. Serial.println("");
  281. Serial.println("WiFi connected");
  282. timeClient.begin();
  283. lcd.init(); // initialize LCD
  284. lcd.backlight(); // turn on LCD backlight
  285. dht.begin();
  286.  
  287. pinMode(10, OUTPUT);
  288. pinMode(D4, OUTPUT);
  289. pinMode(D5, OUTPUT);
  290. pinMode(D7, OUTPUT);
  291. pinMode(D6, INPUT);
  292. pinMode(D3, INPUT_PULLUP);
  293. digitalWrite(10, HIGH);
  294. digitalWrite(D4, HIGH);
  295. digitalWrite(D5, HIGH);
  296. digitalWrite(D7, HIGH);
  297.  
  298.  
  299.  
  300.  
  301.  
  302. }
  303. void loop() {
  304. thinkspeak();
  305. svetlo1();
  306. svetlo2();
  307. hlsvetlo();
  308. rosa();
  309. timeClient.update();
  310. lcdtep();
  311. sertep();
  312. //lcdreset();
  313. /*int stateButtonHL = digitalRead(D3);
  314.  
  315. Serial.println(stateButtonHL);
  316.  
  317. if (stateButtonHL == 0) {
  318. Serial.print("============Není Voda============");
  319. lcd.setCursor(10, 3);
  320. lcd.print("NENI VODA");
  321. }
  322. else {
  323. rosa();
  324. lcd.setCursor(10, 3);
  325. lcd.print("_________");
  326. }
  327.  
  328. */
  329. lcd.setCursor(6, 0);
  330. lcd.print(timeClient.getFormattedTime());
  331.  
  332.  
  333. delay(1000);
  334.  
  335.  
  336. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement