/* Copyright (c) 2015, Majenko Technologies All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * * Neither the name of Majenko Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ // Example works with either Wired or WiFi Ethernet, define one of these values to 1, other to 0 #define USE_WIFI 1 #define USE_WIRED 0 #if USE_WIFI #include #elif USE_WIRED #include // Or W5100lwIP.h or ENC28J60lwIP.h Wiznet5500lwIP eth(1 /* chip select */); // or Wiznet5100lwIP or ENC28J60lwIP #endif #include #include #include #include #ifndef STASSID #define STASSID "your-ssid" #define STAPSK "your-password" #endif const char *ssid = STASSID; const char *password = STAPSK; WebServer server(80); const int led = LED_BUILTIN; void handleRoot() { static int cnt = 0; digitalWrite(led, 1); int sec = millis() / 1000; int hr = sec / 3600; int min = (sec / 60) % 60; sec = sec % 60; StreamString temp; temp.reserve(500); // Preallocate a large chunk to avoid memory fragmentation temp.printf("\ \ \ " BOARD_NAME " Demo\ \ \ \

Hello from the " BOARD_NAME "!

\

Uptime: %02d:%02d:%02d

\

Free Memory: %d

\

Page Count: %d

\ \ \ ", hr, min, sec, rp2040.getFreeHeap(), ++cnt); server.send(200, "text/html", temp); digitalWrite(led, 0); } void handleNotFound() { digitalWrite(led, 1); String message = "File Not Found\n\n"; message += "URI: "; message += server.uri(); message += "\nMethod: "; message += (server.method() == HTTP_GET) ? "GET" : "POST"; message += "\nArguments: "; message += server.args(); message += "\n"; for (uint8_t i = 0; i < server.args(); i++) { message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; } server.send(404, "text/plain", message); digitalWrite(led, 0); } void drawGraph() { String out; out.reserve(2600); char temp[70]; out += "\n"; out += "\n"; out += "\n"; int y = rand() % 130; for (int x = 10; x < 390; x += 10) { int y2 = rand() % 130; sprintf(temp, "\n", x, 140 - y, x + 10, 140 - y2); out += temp; y = y2; } out += "\n\n"; server.send(200, "image/svg+xml", out); } void setup(void) { pinMode(led, OUTPUT); digitalWrite(led, 0); Serial.begin(115200); #if USE_WIFI WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); Serial.println(""); // Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); #elif USE_WIRED // Set up SPI pinout to match your HW SPI.setRX(0); SPI.setCS(1); SPI.setSCK(2); SPI.setTX(3); // Start the Ethernet port if (!eth.begin()) { Serial.println("No wired Ethernet hardware detected. Check pinouts, wiring."); while (1) { delay(1000); } } // Wait for connection while (eth.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.print("IP address: "); Serial.println(eth.localIP()); #endif if (MDNS.begin("picow")) { Serial.println("MDNS responder started"); } server.on("/", handleRoot); server.on("/test.svg", drawGraph); server.on("/inline", []() { server.send(200, "text/plain", "this works as well"); }); server.onNotFound(handleNotFound); server.begin(); Serial.println("HTTP server started"); } void loop(void) { server.handleClient(); MDNS.update(); }