|
| 1 | +/**************************************************************************************************************************** |
| 2 | + Async_WebSocketsServer.ino |
| 3 | +
|
| 4 | + For RP2040W with CYW43439 WiFi |
| 5 | + |
| 6 | + AsyncWebServer_RP2040W is a library for the RP2040W with CYW43439 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/AsyncWebServer_RP2040W |
| 10 | + Licensed under GPLv3 license |
| 11 | + *****************************************************************************************************************************/ |
| 12 | + |
| 13 | + #if !( defined(ARDUINO_RASPBERRY_PI_PICO_W) ) |
| 14 | + #error For RASPBERRY_PI_PICO_W only |
| 15 | +#endif |
| 16 | + |
| 17 | +#define _RP2040W_AWS_LOGLEVEL_ 4 |
| 18 | + |
| 19 | +#include <AsyncWebServer_RP2040W.h> |
| 20 | + |
| 21 | +#include "webpage.h" |
| 22 | + |
| 23 | +char ssid[] = "your_ssid"; // your network SSID (name) |
| 24 | +char pass[] = "12345678"; // your network password (use for WPA, or use as key for WEP), length must be 8+ |
| 25 | + |
| 26 | +int status = WL_IDLE_STATUS; |
| 27 | + |
| 28 | +AsyncWebServer server(80); |
| 29 | +AsyncWebSocket ws("/ws"); |
| 30 | + |
| 31 | +void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) |
| 32 | +{ |
| 33 | + if (type == WS_EVT_CONNECT) |
| 34 | + { |
| 35 | + Serial.printf("ws[Server: %s][ClientID: %u] WSClient connected\n", server->url(), client->id()); |
| 36 | + client->text("Hello from RP2040W Server"); |
| 37 | + } |
| 38 | + else if (type == WS_EVT_DISCONNECT) |
| 39 | + { |
| 40 | + Serial.printf("ws[Server: %s][ClientID: %u] WSClient disconnected\n", server->url(), client->id()); |
| 41 | + } |
| 42 | + else if (type == WS_EVT_ERROR) |
| 43 | + { |
| 44 | + //error was received from the other end |
| 45 | + Serial.printf("ws[Server: %s][ClientID: %u] error(%u): %s\n", server->url(), client->id(), *((uint16_t*)arg), (char*)data); |
| 46 | + } |
| 47 | + else if (type == WS_EVT_PONG) |
| 48 | + { |
| 49 | + //pong message was received (in response to a ping request maybe) |
| 50 | + Serial.printf("ws[Server: %s][ClientID: %u] pong[%u]: %s\n", server->url(), client->id(), len, (len) ? (char*)data : ""); |
| 51 | + } |
| 52 | + else if (type == WS_EVT_DATA) |
| 53 | + { |
| 54 | + //data packet |
| 55 | + AwsFrameInfo * info = (AwsFrameInfo*)arg; |
| 56 | + |
| 57 | + if (info->final && info->index == 0 && info->len == len) |
| 58 | + { |
| 59 | + //the whole message is in a single frame and we got all of it's data |
| 60 | + Serial.printf("ws[Server: %s][ClientID: %u] %s-message[len: %llu]: ", server->url(), client->id(), |
| 61 | + (info->opcode == WS_TEXT) ? "text" : "binary", info->len); |
| 62 | + |
| 63 | + if (info->opcode == WS_TEXT) |
| 64 | + { |
| 65 | + data[len] = 0; |
| 66 | + Serial.printf("%s\n", (char*)data); |
| 67 | + } |
| 68 | + else |
| 69 | + { |
| 70 | + for (size_t i = 0; i < info->len; i++) |
| 71 | + { |
| 72 | + Serial.printf("%02x ", data[i]); |
| 73 | + } |
| 74 | + |
| 75 | + Serial.printf("\n"); |
| 76 | + } |
| 77 | + |
| 78 | + if (info->opcode == WS_TEXT) |
| 79 | + client->text("Got your text message"); |
| 80 | + else |
| 81 | + client->binary("Got your binary message"); |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + //message is comprised of multiple frames or the frame is split into multiple packets |
| 86 | + if (info->index == 0) |
| 87 | + { |
| 88 | + if (info->num == 0) |
| 89 | + { |
| 90 | + Serial.printf("ws[Server: %s][ClientID: %u] %s-message start\n", server->url(), client->id(), |
| 91 | + (info->message_opcode == WS_TEXT) ? "text" : "binary"); |
| 92 | + } |
| 93 | + |
| 94 | + Serial.printf("ws[Server: %s][ClientID: %u] frame[%u] start[%llu]\n", server->url(), client->id(), info->num, info->len); |
| 95 | + } |
| 96 | + |
| 97 | + Serial.printf("ws[Server: %s][ClientID: %u] frame[%u] %s[%llu - %llu]: ", server->url(), client->id(), |
| 98 | + info->num, (info->message_opcode == WS_TEXT) ? "text" : "binary", info->index, info->index + len); |
| 99 | + |
| 100 | + if (info->message_opcode == WS_TEXT) |
| 101 | + { |
| 102 | + data[len] = 0; |
| 103 | + Serial.printf("%s\n", (char*)data); |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + for (size_t i = 0; i < len; i++) |
| 108 | + { |
| 109 | + Serial.printf("%02x ", data[i]); |
| 110 | + } |
| 111 | + |
| 112 | + Serial.printf("\n"); |
| 113 | + } |
| 114 | + |
| 115 | + if ((info->index + len) == info->len) |
| 116 | + { |
| 117 | + Serial.printf("ws[Server: %s][ClientID: %u] frame[%u] end[%llu]\n", server->url(), client->id(), info->num, info->len); |
| 118 | + |
| 119 | + if (info->final) |
| 120 | + { |
| 121 | + Serial.printf("ws[Server: %s][ClientID: %u] %s-message end\n", server->url(), client->id(), |
| 122 | + (info->message_opcode == WS_TEXT) ? "text" : "binary"); |
| 123 | + |
| 124 | + if (info->message_opcode == WS_TEXT) |
| 125 | + client->text("I got your text message"); |
| 126 | + else |
| 127 | + client->binary("I got your binary message"); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +void handleRoot(AsyncWebServerRequest *request) |
| 135 | +{ |
| 136 | + request->send(200, "text/html", webpageCont); |
| 137 | +} |
| 138 | + |
| 139 | +void printWifiStatus() |
| 140 | +{ |
| 141 | + // print the SSID of the network you're attached to: |
| 142 | + Serial.print("SSID: "); |
| 143 | + Serial.println(WiFi.SSID()); |
| 144 | + |
| 145 | + // print your board's IP address: |
| 146 | + IPAddress ip = WiFi.localIP(); |
| 147 | + Serial.print("Local IP Address: "); |
| 148 | + Serial.println(ip); |
| 149 | +} |
| 150 | + |
| 151 | +void setup() |
| 152 | +{ |
| 153 | + Serial.begin(115200); |
| 154 | + while (!Serial && millis() < 5000); |
| 155 | + |
| 156 | + delay(200); |
| 157 | + |
| 158 | + Serial.print("\nStarting Async_WebSocketsServer on "); Serial.println(BOARD_NAME); |
| 159 | + Serial.println(ASYNCTCP_RP2040W_VERSION); |
| 160 | + Serial.println(ASYNC_WEBSERVER_RP2040W_VERSION); |
| 161 | + |
| 162 | + /////////////////////////////////// |
| 163 | + |
| 164 | + // check for the WiFi module: |
| 165 | + if (WiFi.status() == WL_NO_MODULE) |
| 166 | + { |
| 167 | + Serial.println("Communication with WiFi module failed!"); |
| 168 | + // don't continue |
| 169 | + while (true); |
| 170 | + } |
| 171 | + |
| 172 | + Serial.print(F("Connecting to SSID: ")); |
| 173 | + Serial.println(ssid); |
| 174 | + |
| 175 | + status = WiFi.begin(ssid, pass); |
| 176 | + |
| 177 | + delay(1000); |
| 178 | + |
| 179 | + // attempt to connect to WiFi network |
| 180 | + while ( status != WL_CONNECTED) |
| 181 | + { |
| 182 | + delay(500); |
| 183 | + |
| 184 | + // Connect to WPA/WPA2 network |
| 185 | + status = WiFi.status(); |
| 186 | + } |
| 187 | + |
| 188 | + printWifiStatus(); |
| 189 | + |
| 190 | + /////////////////////////////////// |
| 191 | + |
| 192 | + ws.onEvent(onWsEvent); |
| 193 | + server.addHandler(&ws); |
| 194 | + |
| 195 | + server.on("/", handleRoot); |
| 196 | + server.begin(); |
| 197 | +} |
| 198 | + |
| 199 | +void loop() |
| 200 | +{ |
| 201 | +} |
0 commit comments