|
@@ -42,7 +42,7 @@ struct __attribute__((__packed__)) Measurement {
|
42
|
42
|
};
|
43
|
43
|
|
44
|
44
|
struct __attribute__((__packed__)) Header {
|
45
|
|
- uint8_t count;
|
|
45
|
+ uint16_t count;
|
46
|
46
|
uint16_t checksum;
|
47
|
47
|
};
|
48
|
48
|
|
|
@@ -131,7 +131,7 @@ void handleRoot() {
|
131
|
131
|
lastTime = millis();
|
132
|
132
|
waitingForReplies = true;
|
133
|
133
|
|
134
|
|
- Serial.println("broadcast send");
|
|
134
|
+ Serial.println("Sending UDP Broadcast...");
|
135
|
135
|
}
|
136
|
136
|
|
137
|
137
|
// URL nicht vorhanden
|
|
@@ -151,13 +151,17 @@ void handleNotFound(){
|
151
|
151
|
}
|
152
|
152
|
|
153
|
153
|
void setup(void) {
|
154
|
|
- EEPROM.begin(512);
|
|
154
|
+ EEPROM.begin(EEPROM_SIZE);
|
155
|
155
|
|
156
|
156
|
// Debugging
|
157
|
157
|
Serial.begin(115200);
|
158
|
|
- Serial.println("");
|
|
158
|
+ Serial.println();
|
|
159
|
+ Serial.println("ESP-Weather init...");
|
159
|
160
|
|
160
|
|
- SHT21.begin();
|
|
161
|
+ //SHT21.begin();
|
|
162
|
+ // The SHT library is simpy calling Wire.begin(), but the default
|
|
163
|
+ // config does not match the pins i'm using (sda - 2; scl - 0)
|
|
164
|
+ Wire.begin(2, 0);
|
161
|
165
|
|
162
|
166
|
WiFiManager wifiManager;
|
163
|
167
|
// use one or the other, never both!
|
|
@@ -190,8 +194,7 @@ void setup(void) {
|
190
|
194
|
sendNTPpacket(timeServerIP);
|
191
|
195
|
|
192
|
196
|
Udp.begin(localPort);
|
193
|
|
- Serial.println("HTTP server and UDP started");
|
194
|
|
-
|
|
197
|
+ Serial.println("ESP-Weather ready!");
|
195
|
198
|
}
|
196
|
199
|
|
197
|
200
|
void loop(void){
|
|
@@ -199,7 +202,8 @@ void loop(void){
|
199
|
202
|
|
200
|
203
|
// Websocket fuer Browser
|
201
|
204
|
WiFiClient client = serverSocket.available();
|
202
|
|
- if(client.connected() && webSocketServer.handshake(client)) {
|
|
205
|
+ if (client.connected() && webSocketServer.handshake(client)) {
|
|
206
|
+ Serial.println("Building WebSocket Response...");
|
203
|
207
|
|
204
|
208
|
String json = "{\"H\":";
|
205
|
209
|
json += String(SHT21.getHumidity());
|
|
@@ -207,18 +211,20 @@ void loop(void){
|
207
|
211
|
json += "\"T\":";
|
208
|
212
|
json += String(SHT21.getTemperature());
|
209
|
213
|
json += ", \"EEPROM\" : [";
|
210
|
|
- for(int i = 0; i < storage.header.count; i++) {
|
|
214
|
+ for (int i = 0; i < storage.header.count; i++) {
|
211
|
215
|
json += "{\"H\":";
|
212
|
216
|
json += String(storage.data[i].humidity);
|
213
|
217
|
json += ",";
|
214
|
218
|
json += "\"T\":";
|
215
|
219
|
json += String(storage.data[i].temperature);
|
216
|
220
|
json += "}";
|
217
|
|
- if(i < storage.header.count - 1) {
|
|
221
|
+ if (i < storage.header.count - 1) {
|
218
|
222
|
json += ",";
|
219
|
223
|
}
|
220
|
224
|
}
|
221
|
225
|
json += "]}";
|
|
226
|
+
|
|
227
|
+ Serial.println("WebSocket Response:");
|
222
|
228
|
Serial.println(json);
|
223
|
229
|
|
224
|
230
|
webSocketServer.sendData(json);
|
|
@@ -227,14 +233,15 @@ void loop(void){
|
227
|
233
|
}
|
228
|
234
|
|
229
|
235
|
// NTP wiederholen falls keine Antwort
|
230
|
|
- if((timestamp == 0) && ((millis() - lastNTP) > 2000)) {
|
|
236
|
+ if ((timestamp == 0) && ((millis() - lastNTP) > 2000)) {
|
|
237
|
+ Serial.println("NTP packet retry...");
|
231
|
238
|
WiFi.hostByName(ntpServerName, timeServerIP);
|
232
|
239
|
lastNTP = millis();
|
233
|
240
|
sendNTPpacket(timeServerIP);
|
234
|
241
|
}
|
235
|
242
|
|
236
|
243
|
// NTP Paket vom Server erhalten
|
237
|
|
- if(ntp.parsePacket() >= NTP_PACKET_SIZE) {
|
|
244
|
+ if (ntp.parsePacket() >= NTP_PACKET_SIZE) {
|
238
|
245
|
ntp.read(ntpPacketBuffer, NTP_PACKET_SIZE);
|
239
|
246
|
unsigned long highWord = word(ntpPacketBuffer[40], ntpPacketBuffer[41]);
|
240
|
247
|
unsigned long lowWord = word(ntpPacketBuffer[42], ntpPacketBuffer[43]);
|
|
@@ -243,14 +250,17 @@ void loop(void){
|
243
|
250
|
unsigned long epoch = secsSince1900 - seventyYears;
|
244
|
251
|
timestamp = epoch;
|
245
|
252
|
timeReceived = millis();
|
|
253
|
+ Serial.print("Got NTP time: ");
|
246
|
254
|
Serial.println(epoch);
|
247
|
255
|
}
|
248
|
256
|
|
249
|
257
|
// EEPROM-Schreiben jede Stunde
|
250
|
|
- if((((((millis() - timeReceived) / 1000) + timestamp) % 3600) == 0) && (timestamp != 0) && (((millis() - lastStorageTime) > 100000) || storeAtBoot) ) {
|
|
258
|
+ if ((((((millis() - timeReceived) / 1000) + timestamp) % 3600) == 0)
|
|
259
|
+ && (timestamp != 0) && (((millis() - lastStorageTime) > 100000) || storeAtBoot) ) {
|
|
260
|
+ Serial.println("Storing new data packet...");
|
251
|
261
|
lastStorageTime = millis();
|
252
|
262
|
storeAtBoot = 0;
|
253
|
|
- if(storage.header.count < MAX_STORAGE) {
|
|
263
|
+ if (storage.header.count < MAX_STORAGE) {
|
254
|
264
|
storage.header.count++;
|
255
|
265
|
} else {
|
256
|
266
|
for(int i = 0; i < MAX_STORAGE - 1; i++) {
|
|
@@ -264,17 +274,18 @@ void loop(void){
|
264
|
274
|
|
265
|
275
|
// UDP
|
266
|
276
|
int packetSize = Udp.parsePacket();
|
267
|
|
- if(packetSize) {
|
|
277
|
+ if (packetSize) {
|
268
|
278
|
IPAddress remoteIp = Udp.remoteIP();
|
269
|
279
|
// read the packet into packetBufffer
|
270
|
280
|
int len = Udp.read(packetBuffer, 255);
|
271
|
281
|
if (len > 0) {
|
272
|
282
|
packetBuffer[len] = 0;
|
273
|
283
|
}
|
274
|
|
-
|
|
284
|
+
|
|
285
|
+ Serial.print("Got UDP packet: ");
|
275
|
286
|
Serial.println(packetBuffer);
|
276
|
287
|
|
277
|
|
- if(strcmp(packetBuffer, pingBuffer) == 0) {
|
|
288
|
+ if (strcmp(packetBuffer, pingBuffer) == 0) {
|
278
|
289
|
Serial.println("Broadcast");
|
279
|
290
|
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
|
280
|
291
|
Udp.print(echoBuffer);
|
|
@@ -284,8 +295,8 @@ void loop(void){
|
284
|
295
|
}
|
285
|
296
|
}
|
286
|
297
|
|
287
|
|
- if(((millis() - lastTime) >= MAX_WAIT_TIME) && (waitingForReplies == true)) {
|
288
|
|
- Serial.println("waiting done");
|
|
298
|
+ if (((millis() - lastTime) >= MAX_WAIT_TIME) && (waitingForReplies == true)) {
|
|
299
|
+ Serial.println("Timeout, sending response...");
|
289
|
300
|
waitingForReplies = false;
|
290
|
301
|
String message = htmlBegin;
|
291
|
302
|
message += "var clients = Array(";
|
|
@@ -309,7 +320,7 @@ void loop(void){
|
309
|
320
|
}
|
310
|
321
|
|
311
|
322
|
void sendNTPpacket(IPAddress& address) {
|
312
|
|
- Serial.println("sending NTP packet...");
|
|
323
|
+ Serial.println("Sending NTP packet...");
|
313
|
324
|
memset(ntpPacketBuffer, 0, NTP_PACKET_SIZE);
|
314
|
325
|
ntpPacketBuffer[0] = 0b11100011; // LI, Version, Mode
|
315
|
326
|
ntpPacketBuffer[1] = 0;
|
|
@@ -325,4 +336,3 @@ void sendNTPpacket(IPAddress& address) {
|
325
|
336
|
ntp.endPacket();
|
326
|
337
|
}
|
327
|
338
|
|
328
|
|
-
|