commit
eb82bb2b1d
1 changed files with 212 additions and 0 deletions
@ -0,0 +1,212 @@ |
|||
#include <DHT.h> |
|||
#include <ESP8266WiFi.h> |
|||
#include <PubSubClient.h> |
|||
#include <LiquidCrystal_I2C.h> |
|||
|
|||
// 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" |
|||
"<!DOCTYPE HTML>" |
|||
"<html>"); |
|||
htmlPage += F("</p>" |
|||
"<p><b>Temperature:</b> "); |
|||
htmlPage += global_temp_indoor; |
|||
htmlPage += F("</p>" |
|||
"<p><b>Humidity:</b> "); |
|||
htmlPage += global_hym_indoor; |
|||
|
|||
htmlPage += F("</html>" |
|||
"\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); |
|||
} |
|||
Loading…
Reference in new issue