|
| 1 | +/**************************************************************************************************************************** |
| 2 | + AsyncFSBrowser_WT32_ETH01.ino - Dead simple AsyncWebServer for WT32_ETH01 |
| 3 | + |
| 4 | + For LAN8720 Ethernet in WT32_ETH01 (ESP32 + LAN8720) |
| 5 | +
|
| 6 | + AsyncWebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run AsyncWebServer |
| 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_WT32_ETH01 |
| 10 | + Licensed under GPLv3 license |
| 11 | + |
| 12 | + Version: 1.2.3 |
| 13 | +
|
| 14 | + Version Modified By Date Comments |
| 15 | + ------- ----------- ---------- ----------- |
| 16 | + 1.2.3 K Hoang 17/07/2021 Initial porting for WT32_ETH01 (ESP32 + LAN8720). Sync with ESPAsyncWebServer v1.2.3 |
| 17 | + *****************************************************************************************************************************/ |
| 18 | + |
| 19 | +#if !( defined(ESP32) ) |
| 20 | + #error This code is designed for WT32_ETH01 to run on ESP32 platform! Please check your Tools->Board setting. |
| 21 | +#endif |
| 22 | + |
| 23 | +#include <Arduino.h> |
| 24 | + |
| 25 | +#define _ASYNC_WEBSERVER_LOGLEVEL_ 4 |
| 26 | + |
| 27 | +// Select the IP address according to your local network |
| 28 | +IPAddress myIP(192, 168, 2, 232); |
| 29 | +IPAddress myGW(192, 168, 2, 1); |
| 30 | +IPAddress mySN(255, 255, 255, 0); |
| 31 | + |
| 32 | +// Google DNS Server IP |
| 33 | +IPAddress myDNS(8, 8, 8, 8); |
| 34 | + |
| 35 | +#include <AsyncTCP.h> |
| 36 | + |
| 37 | +#include <AsyncWebServer_WT32_ETH01.h> |
| 38 | + |
| 39 | +// SKETCH BEGIN |
| 40 | +AsyncWebServer server(80); |
| 41 | +AsyncWebSocket ws("/ws"); |
| 42 | +AsyncEventSource events("/events"); |
| 43 | + |
| 44 | +void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) |
| 45 | +{ |
| 46 | + if (type == WS_EVT_CONNECT) |
| 47 | + { |
| 48 | + Serial.printf("ws[%s][%u] connect\n", server->url(), client->id()); |
| 49 | + |
| 50 | + client->printf("Hello Client %u :)", client->id()); |
| 51 | + client->ping(); |
| 52 | + } |
| 53 | + else if (type == WS_EVT_DISCONNECT) |
| 54 | + { |
| 55 | + Serial.printf("ws[%s][%u] disconnect\n", server->url(), client->id()); |
| 56 | + } |
| 57 | + else if (type == WS_EVT_ERROR) |
| 58 | + { |
| 59 | + Serial.printf("ws[%s][%u] error(%u): %s\n", server->url(), client->id(), *((uint16_t*)arg), (char*)data); |
| 60 | + } |
| 61 | + else if (type == WS_EVT_PONG) |
| 62 | + { |
| 63 | + Serial.printf("ws[%s][%u] pong[%u]: %s\n", server->url(), client->id(), len, (len) ? (char*)data : ""); |
| 64 | + } |
| 65 | + else if (type == WS_EVT_DATA) |
| 66 | + { |
| 67 | + AwsFrameInfo * info = (AwsFrameInfo*)arg; |
| 68 | + String msg = ""; |
| 69 | + |
| 70 | + if (info->final && info->index == 0 && info->len == len) |
| 71 | + { |
| 72 | + //the whole message is in a single frame and we got all of it's data |
| 73 | + Serial.printf("ws[%s][%u] %s-message[%llu]: ", server->url(), client->id(), (info->opcode == WS_TEXT) ? "text" : "binary", info->len); |
| 74 | + |
| 75 | + if (info->opcode == WS_TEXT) |
| 76 | + { |
| 77 | + for (size_t i = 0; i < info->len; i++) |
| 78 | + { |
| 79 | + msg += (char) data[i]; |
| 80 | + } |
| 81 | + } |
| 82 | + else |
| 83 | + { |
| 84 | + char buff[6]; |
| 85 | + |
| 86 | + for (size_t i = 0; i < info->len; i++) |
| 87 | + { |
| 88 | + sprintf(buff, "%02x ", (uint8_t) data[i]); |
| 89 | + msg += buff ; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + Serial.printf("%s\n", msg.c_str()); |
| 94 | + |
| 95 | + if (info->opcode == WS_TEXT) |
| 96 | + client->text("I got your text message"); |
| 97 | + else |
| 98 | + client->binary("I got your binary message"); |
| 99 | + } |
| 100 | + else |
| 101 | + { |
| 102 | + //message is comprised of multiple frames or the frame is split into multiple packets |
| 103 | + |
| 104 | + if (info->index == 0) |
| 105 | + { |
| 106 | + if (info->num == 0) |
| 107 | + Serial.printf("ws[%s][%u] %s-message start\n", server->url(), client->id(), (info->message_opcode == WS_TEXT) ? "text" : "binary"); |
| 108 | + |
| 109 | + Serial.printf("ws[%s][%u] frame[%u] start[%llu]\n", server->url(), client->id(), info->num, info->len); |
| 110 | + } |
| 111 | + |
| 112 | + Serial.printf("ws[%s][%u] frame[%u] %s[%llu - %llu]: ", server->url(), client->id(), info->num, (info->message_opcode == WS_TEXT) ? "text" : "binary", info->index, info->index + len); |
| 113 | + |
| 114 | + if (info->opcode == WS_TEXT) |
| 115 | + { |
| 116 | + for (size_t i = 0; i < len; i++) |
| 117 | + { |
| 118 | + msg += (char) data[i]; |
| 119 | + } |
| 120 | + } |
| 121 | + else |
| 122 | + { |
| 123 | + char buff[6]; |
| 124 | + |
| 125 | + for (size_t i = 0; i < len; i++) |
| 126 | + { |
| 127 | + sprintf(buff, "%02x ", (uint8_t) data[i]); |
| 128 | + msg += buff ; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + Serial.printf("%s\n", msg.c_str()); |
| 133 | + |
| 134 | + if ((info->index + len) == info->len) |
| 135 | + { |
| 136 | + Serial.printf("ws[%s][%u] frame[%u] end[%llu]\n", server->url(), client->id(), info->num, info->len); |
| 137 | + |
| 138 | + if (info->final) |
| 139 | + { |
| 140 | + Serial.printf("ws[%s][%u] %s-message end\n", server->url(), client->id(), (info->message_opcode == WS_TEXT) ? "text" : "binary"); |
| 141 | + |
| 142 | + if (info->message_opcode == WS_TEXT) |
| 143 | + client->text("I got your text message"); |
| 144 | + else |
| 145 | + client->binary("I got your binary message"); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +const char * hostName = "esp-async"; |
| 153 | +const char* http_username = "admin"; |
| 154 | +const char* http_password = "admin"; |
| 155 | + |
| 156 | +void setup() |
| 157 | +{ |
| 158 | + Serial.begin(115200); |
| 159 | + while (!Serial); |
| 160 | + |
| 161 | + delay(200); |
| 162 | + |
| 163 | + Serial.print(F("\nStart AsyncFSBrowser_WT32_ETH01 on ")); Serial.print(BOARD_NAME); |
| 164 | + Serial.print(F(" with ")); Serial.println(SHIELD_TYPE); |
| 165 | + Serial.println(ASYNC_WEBSERVER_WT32_ETH01_VERSION); |
| 166 | + |
| 167 | + //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO, |
| 168 | + // eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE); |
| 169 | + //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE); |
| 170 | + ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER); |
| 171 | + |
| 172 | + // Static IP, leave without this line to get IP via DHCP |
| 173 | + //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0); |
| 174 | + ETH.config(myIP, myGW, mySN, myDNS); |
| 175 | + |
| 176 | + WT32_ETH01_onEvent(); |
| 177 | + |
| 178 | + WT32_ETH01_waitForConnect(); |
| 179 | + |
| 180 | + ws.onEvent(onWsEvent); |
| 181 | + server.addHandler(&ws); |
| 182 | + |
| 183 | + events.onConnect([](AsyncEventSourceClient * client) |
| 184 | + { |
| 185 | + client->send("hello!", NULL, millis(), 1000); |
| 186 | + }); |
| 187 | + |
| 188 | + server.addHandler(&events); |
| 189 | + |
| 190 | + server.on("/board", HTTP_GET, [](AsyncWebServerRequest * request) |
| 191 | + { |
| 192 | + request->send(200, "text/plain", String(BOARD_NAME)); |
| 193 | + }); |
| 194 | + |
| 195 | + server.onNotFound([](AsyncWebServerRequest * request) |
| 196 | + { |
| 197 | + Serial.printf("NOT_FOUND: "); |
| 198 | + if (request->method() == HTTP_GET) |
| 199 | + Serial.printf("GET"); |
| 200 | + else if (request->method() == HTTP_POST) |
| 201 | + Serial.printf("POST"); |
| 202 | + else if (request->method() == HTTP_DELETE) |
| 203 | + Serial.printf("DELETE"); |
| 204 | + else if (request->method() == HTTP_PUT) |
| 205 | + Serial.printf("PUT"); |
| 206 | + else if (request->method() == HTTP_PATCH) |
| 207 | + Serial.printf("PATCH"); |
| 208 | + else if (request->method() == HTTP_HEAD) |
| 209 | + Serial.printf("HEAD"); |
| 210 | + else if (request->method() == HTTP_OPTIONS) |
| 211 | + Serial.printf("OPTIONS"); |
| 212 | + else |
| 213 | + Serial.printf("UNKNOWN"); |
| 214 | + Serial.printf(" http://%s%s\n", request->host().c_str(), request->url().c_str()); |
| 215 | + |
| 216 | + if (request->contentLength()) |
| 217 | + { |
| 218 | + Serial.printf("_CONTENT_TYPE: %s\n", request->contentType().c_str()); |
| 219 | + Serial.printf("_CONTENT_LENGTH: %u\n", request->contentLength()); |
| 220 | + } |
| 221 | + |
| 222 | + int headers = request->headers(); |
| 223 | + int i; |
| 224 | + |
| 225 | + for (i = 0; i < headers; i++) |
| 226 | + { |
| 227 | + AsyncWebHeader* h = request->getHeader(i); |
| 228 | + Serial.printf("_HEADER[%s]: %s\n", h->name().c_str(), h->value().c_str()); |
| 229 | + } |
| 230 | + |
| 231 | + int params = request->params(); |
| 232 | + |
| 233 | + for (i = 0; i < params; i++) |
| 234 | + { |
| 235 | + AsyncWebParameter* p = request->getParam(i); |
| 236 | + |
| 237 | + if (p->isPost()) |
| 238 | + { |
| 239 | + Serial.printf("_POST[%s]: %s\n", p->name().c_str(), p->value().c_str()); |
| 240 | + } |
| 241 | + else |
| 242 | + { |
| 243 | + Serial.printf("_GET[%s]: %s\n", p->name().c_str(), p->value().c_str()); |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + request->send(404); |
| 248 | + }); |
| 249 | + |
| 250 | + server.onRequestBody([](AsyncWebServerRequest * request, uint8_t *data, size_t len, size_t index, size_t total) |
| 251 | + { |
| 252 | + (void) request; |
| 253 | + |
| 254 | + if (!index) |
| 255 | + Serial.printf("BodyStart: %u\n", total); |
| 256 | + |
| 257 | + Serial.printf("%s", (const char*)data); |
| 258 | + |
| 259 | + if (index + len == total) |
| 260 | + Serial.printf("BodyEnd: %u\n", total); |
| 261 | + }); |
| 262 | + |
| 263 | + server.begin(); |
| 264 | + |
| 265 | + Serial.print("Server started @ "); |
| 266 | + Serial.println(ETH.localIP()); |
| 267 | +} |
| 268 | + |
| 269 | +void loop() |
| 270 | +{ |
| 271 | + ws.cleanupClients(); |
| 272 | +} |
0 commit comments