This repository has been archived by the owner on Feb 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Async_ESP32_MultiTask.ino
468 lines (365 loc) · 12.5 KB
/
Async_ESP32_MultiTask.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
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
/****************************************************************************************************************************
Async_ESP32_MultiTask.ino
For ESP32 boards
Blynk_Async_WM is a library, using AsyncWebServer instead of (ESP8266)WebServer for the ESP8266/ESP32 to enable easy
configuration/reconfiguration and autoconnect/autoreconnect of WiFi/Blynk.
Based on and modified from Blynk library v0.6.1 (https://github.com/blynkkk/blynk-library/releases)
Built by Khoi Hoang (https://github.com/khoih-prog/Blynk_Async_WM)
Licensed under MIT license
********************************************************************************************************************************/
#include "defines.h"
#include "Credentials.h"
#include "dynamicParams.h"
#include <DallasTemperature.h>
#include <OneWire.h>
#define ONE_WIRE_BUS 27 // Temperature
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
//BlynkTimer timer;
//*** TFT *****
#include <TFT_eSPI.h>
#include "bmp.h" // ./ArduinoMenu_library/examples/adafruitGfx_eTFT/TFT_eSPI/ArduinoMenu_LilyGo_TTGO_T-display_demo/bmp.h
#include "Free_Fonts.h" // https://github.com/Bodmer/TFT_eSPI/blob/master/examples/320%20x%20240/Free_Font_Demo/Free_Fonts.h
#include <FS.h>
#include <SPI.h>
#include <Ticker.h>
Ticker led_ticker;
TFT_eSPI tft = TFT_eSPI();
WidgetLED led1(V1);
bool isOnline = false;
bool isBlynkConnected = false;
//***********************************
bool toggle;
float Temperature0 = 100;
float Temperature1 = 100;
float Temperature2 = 100;
float t0 = 200;
float t1 = 200;
float t2 = 200;
volatile bool blynkConnected = false;
#if USE_DYNAMIC_PARAMETERS
void displayCredentials()
{
Serial.println(F("\nYour stored Credentials :"));
for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++)
{
Serial.print(myMenuItems[i].displayName);
Serial.print(F(" = "));
Serial.println(myMenuItems[i].pdata);
}
}
void checkAndDisplayCredentials()
{
static bool displayedCredentials = false;
if (!displayedCredentials)
{
for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++)
{
if (!strlen(myMenuItems[i].pdata))
{
break;
}
if ( i == (NUM_MENU_ITEMS - 1) )
{
displayedCredentials = true;
displayCredentials();
}
}
}
}
#endif
//**** displayLocation *****************
void displayLocation()
{
tft.setFreeFont(FSSB12);
tft.fillScreen(TFT_BLACK);
tft.setCursor(60, 30);
tft.setTextColor(TFT_GREEN, TFT_BLACK);
tft.println("#01:");
tft.setCursor(60, 75);
tft.setTextColor(TFT_BLUE, TFT_BLACK);
tft.println("#02:");
tft.setCursor(60, 120);
tft.setTextColor(TFT_ORANGE, TFT_BLACK);
tft.println("#03:");
}
void displayTemp()
{
// *********** LED toggle **************
if (toggle == 1)
{
tft.fillRect(4, 104, 33, 33, TFT_BLACK);
tft.pushImage(5, 105, 30, 30, rec);
led1.on();
digitalWrite(LED_BUILTIN, 1);
}
else
{
tft.fillRect(4, 104, 33, 33, TFT_BLACK);
led1.off();
digitalWrite(LED_BUILTIN, 0);
}
//***************************************
tft.setFreeFont(FSSB18);
// Define true to simulate DS18B20s for testing if you don't have real DS18B20
#define SIMULATE_ONLY true
#if SIMULATE_ONLY
float curTime = millis() % 100000;
Serial.println("=================");
Serial.printf("Time = %ld ms\n", millis());
Temperature0 = 25.0 + curTime / 100000;
Temperature1 = 26.0 + curTime / 125000;
Temperature2 = 27.0 + curTime / 150000;
#endif
if (Temperature0 != t0)
{
tft.fillRect(120, 0, 120, 39, TFT_BLACK);
tft.setCursor(120, 30);
tft.setTextColor(TFT_GREEN, TFT_BLACK);
tft.println(Temperature0);
Serial.printf("T0 = %5.2f\n", Temperature0);
t0 = Temperature0;
}
if (Temperature1 != t1)
{
tft.fillRect (120, 45, 120, 40, TFT_BLACK);
tft.setCursor(120, 75);
tft.setTextColor(TFT_BLUE, TFT_BLACK);
tft.println(Temperature1);
Serial.printf("T1 = %5.2f\n", Temperature1);
t1 = Temperature1;
}
if (Temperature2 != t2)
{
tft.fillRect (120, 90, 120, 40, TFT_BLACK);
tft.setCursor(120, 120);
tft.setTextColor(TFT_ORANGE, TFT_BLACK);
tft.println(Temperature2);
Serial.printf("T2 = %5.2f\n", Temperature2);
t2 = Temperature2;
}
toggle = !toggle;
}
void readTemp()
{
sensors.requestTemperatures();
Temperature0 = sensors.getTempCByIndex(0);
Temperature1 = sensors.getTempCByIndex(1);
Temperature2 = sensors.getTempCByIndex(2);
//******* wifi ***********************************
if (Blynk.connected())
{
tft.pushImage(5, 5, 30, 30, wifi);
Blynk.virtualWrite(V2, Temperature0);
Blynk.virtualWrite(V3, Temperature1);
Blynk.virtualWrite(V4, Temperature2);
}
else
{
tft.fillRect(5, 5, 30, 30, TFT_BLACK);
}
}
void set_led(byte status)
{
digitalWrite(LED_BUILTIN, status);
}
void checkBlynk()
{
static int num = 1;
if (blynkConnected)
{
set_led(HIGH);
led_ticker.once_ms(111, set_led, (byte) LOW);
Serial.print(F("B"));
}
else
{
Serial.print(F("F"));
}
if (num == 80)
{
Serial.println();
num = 1;
}
else if (num++ % 10 == 0)
{
Serial.print(F(" "));
}
}
////////////////// Start Free-RTOS related code //////////////////
void DisplayEveryNSec( void * pvParameters )
{
#define DISPLAY_INTERVAL_MS 1000L
for (;;)
{
displayTemp();
vTaskDelay(DISPLAY_INTERVAL_MS / portTICK_PERIOD_MS);
}
}
void SensorReadEveryNSec( void * pvParameters )
{
#define SENSOR_READ_INTERVAL_MS 5000L
for (;;)
{
readTemp();
vTaskDelay(SENSOR_READ_INTERVAL_MS / portTICK_PERIOD_MS);
}
}
void InternetBlynkStatus( void * pvParameters )
{
// Check Internet connection every PING_INTERVAL_MS if already Online
#define CHECK_BLYNK_INTERVAL_MS 10000L
for (;;)
{
checkBlynk();
vTaskDelay(CHECK_BLYNK_INTERVAL_MS / portTICK_PERIOD_MS);
}
}
void BlynkRun( void * pvParameters )
{
#define BLYNK_RUN_INTERVAL_MS 250L
for (;;)
{
Blynk.run();
#if USE_DYNAMIC_PARAMETERS
checkAndDisplayCredentials();
#endif
vTaskDelay(BLYNK_RUN_INTERVAL_MS / portTICK_PERIOD_MS);
}
}
void BlynkCheck( void * pvParameters )
{
#define BLYNK_CHECK_INTERVAL_MS 5000L
bool wifiConnected = false;
for (;;)
{
wifiConnected = (WiFi.status() == WL_CONNECTED);
if ( wifiConnected && Blynk.connected() )
{
blynkConnected = true;
}
else
{
blynkConnected = false;
if (wifiConnected)
Serial.println(F("\nBlynk Disconnected"));
else
Serial.println(F("\nWiFi Disconnected"));
}
vTaskDelay(BLYNK_CHECK_INTERVAL_MS / portTICK_PERIOD_MS);
}
}
/////////// Set Core and Priority. Be careful here ///////////
#define USING_CORE_1 0
#if ( ARDUINO_ESP32S2_DEV || ARDUINO_FEATHERS2 || ARDUINO_PROS2 || ARDUINO_MICROS2 )
#define USING_CORE_2 0
#else
#define USING_CORE_2 1
#endif
//Low priority numbers denote low priority tasks. The idle task has priority 0 (tskIDLE_PRIORITY).
//MAX_PRIORITIES = 25 => Can use priority from 0-24
#define DisplayEveryNSec_Priority ( 3 | portPRIVILEGE_BIT )
#define SensorReadEveryNSec_Priority ( 4 | portPRIVILEGE_BIT )
#define InternetBlynkStatus_Priority ( 3 | portPRIVILEGE_BIT )
// Don't use BlynkRun_Priority too low, can drop Blynk connection, especially in SSL
#define BlynkRun_Priority ( 15 | portPRIVILEGE_BIT )
#define BlynkCheck_Priority ( BlynkRun_Priority )
////////////////// End Free-RTOS related code //////////////////
#if USING_CUSTOMS_STYLE
const char NewCustomsStyle[] /*PROGMEM*/ = "<style>div,input{padding:5px;font-size:1em;}input{width:95%;}body{text-align: center;}\
button{background-color:blue;color:white;line-height:2.4rem;font-size:1.2rem;width:100%;}fieldset{border-radius:0.3rem;margin:0px;}</style>";
#endif
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
while (!Serial);
delay(200);
#if ( USE_LITTLEFS || USE_SPIFFS)
Serial.print(F("\nStarting Async_ESP32_MultiTask using "));
Serial.print(CurrentFileFS);
#else
Serial.print(F("\nStarting Async_ESP32_MultiTask using EEPROM"));
#endif
#if USE_SSL
Serial.print(F(" with SSL on ")); Serial.println(ARDUINO_BOARD);
#else
Serial.print(F(" without SSL on ")); Serial.println(ARDUINO_BOARD);
#endif
Serial.println(BLYNK_ASYNC_WM_VERSION);
Serial.println(ESP_DOUBLE_RESET_DETECTOR_VERSION);
tft.begin();
sensors.begin();
delay(100);
tft.setRotation(1);
tft.setSwapBytes(true);
delay(200);
tft.fillScreen(TFT_BLUE);
delay(500);
displayLocation();
sensors.setResolution(12);
// Set config portal SSID and Password
Blynk.setConfigPortal("xxxxxxxxxxxx", "xxxxxxxxxxxx");
// Set config portal IP address
Blynk.setConfigPortalIP(IPAddress(192, 168, 200, 4));
// Set config portal channel, default = 1. Use 0 => random channel from 1-13 to avoid conflict
Blynk.setConfigPortalChannel(0);
// From v1.0.5, select either one of these to set static IP + DNS
Blynk.setSTAStaticIPConfig(IPAddress(192, 168, 2, 230), IPAddress(192, 168, 2, 1), IPAddress(255, 255, 255, 0));
//Blynk.setSTAStaticIPConfig(IPAddress(192, 168, 2, 220), IPAddress(192, 168, 2, 1), IPAddress(255, 255, 255, 0),
// IPAddress(192, 168, 2, 1), IPAddress(8, 8, 8, 8));
//Blynk.setSTAStaticIPConfig(IPAddress(192, 168, 2, 220), IPAddress(192, 168, 2, 1), IPAddress(255, 255, 255, 0),
// IPAddress(4, 4, 4, 4), IPAddress(8, 8, 8, 8));
//////////////////////////////////////////////
#if USING_CUSTOMS_STYLE
Blynk.setCustomsStyle(NewCustomsStyle);
#endif
#if USING_CUSTOMS_HEAD_ELEMENT
Blynk.setCustomsHeadElement("<style>html{filter: invert(10%);}</style>");
#endif
#if USING_CORS_FEATURE
Blynk.setCORSHeader("Your Access-Control-Allow-Origin");
#endif
//////////////////////////////////////////////
// Use this to default DHCP hostname to ESP8266-XXXXXX or ESP32-XXXXXX
//Blynk.begin();
// Use this to personalize DHCP hostname (RFC952 conformed)
// 24 chars max,- only a..z A..Z 0..9 '-' and no '-' as last char
//Blynk.begin("ESP32-WM-Config");
Blynk.begin(HOST_NAME);
if (Blynk.connected())
{
#if ( USE_LITTLEFS || USE_SPIFFS)
Serial.print(F("\nBlynk ESP32 using "));
Serial.print(CurrentFileFS);
Serial.println(F(" connected."));
#else
Serial.println(F("\nBlynk ESP32 using EEPROM connected."));
Serial.printf("EEPROM size = %d bytes, EEPROM start address = %d / 0x%X\n", EEPROM_SIZE, EEPROM_START, EEPROM_START);
#endif
Serial.print(F("Board Name : ")); Serial.println(Blynk.getBoardName());
}
////////////////// Tasks creation //////////////////
#if (BLYNK_WM_RTOS_DEBUG > 0)
Serial.println(F("*********************************************************"));
Serial.printf("configUSE_PORT_OPTIMISED_TASK_SELECTION = %s\n", configUSE_PORT_OPTIMISED_TASK_SELECTION ? "true" : "false");
Serial.printf("configUSE_TIME_SLICING = %s\n", configUSE_TIME_SLICING ? "true" : "false");
Serial.printf("configMAX_PRIORITIES = %d, portPRIVILEGE_BIT = %d\n", configMAX_PRIORITIES, portPRIVILEGE_BIT);
Serial.printf("Task Priority : InternetBlynkStatus = %d, BlynkRun/BlynkCheck = %d\n", InternetBlynkStatus_Priority, BlynkRun_Priority);
Serial.printf("Task Priority : SensorReadEveryNSec = %d, DisplayEveryNSec = %d\n", SensorReadEveryNSec_Priority, DisplayEveryNSec_Priority);
Serial.println(F("*********************************************************"));
#endif
// RTOS function prototype
//BaseType_t xTaskCreatePinnedToCore(TaskFunction_t pvTaskCode, const char *constpcName, const uint32_t STACK_SIZE,
// void *constpvParameters, UBaseType_t IDLE_PRIORITY, TaskHandle_t *constpvCreatedTask, const BaseType_t xCoreID)
xTaskCreatePinnedToCore( BlynkRun, "BlynkRun", 50000, NULL, BlynkRun_Priority, NULL, USING_CORE_2);
xTaskCreatePinnedToCore( BlynkCheck, "BlynkCheck", 5000, NULL, BlynkCheck_Priority, NULL, USING_CORE_2);
xTaskCreatePinnedToCore( DisplayEveryNSec, "DisplayEveryNSec", 20000, NULL, DisplayEveryNSec_Priority, NULL, USING_CORE_2);
xTaskCreatePinnedToCore( SensorReadEveryNSec, "SensorReadEveryNSec", 20000, NULL, SensorReadEveryNSec_Priority, NULL, USING_CORE_2);
xTaskCreatePinnedToCore( InternetBlynkStatus, "InternetBlynkStatus", 5000, NULL, InternetBlynkStatus_Priority, NULL, USING_CORE_2);
////////////////// End Tasks creation //////////////////
}
void loop()
{
// No more Blynk.run() and timer.run() here. All is tasks
}