From eb82bb2b1d18170bb613a21e3edd93a63d519585 Mon Sep 17 00:00:00 2001
From: Domagoj Zecevic
Temperature: "); + htmlPage += global_temp_indoor; + htmlPage += F("
" + "Humidity: "); + htmlPage += global_hym_indoor; + + htmlPage += F("" + "\r\n"); + return htmlPage; +} + +void setup() { + Serial.begin(115200); + initWiFi(); + dht_outdoor.begin(); // Start DHT + dht_indoor.begin(); + + // initialize LCD + lcd.begin(5, 4); + lcd.init(); + lcd.backlight(); + + pinMode(D3, INPUT); + + server.begin(); + Serial.printf("Web server started, open %s in a web browser\n", WiFi.localIP().toString().c_str()); + + //connect to MQTT + MQTTclient.setServer(mqttServer, mqttPort); + //MQTTclient.setCallback(callback); + + while (!MQTTclient.connected()) { + + if (MQTTclient.connect("ESP32Client", mqttUser, mqttPassword )) { + Serial.println("Connected to mqtt!"); + } else { + Serial.print("failed with state "); + Serial.print(MQTTclient.state()); + delay(2000); + } + } +//show 5 sec IP on display +lcd.setCursor(0, 0); +lcd.print("IP Adresa:"); +lcd.setCursor(0, 1); +lcd.print(WiFi.localIP().toString().c_str()); +delay(5000); +lcd.noBacklight(); +} + +void backlight(){ + lcd.backlight(); + show_display(); + delay(5000); + lcd.noBacklight(); + lcd.clear(); +} + +void show_display(){ + lcd.clear(); + lcd.setCursor(0, 0); + lcd.print("Unutra: "); + lcd.print(String(global_temp_indoor).c_str()); + lcd.setCursor(0,1); + lcd.print("Vani: "); + lcd.print(String(global_temp_outdoor).c_str()); +} + +void loop() { + int button = digitalRead(D3); + Serial.print("Button: "); + Serial.println(button); + if (button == 0) { + backlight(); + } + + + unsigned long currentMillis = millis(); + //if(currentMillis - previousMillis > interval) { + // previousMillis = currentMillis; + // if (!MQTTclient.connected()) { + // WiFi.reconnect(); + // reconect(); + // } + // MQTTclient.publish("my/BedRoom/Temperature", String(global_temp_indoor).c_str()); + // MQTTclient.publish("my/BedRoom/Humidity", String(global_hym_indoor).c_str()); + // } + + WiFiClient serverClient = server.available(); + if (serverClient) + { + while (serverClient.connected()) + { + // read line by line what the client (web browser) is requesting + if (serverClient.available()) + { + String line = serverClient.readStringUntil('\r'); + if (line.length() == 1 && line[0] == '\n') + { + serverClient.println(prepareHtmlPage()); + break; + } + } + } + + while (serverClient.available()) { + serverClient.read(); + } + // close the connection: + serverClient.stop(); + } + + global_temp_indoor = dht_indoor.readTemperature(); + global_hym_indoor = dht_indoor.readHumidity(); + + Serial.println("Indoor Sensor"); + Serial.print("Temperatrure: "); + Serial.println(global_temp_indoor); + Serial.print("Humidity: "); + Serial.println(global_hym_indoor); + + global_temp_outdoor = dht_outdoor.readTemperature(); + global_hym_outdoor = dht_outdoor.readHumidity(); + + Serial.println("Outdoor Sensor"); + Serial.print("Temperatrure: "); + Serial.println(global_temp_outdoor); + Serial.print("Humidity: "); + Serial.println(global_hym_outdoor); + + show_display(); + delay(2000); +}