Bonjoursà tous,
Voilà , à partir d'un code qui fonctionne parfaitement (même à plus de 30 mètres) , Le sketch lit un fichier SPIFFS le place dans un 'array' et
la fonction loop publie les données actuelles puis
publie les informations du fichier en sens inverse pour avoir les données récentes en premier.
J'ai tenté d'ajouter le deep sleep à la fin de setup.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP); esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Et à la fin de la fonction loop:
La carte s'éveille à chaque 2 minutes.Et là ça craint, puisque le code du setup s'exécute mais que la fonction loop ne semble pas fonctionner.
Code : Sélectionner tout - Visualiser dans une fenêtre à part esp_deep_sleep_start();
Voici le output du fichier quand il est vide:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT) configsip: 0, SPIWP:0xee clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00 mode:DIO, clock div:1 load:0x3fff0018,len:4 load:0x3fff001c,len:1216 ho 0 tail 12 room 4 load:0x40078000,len:9720 ho 0 tail 12 room 4 load:0x40080400,len:6352 entry 0x400806b8 Connecting to 23JANVIER-2012_Network_1 .................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................... WiFi connected IP address: 189.143.0.107 ====== Setup Start reading from SPIFFS file ======= ====== Setup End reading from SPIFFS file ======= 0 0 Client Connected bootCount : 1
Ci dessous la page web sans le deep sleep:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 /* ESP32 VROOM PIN 8 pour data 3.3 volts DHT11-DHT22 Sensor with Arduino - Random Nerd Tutorials.zip Rui Santos Complete Project Details http://randomnerdtutorials.com ------------------------------- So, my measurements show that the wake-up time for a 8MHz Arduino is ~1.95ms. And it perfectly matches the datasheet, as it states the Start-up Time from Powerdown and Power-save is 16k CPU cycles for a crystal oscillator, 2ms with the 8MHz crystal. Arduinos with 16MHz clocks should have the wake-up time of around 1ms. ---------------------------- */ #include <EEPROM.h> #include "FS.h" #include "SPIFFS.h" #include <time.h> #include <WiFi.h> #include "DHT.h" #include <NTPClient.h> // Uncomment one of the lines below for whatever DHT sensor type you're using! #define DHTTYPE DHT11 // DHT 11 //#define DHTTYPE DHT21 // DHT 21 (AM2301) //#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 // define the number of bytes you want to access #define EEPROM_SIZE 1 #define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */ #define TIME_TO_SLEEP 120 /* Time ESP32 will go to sleep (in seconds) */ RTC_DATA_ATTR int bootCount = 0; // Replace with your network credentials const char* ssid = ""; const char* password = ""; //ipconfig 189.143.0.106 ou 107 WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP); WiFiServer server(80); // DHT Sensor const int DHTPin = 16; // Initialize DHT sensor. DHT dht(DHTPin, DHTTYPE); // Temporary variables static char celsiusTemp[7]; static char fahrenheitTemp[7]; static char humidityTemp[7]; String aNewLine = "\n"; char linebuf[80]; float hic; byte pinToRead = 15; int charcount = 0; int val = 320; uint8_t xCnt, maxCount = 20; uint8_t decCnt = ( maxCount - 1); String datArray[20]; // our data array holding 30 elements String myDate() { // Variables to save date and time String formattedDate; String dayStamp; String timeStamp; String myDate; while (!timeClient.update()) { timeClient.forceUpdate(); } timeClient.setTimeOffset(-18000); formattedDate = timeClient.getFormattedDate(); int splitT = formattedDate.indexOf("T"); dayStamp = formattedDate.substring(0, splitT); timeStamp = formattedDate.substring(splitT + 1, formattedDate.length() - 1); // myDate = dayStamp; // + " " + timeStamp; //Serial.println(myDate); return myDate; } void eraseFile(String inFile) { Serial.println("Erasing: " + inFile); File file = SPIFFS.open(inFile, "w"); if (!file) { Serial.println("Failed to open " + inFile + "for erasing"); return; } if (file.print("")) { Serial.println("File erased"); } else { Serial.println("Erasing failed"); } file.close(); } void readData() { int xCnt = 0; File f = SPIFFS.open("/gardenTemp.txt", "r"); if (!f) { Serial.println("file open failed"); } Serial.println("====== Reading from SPIFFS file ======="); while (f.available()) { //Lets read line by line from the file String line = f.readStringUntil('\n'); Serial.print(xCnt); Serial.print(" "); Serial.println(line); xCnt ++; } f.close(); Serial.println("====== End reading from SPIFFS file ======="); } //============ apPend Start ================ void apPend(String message) { File f = SPIFFS.open("/gardenTemp.txt", "a"); if (!f) { Serial.println("Failed to open file for apPending"); return; } Serial.printf("ApPending to file: /gardenTemp.txt\n"); if (f.print(message)) { Serial.println("apPended to file"); } else { Serial.println("ApPend failed"); } f.close(); } //============ apPend End ================ double dewPoint(double celsius, double humidity) { // (1) Saturation Vapor Pressure = ESGG(T) double RATIO = 373.15 / (273.15 + celsius); double RHS = -7.90298 * (RATIO - 1); RHS += 5.02808 * log10(RATIO); RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1 / RATIO ))) - 1) ; RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1) ; RHS += log10(1013.246); // factor -3 is to adjust units - Vapor Pressure SVP * humidity double VP = pow(10, RHS - 3) * humidity; // (2) DEWPOINT = F(Vapor Pressure) double T = log(VP / 0.61078); // temp var return (241.88 * T) / (17.558 - T); } void listDir(fs::FS &fs, const char * dirname, uint8_t levels) { Serial.printf("Listing directory: %s\n", dirname); File root = fs.open(dirname); if (!root) { Serial.println("Failed to open directory"); return; } if (!root.isDirectory()) { Serial.println("Not a directory"); return; } //============== // Variables to save date and time String formattedDate; String dayStamp; String timeStamp; String myDate; while (!timeClient.update()) { timeClient.forceUpdate(); } timeClient.setTimeOffset(-18000); formattedDate = timeClient.getFormattedDate(); int splitT = formattedDate.indexOf("T"); dayStamp = formattedDate.substring(0, splitT); timeStamp = formattedDate.substring(splitT + 1, formattedDate.length() - 1); // myDate = dayStamp + " " + timeStamp; //======================= File file = root.openNextFile(); while (file) { if (file.isDirectory()) { Serial.print(" DIR : "); Serial.print (file.name()); time_t t = file.getLastWrite(); struct tm * tmstruct = localtime(&t); Serial.println(" LAST WRITE: " + myDate + "\n"); if (levels) { listDir(fs, file.name(), levels - 1); } } else { Serial.print(" FILE: "); Serial.print(file.name()); Serial.print(" SIZE: "); Serial.print(file.size()); time_t t = file.getLastWrite(); struct tm * tmstruct = localtime(&t); Serial.printf(" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, ( tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour , tmstruct->tm_min, tmstruct->tm_sec); } file = root.openNextFile(); } } //=========== SETUP BEGIN ==================================== void setup() { uint8_t xCnt = 0; // initialize the DHT sensor dht.begin(); //Initialize serial and wait for port to open: Serial.begin(115200); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } // We start by connecting to a WiFi network Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); // attempt to connect to Wifi network: while (WiFi.status() != WL_CONNECTED) { // Connect to WPA/WPA2 network. //Change this line if using open or WEP network: Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); server.begin(); //if a temp file has already been in use, open it and load it's data to array. SPIFFS.begin(); //listDir(SPIFFS, "/", 0); if (!SPIFFS.begin()) { Serial.println(" ! SPIFFS.begin()"); return; } File f = SPIFFS.open("/gardenTemp.txt", "r"); if (!f) { Serial.println("file open failed"); } Serial.println("====== Setup Start reading from SPIFFS file ======="); while (f.available() && xCnt < maxCount) { //Lets read line by line from the file String line = f.readStringUntil('\n'); if (line != 0) { Serial.print(xCnt); Serial.print(" "); Serial.println(line); datArray[xCnt] = line; xCnt ++; } } f.close(); Serial.println("====== Setup End reading from SPIFFS file ======="); esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP); esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); } //=========== SETUP END ==================================== void loop() { Serial.println("Client Connected bootCount : " + (String)(bootCount++)); // listen for incoming clients WiFiClient client = server.available(); if (client) { Serial.println("New client"); memset(linebuf, 0, sizeof(linebuf)); charcount = 0; // an http request ends with a blank line boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); Serial.write(c); //read char by char HTTP request linebuf[charcount] = c; if (charcount < sizeof(linebuf) - 1) charcount++; // if you've gotten to the end of the line (received a newline // character) and the line is blank, the http request has ended, // so you can send a reply if (c == '\n' && currentLineIsBlank) { // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) float h = dht.readHumidity(); // Read temperature as Celsius (the default) float t = dht.readTemperature(); double dew = dewPoint(t, h); // Read temperature as Fahrenheit (isFahrenheit = true) float f = dht.readTemperature(true); // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t) || isnan(f)) { Serial.println("Failed to read from DHT sensor!"); strcpy(celsiusTemp, "Failed"); strcpy(fahrenheitTemp, "Failed"); strcpy(humidityTemp, "Failed"); } else { // Computes temperature values in Celsius + Fahrenheit and Humidity hic = dht.computeHeatIndex(t, h, false); dtostrf(hic, 6, 2, celsiusTemp); float hif = dht.computeHeatIndex(f, h); dtostrf(hif, 6, 2, fahrenheitTemp); dtostrf(h, 6, 2, humidityTemp); // You can delete the following Serial.print's, it's just for debugging purposes val = touchRead(pinToRead); val = map(val, 500, 0, 0, 100); //may use an exponential function Serial.print("myDate() "); aNewLine += myDate(); Serial.println(aNewLine); aNewLine += " : " + (String)val + "% Soil Moisture\tTemperature: " ; aNewLine += (String)t + " °C Humidity: " + (String)h; aNewLine += "%\t Heat index: " + (String)hic + " °C Dew " + (String)dew + "%\n" ; apPend(aNewLine); Serial.println(aNewLine); for (int stat = decCnt; stat >= 0; stat--) { if (datArray[stat] != 0) { Serial.println(datArray[stat]); } Serial.println(datArray[stat]); } } // send a standard http response header client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); //client.println("Content-Type: image/gif"); client.println("Connection: close"); // the connection will be closed after completion of the response client.println(); client.println("<!DOCTYPE HTML><html lang=\"fr\"><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"); client.println("<meta http-equiv=\"refresh\" content=\"30\"></head>"); client.println("<body style=\"color:#f3d6e1;\"><div style=\"font-size: 3.5rem;color:green;\"><p style=\"text-align:center;\">Météo</p>"); if (atoi(celsiusTemp) >= 25) { client.println("<div style=\"color: #930000;\"><p style=\"text-align:center;\">"); } else if (atoi(celsiusTemp) < 25 && atoi(celsiusTemp) >= 5) { client.println("<div style=\"color: #006601;\"><p style=\"text-align:center;\">"); } else if (atoi(celsiusTemp) < 5) { client.println("<div style=\"color: #009191;\">"); } client.println("<p style=\"text-align:center;font-size:14pt;\">"); client.println(myDate()); client.println("</p><p></p><p style=\"text-align:center;\">"); client.print(celsiusTemp); //<img src=\"./temperature.gif\" width=\"504\" height=\"459\"> client.print("°C "); client.print(fahrenheitTemp); client.println(" °F</p><p style=\"text-align:center;\">"); client.print(humidityTemp); client.print(" %H "); client.print(dew); client.println(" ° Rosé</p><p style=\"text-align:center;\">"); client.print(hic); client.print(" °Ressenti "); client.print(val); client.print(" %Soil Moisture</p><p style=\"text-align:center;font-size:12pt;\">"); for (int stat = decCnt; stat >= 0; stat--) { if (datArray[stat] != 0) { client.print(datArray[stat] + "<br>"); } } client.print("</p></div>"); client.print("</body></html>"); break; } if (c == '\n') { // you're starting a new line currentLineIsBlank = true; memset(linebuf, 0, sizeof(linebuf)); charcount = 0; } else if (c != '\r') { // you've gotten a character on the current line currentLineIsBlank = false; } } } // give the web browser time to receive the data delay(10000); // close the connection: client.stop(); Serial.println("Client disconnected for : " + (String)(TIME_TO_SLEEP / 60) + " minutes"); } Serial.flush(); esp_deep_sleep_start(); //delay(60000); }
Merci de votre attention,
JPDaviau
Partager