|
@@ -25,8 +25,11 @@
|
25
|
25
|
#include <WiFiManager.h>
|
26
|
26
|
#include "config.h"
|
27
|
27
|
#include "ntp.h"
|
|
28
|
+#include "static.h"
|
28
|
29
|
#include "storage.h"
|
29
|
30
|
|
|
31
|
+#define DEBUG
|
|
32
|
+
|
30
|
33
|
SHT21 sensor;
|
31
|
34
|
ESP8266WebServer server(WEB_PORT);
|
32
|
35
|
WiFiServer serverSocket(WEBSOCKET_PORT);
|
|
@@ -41,8 +44,10 @@ byte storeAtBoot = 1;
|
41
|
44
|
unsigned long lastTime = 0;
|
42
|
45
|
bool waitingForReplies = false;
|
43
|
46
|
|
44
|
|
-void handleRoot() {
|
|
47
|
+static void handleRoot() {
|
|
48
|
+#ifdef DEBUG
|
45
|
49
|
Serial.println("Sending UDP Broadcast...");
|
|
50
|
+#endif // DEBUG
|
46
|
51
|
|
47
|
52
|
// Send UDP broadcast to other modules
|
48
|
53
|
udp.beginPacket(broadcastIP, BROADCAST_PORT);
|
|
@@ -54,12 +59,21 @@ void handleRoot() {
|
54
|
59
|
waitingForReplies = true;
|
55
|
60
|
}
|
56
|
61
|
|
57
|
|
-void handleJS() {
|
|
62
|
+static void handleJS() {
|
58
|
63
|
String message = F(JS_FILE);
|
59
|
64
|
server.send(200, "text/javascript", message);
|
60
|
65
|
}
|
61
|
66
|
|
62
|
|
-void handleNotFound() {
|
|
67
|
+static void handleCSS() {
|
|
68
|
+ String message = F(CSS_FILE);
|
|
69
|
+ server.send(200, "text/css", message);
|
|
70
|
+}
|
|
71
|
+
|
|
72
|
+static void handleFavicon() {
|
|
73
|
+ server.send_P(200, faviconMimeType, (PGM_P)favicon, faviconSize);
|
|
74
|
+}
|
|
75
|
+
|
|
76
|
+static void handleNotFound() {
|
63
|
77
|
String message = "File Not Found\n\n";
|
64
|
78
|
message += "URI: ";
|
65
|
79
|
message += server.uri();
|
|
@@ -75,10 +89,12 @@ void handleNotFound() {
|
75
|
89
|
}
|
76
|
90
|
|
77
|
91
|
void setup(void) {
|
78
|
|
- // Debugging
|
79
|
|
- Serial.begin(115200);
|
|
92
|
+ Serial.begin(DEBUG_BAUDRATE);
|
|
93
|
+
|
|
94
|
+#ifdef DEBUG
|
80
|
95
|
Serial.println();
|
81
|
96
|
Serial.println("ESP-Weather init...");
|
|
97
|
+#endif // DEBUG
|
82
|
98
|
|
83
|
99
|
//sensor.begin();
|
84
|
100
|
// The SHT library is simpy calling Wire.begin(), but the default
|
|
@@ -100,28 +116,36 @@ void setup(void) {
|
100
|
116
|
if (WiFi.status() != WL_CONNECTED) {
|
101
|
117
|
while (WiFi.status() != WL_CONNECTED) {
|
102
|
118
|
delay(500);
|
|
119
|
+#ifdef DEBUG
|
103
|
120
|
Serial.print(".");
|
|
121
|
+#endif // DEBUG
|
104
|
122
|
}
|
|
123
|
+#ifdef DEBUG
|
105
|
124
|
Serial.println("");
|
|
125
|
+#endif // DEBUG
|
106
|
126
|
}
|
107
|
127
|
|
|
128
|
+#ifdef DEBUG
|
108
|
129
|
Serial.print("IP address: ");
|
109
|
130
|
Serial.println(WiFi.localIP());
|
|
131
|
+#endif // DEBUG
|
110
|
132
|
|
111
|
133
|
broadcastIP = ~WiFi.subnetMask() | WiFi.gatewayIP();
|
112
|
134
|
|
113
|
135
|
server.on("/", handleRoot);
|
114
|
136
|
server.on("/index.html", handleRoot);
|
115
|
137
|
server.on("/view.js", handleJS);
|
|
138
|
+ server.on("/style.css", handleCSS);
|
|
139
|
+ server.on("/favicon.ico", handleFavicon);
|
116
|
140
|
server.onNotFound(handleNotFound);
|
117
|
141
|
server.begin();
|
118
|
|
-
|
119
|
142
|
serverSocket.begin();
|
120
|
|
-
|
121
|
143
|
ntpInit();
|
122
|
|
-
|
123
|
144
|
udp.begin(BROADCAST_PORT);
|
|
145
|
+
|
|
146
|
+#ifdef DEBUG
|
124
|
147
|
Serial.println("ESP-Weather ready!");
|
|
148
|
+#endif // DEBUG
|
125
|
149
|
}
|
126
|
150
|
|
127
|
151
|
void loop(void) {
|
|
@@ -152,8 +176,10 @@ void loop(void) {
|
152
|
176
|
}
|
153
|
177
|
json += "]}";
|
154
|
178
|
|
|
179
|
+#ifdef DEBUG
|
155
|
180
|
Serial.println("WebSocket Response:");
|
156
|
181
|
Serial.println(json);
|
|
182
|
+#endif // DEBUG
|
157
|
183
|
|
158
|
184
|
webSocketServer.sendData(json);
|
159
|
185
|
client.flush();
|
|
@@ -163,7 +189,9 @@ void loop(void) {
|
163
|
189
|
// EEPROM-Schreiben jede Stunde
|
164
|
190
|
if ((((((millis() - timeReceived) / 1000) + timestamp) % 3600) == 0)
|
165
|
191
|
&& (timestamp != 0) && (((millis() - lastStorageTime) > 100000) || storeAtBoot) ) {
|
|
192
|
+#ifdef DEBUG
|
166
|
193
|
Serial.println("Storing new data packet...");
|
|
194
|
+#endif // DEBUG
|
167
|
195
|
lastStorageTime = millis();
|
168
|
196
|
storeAtBoot = 0;
|
169
|
197
|
if (storage.header.count < MAX_STORAGE) {
|
|
@@ -188,11 +216,15 @@ void loop(void) {
|
188
|
216
|
packetBuffer[len] = 0;
|
189
|
217
|
}
|
190
|
218
|
|
|
219
|
+#ifdef DEBUG
|
191
|
220
|
Serial.print("Got UDP packet: ");
|
192
|
221
|
Serial.println(packetBuffer);
|
|
222
|
+#endif // DEBUG
|
193
|
223
|
|
194
|
224
|
if (strcmp(packetBuffer, UDP_PING_CONTENTS) == 0) {
|
|
225
|
+#ifdef DEBUG
|
195
|
226
|
Serial.println("Broadcast");
|
|
227
|
+#endif // DEBUG
|
196
|
228
|
udp.beginPacket(udp.remoteIP(), udp.remotePort());
|
197
|
229
|
udp.print(UDP_ECHO_CONTENTS);
|
198
|
230
|
udp.endPacket();
|
|
@@ -202,7 +234,10 @@ void loop(void) {
|
202
|
234
|
}
|
203
|
235
|
|
204
|
236
|
if (((millis() - lastTime) >= MAX_BROADCAST_WAIT_TIME) && (waitingForReplies == true)) {
|
|
237
|
+#ifdef DEBUG
|
205
|
238
|
Serial.println("Timeout, sending response...");
|
|
239
|
+#endif // DEBUG
|
|
240
|
+
|
206
|
241
|
waitingForReplies = false;
|
207
|
242
|
String message = F(HTML_BEGIN);
|
208
|
243
|
message += "var clients = Array(";
|