|
| 1 | +#include <LwIP.h> |
| 2 | +#include <STM32Ethernet.h> |
| 3 | + |
| 4 | +#include <STM32AsyncTCP.h> |
| 5 | +#include <AsyncWebServer_STM32.h> |
| 6 | + |
| 7 | +// Enter a MAC address and IP address for your controller below. |
| 8 | +#define NUMBER_OF_MAC 20 |
| 9 | + |
| 10 | +byte mac[][NUMBER_OF_MAC] = |
| 11 | +{ |
| 12 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x01 }, |
| 13 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x02 }, |
| 14 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x03 }, |
| 15 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x04 }, |
| 16 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x05 }, |
| 17 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x06 }, |
| 18 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x07 }, |
| 19 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x08 }, |
| 20 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x09 }, |
| 21 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0A }, |
| 22 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0B }, |
| 23 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0C }, |
| 24 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0D }, |
| 25 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0E }, |
| 26 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0F }, |
| 27 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x10 }, |
| 28 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x11 }, |
| 29 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x12 }, |
| 30 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x13 }, |
| 31 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x14 }, |
| 32 | +}; |
| 33 | +// Select the IP address according to your local network |
| 34 | +IPAddress ip(192, 168, 2, 232); |
| 35 | + |
| 36 | +// SKETCH BEGIN |
| 37 | +AsyncWebServer server(80); |
| 38 | +AsyncWebSocket ws("/ws"); |
| 39 | +AsyncEventSource events("/events"); |
| 40 | + |
| 41 | +void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) |
| 42 | +{ |
| 43 | + if (type == WS_EVT_CONNECT) |
| 44 | + { |
| 45 | + Serial.printf("ws[%s][%u] connect\n", server->url(), client->id()); |
| 46 | + client->printf("Hello Client %u :)", client->id()); |
| 47 | + client->ping(); |
| 48 | + } |
| 49 | + else if (type == WS_EVT_DISCONNECT) |
| 50 | + { |
| 51 | + Serial.printf("ws[%s][%u] disconnect\n", server->url(), client->id()); |
| 52 | + } |
| 53 | + else if (type == WS_EVT_ERROR) |
| 54 | + { |
| 55 | + Serial.printf("ws[%s][%u] error(%u): %s\n", server->url(), client->id(), *((uint16_t*)arg), (char*)data); |
| 56 | + } |
| 57 | + else if (type == WS_EVT_PONG) |
| 58 | + { |
| 59 | + Serial.printf("ws[%s][%u] pong[%u]: %s\n", server->url(), client->id(), len, (len) ? (char*)data : ""); |
| 60 | + } |
| 61 | + else if (type == WS_EVT_DATA) |
| 62 | + { |
| 63 | + AwsFrameInfo * info = (AwsFrameInfo*)arg; |
| 64 | + String msg = ""; |
| 65 | + |
| 66 | + if (info->final && info->index == 0 && info->len == len) |
| 67 | + { |
| 68 | + //the whole message is in a single frame and we got all of it's data |
| 69 | + Serial.printf("ws[%s][%u] %s-message[%llu]: ", server->url(), client->id(), (info->opcode == WS_TEXT) ? "text" : "binary", info->len); |
| 70 | + |
| 71 | + if (info->opcode == WS_TEXT) |
| 72 | + { |
| 73 | + for (size_t i = 0; i < info->len; i++) |
| 74 | + { |
| 75 | + msg += (char) data[i]; |
| 76 | + } |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + char buff[3]; |
| 81 | + |
| 82 | + for (size_t i = 0; i < info->len; i++) |
| 83 | + { |
| 84 | + sprintf(buff, "%02x ", (uint8_t) data[i]); |
| 85 | + msg += buff ; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + Serial.printf("%s\n", msg.c_str()); |
| 90 | + |
| 91 | + if (info->opcode == WS_TEXT) |
| 92 | + client->text("I got your text message"); |
| 93 | + else |
| 94 | + client->binary("I got your binary message"); |
| 95 | + } |
| 96 | + else |
| 97 | + { |
| 98 | + //message is comprised of multiple frames or the frame is split into multiple packets |
| 99 | + |
| 100 | + if (info->index == 0) |
| 101 | + { |
| 102 | + if (info->num == 0) |
| 103 | + Serial.printf("ws[%s][%u] %s-message start\n", server->url(), client->id(), (info->message_opcode == WS_TEXT) ? "text" : "binary"); |
| 104 | + |
| 105 | + Serial.printf("ws[%s][%u] frame[%u] start[%llu]\n", server->url(), client->id(), info->num, info->len); |
| 106 | + } |
| 107 | + |
| 108 | + 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); |
| 109 | + |
| 110 | + if (info->opcode == WS_TEXT) |
| 111 | + { |
| 112 | + for (size_t i = 0; i < len; i++) |
| 113 | + { |
| 114 | + msg += (char) data[i]; |
| 115 | + } |
| 116 | + } |
| 117 | + else |
| 118 | + { |
| 119 | + char buff[3]; |
| 120 | + |
| 121 | + for (size_t i = 0; i < len; i++) |
| 122 | + { |
| 123 | + sprintf(buff, "%02x ", (uint8_t) data[i]); |
| 124 | + msg += buff ; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + Serial.printf("%s\n", msg.c_str()); |
| 129 | + |
| 130 | + if ((info->index + len) == info->len) |
| 131 | + { |
| 132 | + Serial.printf("ws[%s][%u] frame[%u] end[%llu]\n", server->url(), client->id(), info->num, info->len); |
| 133 | + |
| 134 | + if (info->final) |
| 135 | + { |
| 136 | + Serial.printf("ws[%s][%u] %s-message end\n", server->url(), client->id(), (info->message_opcode == WS_TEXT) ? "text" : "binary"); |
| 137 | + |
| 138 | + if (info->message_opcode == WS_TEXT) |
| 139 | + client->text("I got your text message"); |
| 140 | + else |
| 141 | + client->binary("I got your binary message"); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | + |
| 149 | +const char * hostName = "esp-async"; |
| 150 | +const char* http_username = "admin"; |
| 151 | +const char* http_password = "admin"; |
| 152 | + |
| 153 | +void setup() |
| 154 | +{ |
| 155 | + Serial.begin(115200); |
| 156 | + while (!Serial); |
| 157 | + |
| 158 | + Serial.println("\nStarting AsyncFSBrowser_STM32 on " + String(BOARD_NAME)); |
| 159 | + |
| 160 | + // start the ethernet connection and the server |
| 161 | + // Use random mac |
| 162 | + uint16_t index = millis() % NUMBER_OF_MAC; |
| 163 | + |
| 164 | + // Use Static IP |
| 165 | + //Ethernet.begin(mac[index], ip); |
| 166 | + // Use DHCP dynamic IP and random mac |
| 167 | + Ethernet.begin(mac[index]); |
| 168 | + |
| 169 | + ws.onEvent(onWsEvent); |
| 170 | + server.addHandler(&ws); |
| 171 | + |
| 172 | + events.onConnect([](AsyncEventSourceClient * client) |
| 173 | + { |
| 174 | + client->send("hello!", NULL, millis(), 1000); |
| 175 | + }); |
| 176 | + |
| 177 | + server.addHandler(&events); |
| 178 | + |
| 179 | + server.on("/board", HTTP_GET, [](AsyncWebServerRequest * request) |
| 180 | + { |
| 181 | + request->send(200, "text/plain", String(BOARD_NAME)); |
| 182 | + }); |
| 183 | + |
| 184 | + server.onNotFound([](AsyncWebServerRequest * request) |
| 185 | + { |
| 186 | + Serial.printf("NOT_FOUND: "); |
| 187 | + if (request->method() == HTTP_GET) |
| 188 | + Serial.printf("GET"); |
| 189 | + else if (request->method() == HTTP_POST) |
| 190 | + Serial.printf("POST"); |
| 191 | + else if (request->method() == HTTP_DELETE) |
| 192 | + Serial.printf("DELETE"); |
| 193 | + else if (request->method() == HTTP_PUT) |
| 194 | + Serial.printf("PUT"); |
| 195 | + else if (request->method() == HTTP_PATCH) |
| 196 | + Serial.printf("PATCH"); |
| 197 | + else if (request->method() == HTTP_HEAD) |
| 198 | + Serial.printf("HEAD"); |
| 199 | + else if (request->method() == HTTP_OPTIONS) |
| 200 | + Serial.printf("OPTIONS"); |
| 201 | + else |
| 202 | + Serial.printf("UNKNOWN"); |
| 203 | + Serial.printf(" http://%s%s\n", request->host().c_str(), request->url().c_str()); |
| 204 | + |
| 205 | + if (request->contentLength()) |
| 206 | + { |
| 207 | + Serial.printf("_CONTENT_TYPE: %s\n", request->contentType().c_str()); |
| 208 | + Serial.printf("_CONTENT_LENGTH: %u\n", request->contentLength()); |
| 209 | + } |
| 210 | + |
| 211 | + int headers = request->headers(); |
| 212 | + int i; |
| 213 | + |
| 214 | + for (i = 0; i < headers; i++) |
| 215 | + { |
| 216 | + AsyncWebHeader* h = request->getHeader(i); |
| 217 | + Serial.printf("_HEADER[%s]: %s\n", h->name().c_str(), h->value().c_str()); |
| 218 | + } |
| 219 | + |
| 220 | + int params = request->params(); |
| 221 | + |
| 222 | + for (i = 0; i < params; i++) |
| 223 | + { |
| 224 | + AsyncWebParameter* p = request->getParam(i); |
| 225 | + |
| 226 | + if (p->isPost()) |
| 227 | + { |
| 228 | + Serial.printf("_POST[%s]: %s\n", p->name().c_str(), p->value().c_str()); |
| 229 | + } |
| 230 | + else |
| 231 | + { |
| 232 | + Serial.printf("_GET[%s]: %s\n", p->name().c_str(), p->value().c_str()); |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + request->send(404); |
| 237 | + }); |
| 238 | + |
| 239 | + server.onRequestBody([](AsyncWebServerRequest * request, uint8_t *data, size_t len, size_t index, size_t total) |
| 240 | + { |
| 241 | + if (!index) |
| 242 | + Serial.printf("BodyStart: %u\n", total); |
| 243 | + |
| 244 | + Serial.printf("%s", (const char*)data); |
| 245 | + |
| 246 | + if (index + len == total) |
| 247 | + Serial.printf("BodyEnd: %u\n", total); |
| 248 | + }); |
| 249 | + |
| 250 | + server.begin(); |
| 251 | + |
| 252 | + Serial.print("Server started @ "); |
| 253 | + Serial.println(Ethernet.localIP()); |
| 254 | +} |
| 255 | + |
| 256 | +void loop() |
| 257 | +{ |
| 258 | + ws.cleanupClients(); |
| 259 | +} |
0 commit comments