Skip to content
This repository was archived by the owner on Feb 4, 2023. It is now read-only.

Commit 2e2e2f3

Browse files
authored
v1.3.0 to fix crash when using AsyncWebSockets
#### Releases v1.3.0 1. Fix `crash` when using `AsyncWebSockets server`. Check [Can't connect to AsyncWebSocketServer_RP2040 via javascript #5](#5) 2. Add example [Async_WebSocketsServer](https://github.com/khoih-prog/AsyncWebServer_RP2040W/tree/main/examples/Async_WebSocketsServer) to demo the AsyncWebSockets Server with a `Python` [WSClient.py](https://github.com/khoih-prog/AsyncWebServer_RP2040W/tree/main/examples/Async_WebSocketsServer/WSClient_Python/WSClient.py)
1 parent 07f3fcc commit 2e2e2f3

File tree

3 files changed

+257
-0
lines changed

3 files changed

+257
-0
lines changed
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
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+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Run by $ python3.8 WSClient.py
2+
# From websocket-client package. Install by $ pip3 install websocket-client
3+
import websocket
4+
import time
5+
6+
ws = websocket.WebSocket()
7+
#ws.connect("ws://192.168.2.98/ws")
8+
ws.connect("ws://192.168.2.77/ws")
9+
#ws.connect("ws://192.168.2.103/ws")
10+
11+
while True:
12+
ws.send("Hello, Server")
13+
result = ws.recv()
14+
print(result)
15+
time.sleep(10)
16+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//=====================
2+
//HTML code for webpage
3+
//=====================
4+
5+
const char webpageCont[] PROGMEM =
6+
R"=====(
7+
<!DOCTYPE HTML>
8+
<html>
9+
<title>RP2040W AsyncSocketServer</title>
10+
11+
<!---------------------------CSS-------------------------->
12+
13+
<style>
14+
15+
h1 {font-size: 40px; color: red; text-align: center}
16+
17+
</style>
18+
19+
<!--------------------------HTML-------------------------->
20+
21+
<body>
22+
23+
<h1>RP2040W AsyncSocketServer</h1>
24+
25+
</body>
26+
27+
<!----------------------JavaScript------------------------>
28+
29+
<script>
30+
31+
var websoc = new WebSocket('ws://'+window.location.hostname+':80/ws');
32+
33+
websoc.onopen = function()
34+
{
35+
window.alert("Client Connected");
36+
};
37+
38+
</script>
39+
</html>
40+
)=====";

0 commit comments

Comments
 (0)