|
| 1 | +/**************************************************************************************************************************** |
| 2 | + AsyncWebServer_SendChunked.ino |
| 3 | +
|
| 4 | + For Portenta_H7 (STM32H7) with Vision-Shield Ethernet or Murata WiFi |
| 5 | +
|
| 6 | + Portenta_H7_AsyncWebServer is a library for the Portenta_H7 with Vision-Shield Ethernet or Murata WiFi |
| 7 | +
|
| 8 | + Based on and modified from ESPAsyncWebServer (https://github.com/me-no-dev/ESPAsyncWebServer) |
| 9 | + Built by Khoi Hoang https://github.com/khoih-prog/Portenta_H7_AsyncWebServer |
| 10 | + Licensed under GPLv3 license |
| 11 | + *****************************************************************************************************************************/ |
| 12 | + |
| 13 | +#if !( defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) ) |
| 14 | + #error For Portenta_H7 only |
| 15 | +#endif |
| 16 | + |
| 17 | +#define _PORTENTA_H7_ATCP_LOGLEVEL_ 1 |
| 18 | +#define _PORTENTA_H7_AWS_LOGLEVEL_ 4 |
| 19 | + |
| 20 | +#define USE_ETHERNET_PORTENTA_H7 true |
| 21 | + |
| 22 | +#include <Portenta_Ethernet.h> |
| 23 | +#include <Ethernet.h> |
| 24 | +#warning Using Portenta_Ethernet lib for Portenta_H7. |
| 25 | + |
| 26 | +#include <Portenta_H7_AsyncWebServer.h> |
| 27 | + |
| 28 | +// In bytes |
| 29 | +#define STRING_SIZE 50000 |
| 30 | + |
| 31 | +// Enter a MAC address and IP address for your controller below. |
| 32 | +#define NUMBER_OF_MAC 20 |
| 33 | + |
| 34 | +byte mac[][NUMBER_OF_MAC] = |
| 35 | +{ |
| 36 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x01 }, |
| 37 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x02 }, |
| 38 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x03 }, |
| 39 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x04 }, |
| 40 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x05 }, |
| 41 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x06 }, |
| 42 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x07 }, |
| 43 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x08 }, |
| 44 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x09 }, |
| 45 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0A }, |
| 46 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0B }, |
| 47 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0C }, |
| 48 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0D }, |
| 49 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0E }, |
| 50 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0F }, |
| 51 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x10 }, |
| 52 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x11 }, |
| 53 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x12 }, |
| 54 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x13 }, |
| 55 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x14 }, |
| 56 | +}; |
| 57 | +// Select the IP address according to your local network |
| 58 | +IPAddress ip(192, 168, 2, 232); |
| 59 | + |
| 60 | +AsyncWebServer server(80); |
| 61 | + |
| 62 | +int reqCount = 0; // number of requests received |
| 63 | + |
| 64 | +#define BUFFER_SIZE 512 |
| 65 | +char temp[BUFFER_SIZE]; |
| 66 | + |
| 67 | +void createPage(String &pageInput) |
| 68 | +{ |
| 69 | + int sec = millis() / 1000; |
| 70 | + int min = sec / 60; |
| 71 | + int hr = min / 60; |
| 72 | + int day = hr / 24; |
| 73 | + |
| 74 | + snprintf(temp, BUFFER_SIZE - 1, |
| 75 | + "<html>\ |
| 76 | +<head>\ |
| 77 | +<meta http-equiv='refresh' content='5'/>\ |
| 78 | +<title>AsyncWebServer-%s</title>\ |
| 79 | +<style>\ |
| 80 | +body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\ |
| 81 | +</style>\ |
| 82 | +</head>\ |
| 83 | +<body>\ |
| 84 | +<h2>AsyncWebServer_SendChunked_Portenta_H7!</h2>\ |
| 85 | +<h3>running on %s</h3>\ |
| 86 | +<p>Uptime: %d d %02d:%02d:%02d</p>\ |
| 87 | +</body>\ |
| 88 | +</html>", BOARD_NAME, BOARD_NAME, day, hr % 24, min % 60, sec % 60); |
| 89 | + |
| 90 | + pageInput = temp; |
| 91 | +} |
| 92 | + |
| 93 | +void handleNotFound(AsyncWebServerRequest *request) |
| 94 | +{ |
| 95 | + String message = "File Not Found\n\n"; |
| 96 | + |
| 97 | + message += "URI: "; |
| 98 | + message += request->url(); |
| 99 | + message += "\nMethod: "; |
| 100 | + message += (request->method() == HTTP_GET) ? "GET" : "POST"; |
| 101 | + message += "\nArguments: "; |
| 102 | + message += request->args(); |
| 103 | + message += "\n"; |
| 104 | + |
| 105 | + for (uint8_t i = 0; i < request->args(); i++) |
| 106 | + { |
| 107 | + message += " " + request->argName(i) + ": " + request->arg(i) + "\n"; |
| 108 | + } |
| 109 | + |
| 110 | + request->send(404, "text/plain", message); |
| 111 | +} |
| 112 | + |
| 113 | +String out; |
| 114 | + |
| 115 | +void handleRoot(AsyncWebServerRequest *request) |
| 116 | +{ |
| 117 | + char temp[70]; |
| 118 | + |
| 119 | + // clear the String to start over |
| 120 | + out = String(); |
| 121 | + |
| 122 | + createPage(out); |
| 123 | + |
| 124 | + out += "<html><body>\r\n<table><tr><th>INDEX</th><th>DATA</th></tr>"; |
| 125 | + |
| 126 | + for (uint16_t lineIndex = 0; lineIndex < 500; lineIndex++) |
| 127 | + { |
| 128 | + out += "<tr><td>"; |
| 129 | + out += String(lineIndex); |
| 130 | + out += "</td><td>"; |
| 131 | + out += "Portenta_H7_AsyncWebServer_SendChunked_ABCDEFGHIJKLMNOPQRSTUVWXYZ</td></tr>"; |
| 132 | + } |
| 133 | + |
| 134 | + out += "</table></body></html>\r\n"; |
| 135 | + |
| 136 | + AWS_LOGDEBUG1("Total length to send in chunks =", out.length()); |
| 137 | + |
| 138 | + AsyncWebServerResponse *response = request->beginChunkedResponse("text/html", [](uint8_t *buffer, size_t maxLen, size_t filledLength) -> size_t |
| 139 | + { |
| 140 | + size_t len = min(maxLen, out.length() - filledLength); |
| 141 | + memcpy(buffer, out.c_str() + filledLength, len); |
| 142 | + |
| 143 | + AWS_LOGDEBUG1("Bytes sent in chunk =", len); |
| 144 | + |
| 145 | + return len; |
| 146 | + }); |
| 147 | + |
| 148 | + request->send(response); |
| 149 | +} |
| 150 | + |
| 151 | +void setup() |
| 152 | +{ |
| 153 | + out.reserve(STRING_SIZE); |
| 154 | + |
| 155 | + Serial.begin(115200); |
| 156 | + |
| 157 | + while (!Serial && millis() < 5000); |
| 158 | + |
| 159 | + delay(200); |
| 160 | + |
| 161 | + Serial.print("\nStart AsyncWebServer_SendChunked on "); |
| 162 | + Serial.print(BOARD_NAME); |
| 163 | + Serial.print(" with "); |
| 164 | + Serial.println(SHIELD_TYPE); |
| 165 | + Serial.println(PORTENTA_H7_ASYNC_TCP_VERSION); |
| 166 | + Serial.println(PORTENTA_H7_ASYNC_WEBSERVER_VERSION); |
| 167 | + |
| 168 | + /////////////////////////////////// |
| 169 | + |
| 170 | + // start the ethernet connection and the server |
| 171 | + // Use random mac |
| 172 | + uint16_t index = millis() % NUMBER_OF_MAC; |
| 173 | + |
| 174 | + // Use Static IP |
| 175 | + //Ethernet.begin(mac[index], ip); |
| 176 | + // Use DHCP dynamic IP and random mac |
| 177 | + Ethernet.begin(mac[index]); |
| 178 | + |
| 179 | + if (Ethernet.hardwareStatus() == EthernetNoHardware) |
| 180 | + { |
| 181 | + Serial.println("No Ethernet found. Stay here forever"); |
| 182 | + |
| 183 | + while (true) |
| 184 | + { |
| 185 | + delay(1); // do nothing, no point running without Ethernet hardware |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + if (Ethernet.linkStatus() == LinkOFF) |
| 190 | + { |
| 191 | + Serial.println("Not connected Ethernet cable"); |
| 192 | + } |
| 193 | + |
| 194 | + Serial.print(F("Using mac index = ")); |
| 195 | + Serial.println(index); |
| 196 | + |
| 197 | + Serial.print(F("Connected! IP address: ")); |
| 198 | + Serial.println(Ethernet.localIP()); |
| 199 | + |
| 200 | + /////////////////////////////////// |
| 201 | + |
| 202 | + server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) |
| 203 | + { |
| 204 | + handleRoot(request); |
| 205 | + }); |
| 206 | + |
| 207 | + server.on("/inline", [](AsyncWebServerRequest * request) |
| 208 | + { |
| 209 | + request->send(200, "text/plain", "This works as well"); |
| 210 | + }); |
| 211 | + |
| 212 | + server.onNotFound(handleNotFound); |
| 213 | + |
| 214 | + server.begin(); |
| 215 | + |
| 216 | + Serial.print(F("AsyncWebServer is @ IP : ")); |
| 217 | + Serial.println(Ethernet.localIP()); |
| 218 | +} |
| 219 | + |
| 220 | +void heartBeatPrint() |
| 221 | +{ |
| 222 | + static int num = 1; |
| 223 | + |
| 224 | + Serial.print(F(".")); |
| 225 | + |
| 226 | + if (num == 80) |
| 227 | + { |
| 228 | + Serial.println(); |
| 229 | + num = 1; |
| 230 | + } |
| 231 | + else if (num++ % 10 == 0) |
| 232 | + { |
| 233 | + Serial.print(F(" ")); |
| 234 | + } |
| 235 | +} |
| 236 | + |
| 237 | +void check_status() |
| 238 | +{ |
| 239 | + static unsigned long checkstatus_timeout = 0; |
| 240 | + |
| 241 | +#define STATUS_CHECK_INTERVAL 10000L |
| 242 | + |
| 243 | + // Send status report every STATUS_REPORT_INTERVAL (60) seconds: we don't need to send updates frequently if there is no status change. |
| 244 | + if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0)) |
| 245 | + { |
| 246 | + heartBeatPrint(); |
| 247 | + checkstatus_timeout = millis() + STATUS_CHECK_INTERVAL; |
| 248 | + } |
| 249 | +} |
| 250 | + |
| 251 | +void loop() |
| 252 | +{ |
| 253 | + check_status(); |
| 254 | +} |
0 commit comments