From eb82bb2b1d18170bb613a21e3edd93a63d519585 Mon Sep 17 00:00:00 2001 From: Domagoj Zecevic Date: Sat, 25 Feb 2023 19:27:55 +0100 Subject: [PATCH] initial commit --- tata_station/tata_station.ino | 212 ++++++++++++++++++++++++++++++++++ 1 file changed, 212 insertions(+) create mode 100644 tata_station/tata_station.ino diff --git a/tata_station/tata_station.ino b/tata_station/tata_station.ino new file mode 100644 index 0000000..73d59b9 --- /dev/null +++ b/tata_station/tata_station.ino @@ -0,0 +1,212 @@ +#include +#include +#include +#include + +// set the LCD number of columns and rows +int lcdColumns = 16; +int lcdRows = 2; +LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows); + +// WiFi Info +const char* ssid = "IoT-Zecevic"; +const char* password = "IoTSifra123456!"; + +//MQTT data +const char* mqttServer = "192.168.1.11"; +const int mqttPort = 1883; +const char* mqttUser = "yourMQTTuser"; +const char* mqttPassword = "yourMQTTpassword"; +WiFiClient espClient; +PubSubClient MQTTclient(espClient); + + + +// DHT setup +#define DHTPIN 2 +#define DHTTYPE DHT22 +DHT dht_outdoor(DHTPIN, DHTTYPE); + +// DHT setup inside +#define DHTPIN D5 +#define DHTTYPE DHT11 +DHT dht_indoor(DHTPIN, DHTTYPE); + +// Global Vars +float global_temp_indoor; +float global_hym_indoor; +float global_temp_outdoor; +float global_hym_outdoor; +//update +long previousMillis = 0; +long interval = 60000; //ms + +long previousMillis1 = 0; +long interval1 = 2000; //ms + +//Start web server +WiFiServer server(80); + +void initWiFi() { + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, password); + Serial.print("Connecting to WiFi .."); + while (WiFi.status() != WL_CONNECTED) { + Serial.print('.'); + delay(1000); + } + Serial.println(WiFi.localIP()); +} + +void reconect() { + while (MQTTclient.connect("ESP32Client", mqttUser, mqttPassword )) { + Serial.println("Connecting to MQTT..."); + } +} + +// prepare a web page to be send to a client (web browser) +String prepareHtmlPage() +{ + String htmlPage; + htmlPage.reserve(1024); // prevent ram fragmentation + htmlPage = F("HTTP/1.1 200 OK\r\n" + "Content-Type: text/html\r\n" + "Connection: close\r\n" // the connection will be closed after completion of the response + "Refresh: 5\r\n" // refresh the page automatically every 5 sec + "\r\n" + "" + ""); + htmlPage += F("

" + "

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); +}