|
| 1 | +/**************************************************************************************************************************** |
| 2 | + AsyncWebServer_SendChunked.ino |
| 3 | +
|
| 4 | + For ESP8266 using W5x00/ENC8266 Ethernet |
| 5 | +
|
| 6 | + AsyncWebServer_Ethernet is a library for the Ethernet with lwIP_5100, lwIP_5500 or lwIP_enc28j60 library |
| 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/AsyncWebServer_Ethernet |
| 10 | + Licensed under GPLv3 license |
| 11 | + *****************************************************************************************************************************/ |
| 12 | + |
| 13 | +#include "defines.h" |
| 14 | + |
| 15 | +#include <AsyncWebServer_Ethernet.h> |
| 16 | + |
| 17 | +// In bytes |
| 18 | +#define STRING_SIZE 50000 |
| 19 | + |
| 20 | +AsyncWebServer server(80); |
| 21 | + |
| 22 | +int reqCount = 0; // number of requests received |
| 23 | + |
| 24 | +#define BUFFER_SIZE 512 |
| 25 | +char temp[BUFFER_SIZE]; |
| 26 | + |
| 27 | +void createPage(String &pageInput) |
| 28 | +{ |
| 29 | + int sec = millis() / 1000; |
| 30 | + int min = sec / 60; |
| 31 | + int hr = min / 60; |
| 32 | + int day = hr / 24; |
| 33 | + |
| 34 | + snprintf(temp, BUFFER_SIZE - 1, |
| 35 | + "<html>\ |
| 36 | +<head>\ |
| 37 | +<meta http-equiv='refresh' content='5'/>\ |
| 38 | +<title>AsyncWebServer-%s</title>\ |
| 39 | +<style>\ |
| 40 | +body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\ |
| 41 | +</style>\ |
| 42 | +</head>\ |
| 43 | +<body>\ |
| 44 | +<h2>AsyncWebServer_SendChunked_WT32_ETH01!</h2>\ |
| 45 | +<h3>running on %s</h3>\ |
| 46 | +<p>Uptime: %d d %02d:%02d:%02d</p>\ |
| 47 | +</body>\ |
| 48 | +</html>", BOARD_NAME, BOARD_NAME, day, hr % 24, min % 60, sec % 60); |
| 49 | + |
| 50 | + pageInput = temp; |
| 51 | +} |
| 52 | + |
| 53 | +void handleNotFound(AsyncWebServerRequest *request) |
| 54 | +{ |
| 55 | + String message = "File Not Found\n\n"; |
| 56 | + |
| 57 | + message += "URI: "; |
| 58 | + message += request->url(); |
| 59 | + message += "\nMethod: "; |
| 60 | + message += (request->method() == HTTP_GET) ? "GET" : "POST"; |
| 61 | + message += "\nArguments: "; |
| 62 | + message += request->args(); |
| 63 | + message += "\n"; |
| 64 | + |
| 65 | + for (uint8_t i = 0; i < request->args(); i++) |
| 66 | + { |
| 67 | + message += " " + request->argName(i) + ": " + request->arg(i) + "\n"; |
| 68 | + } |
| 69 | + |
| 70 | + request->send(404, "text/plain", message); |
| 71 | +} |
| 72 | + |
| 73 | +String out; |
| 74 | + |
| 75 | +void handleRoot(AsyncWebServerRequest *request) |
| 76 | +{ |
| 77 | + // clear the String to start over |
| 78 | + out = String(); |
| 79 | + |
| 80 | + createPage(out); |
| 81 | + |
| 82 | + out += "<html><body>\r\n<table><tr><th>INDEX</th><th>DATA</th></tr>"; |
| 83 | + |
| 84 | + for (uint16_t lineIndex = 0; lineIndex < 500; lineIndex++) |
| 85 | + { |
| 86 | + out += "<tr><td>"; |
| 87 | + out += String(lineIndex); |
| 88 | + out += "</td><td>"; |
| 89 | + out += "AsyncWebServer_SendChunked_ABCDEFGHIJKLMNOPQRSTUVWXYZ</td></tr>"; |
| 90 | + } |
| 91 | + |
| 92 | + out += "</table></body></html>\r\n"; |
| 93 | + |
| 94 | + LOGDEBUG1("Total length to send in chunks =", out.length()); |
| 95 | + |
| 96 | + AsyncWebServerResponse *response = request->beginChunkedResponse("text/html", [](uint8_t *buffer, size_t maxLen, size_t filledLength) -> size_t |
| 97 | + { |
| 98 | + size_t len = min(maxLen, out.length() - filledLength); |
| 99 | + memcpy(buffer, out.c_str() + filledLength, len); |
| 100 | + |
| 101 | + LOGDEBUG1("Bytes sent in chunk =", len); |
| 102 | + |
| 103 | + return len; |
| 104 | + }); |
| 105 | + |
| 106 | + request->send(response); |
| 107 | +} |
| 108 | + |
| 109 | +void initEthernet() |
| 110 | +{ |
| 111 | + SPI.begin(); |
| 112 | + SPI.setClockDivider(SPI_CLOCK_DIV4); |
| 113 | + SPI.setBitOrder(MSBFIRST); |
| 114 | + SPI.setDataMode(SPI_MODE0); |
| 115 | + |
| 116 | +#if !USING_DHCP |
| 117 | + eth.config(localIP, gateway, netMask, gateway); |
| 118 | +#endif |
| 119 | + |
| 120 | + eth.setDefault(); |
| 121 | + |
| 122 | + if (!eth.begin()) |
| 123 | + { |
| 124 | + Serial.println("No Ethernet hardware ... Stop here"); |
| 125 | + |
| 126 | + while (true) |
| 127 | + { |
| 128 | + delay(1000); |
| 129 | + } |
| 130 | + } |
| 131 | + else |
| 132 | + { |
| 133 | + Serial.print("Connecting to network : "); |
| 134 | + |
| 135 | + while (!eth.connected()) |
| 136 | + { |
| 137 | + Serial.print("."); |
| 138 | + delay(1000); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + Serial.println(); |
| 143 | + |
| 144 | +#if USING_DHCP |
| 145 | + Serial.print("Ethernet DHCP IP address: "); |
| 146 | +#else |
| 147 | + Serial.print("Ethernet Static IP address: "); |
| 148 | +#endif |
| 149 | + |
| 150 | + Serial.println(eth.localIP()); |
| 151 | +} |
| 152 | + |
| 153 | +void setup() |
| 154 | +{ |
| 155 | + out.reserve(STRING_SIZE); |
| 156 | + |
| 157 | + pinMode(LED_BUILTIN, OUTPUT); |
| 158 | + digitalWrite(LED_BUILTIN, LED_OFF); |
| 159 | + |
| 160 | + Serial.begin(115200); |
| 161 | + |
| 162 | + while (!Serial && millis() < 5000); |
| 163 | + |
| 164 | + delay(200); |
| 165 | + |
| 166 | + Serial.print("\nStart AsyncWebServer_SendChunked on "); |
| 167 | + Serial.print(BOARD_NAME); |
| 168 | + Serial.print(" with "); |
| 169 | + Serial.println(SHIELD_TYPE); |
| 170 | + Serial.println(ASYNC_WEBSERVER_ETHERNET_VERSION); |
| 171 | + |
| 172 | + initEthernet(); |
| 173 | + |
| 174 | + /////////////////////////////////// |
| 175 | + |
| 176 | + server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) |
| 177 | + { |
| 178 | + handleRoot(request); |
| 179 | + }); |
| 180 | + |
| 181 | + server.on("/inline", [](AsyncWebServerRequest * request) |
| 182 | + { |
| 183 | + request->send(200, "text/plain", "This works as well"); |
| 184 | + }); |
| 185 | + |
| 186 | + server.onNotFound(handleNotFound); |
| 187 | + |
| 188 | + server.begin(); |
| 189 | + |
| 190 | + Serial.print(F("AsyncWebServer is @ IP : ")); |
| 191 | + Serial.println(eth.localIP()); |
| 192 | +} |
| 193 | + |
| 194 | +void heartBeatPrint() |
| 195 | +{ |
| 196 | + static int num = 1; |
| 197 | + |
| 198 | + Serial.print(F(".")); |
| 199 | + |
| 200 | + if (num == 80) |
| 201 | + { |
| 202 | + Serial.println(); |
| 203 | + num = 1; |
| 204 | + } |
| 205 | + else if (num++ % 10 == 0) |
| 206 | + { |
| 207 | + Serial.print(F(" ")); |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +void check_status() |
| 212 | +{ |
| 213 | + static unsigned long checkstatus_timeout = 0; |
| 214 | + |
| 215 | +#define STATUS_CHECK_INTERVAL 10000L |
| 216 | + |
| 217 | + // Send status report every STATUS_REPORT_INTERVAL (60) seconds: we don't need to send updates frequently if there is no status change. |
| 218 | + if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0)) |
| 219 | + { |
| 220 | + heartBeatPrint(); |
| 221 | + checkstatus_timeout = millis() + STATUS_CHECK_INTERVAL; |
| 222 | + } |
| 223 | +} |
| 224 | + |
| 225 | +void loop() |
| 226 | +{ |
| 227 | + check_status(); |
| 228 | +} |
0 commit comments