|
| 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 | +// To modify https://github.com/earlephilhower/arduino-pico/blob/master/variants/rpipicow/picow_init.cpp |
| 44 | +// Check https://github.com/khoih-prog/AsyncWebServer_RP2040W/issues/3#issuecomment-1255676644 |
| 45 | + |
| 46 | +#if !( defined(ARDUINO_RASPBERRY_PI_PICO_W) ) |
| 47 | + #error For RASPBERRY_PI_PICO_W only |
| 48 | +#endif |
| 49 | + |
| 50 | +#define _RP2040W_AWS_LOGLEVEL_ 1 |
| 51 | + |
| 52 | +/////////////////////////////////////////////////////////////////// |
| 53 | + |
| 54 | +#include <pico/cyw43_arch.h> |
| 55 | + |
| 56 | +/////////////////////////////////////////////////////////////////// |
| 57 | + |
| 58 | +#include <AsyncWebServer_RP2040W.h> |
| 59 | + |
| 60 | +char ssid[] = "your_ssid"; // your network SSID (name) |
| 61 | +char pass[] = "12345678"; // your network password (use for WPA, or use as key for WEP), length must be 8+ |
| 62 | + |
| 63 | +int status = WL_IDLE_STATUS; |
| 64 | + |
| 65 | +AsyncWebServer server(80); |
| 66 | + |
| 67 | +int reqCount = 0; // number of requests received |
| 68 | + |
| 69 | +#define LED_OFF LOW |
| 70 | +#define LED_ON HIGH |
| 71 | + |
| 72 | +#define BUFFER_SIZE 512 |
| 73 | +char temp[BUFFER_SIZE]; |
| 74 | + |
| 75 | +void handleRoot(AsyncWebServerRequest *request) |
| 76 | +{ |
| 77 | + digitalWrite(LED_BUILTIN, LED_ON); |
| 78 | + |
| 79 | + int sec = millis() / 1000; |
| 80 | + int min = sec / 60; |
| 81 | + int hr = min / 60; |
| 82 | + int day = hr / 24; |
| 83 | + |
| 84 | + snprintf(temp, BUFFER_SIZE - 1, |
| 85 | + "<html>\ |
| 86 | +<head>\ |
| 87 | +<meta http-equiv='refresh' content='5'/>\ |
| 88 | +<title>AsyncWebServer-%s</title>\ |
| 89 | +<style>\ |
| 90 | +body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\ |
| 91 | +</style>\ |
| 92 | +</head>\ |
| 93 | +<body>\ |
| 94 | +<h2>AsyncWebServer_RP2040W!</h2>\ |
| 95 | +<h3>running WiFi on %s</h3>\ |
| 96 | +<p>Uptime: %d d %02d:%02d:%02d</p>\ |
| 97 | +<img src=\"/test.svg\" />\ |
| 98 | +</body>\ |
| 99 | +</html>", BOARD_NAME, BOARD_NAME, day, hr % 24, min % 60, sec % 60); |
| 100 | + |
| 101 | + request->send(200, "text/html", temp); |
| 102 | + |
| 103 | + digitalWrite(LED_BUILTIN, LED_OFF); |
| 104 | +} |
| 105 | + |
| 106 | +void handleNotFound(AsyncWebServerRequest *request) |
| 107 | +{ |
| 108 | + digitalWrite(LED_BUILTIN, LED_ON); |
| 109 | + String message = "File Not Found\n\n"; |
| 110 | + |
| 111 | + message += "URI: "; |
| 112 | + message += request->url(); |
| 113 | + message += "\nMethod: "; |
| 114 | + message += (request->method() == HTTP_GET) ? "GET" : "POST"; |
| 115 | + message += "\nArguments: "; |
| 116 | + message += request->args(); |
| 117 | + message += "\n"; |
| 118 | + |
| 119 | + for (uint8_t i = 0; i < request->args(); i++) |
| 120 | + { |
| 121 | + message += " " + request->argName(i) + ": " + request->arg(i) + "\n"; |
| 122 | + } |
| 123 | + |
| 124 | + request->send(404, "text/plain", message); |
| 125 | + digitalWrite(LED_BUILTIN, LED_OFF); |
| 126 | +} |
| 127 | + |
| 128 | +void drawGraph(AsyncWebServerRequest *request) |
| 129 | +{ |
| 130 | + String out; |
| 131 | + |
| 132 | + out.reserve(3000); |
| 133 | + char temp[70]; |
| 134 | + |
| 135 | + digitalWrite(LED_BUILTIN, LED_ON); |
| 136 | + |
| 137 | + out += "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"310\" height=\"150\">\n"; |
| 138 | + out += "<rect width=\"310\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"2\" stroke=\"rgb(0, 0, 0)\" />\n"; |
| 139 | + out += "<g stroke=\"blue\">\n"; |
| 140 | + int y = rand() % 130; |
| 141 | + |
| 142 | + for (int x = 10; x < 300; x += 10) |
| 143 | + { |
| 144 | + int y2 = rand() % 130; |
| 145 | + sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"2\" />\n", x, 140 - y, x + 10, 140 - y2); |
| 146 | + out += temp; |
| 147 | + y = y2; |
| 148 | + } |
| 149 | + out += "</g>\n</svg>\n"; |
| 150 | + |
| 151 | + request->send(200, "image/svg+xml", out); |
| 152 | + |
| 153 | + digitalWrite(LED_BUILTIN, LED_OFF); |
| 154 | +} |
| 155 | + |
| 156 | +void printWifiStatus() |
| 157 | +{ |
| 158 | + // print the SSID of the network you're attached to: |
| 159 | + Serial.print("SSID: "); |
| 160 | + Serial.println(WiFi.SSID()); |
| 161 | + |
| 162 | + // print your board's IP address: |
| 163 | + IPAddress ip = WiFi.localIP(); |
| 164 | + Serial.print("Local IP Address: "); |
| 165 | + Serial.println(ip); |
| 166 | + |
| 167 | + // print your board's country code |
| 168 | + // #define CYW43_COUNTRY(A, B, REV) ((unsigned char)(A) | ((unsigned char)(B) << 8) | ((REV) << 16)) |
| 169 | + uint32_t myCountryCode = cyw43_arch_get_country_code(); |
| 170 | + char countryCode[3] = { 0, 0, 0 }; |
| 171 | + |
| 172 | + countryCode[0] = myCountryCode & 0xFF; |
| 173 | + countryCode[1] = (myCountryCode >> 8) & 0xFF; |
| 174 | + |
| 175 | + Serial.print("Country code: "); Serial.println(countryCode); |
| 176 | +} |
| 177 | + |
| 178 | +void setup() |
| 179 | +{ |
| 180 | + pinMode(LED_BUILTIN, OUTPUT); |
| 181 | + digitalWrite(LED_BUILTIN, LED_OFF); |
| 182 | + |
| 183 | + Serial.begin(115200); |
| 184 | + while (!Serial); |
| 185 | + |
| 186 | + delay(200); |
| 187 | + |
| 188 | + Serial.print("\nStart Async_AdvancedWebServer_Country on "); Serial.print(BOARD_NAME); |
| 189 | + Serial.print(" with "); Serial.println(SHIELD_TYPE); |
| 190 | + Serial.println(ASYNCTCP_RP2040W_VERSION); |
| 191 | + Serial.println(ASYNC_WEBSERVER_RP2040W_VERSION); |
| 192 | + |
| 193 | + /////////////////////////////////// |
| 194 | + |
| 195 | + // check for the WiFi module: |
| 196 | + if (WiFi.status() == WL_NO_MODULE) |
| 197 | + { |
| 198 | + Serial.println("Communication with WiFi module failed!"); |
| 199 | + // don't continue |
| 200 | + while (true); |
| 201 | + } |
| 202 | + |
| 203 | + Serial.print(F("Connecting to SSID: ")); |
| 204 | + Serial.println(ssid); |
| 205 | + |
| 206 | + status = WiFi.begin(ssid, pass); |
| 207 | + |
| 208 | + delay(1000); |
| 209 | + |
| 210 | + // attempt to connect to WiFi network |
| 211 | + while ( status != WL_CONNECTED) |
| 212 | + { |
| 213 | + delay(500); |
| 214 | + |
| 215 | + // Connect to WPA/WPA2 network |
| 216 | + status = WiFi.status(); |
| 217 | + } |
| 218 | + |
| 219 | + printWifiStatus(); |
| 220 | + |
| 221 | + /////////////////////////////////// |
| 222 | + |
| 223 | + server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) |
| 224 | + { |
| 225 | + handleRoot(request); |
| 226 | + }); |
| 227 | + |
| 228 | + server.on("/test.svg", HTTP_GET, [](AsyncWebServerRequest * request) |
| 229 | + { |
| 230 | + drawGraph(request); |
| 231 | + }); |
| 232 | + |
| 233 | + server.on("/inline", [](AsyncWebServerRequest * request) |
| 234 | + { |
| 235 | + request->send(200, "text/plain", "This works as well"); |
| 236 | + }); |
| 237 | + |
| 238 | + server.onNotFound(handleNotFound); |
| 239 | + |
| 240 | + server.begin(); |
| 241 | + |
| 242 | + Serial.print(F("HTTP EthernetWebServer is @ IP : ")); |
| 243 | + Serial.println(WiFi.localIP()); |
| 244 | +} |
| 245 | + |
| 246 | +void heartBeatPrint() |
| 247 | +{ |
| 248 | + static int num = 1; |
| 249 | + |
| 250 | + Serial.print(F(".")); |
| 251 | + |
| 252 | + if (num == 80) |
| 253 | + { |
| 254 | + Serial.println(); |
| 255 | + num = 1; |
| 256 | + } |
| 257 | + else if (num++ % 10 == 0) |
| 258 | + { |
| 259 | + Serial.print(F(" ")); |
| 260 | + } |
| 261 | +} |
| 262 | + |
| 263 | +void check_status() |
| 264 | +{ |
| 265 | + static unsigned long checkstatus_timeout = 0; |
| 266 | + |
| 267 | +#define STATUS_CHECK_INTERVAL 10000L |
| 268 | + |
| 269 | + // Send status report every STATUS_REPORT_INTERVAL (60) seconds: we don't need to send updates frequently if there is no status change. |
| 270 | + if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0)) |
| 271 | + { |
| 272 | + heartBeatPrint(); |
| 273 | + checkstatus_timeout = millis() + STATUS_CHECK_INTERVAL; |
| 274 | + } |
| 275 | +} |
| 276 | + |
| 277 | +void loop() |
| 278 | +{ |
| 279 | + check_status(); |
| 280 | +} |
0 commit comments