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

Commit a647b54

Browse files
authored
Add example with country code
Permit to set country code ``` // Select country code, if necessary // For example: CYW43_COUNTRY_AUSTRALIA, CYW43_COUNTRY_CANADA, CYW43_COUNTRY_CHINA, CYW43_COUNTRY_FRANCE, CYW43_COUNTRY_GERMANY, CYW43_COUNTRY_INDIA, CYW43_COUNTRY_ITALY, // CYW43_COUNTRY_JAPAN, CYW43_COUNTRY_NETHERLANDS, CYW43_COUNTRY_SOUTH_KOREA, CYW43_COUNTRY_SWEDEN, CYW43_COUNTRY_UK, CYW43_COUNTRY_USA, etc. ```
1 parent 6b4f24e commit a647b54

File tree

1 file changed

+275
-0
lines changed

1 file changed

+275
-0
lines changed
Lines changed: 275 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
/****************************************************************************************************************************
2+
Async_AdvancedWebServer.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+
Copyright (c) 2015, Majenko Technologies
13+
All rights reserved.
14+
15+
Redistribution and use in source and binary forms, with or without modification,
16+
are permitted provided that the following conditions are met:
17+
18+
Redistributions of source code must retain the above copyright notice, this
19+
list of conditions and the following disclaimer.
20+
21+
Redistributions in binary form must reproduce the above copyright notice, this
22+
list of conditions and the following disclaimer in the documentation and/or
23+
other materials provided with the distribution.
24+
25+
Neither the name of Majenko Technologies nor the names of its
26+
contributors may be used to endorse or promote products derived from
27+
this software without specific prior written permission.
28+
29+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
30+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
32+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
33+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
36+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39+
*****************************************************************************************************************************/
40+
41+
// See the list of country codes in
42+
// https://github.com/earlephilhower/cyw43-driver/blob/02533c10a018c6550e9f66f7699e21356f5e4609/src/cyw43_country.h#L59-L111
43+
44+
#if !( defined(ARDUINO_RASPBERRY_PI_PICO_W) )
45+
#error For RASPBERRY_PI_PICO_W only
46+
#endif
47+
48+
#define _RP2040W_AWS_LOGLEVEL_ 1
49+
50+
#include <pico/cyw43_arch.h>
51+
52+
#include <AsyncWebServer_RP2040W.h>
53+
54+
char ssid[] = "your_ssid"; // your network SSID (name)
55+
char pass[] = "12345678"; // your network password (use for WPA, or use as key for WEP), length must be 8+
56+
57+
int status = WL_IDLE_STATUS;
58+
59+
AsyncWebServer server(80);
60+
61+
int reqCount = 0; // number of requests received
62+
63+
#define LED_OFF LOW
64+
#define LED_ON HIGH
65+
66+
#define BUFFER_SIZE 512
67+
char temp[BUFFER_SIZE];
68+
69+
void handleRoot(AsyncWebServerRequest *request)
70+
{
71+
digitalWrite(LED_BUILTIN, LED_ON);
72+
73+
int sec = millis() / 1000;
74+
int min = sec / 60;
75+
int hr = min / 60;
76+
int day = hr / 24;
77+
78+
snprintf(temp, BUFFER_SIZE - 1,
79+
"<html>\
80+
<head>\
81+
<meta http-equiv='refresh' content='5'/>\
82+
<title>AsyncWebServer-%s</title>\
83+
<style>\
84+
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
85+
</style>\
86+
</head>\
87+
<body>\
88+
<h2>AsyncWebServer_RP2040W!</h2>\
89+
<h3>running WiFi on %s</h3>\
90+
<p>Uptime: %d d %02d:%02d:%02d</p>\
91+
<img src=\"/test.svg\" />\
92+
</body>\
93+
</html>", BOARD_NAME, BOARD_NAME, day, hr % 24, min % 60, sec % 60);
94+
95+
request->send(200, "text/html", temp);
96+
97+
digitalWrite(LED_BUILTIN, LED_OFF);
98+
}
99+
100+
void handleNotFound(AsyncWebServerRequest *request)
101+
{
102+
digitalWrite(LED_BUILTIN, LED_ON);
103+
String message = "File Not Found\n\n";
104+
105+
message += "URI: ";
106+
message += request->url();
107+
message += "\nMethod: ";
108+
message += (request->method() == HTTP_GET) ? "GET" : "POST";
109+
message += "\nArguments: ";
110+
message += request->args();
111+
message += "\n";
112+
113+
for (uint8_t i = 0; i < request->args(); i++)
114+
{
115+
message += " " + request->argName(i) + ": " + request->arg(i) + "\n";
116+
}
117+
118+
request->send(404, "text/plain", message);
119+
digitalWrite(LED_BUILTIN, LED_OFF);
120+
}
121+
122+
void drawGraph(AsyncWebServerRequest *request)
123+
{
124+
String out;
125+
126+
out.reserve(3000);
127+
char temp[70];
128+
129+
digitalWrite(LED_BUILTIN, LED_ON);
130+
131+
out += "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"310\" height=\"150\">\n";
132+
out += "<rect width=\"310\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"2\" stroke=\"rgb(0, 0, 0)\" />\n";
133+
out += "<g stroke=\"blue\">\n";
134+
int y = rand() % 130;
135+
136+
for (int x = 10; x < 300; x += 10)
137+
{
138+
int y2 = rand() % 130;
139+
sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"2\" />\n", x, 140 - y, x + 10, 140 - y2);
140+
out += temp;
141+
y = y2;
142+
}
143+
out += "</g>\n</svg>\n";
144+
145+
request->send(200, "image/svg+xml", out);
146+
147+
digitalWrite(LED_BUILTIN, LED_OFF);
148+
}
149+
150+
void printWifiStatus()
151+
{
152+
// print the SSID of the network you're attached to:
153+
Serial.print("SSID: ");
154+
Serial.println(WiFi.SSID());
155+
156+
// print your board's IP address:
157+
IPAddress ip = WiFi.localIP();
158+
Serial.print("Local IP Address: ");
159+
Serial.println(ip);
160+
}
161+
162+
void setup()
163+
{
164+
pinMode(LED_BUILTIN, OUTPUT);
165+
digitalWrite(LED_BUILTIN, LED_OFF);
166+
167+
Serial.begin(115200);
168+
while (!Serial);
169+
170+
delay(200);
171+
172+
Serial.print("\nStart Async_AdvancedWebServer on "); Serial.print(BOARD_NAME);
173+
Serial.print(" with "); Serial.println(SHIELD_TYPE);
174+
Serial.println(ASYNCTCP_RP2040W_VERSION);
175+
Serial.println(ASYNC_WEBSERVER_RP2040W_VERSION);
176+
177+
///////////////////////////////////
178+
179+
// check for the WiFi module:
180+
if (WiFi.status() == WL_NO_MODULE)
181+
{
182+
Serial.println("Communication with WiFi module failed!");
183+
// don't continue
184+
while (true);
185+
}
186+
187+
Serial.print(F("Connecting to SSID: "));
188+
Serial.println(ssid);
189+
190+
// Select country code, if necessary
191+
// For example: CYW43_COUNTRY_AUSTRALIA, CYW43_COUNTRY_CANADA, CYW43_COUNTRY_CHINA, CYW43_COUNTRY_FRANCE, CYW43_COUNTRY_GERMANY, CYW43_COUNTRY_INDIA, CYW43_COUNTRY_ITALY,
192+
// CYW43_COUNTRY_JAPAN, CYW43_COUNTRY_NETHERLANDS, CYW43_COUNTRY_SOUTH_KOREA, CYW43_COUNTRY_SWEDEN, CYW43_COUNTRY_UK, CYW43_COUNTRY_USA, etc.
193+
if (cyw43_arch_init_with_country(CYW43_COUNTRY_CANADA))
194+
{
195+
Serial.println("Error setting country code!");
196+
// don't continue
197+
while (true);
198+
}
199+
200+
status = WiFi.begin(ssid, pass);
201+
202+
delay(1000);
203+
204+
// attempt to connect to WiFi network
205+
while ( status != WL_CONNECTED)
206+
{
207+
delay(500);
208+
209+
// Connect to WPA/WPA2 network
210+
status = WiFi.status();
211+
}
212+
213+
printWifiStatus();
214+
215+
///////////////////////////////////
216+
217+
218+
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request)
219+
{
220+
handleRoot(request);
221+
});
222+
223+
server.on("/test.svg", HTTP_GET, [](AsyncWebServerRequest * request)
224+
{
225+
drawGraph(request);
226+
});
227+
228+
server.on("/inline", [](AsyncWebServerRequest * request)
229+
{
230+
request->send(200, "text/plain", "This works as well");
231+
});
232+
233+
server.onNotFound(handleNotFound);
234+
235+
server.begin();
236+
237+
Serial.print(F("HTTP EthernetWebServer is @ IP : "));
238+
Serial.println(WiFi.localIP());
239+
}
240+
241+
void heartBeatPrint()
242+
{
243+
static int num = 1;
244+
245+
Serial.print(F("."));
246+
247+
if (num == 80)
248+
{
249+
Serial.println();
250+
num = 1;
251+
}
252+
else if (num++ % 10 == 0)
253+
{
254+
Serial.print(F(" "));
255+
}
256+
}
257+
258+
void check_status()
259+
{
260+
static unsigned long checkstatus_timeout = 0;
261+
262+
#define STATUS_CHECK_INTERVAL 10000L
263+
264+
// Send status report every STATUS_REPORT_INTERVAL (60) seconds: we don't need to send updates frequently if there is no status change.
265+
if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0))
266+
{
267+
heartBeatPrint();
268+
checkstatus_timeout = millis() + STATUS_CHECK_INTERVAL;
269+
}
270+
}
271+
272+
void loop()
273+
{
274+
check_status();
275+
}

0 commit comments

Comments
 (0)