This repository was archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathAsync_AdvancedWebServer_SendChunked_MQTT.ino
520 lines (395 loc) · 13.6 KB
/
Async_AdvancedWebServer_SendChunked_MQTT.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
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
508
509
510
511
512
513
514
515
516
517
518
519
520
/****************************************************************************************************************************
Async_AdvancedWebServer_SendChunked_MQTT.ino
For RP2040W with CYW43439 WiFi
AsyncWebServer_RP2040W is a library for the RP2040W with CYW43439 WiFi
Based on and modified from ESPAsyncWebServer (https://github.com/me-no-dev/ESPAsyncWebServer)
Built by Khoi Hoang https://github.com/khoih-prog/AsyncWebServer_RP2040W
Licensed under GPLv3 license
Copyright (c) 2015, Majenko Technologies
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
Neither the name of Majenko Technologies nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************************************************************/
// See the list of country codes in
// https://github.com/earlephilhower/cyw43-driver/blob/02533c10a018c6550e9f66f7699e21356f5e4609/src/cyw43_country.h#L59-L111
// To modify https://github.com/earlephilhower/arduino-pico/blob/master/variants/rpipicow/picow_init.cpp
// Check https://github.com/khoih-prog/AsyncWebServer_RP2040W/issues/3#issuecomment-1255676644
#if !( defined(ARDUINO_RASPBERRY_PI_PICO_W) )
#error For RASPBERRY_PI_PICO_W only
#endif
#define _RP2040W_AWS_LOGLEVEL_ 1
#define _ASYNC_MQTT_LOGLEVEL_ 1
///////////////////////////////////////////////////////////////////
#include <pico/cyw43_arch.h>
///////////////////////////////////////////////////////////////////
#include <Ticker.h>
// Check connection every 1s
#define MQTT_CHECK_INTERVAL_MS 1000
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include <AsyncMqtt_Generic.h>
#include <AsyncWebServer_RP2040W.h>
// In bytes
#define STRING_SIZE 40000
AsyncWebServer server(80);
AsyncMqttClient mqttClient;
#define MQTT_HOST "broker.emqx.io" // Broker address
#define MQTT_PORT 1883
const char *PubTopic = "async-mqtt/RP2040W_Pub"; // Topic to publish
#define WIFI_SSID "your_SSID"
#define WIFI_PASSWORD "your_PASSWORD"
int reqCount = 0; // number of requests received
int status = WL_IDLE_STATUS;
#define LED_OFF LOW
#define LED_ON HIGH
////////////////////////////////////////////////////
void connectToMqtt();
void connectToMqttLoop();
// Repeat forever, millis() resolution
Ticker connectToMqttTicker(connectToMqttLoop, MQTT_CHECK_INTERVAL_MS, 0, MILLIS);
bool connectedWiFi = false;
bool connectedMQTT = false;
void printWifiStatus()
{
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("Local IP Address: ");
Serial.println(ip);
// print your board's country code
// #define CYW43_COUNTRY(A, B, REV) ((unsigned char)(A) | ((unsigned char)(B) << 8) | ((REV) << 16))
uint32_t myCountryCode = cyw43_arch_get_country_code();
char countryCode[3] = { 0, 0, 0 };
countryCode[0] = myCountryCode & 0xFF;
countryCode[1] = (myCountryCode >> 8) & 0xFF;
Serial.print("Country code: ");
Serial.println(countryCode);
}
bool connectToWifi()
{
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE)
{
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
Serial.print(F("Connecting to SSID: "));
Serial.println(WIFI_SSID);
#define MAX_NUM_WIFI_CONNECT_TRIES_PER_LOOP 20
uint8_t numWiFiConnectTries = 0;
// attempt to connect to WiFi network
while ( (status != WL_CONNECTED) && (numWiFiConnectTries++ < MAX_NUM_WIFI_CONNECT_TRIES_PER_LOOP) )
{
status = WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
delay(500);
}
if (status != WL_CONNECTED)
{
// Restart for Portenta as something is very wrong
Serial.println("Resetting. Can't connect to any WiFi");
NVIC_SystemReset();
}
printWifiStatus();
connectedWiFi = (status == WL_CONNECTED);
return (status == WL_CONNECTED);
}
bool isWiFiConnected()
{
// You can change longer or shorter depending on your network response
// Shorter => more responsive, but more ping traffic
static uint8_t theTTL = 10;
// Use ping() to test TCP connections
if (WiFi.ping(WiFi.gatewayIP(), theTTL) == theTTL)
{
return true;
}
return false;
}
void connectToMqttLoop()
{
//if ( (WiFi.status() == WL_CONNECTED) && (WiFi.RSSI() != 0) ) // temporary workaround
if (isWiFiConnected())
{
if (!connectedMQTT)
{
mqttClient.connect();
}
if (!connectedWiFi)
{
Serial.println("WiFi reconnected");
connectedWiFi = true;
}
}
else
{
if (connectedWiFi)
{
Serial.println("WiFi disconnected. Reconnecting");
connectedWiFi = false;
connectToWifi();
}
}
}
void connectToMqtt()
{
Serial.println("Connecting to MQTT...");
mqttClient.connect();
}
void printSeparationLine()
{
Serial.println("************************************************");
}
void onMqttConnect(bool sessionPresent)
{
Serial.print("Connected to MQTT broker: ");
Serial.print(MQTT_HOST);
Serial.print(", port: ");
Serial.println(MQTT_PORT);
Serial.print("PubTopic: ");
Serial.println(PubTopic);
connectedMQTT = true;
printSeparationLine();
Serial.print("Session present: ");
Serial.println(sessionPresent);
uint16_t packetIdSub = mqttClient.subscribe(PubTopic, 2);
Serial.print("Subscribing at QoS 2, packetId: ");
Serial.println(packetIdSub);
mqttClient.publish(PubTopic, 0, true, "RP2040W Test1");
Serial.println("Publishing at QoS 0");
uint16_t packetIdPub1 = mqttClient.publish(PubTopic, 1, true, "RP2040W Test2");
Serial.print("Publishing at QoS 1, packetId: ");
Serial.println(packetIdPub1);
uint16_t packetIdPub2 = mqttClient.publish(PubTopic, 2, true, "RP2040W Test3");
Serial.print("Publishing at QoS 2, packetId: ");
Serial.println(packetIdPub2);
printSeparationLine();
}
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason)
{
(void) reason;
connectedMQTT = false;
Serial.println("Disconnected from MQTT.");
}
void onMqttSubscribe(const uint16_t& packetId, const uint8_t& qos)
{
Serial.println("Subscribe acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
Serial.print(" qos: ");
Serial.println(qos);
}
void onMqttUnsubscribe(const uint16_t& packetId)
{
Serial.println("Unsubscribe acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
}
void onMqttMessage(char* topic, char* payload, const AsyncMqttClientMessageProperties& properties,
const size_t& len, const size_t& index, const size_t& total)
{
char message[len + 1];
memcpy(message, payload, len);
message[len] = 0;
Serial.println("Publish received.");
Serial.print(" topic: ");
Serial.println(topic);
Serial.print(" message: ");
Serial.println(message);
Serial.print(" qos: ");
Serial.println(properties.qos);
Serial.print(" dup: ");
Serial.println(properties.dup);
Serial.print(" retain: ");
Serial.println(properties.retain);
Serial.print(" len: ");
Serial.println(len);
Serial.print(" index: ");
Serial.println(index);
Serial.print(" total: ");
Serial.println(total);
}
void onMqttPublish(const uint16_t& packetId)
{
Serial.println("Publish acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
}
///////////////////////////////////////////////////
#define BUFFER_SIZE 512
char temp[BUFFER_SIZE];
void handleRoot(AsyncWebServerRequest *request)
{
static uint32_t pageCount = 0;
static uint32_t maxfreeHeap = 0;
static uint32_t minFreeHeap = 0xFFFFFFFF;
uint32_t curFreeHeap = rp2040.getFreeHeap();
if (maxfreeHeap < curFreeHeap)
maxfreeHeap = curFreeHeap;
if (minFreeHeap > curFreeHeap)
minFreeHeap = curFreeHeap;
digitalWrite(LED_BUILTIN, LED_ON);
int sec = millis() / 1000;
int min = sec / 60;
int hr = min / 60;
int day = hr / 24;
snprintf(temp, BUFFER_SIZE - 1,
"<html>\
<head>\
<meta http-equiv='refresh' content='5'/>\
<title>AsyncWebServer-%s</title>\
<style>\
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
</style>\
</head>\
<body>\
<h2>AsyncWebServer_SendChunked_MQTT!</h2>\
<h3>running WiFi on %s</h3>\
<p>Uptime: %d d %02d:%02d:%02d, pageCount: %lu</p>\
<p>Heap Free: %lu, Max: %lu, Min: %lu</p>\
<img src=\"/test.svg\" />\
</body>\
</html>", BOARD_NAME, BOARD_NAME, day, hr % 24, min % 60, sec % 60, ++pageCount, curFreeHeap, maxfreeHeap, minFreeHeap);
request->send(200, "text/html", temp);
digitalWrite(LED_BUILTIN, LED_OFF);
}
void handleNotFound(AsyncWebServerRequest *request)
{
digitalWrite(LED_BUILTIN, LED_ON);
String message = "File Not Found\n\n";
message += "URI: ";
message += request->url();
message += "\nMethod: ";
message += (request->method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += request->args();
message += "\n";
for (uint8_t i = 0; i < request->args(); i++)
{
message += " " + request->argName(i) + ": " + request->arg(i) + "\n";
}
request->send(404, "text/plain", message);
digitalWrite(LED_BUILTIN, LED_OFF);
}
String out;
void drawGraph(AsyncWebServerRequest *request)
{
out.reserve(STRING_SIZE);
char temp[70];
out = String();
digitalWrite(LED_BUILTIN, LED_ON);
out += "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"1810\" height=\"150\">\n";
out += "<rect width=\"1810\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"2\" stroke=\"rgb(0, 0, 0)\" />\n";
out += "<g stroke=\"blue\">\n";
int y = rand() % 130;
for (int x = 10; x < 5000; x += 10)
{
int y2 = rand() % 130;
sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"2\" />\n", x, 140 - y, x + 10, 140 - y2);
out += temp;
y = y2;
}
out += "</g>\n</svg>\n";
AWS_LOGDEBUG1("Total length to send in chunks =", out.length());
AsyncWebServerResponse *response = request->beginChunkedResponse("image/svg+xml", [](uint8_t *buffer, size_t maxLen, size_t filledLength) -> size_t
{
size_t len = min(maxLen, out.length() - filledLength);
memcpy(buffer, out.c_str() + filledLength, len);
AWS_LOGDEBUG1("Bytes sent in chunk =", len);
return len;
});
request->send(response);
digitalWrite(LED_BUILTIN, LED_OFF);
}
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LED_OFF);
Serial.begin(115200);
while (!Serial && millis() < 5000);
delay(200);
Serial.print("\nStart Async_AdvancedWebServer_SendChunked_MQTT on ");
Serial.print(BOARD_NAME);
Serial.print(" with ");
Serial.println(SHIELD_TYPE);
Serial.println(ASYNCTCP_RP2040W_VERSION);
Serial.println(ASYNC_WEBSERVER_RP2040W_VERSION);
Serial.println(ASYNC_MQTT_GENERIC_VERSION);
///////////////////////////////////
connectToWifi();
///////////////////////////////////
mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
mqttClient.onSubscribe(onMqttSubscribe);
mqttClient.onUnsubscribe(onMqttUnsubscribe);
mqttClient.onMessage(onMqttMessage);
mqttClient.onPublish(onMqttPublish);
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
connectToMqttTicker.start(); //start the ticker.
connectToMqtt();
///////////////////////////////////
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request)
{
handleRoot(request);
});
server.on("/test.svg", HTTP_GET, [](AsyncWebServerRequest * request)
{
drawGraph(request);
});
server.on("/inline", [](AsyncWebServerRequest * request)
{
request->send(200, "text/plain", "This works as well");
});
server.onNotFound(handleNotFound);
server.begin();
Serial.print(F("AsyncWebServer is @ IP : "));
Serial.println(WiFi.localIP());
}
void heartBeatPrint()
{
static int num = 1;
Serial.print(F("."));
if (num == 80)
{
Serial.println();
num = 1;
}
else if (num++ % 10 == 0)
{
Serial.print(F(" "));
}
}
void check_status()
{
static unsigned long checkstatus_timeout = 0;
#define STATUS_CHECK_INTERVAL 10000L
// Send status report every STATUS_REPORT_INTERVAL (60) seconds: we don't need to send updates frequently if there is no status change.
if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0))
{
heartBeatPrint();
checkstatus_timeout = millis() + STATUS_CHECK_INTERVAL;
}
}
void loop()
{
check_status();
}