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

Commit b71cf4e

Browse files
authored
v1.4.0 to add LittleFS functions
#### Release v1.4.0 1. Add `LittleFS` functions such as AsyncFSWebServer 2. Add examples [AsyncFSWebServer](https://github.com/khoih-prog/AsyncWebServer_RP2040W/tree/main/examples/AsyncFSWebServer) and [AsyncFSWebServer_Complex](https://github.com/khoih-prog/AsyncWebServer_RP2040W/tree/main/examples/AsyncFSWebServer_Complex) to demo the new feature
1 parent 89ef6ac commit b71cf4e

16 files changed

+950
-0
lines changed
Lines changed: 357 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,357 @@
1+
/****************************************************************************************************************************
2+
AsyncFSWebServer.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+
/*****************************************************************************************************************************
14+
How To Use:
15+
1) Upload the contents of the data folder with MkSPIFFS Tool ("ESP8266 Sketch Data Upload" in Tools menu in Arduino IDE)
16+
2) or you can upload the contents of a folder if you CD in that folder and run the following command:
17+
for file in `\ls -A1`; do curl -F "file=@$PWD/$file" localIPAddress/edit; done
18+
3) access the sample web page at http://localIPAddress/
19+
*****************************************************************************************************************************/
20+
21+
#if !( defined(ARDUINO_RASPBERRY_PI_PICO_W) )
22+
#error For RASPBERRY_PI_PICO_W only
23+
#endif
24+
25+
#define _RP2040W_AWS_LOGLEVEL_ 4
26+
27+
///////////////////////////////////////////////////////////////////
28+
29+
#include <pico/cyw43_arch.h>
30+
31+
///////////////////////////////////////////////////////////////////
32+
33+
#include <LittleFS.h>
34+
35+
#include <AsyncFSEditor_RP2040W.h>
36+
37+
#include <AsyncWebServer_RP2040W.h>
38+
39+
char ssid[] = "your_ssid"; // your network SSID (name)
40+
char pass[] = "12345678"; // your network password (use for WPA, or use as key for WEP), length must be 8+
41+
42+
int status = WL_IDLE_STATUS;
43+
44+
// SKETCH BEGIN
45+
AsyncWebServer server(80);
46+
AsyncWebSocket ws("/ws");
47+
AsyncEventSource events("/events");
48+
49+
const char* hostName = "rp2040w-async";
50+
const char* http_username = "admin";
51+
const char* http_password = "admin";
52+
53+
///////////////////////////////////////////////////
54+
55+
void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len)
56+
{
57+
if (type == WS_EVT_CONNECT)
58+
{
59+
Serial.printf("ws[%s][%u] connect\n", server->url(), client->id());
60+
client->printf("Hello Client %ld :)", client->id());
61+
client->ping();
62+
}
63+
else if (type == WS_EVT_DISCONNECT)
64+
{
65+
Serial.printf("ws[%s][%u] disconnect\n", server->url(), client->id());
66+
}
67+
else if (type == WS_EVT_ERROR)
68+
{
69+
Serial.printf("ws[%s][%u] error(%u): %s\n", server->url(), client->id(), *((uint16_t*)arg), (char*)data);
70+
}
71+
else if (type == WS_EVT_PONG)
72+
{
73+
Serial.printf("ws[%s][%u] pong[%u]: %s\n", server->url(), client->id(), len, (len) ? (char*)data : "");
74+
}
75+
else if (type == WS_EVT_DATA)
76+
{
77+
AwsFrameInfo * info = (AwsFrameInfo*)arg;
78+
String msg = "";
79+
80+
if (info->final && info->index == 0 && info->len == len)
81+
{
82+
//the whole message is in a single frame and we got all of it's data
83+
Serial.printf("ws[%s][%u] %s-message[%llu]: ", server->url(), client->id(), (info->opcode == WS_TEXT) ? "text" : "binary", info->len);
84+
85+
if (info->opcode == WS_TEXT)
86+
{
87+
for (size_t i = 0; i < info->len; i++)
88+
{
89+
msg += (char) data[i];
90+
}
91+
}
92+
else
93+
{
94+
char buff[3];
95+
96+
for (size_t i = 0; i < info->len; i++)
97+
{
98+
sprintf(buff, "%02x ", (uint8_t) data[i]);
99+
msg += buff ;
100+
}
101+
}
102+
103+
Serial.printf("%s\n", msg.c_str());
104+
105+
if (info->opcode == WS_TEXT)
106+
client->text("I got your text message");
107+
else
108+
client->binary("I got your binary message");
109+
}
110+
else
111+
{
112+
//message is comprised of multiple frames or the frame is split into multiple packets
113+
if (info->index == 0)
114+
{
115+
if (info->num == 0)
116+
Serial.printf("ws[%s][%u] %s-message start\n", server->url(), client->id(), (info->message_opcode == WS_TEXT) ? "text" : "binary");
117+
118+
Serial.printf("ws[%s][%u] frame[%u] start[%llu]\n", server->url(), client->id(), info->num, info->len);
119+
}
120+
121+
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);
122+
123+
if (info->opcode == WS_TEXT)
124+
{
125+
for (size_t i = 0; i < len; i++)
126+
{
127+
msg += (char) data[i];
128+
}
129+
}
130+
else
131+
{
132+
char buff[3];
133+
134+
for (size_t i = 0; i < len; i++)
135+
{
136+
sprintf(buff, "%02x ", (uint8_t) data[i]);
137+
msg += buff ;
138+
}
139+
}
140+
141+
Serial.printf("%s\n", msg.c_str());
142+
143+
if ((info->index + len) == info->len)
144+
{
145+
Serial.printf("ws[%s][%u] frame[%u] end[%llu]\n", server->url(), client->id(), info->num, info->len);
146+
147+
if (info->final)
148+
{
149+
Serial.printf("ws[%s][%u] %s-message end\n", server->url(), client->id(), (info->message_opcode == WS_TEXT) ? "text" : "binary");
150+
151+
if (info->message_opcode == WS_TEXT)
152+
client->text("I got your text message");
153+
else
154+
client->binary("I got your binary message");
155+
}
156+
}
157+
}
158+
}
159+
}
160+
161+
////////////////////////////////////////////////////
162+
163+
void initFS()
164+
{
165+
// Initialize LittleFS/SPIFFS file-system
166+
if (!LittleFS.begin())
167+
{
168+
LittleFS.format();
169+
170+
if (!LittleFS.begin())
171+
{
172+
while (true)
173+
{
174+
Serial.println(F("LittleFS failed!"));
175+
176+
// Stay forever here as useless to go further
177+
delay(5000);
178+
}
179+
}
180+
}
181+
}
182+
183+
///////////////////////////////////////////////////
184+
185+
void initWebServer()
186+
{
187+
ws.onEvent(onWsEvent);
188+
server.addHandler(&ws);
189+
190+
events.onConnect([](AsyncEventSourceClient * client)
191+
{
192+
client->send("hello!", NULL, millis(), 1000);
193+
});
194+
195+
server.addHandler(&events);
196+
197+
server.addHandler(new AsyncFSEditor(http_username,http_password));
198+
199+
server.serveStatic("/", LittleFS, "/").setDefaultFile("index.htm");
200+
201+
server.onNotFound([](AsyncWebServerRequest * request)
202+
{
203+
Serial.printf("NOT_FOUND: ");
204+
if (request->method() == HTTP_GET)
205+
Serial.printf("GET");
206+
else if (request->method() == HTTP_POST)
207+
Serial.printf("POST");
208+
else if (request->method() == HTTP_DELETE)
209+
Serial.printf("DELETE");
210+
else if (request->method() == HTTP_PUT)
211+
Serial.printf("PUT");
212+
else if (request->method() == HTTP_PATCH)
213+
Serial.printf("PATCH");
214+
else if (request->method() == HTTP_HEAD)
215+
Serial.printf("HEAD");
216+
else if (request->method() == HTTP_OPTIONS)
217+
Serial.printf("OPTIONS");
218+
else
219+
Serial.printf("UNKNOWN");
220+
221+
Serial.printf(" http://%s%s\n", request->host().c_str(), request->url().c_str());
222+
223+
if (request->contentLength())
224+
{
225+
Serial.printf("_CONTENT_TYPE: %s\n", request->contentType().c_str());
226+
Serial.printf("_CONTENT_LENGTH: %u\n", request->contentLength());
227+
}
228+
229+
int headers = request->headers();
230+
int i;
231+
232+
for (i = 0; i < headers; i++)
233+
{
234+
AsyncWebHeader* h = request->getHeader(i);
235+
Serial.printf("_HEADER[%s]: %s\n", h->name().c_str(), h->value().c_str());
236+
}
237+
238+
int params = request->params();
239+
240+
for (i = 0; i < params; i++)
241+
{
242+
AsyncWebParameter* p = request->getParam(i);
243+
244+
if (p->isFile())
245+
{
246+
Serial.printf("_FILE[%s]: %s, size: %u\n", p->name().c_str(), p->value().c_str(), p->size());
247+
}
248+
else if (p->isPost())
249+
{
250+
Serial.printf("_POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
251+
}
252+
else
253+
{
254+
Serial.printf("_GET[%s]: %s\n", p->name().c_str(), p->value().c_str());
255+
}
256+
}
257+
258+
request->send(404);
259+
});
260+
261+
server.onFileUpload([](AsyncWebServerRequest * request, const String & filename, size_t index, uint8_t *data, size_t len, bool final)
262+
{
263+
if (!index)
264+
Serial.printf("UploadStart: %s\n", filename.c_str());
265+
266+
Serial.printf("%s", (const char*)data);
267+
268+
if (final)
269+
Serial.printf("UploadEnd: %s (%u)\n", filename.c_str(), index + len);
270+
});
271+
272+
server.onRequestBody([](AsyncWebServerRequest * request, uint8_t *data, size_t len, size_t index, size_t total)
273+
{
274+
if (!index)
275+
Serial.printf("BodyStart: %u\n", total);
276+
277+
Serial.printf("%s", (const char*)data);
278+
279+
if (index + len == total)
280+
Serial.printf("BodyEnd: %u\n", total);
281+
});
282+
283+
server.begin();
284+
}
285+
286+
///////////////////////////////////////////////////
287+
288+
void printWifiStatus()
289+
{
290+
// print the SSID of the network you're attached to:
291+
Serial.print("SSID: ");
292+
Serial.println(WiFi.SSID());
293+
294+
// print your board's IP address:
295+
IPAddress ip = WiFi.localIP();
296+
Serial.print("Local IP Address: ");
297+
Serial.println(ip);
298+
}
299+
300+
void setup()
301+
{
302+
Serial.begin(115200);
303+
while (!Serial && millis() < 5000);
304+
305+
delay(200);
306+
307+
Serial.print("\nStart AsyncFSWebServer on "); Serial.print(BOARD_NAME);
308+
Serial.print(" with "); Serial.println(SHIELD_TYPE);
309+
Serial.println(ASYNCTCP_RP2040W_VERSION);
310+
Serial.println(ASYNC_WEBSERVER_RP2040W_VERSION);
311+
312+
///////////////////////////////////
313+
314+
// check for the WiFi module:
315+
if (WiFi.status() == WL_NO_MODULE)
316+
{
317+
Serial.println("Communication with WiFi module failed!");
318+
// don't continue
319+
while (true);
320+
}
321+
322+
Serial.print(F("Connecting to SSID: "));
323+
Serial.println(ssid);
324+
325+
status = WiFi.begin(ssid, pass);
326+
327+
delay(1000);
328+
329+
// attempt to connect to WiFi network
330+
while ( status != WL_CONNECTED)
331+
{
332+
delay(500);
333+
334+
// Connect to WPA/WPA2 network
335+
status = WiFi.status();
336+
}
337+
338+
printWifiStatus();
339+
340+
///////////////////////////////////
341+
342+
initFS();
343+
344+
initWebServer();
345+
346+
Serial.print("AsyncWebServer started @");
347+
Serial.println(WiFi.localIP());
348+
349+
Serial.print(F("Open http://"));
350+
Serial.print(WiFi.localIP());
351+
Serial.println(F("/edit to see the file browser"));
352+
}
353+
354+
void loop()
355+
{
356+
ws.cleanupClients();
357+
}
40.2 KB
Loading
8.12 KB
Loading
10.9 KB
Loading
4.02 KB
Binary file not shown.
1.12 KB
Binary file not shown.
1.92 KB
Binary file not shown.

0 commit comments

Comments
 (0)