Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <DHT.h>
- #include <ESP8266WiFi.h>
- #include <NTPClient.h>
- #include <WiFiUdp.h>
- #include <LiquidCrystal_I2C.h>
- String apiKey = "C4IIJ674M262G2YZ"; // Enter your Write API key here
- const char* ssid = "Lorencovi"; // Enter your WiFi Name
- const char* pass = "LIKB29,ole.51783"; // Enter your WiFi Password
- const char* server = "api.thingspeak.com";
- // set the LCD number of columns and rows
- int lcdColumns = 20;
- int lcdRows = 4;
- // set LCD address, number of columns and rows
- // if you don't know your display address, run an I2C scanner sketch
- LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
- #define pinDHT D6 // GPIO Pin where the dht11 is connected
- #define typDHT22 DHT22
- DHT dht(pinDHT, DHT22);
- WiFiClient client;
- #define RELE1 10 // světlo 1 sd3 // pin SD3
- #define RELE2 4 // světlo 2 sd2 // pin SD2
- #define RELE3 5 // Hl.světlo // pin D5
- #define RELE4 7 // rosa // pin D7
- unsigned long lastMillis = 0;
- unsigned long zobrMillis = 0;
- unsigned long serMillis = 0;
- unsigned long lcdMillis = 0;
- WiFiUDP ntpUDP;
- NTPClient timeClient(ntpUDP, "cz.pool.ntp.org", 7200, 60000);
- //==================================================================
- //=============Časové spínání=======================================
- //============= Halogen 1 =========================================
- void svetlo1() {
- // Časové spínání
- int Hod = timeClient.getHours();
- int Min = timeClient.getMinutes();
- int Sec = timeClient.getSeconds();
- if (Hod == 6 && Min >= 50 && Hod < 7 ) {
- digitalWrite(10, LOW);
- Serial.println("Světlo 1 svítí");
- lcd.setCursor(0, 2);
- lcd.print("SV1 _ON");
- }
- if (Hod == 20 && Min >= 0 && Min < 5) {
- digitalWrite(10, LOW);
- Serial.println("Světlo 1 svítí");
- lcd.setCursor(0, 2);
- lcd.print("SV1 _ON");
- }
- /*if (Hod == 19 && Min == 32) {
- digitalWrite(10,LOW);
- Serial.println("Test");
- }*/
- else {
- //Serial.println("Světlo 1 nesvítí");
- digitalWrite(10, HIGH);
- lcd.setCursor(0, 2);
- lcd.print("SV1 OFF");
- }
- }
- //===================================================================
- //============= Halogen 2 ==========================================
- void svetlo2() {
- // Časové spínání
- int Hod = timeClient.getHours();
- int Min = timeClient.getMinutes();
- int Sec = timeClient.getSeconds();
- if (Hod == 6 && Min >= 55 && Hod < 7 ) {
- digitalWrite(D4, LOW);
- Serial.println("Světlo 2 svítí");
- lcd.setCursor(10, 2);
- lcd.print("SV2 _ON");
- }
- if (Hod == 20 && Min >= 0 && Min < 10) {
- digitalWrite(D4, LOW);
- Serial.println("Světlo 2 svítí");
- lcd.setCursor(10, 2);
- lcd.print("SV2 _ON");
- }
- else {
- //Serial.println("Světlo 2 nesvítí");
- digitalWrite(D4, HIGH);
- lcd.setCursor(10, 2);
- lcd.print("SV2 OFF");
- }
- }
- //====================================================================
- //============= Hlavní světlo =======================================
- void hlsvetlo() {
- // Časové spínání
- int Hod = timeClient.getHours();
- int Min = timeClient.getMinutes();
- int Sec = timeClient.getSeconds();
- if (Hod >= 7 && Hod < 20 ) {
- digitalWrite(D5, LOW);
- Serial.println("Hlavní světlo svítí");
- lcd.setCursor(0, 3);
- lcd.print("HLSV _ON");
- }
- /*
- if (Hod >= 7 && Min >= 30) {
- digitalWrite(D5,LOW);
- Serial.println("Hlavní světlo svítí");
- }
- if (Hod >= 18 && Min >= 20) {
- digitalWrite(D5,HIGH);
- Serial.println("Hlavní světlo nesvítí");
- }*/
- else {
- digitalWrite(D5, HIGH);
- //Serial.println("Hlavní světlo nesvítí");
- lcd.setCursor(0, 3);
- lcd.print("HLSV OFF");
- }
- }
- //======================================================================
- //================== Rošení ==========================================
- void rosa() {
- // Časové spínání
- int Hod = timeClient.getHours();
- int Min = timeClient.getMinutes();
- int Sec = timeClient.getSeconds();
- if (Hod == 8 && Min == 0 && Sec >= 40) {
- digitalWrite(D7, LOW);
- Serial.println("Rošení");
- lcd.setCursor(10, 3);
- lcd.print("ROSENI");
- }
- if (Hod == 13 && Min == 0 && Sec >= 40) {
- digitalWrite(D7, LOW);
- Serial.println("Rošení");
- lcd.setCursor(10, 3);
- lcd.print("ROSENI");
- }
- if (Hod == 18 && Min == 0 && Sec >= 40) {
- digitalWrite(D7, LOW);
- Serial.println("Rošení");
- lcd.setCursor(10, 3);
- lcd.print("ROSENI");
- }
- else {
- digitalWrite(D7, HIGH);
- lcd.setCursor(10, 3);
- lcd.print("_________");
- }
- }
- //=========================================================================
- //===================== Výpis teplot na displey ===========================
- void lcdtep() {
- if (millis() - zobrMillis > 6000) {
- float a = dht.readHumidity();
- float b = dht.readTemperature();
- if (isnan(b) || isnan(a))
- {
- //Serial.println("Chyba čtení z DHT sensoru!");
- return;
- }
- lcd.setCursor(1, 1);
- lcd.print("T:");
- lcd.print(b);
- lcd.print("C");
- lcd.setCursor(11, 1);
- lcd.print("V:");
- lcd.print(a);
- lcd.print("%");
- zobrMillis = millis();
- }
- }
- //========================================================================
- //=================== Výpis teplot na serial print ========================
- void sertep() {
- if (millis() - serMillis > 1000) {
- float c = dht.readHumidity();
- float d = dht.readTemperature();
- if (isnan(c) || isnan(d))
- {
- Serial.println("Chyba čtení z DHT sensoru!");
- return;
- }
- Serial.println(timeClient.getFormattedTime());
- Serial.print("Teplota: ");
- Serial.print(d);
- Serial.print(" °C, Vlhkost: ");
- Serial.print(c);
- Serial.println("%");
- Serial.println("Waiting...");
- serMillis = millis();
- }
- }
- //=========================================================================
- //=================== Odesilani na Thinkspeak ============================
- void thinkspeak() {
- if (millis() - lastMillis > 60000) {
- float h = dht.readHumidity();
- float t = dht.readTemperature();
- if (isnan(h) || isnan(t))
- {
- Serial.println("Chyba čtení z DHT sensoru!");
- return;
- }
- else {
- if (client.connect(server, 80))
- {
- String postStr = apiKey;
- postStr += "&field1=";
- postStr += String(t);
- postStr += "&field2=";
- postStr += String(h);
- postStr += "\r\n\r\n";
- client.print("POST /update HTTP/1.1\n");
- client.print("Host: api.thingspeak.com\n");
- client.print("Connection: close\n");
- client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
- client.print("Content-Type: application/x-www-form-urlencoded\n");
- client.print("Content-Length: ");
- client.print(postStr.length());
- client.print("\n\n");
- client.print(postStr);
- client.stop();
- Serial.println("xxxxxxxxxxxxxxxxx");
- Serial.print("Send to Thingspeak");
- Serial.println(timeClient.getFormattedTime());
- Serial.println("xxxxxxxxxxxxxxxxx");
- lastMillis = millis();
- }
- }
- }
- }
- //===================================================================================================
- void lcdreset() {
- if (millis() - lcdMillis > 600000) {
- lcd.backlight();
- lcd.clear();
- lastMillis = millis();
- }
- }
- void setup() {
- Serial.begin(115200);
- delay(10);
- Serial.println("Connecting to ");
- Serial.println(ssid);
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, pass);
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(550);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- timeClient.begin();
- lcd.init(); // initialize LCD
- lcd.backlight(); // turn on LCD backlight
- dht.begin();
- pinMode(10, OUTPUT);
- pinMode(D4, OUTPUT);
- pinMode(D5, OUTPUT);
- pinMode(D7, OUTPUT);
- pinMode(D6, INPUT);
- pinMode(D3, INPUT_PULLUP);
- digitalWrite(10, HIGH);
- digitalWrite(D4, HIGH);
- digitalWrite(D5, HIGH);
- digitalWrite(D7, HIGH);
- }
- void loop() {
- thinkspeak();
- svetlo1();
- svetlo2();
- hlsvetlo();
- rosa();
- timeClient.update();
- lcdtep();
- sertep();
- //lcdreset();
- /*int stateButtonHL = digitalRead(D3);
- Serial.println(stateButtonHL);
- if (stateButtonHL == 0) {
- Serial.print("============Není Voda============");
- lcd.setCursor(10, 3);
- lcd.print("NENI VODA");
- }
- else {
- rosa();
- lcd.setCursor(10, 3);
- lcd.print("_________");
- }
- */
- lcd.setCursor(6, 0);
- lcd.print(timeClient.getFormattedTime());
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement