-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZ-Weather.ino
70 lines (56 loc) · 1.73 KB
/
Z-Weather.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <ESP8266WiFi.h>
#include <WiFiManager.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <EEPROM.h>
#include "config.h"
#include "web_pages.h"
#include "web_server.h"
#include "wifi_manager.h"
#include "mqtt_manager.h"
#include "dht_manager.h"
const char* projectName = "Z-Weather";
const char* defaultPassword = "zweather";
unsigned long lastPublishTime = 0;
bool isMqttConfigLoaded = false;
void setup() {
Serial.begin(115200);
Serial.println();
setupDHT(DHTPIN, DHTTYPE);
setupWiFi();
setupWebServer();
MDNS.begin(projectName);
loadMQTTConfig();
isMqttConfigLoaded = (mqttConfig.broker[0] != '\0') && (mqttConfig.port != 0) && (mqttConfig.topic[0] != '\0');
if (isMqttConfigLoaded) {
mqttClient.setServer(mqttConfig.broker, mqttConfig.port);
if (!mqttClient.connected()) {
connectMQTT();
}
}
configTime(GMT_OFFSET_SEC, DAYLIGHT_OFFSET_SEC, NTP_SERVER);
}
void loop() {
MDNS.update();
server.handleClient();
if (!isMqttConfigLoaded) {
Serial.println("MQTT configuration not loaded. Please set up the MQTT Broker IP, Port, and Topic via the web interface (/config).");
return;
}
mqttClient.loop();
unsigned long currentMillis = millis();
if (currentMillis - lastPublishTime >= 60000) {
lastPublishTime = currentMillis;
if (mqttClient.connected()) {
float temp = readDHTTemperature();
float hum = readDHTHumidity();
if (temp != -999.0 && hum != -999.0) {
sendDataToMQTT(temp, hum);
} else {
Serial.println("Invalid sensor readings, skipping MQTT publish.");
}
} else {
Serial.printf("MQTT client not connected. Broker: %s, Port: %d. Skipping publish.\n", mqttConfig.broker, mqttConfig.port);
}
}
}