-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrototype.ino
More file actions
507 lines (420 loc) · 14.6 KB
/
Copy pathPrototype.ino
File metadata and controls
507 lines (420 loc) · 14.6 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <DHT.h>
#include <ArduinoJson.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
// WiFi credentials
const char* ssid = "KRC-101C";
const char* password = "krc101c@";
// Server configuration
const char* serverURL = "https://lostdevs.io/ctrl1/master.php";
const char* secretKey = "lostdev-sensor1-1008200303082003";
// DHT sensor configuration a
#define DHT_PIN 2 // GPIO2 (D4 on NodeMCU)
#define DHT_TYPE DHT11
// #define DHT_TYPE DHT22
DHT dht(DHT_PIN, DHT_TYPE);
// NTP Client
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 19800, 60000); // IST offset: 5.5 hours = 19800 seconds
// BootConfig.h
const int SENSOR_ID = 1;
const float LATITUDE = 28.637270;
const float LONGITUDE = 77.170277;
// sensor reading and data transmission
unsigned long lastSensorReading = 0;
unsigned long lastDataTransmission = 0;
const unsigned long sensorReadInterval = 3000; // Read DHT11 every 3 seconds (respects 2.5s minimum)
const unsigned long transmissionInterval = 500; // Send data every 0.5 seconds (120 packets/minute)
// Cached sensor data structure
struct SensorData {
float temperature;
float humidity;
bool isValid;
unsigned long lastReadTime;
int readingAge; // How many transmissions have used this reading
} cachedData = {-999.0, -999.0, false, 0, 0};
// Error tracking
int wifiFailures = 0;
int sensorFailures = 0;
int httpFailures = 0;
unsigned long lastSuccessfulReading = 0;
unsigned long totalTransmissions = 0; // Track total packets sent
WiFiClient wifiClient;
HTTPClient http;
void setup() {
Serial.begin(115200);
delay(2000);
Serial.println("\n\n=== ESP8266 HIGH-FREQUENCY DHT Data Logger Starting ===");
Serial.print("Chip ID: ");
Serial.println(ESP.getChipId(), HEX);
Serial.print("Free Heap: ");
Serial.println(ESP.getFreeHeap());
Serial.println("Target URL: https://lostdevs.io/ctrl1/master.php");
Serial.printf("Sensor read interval: %lu ms\n", sensorReadInterval);
Serial.printf("Transmission interval: %lu ms\n", transmissionInterval);
Serial.printf("Expected packets per minute: %.1f\n", 60000.0 / transmissionInterval);
Serial.println("=============================================\n");
Serial.println("Initializing DHT sensor...");
dht.begin();
delay(3000);
// Test DHT sensor
Serial.println("Testing DHT sensor...");
bool sensorWorking = false;
for (int i = 0; i < 5; i++) {
delay(2500); // DHT11 needs at least 2 seconds between readings
float testTemp = dht.readTemperature();
float testHumid = dht.readHumidity();
Serial.printf("Test %d: Temp=%.1f°C, Humidity=%.1f%%\n", i+1, testTemp, testHumid);
if (!isnan(testTemp) && !isnan(testHumid)) {
Serial.printf("DHT11 working - Temp: %.0f°C, Humidity: %.0f%%\n", testTemp, testHumid);
sensorWorking = true;
// ADDED: Initialize cached data with first valid reading
cachedData.temperature = testTemp;
cachedData.humidity = testHumid;
cachedData.isValid = true;
cachedData.lastReadTime = millis();
cachedData.readingAge = 0;
break;
}
}
if (!sensorWorking) {
Serial.println("DHT sensor FAILED after 5 attempts!");
}
// Connect to WiFi
connectToWiFi();
// Initialize NTP client for India Standard Time
Serial.println("Initializing NTP client for India Standard Time (IST)...");
timeClient.begin();
timeClient.setTimeOffset(19800); // IST = UTC + 5:30 hours = 19800 seconds
Serial.println("Getting initial time from NTP server...");
int ntpAttempts = 0;
bool ntpSuccess = false;
while (ntpAttempts < 20) {
if (timeClient.update()) {
ntpSuccess = true;
break;
}
Serial.print(".");
delay(1000);
ntpAttempts++;
if (ntpAttempts == 10) {
Serial.println("\nTrying Indian NTP server...");
timeClient.setPoolServerName("in.pool.ntp.org"); // Use India NTP pool
}
}
if (ntpSuccess) {
Serial.println("NTP time synchronized to India Standard Time (IST)");
Serial.print("Current IST time: ");
Serial.println(getISOTimestamp());
} else {
Serial.println("Warning: Could not sync with NTP server");
}
Serial.println("\nSetup completed successfully!");
Serial.println("Starting high-frequency data collection...\n");
}
void loop() {
unsigned long currentTime = millis();
// STEP 1: Check if it's time to read the sensor (every 3 seconds)
if (currentTime - lastSensorReading >= sensorReadInterval) {
readSensorData();
lastSensorReading = currentTime;
}
// STEP 2: Check if it's time to transmit data (every 5 seconds, or whatever you set)
if (currentTime - lastDataTransmission >= transmissionInterval) {
transmitCachedData();
lastDataTransmission = currentTime;
}
delay(50); // Small delay to prevent excessive CPU usage
}
// ADDED: Function to read sensor data and update cache
void readSensorData() {
unsigned long readStartTime = millis();
// Ensure WiFi is connected
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi disconnected. Reconnecting...");
wifiFailures++;
connectToWiFi();
}
// Update time from NTP server periodically
static unsigned long lastNtpUpdate = 0;
if (millis() - lastNtpUpdate > 60000) {
timeClient.update();
lastNtpUpdate = millis();
}
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
unsigned long readEndTime = millis();
unsigned long readDuration = readEndTime - readStartTime;
// Check if readings are valid
if (isnan(temperature) || isnan(humidity)) {
Serial.println("⚠️ Failed to read from DHT sensor! Using cached data.");
sensorFailures++;
// Don't update cache - keep using last valid reading
} else {
// Update cache with new valid reading
cachedData.temperature = temperature;
cachedData.humidity = humidity;
cachedData.isValid = true;
cachedData.lastReadTime = millis();
cachedData.readingAge = 0; // Reset age counter
lastSuccessfulReading = millis();
Serial.printf("📊 NEW SENSOR DATA: Temp=%.1f°C, Humidity=%.1f%% (read in %lums)\n",
temperature, humidity, readDuration);
}
}
// ADDED: Function to transmit cached data
void transmitCachedData() {
if (!cachedData.isValid) {
Serial.println("❌ No valid cached data to transmit!");
return;
}
totalTransmissions++;
cachedData.readingAge++;
unsigned long dataAge = millis() - cachedData.lastReadTime;
String timestamp = getISOTimestamp();
// Create payload with cached data
String formPayload = createFormPayload(
cachedData.temperature,
cachedData.humidity,
timestamp,
dataAge,
false, // Not a sensor error since we have valid cached data
cachedData.readingAge
);
// Send data to server
bool httpSuccess = sendDataToServer(formPayload);
if (!httpSuccess) {
httpFailures++;
}
// Enhanced debug output
Serial.println("=== DATA TRANSMISSION ===");
Serial.printf("📤 Packet #%lu sent\n", totalTransmissions);
Serial.printf("📊 Data: Temp=%.1f°C, Humidity=%.1f%%\n", cachedData.temperature, cachedData.humidity);
Serial.printf("⏱️ Data age: %lu ms (reading #%d)\n", dataAge, cachedData.readingAge);
Serial.printf("📈 Transmission rate: %.1f packets/min\n", totalTransmissions * 60000.0 / millis());
Serial.printf("✅ Status: %s\n", httpSuccess ? "SUCCESS" : "FAILED");
Serial.println("========================\n");
}
void connectToWiFi() {
Serial.println("\n=== WiFi Connection ===");
Serial.print("Connecting to: ");
Serial.println(ssid);
WiFi.disconnect();
delay(1000);
WiFi.mode(WIFI_STA);
delay(1000);
WiFi.begin(ssid, password);
Serial.print("Connecting");
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 30) {
delay(1000);
Serial.print(".");
attempts++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println();
Serial.println("WiFi Connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Signal strength: ");
Serial.print(WiFi.RSSI());
Serial.println(" dBm");
} else {
Serial.println();
Serial.println("WiFi connection FAILED!");
Serial.println("Restarting in 10 seconds...");
delay(10000);
ESP.restart();
}
Serial.println("=====================\n");
}
String getISOTimestamp() {
unsigned long epochTime = timeClient.getEpochTime();
int year, month, day, hour, minute, second;
second = epochTime % 60;
epochTime /= 60;
minute = epochTime % 60;
epochTime /= 60;
hour = epochTime % 24;
epochTime /= 24;
long days = epochTime;
year = 1970;
while (days >= 365) {
if (isLeapYear(year)) {
if (days >= 366) {
days -= 366;
year++;
} else {
break;
}
} else {
days -= 365;
year++;
}
}
int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (isLeapYear(year)) {
daysInMonth[1] = 29;
}
month = 1;
while (days >= daysInMonth[month - 1]) {
days -= daysInMonth[month - 1];
month++;
}
day = days + 1;
char isoTime[25];
sprintf(isoTime, "%04d-%02d-%02dT%02d:%02d:%02dZ", year, month, day, hour, minute, second);
return String(isoTime);
}
bool isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
// MODIFIED: Enhanced payload creation with caching metadata
String createFormPayload(float temp, float humid, String timestamp, unsigned long dataAge, bool sensorError, int readingAge) {
String payload = "";
// Authentication
payload += "secret=" + String(secretKey);
// Sensor ID
payload += "&sensor_unique_id=" + String(SENSOR_ID);
// Temperature in Kelvin
float tempKelvin;
if (sensorError) {
tempKelvin = -999.0;
} else {
tempKelvin = temp + 273.15;
}
payload += "&Temperature(K)=" + String(tempKelvin, 2); // 2 decimal places
// Humidity
if (sensorError) {
payload += "&humidity(%)=-999";
} else {
payload += "&humidity(%)=" + String((int)round(humid)); // Integer
}
// Coordinates
payload += "&sensor_longitude=" + String(LONGITUDE, 6);
payload += "&sensor_latitude=" + String(LATITUDE, 6);
// Timestamp
payload += "&receiving_date=" + timestamp;
// RDF metadata
payload += "&rdf_metadata=sensor_type:DHT11,location:indoor,purpose:environmental_monitoring,transmission_mode:cached_high_frequency";
// ENHANCED: Download metadata with caching info
payload += "&download_metadata=chip_id:" + String(ESP.getChipId(), HEX) +
",data_age_ms:" + String(dataAge) +
",reading_age:" + String(readingAge) +
",total_transmissions:" + String(totalTransmissions) +
",wifi_failures:" + String(wifiFailures) +
",sensor_failures:" + String(sensorFailures) +
",http_failures:" + String(httpFailures);
// Expected noise - lower for cached data since it's the same reading
if (sensorError) {
payload += "&expected_noise=high";
} else if (readingAge > 1) {
payload += "&expected_noise=low"; // Cached data has no sensor noise
} else {
payload += "&expected_noise=medium"; // Fresh DHT11 reading
}
// Spike detection - only relevant for fresh readings
static float lastTemp = -999;
static float lastHumid = -999;
String spikeStatus = "none";
if (!sensorError && readingAge == 1) { // Only check spikes on fresh readings
if (lastTemp != -999 && lastHumid != -999) {
float tempDiff = abs(temp - lastTemp);
float humidDiff = abs(humid - lastHumid);
if (tempDiff > 5.0 || humidDiff > 10.0) {
spikeStatus = "detected";
}
}
lastTemp = temp;
lastHumid = humid;
} else if (readingAge > 1) {
spikeStatus = "cached"; // Indicate this is cached data
}
payload += "&spike=" + spikeStatus;
return payload;
}
// HTTP client with better error handling
bool sendDataToServer(String payload) {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi not connected. Cannot send data.");
return false;
}
// WiFiClientSecure
WiFiClientSecure secureClient;
secureClient.setInsecure(); // For testing - in production will use proper certificates
http.begin(secureClient, serverURL);
// headers
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
http.addHeader("User-Agent", "ESP8266-DHT-Logger/2.0-HighFreq");
http.setTimeout(15000); // 15 second timeout
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
int httpResponseCode = http.POST(payload);
if (httpResponseCode > 0) {
String response = http.getString();
// Only show detailed response for errors or first few transmissions
if (httpResponseCode != 200 || totalTransmissions <= 5) {
Serial.print("HTTP Response Code: ");
Serial.println(httpResponseCode);
Serial.print("Response: ");
Serial.println(response);
}
// Handle different response codes
if (httpResponseCode == 301 || httpResponseCode == 302) {
Serial.println("⚠️ REDIRECT DETECTED!");
String location = http.header("Location");
if (location.length() > 0) {
Serial.print("Redirect to: ");
Serial.println(location);
}
}
http.end();
return (httpResponseCode >= 200 && httpResponseCode < 300);
} else {
Serial.print("HTTP Error Code: ");
Serial.println(httpResponseCode);
// Detailed error reporting
switch(httpResponseCode) {
case HTTPC_ERROR_CONNECTION_REFUSED:
Serial.println("Connection refused");
break;
case HTTPC_ERROR_SEND_HEADER_FAILED:
Serial.println("Send header failed");
break;
case HTTPC_ERROR_SEND_PAYLOAD_FAILED:
Serial.println("Send payload failed");
break;
case HTTPC_ERROR_NOT_CONNECTED:
Serial.println("Not connected");
break;
case HTTPC_ERROR_CONNECTION_LOST:
Serial.println("Connection lost");
break;
case HTTPC_ERROR_NO_STREAM:
Serial.println("No stream");
break;
case HTTPC_ERROR_NO_HTTP_SERVER:
Serial.println("No HTTP server");
break;
case HTTPC_ERROR_TOO_LESS_RAM:
Serial.println("Too less RAM");
break;
case HTTPC_ERROR_ENCODING:
Serial.println("Encoding error");
break;
case HTTPC_ERROR_STREAM_WRITE:
Serial.println("Stream write error");
break;
case HTTPC_ERROR_READ_TIMEOUT:
Serial.println("Read timeout");
break;
default:
Serial.println("Unknown error");
break;
}
http.end();
return false;
}
}