commit
a2cc4eade5
1 changed files with 142 additions and 0 deletions
@ -0,0 +1,142 @@ |
|||||
|
#include <DHT.h> |
||||
|
#include <ESP8266WiFi.h> |
||||
|
#include <PubSubClient.h> |
||||
|
|
||||
|
// 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 DHT11 |
||||
|
DHT dht(DHTPIN, DHTTYPE); |
||||
|
|
||||
|
// Global Vars
|
||||
|
float global_temp; |
||||
|
float global_hym; |
||||
|
//update
|
||||
|
long previousMillis = 0; |
||||
|
long interval = 60000; //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; |
||||
|
htmlPage += F("</p>" |
||||
|
"<p><b>Humidity:</b> "); |
||||
|
htmlPage += global_hym; |
||||
|
|
||||
|
htmlPage += F("</html>" |
||||
|
"\r\n"); |
||||
|
return htmlPage; |
||||
|
} |
||||
|
|
||||
|
void setup() { |
||||
|
Serial.begin(115200); |
||||
|
initWiFi(); |
||||
|
dht.begin(); // Start DHT
|
||||
|
|
||||
|
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); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void loop() { |
||||
|
global_temp = dht.readTemperature(); |
||||
|
global_hym = dht.readHumidity(); |
||||
|
|
||||
|
Serial.print("Temperatrure: "); |
||||
|
Serial.println(global_temp); |
||||
|
Serial.print("Humidity: "); |
||||
|
Serial.println(global_hym); |
||||
|
|
||||
|
unsigned long currentMillis = millis(); |
||||
|
if(currentMillis - previousMillis > interval) { |
||||
|
previousMillis = currentMillis; |
||||
|
if (!MQTTclient.connected()) { |
||||
|
WiFi.reconnect(); |
||||
|
reconect(); |
||||
|
} |
||||
|
MQTTclient.publish("my/BedRoom/Temperature", String(global_temp).c_str()); |
||||
|
MQTTclient.publish("my/BedRoom/Humidity", String(global_hym).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(); |
||||
|
} |
||||
|
|
||||
|
delay(2000); |
||||
|
} |
||||
Loading…
Reference in new issue