|
| 1 | +#include <WiFi.h> |
| 2 | +#include <WiFiClient.h> |
| 3 | +#include <WebServer.h> |
| 4 | +#include <LEAmDNS.h> |
| 5 | + |
| 6 | +#ifndef STASSID |
| 7 | +#define STASSID "your-ssid" |
| 8 | +#define STAPSK "your-password" |
| 9 | +#endif |
| 10 | + |
| 11 | +const char* ssid = STASSID; |
| 12 | +const char* password = STAPSK; |
| 13 | + |
| 14 | +WebServer server(80); |
| 15 | + |
| 16 | +const int led = LED_BUILTIN; |
| 17 | + |
| 18 | +void handleRoot() { |
| 19 | + digitalWrite(led, 1); |
| 20 | + server.send(200, "text/plain", "hello from pico w!\r\n"); |
| 21 | + digitalWrite(led, 0); |
| 22 | +} |
| 23 | + |
| 24 | +void handleNotFound() { |
| 25 | + digitalWrite(led, 1); |
| 26 | + String message = "File Not Found\n\n"; |
| 27 | + message += "URI: "; |
| 28 | + message += server.uri(); |
| 29 | + message += "\nMethod: "; |
| 30 | + message += (server.method() == HTTP_GET) ? "GET" : "POST"; |
| 31 | + message += "\nArguments: "; |
| 32 | + message += server.args(); |
| 33 | + message += "\n"; |
| 34 | + for (uint8_t i = 0; i < server.args(); i++) { |
| 35 | + message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; |
| 36 | + } |
| 37 | + server.send(404, "text/plain", message); |
| 38 | + digitalWrite(led, 0); |
| 39 | +} |
| 40 | + |
| 41 | +void setup(void) { |
| 42 | + pinMode(led, OUTPUT); |
| 43 | + digitalWrite(led, 0); |
| 44 | + Serial.begin(115200); |
| 45 | + WiFi.mode(WIFI_STA); |
| 46 | + WiFi.begin(ssid, password); |
| 47 | + Serial.println(""); |
| 48 | + |
| 49 | + // Wait for connection |
| 50 | + while (WiFi.status() != WL_CONNECTED) { |
| 51 | + delay(500); |
| 52 | + Serial.print("."); |
| 53 | + } |
| 54 | + Serial.println(""); |
| 55 | + Serial.print("Connected to "); |
| 56 | + Serial.println(ssid); |
| 57 | + Serial.print("IP address: "); |
| 58 | + Serial.println(WiFi.localIP()); |
| 59 | + |
| 60 | + if (MDNS.begin("picow")) { |
| 61 | + Serial.println("MDNS responder started"); |
| 62 | + } |
| 63 | + |
| 64 | + server.on("/", handleRoot); |
| 65 | + |
| 66 | + server.on("/inline", []() { |
| 67 | + server.send(200, "text/plain", "this works as well"); |
| 68 | + }); |
| 69 | + |
| 70 | + server.on("/gif", []() { |
| 71 | + static const uint8_t gif[] = { |
| 72 | + 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x10, 0x00, 0x10, 0x00, 0x80, 0x01, |
| 73 | + 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x2c, 0x00, 0x00, 0x00, 0x00, |
| 74 | + 0x10, 0x00, 0x10, 0x00, 0x00, 0x02, 0x19, 0x8c, 0x8f, 0xa9, 0xcb, 0x9d, |
| 75 | + 0x00, 0x5f, 0x74, 0xb4, 0x56, 0xb0, 0xb0, 0xd2, 0xf2, 0x35, 0x1e, 0x4c, |
| 76 | + 0x0c, 0x24, 0x5a, 0xe6, 0x89, 0xa6, 0x4d, 0x01, 0x00, 0x3b |
| 77 | + }; |
| 78 | + char gif_colored[sizeof(gif)]; |
| 79 | + memcpy_P(gif_colored, gif, sizeof(gif)); |
| 80 | + // Set the background to a random set of colors |
| 81 | + gif_colored[16] = millis() % 256; |
| 82 | + gif_colored[17] = millis() % 256; |
| 83 | + gif_colored[18] = millis() % 256; |
| 84 | + server.send(200, "image/gif", gif_colored, sizeof(gif_colored)); |
| 85 | + }); |
| 86 | + |
| 87 | + server.onNotFound(handleNotFound); |
| 88 | + |
| 89 | + ///////////////////////////////////////////////////////// |
| 90 | + // Hook examples |
| 91 | + |
| 92 | + server.addHook([](const String & method, const String & url, WiFiClient * client, WebServer::ContentTypeFunction contentType) { |
| 93 | + (void)method; // GET, PUT, ... |
| 94 | + (void)url; // example: /root/myfile.html |
| 95 | + (void)client; // the webserver tcp client connection |
| 96 | + (void)contentType; // contentType(".html") => "text/html" |
| 97 | + Serial.printf("A useless web hook has passed\n"); |
| 98 | + return WebServer::CLIENT_REQUEST_CAN_CONTINUE; |
| 99 | + }); |
| 100 | + |
| 101 | + server.addHook([](const String&, const String & url, WiFiClient*, WebServer::ContentTypeFunction) { |
| 102 | + if (url.startsWith("/fail")) { |
| 103 | + Serial.printf("An always failing web hook has been triggered\n"); |
| 104 | + return WebServer::CLIENT_MUST_STOP; |
| 105 | + } |
| 106 | + return WebServer::CLIENT_REQUEST_CAN_CONTINUE; |
| 107 | + }); |
| 108 | + |
| 109 | + server.addHook([](const String&, const String & url, WiFiClient * client, WebServer::ContentTypeFunction) { |
| 110 | + if (url.startsWith("/dump")) { |
| 111 | + Serial.printf("The dumper web hook is on the run\n"); |
| 112 | + |
| 113 | + // Here the request is not interpreted, so we cannot for sure |
| 114 | + // swallow the exact amount matching the full request+content, |
| 115 | + // hence the tcp connection cannot be handled anymore by the |
| 116 | + auto last = millis(); |
| 117 | + while ((millis() - last) < 500) { |
| 118 | + char buf[32]; |
| 119 | + size_t len = client->read((uint8_t*)buf, sizeof(buf)); |
| 120 | + if (len > 0) { |
| 121 | + Serial.printf("(<%d> chars)", (int)len); |
| 122 | + Serial.write(buf, len); |
| 123 | + last = millis(); |
| 124 | + } |
| 125 | + } |
| 126 | + // Two choices: return MUST STOP and webserver will close it |
| 127 | + // (we already have the example with '/fail' hook) |
| 128 | + // or IS GIVEN and webserver will forget it |
| 129 | + // trying with IS GIVEN and storing it on a dumb WiFiClient. |
| 130 | + // check the client connection: it should not immediately be closed |
| 131 | + // (make another '/dump' one to close the first) |
| 132 | + Serial.printf("\nTelling server to forget this connection\n"); |
| 133 | + static WiFiClient forgetme = *client; // stop previous one if present and transfer client refcounter |
| 134 | + return WebServer::CLIENT_IS_GIVEN; |
| 135 | + } |
| 136 | + return WebServer::CLIENT_REQUEST_CAN_CONTINUE; |
| 137 | + }); |
| 138 | + |
| 139 | + // Hook examples |
| 140 | + ///////////////////////////////////////////////////////// |
| 141 | + |
| 142 | + server.begin(); |
| 143 | + Serial.println("HTTP server started"); |
| 144 | +} |
| 145 | + |
| 146 | +void loop(void) { |
| 147 | + server.handleClient(); |
| 148 | + MDNS.update(); |
| 149 | +} |
0 commit comments