|
| 1 | +/* |
| 2 | + Copyright (c) 2015, Majenko Technologies |
| 3 | + All rights reserved. |
| 4 | +
|
| 5 | + Redistribution and use in source and binary forms, with or without modification, |
| 6 | + are permitted provided that the following conditions are met: |
| 7 | +
|
| 8 | + * * Redistributions of source code must retain the above copyright notice, this |
| 9 | + list of conditions and the following disclaimer. |
| 10 | +
|
| 11 | + * * Redistributions in binary form must reproduce the above copyright notice, this |
| 12 | + list of conditions and the following disclaimer in the documentation and/or |
| 13 | + other materials provided with the distribution. |
| 14 | +
|
| 15 | + * * Neither the name of Majenko Technologies nor the names of its |
| 16 | + contributors may be used to endorse or promote products derived from |
| 17 | + this software without specific prior written permission. |
| 18 | +
|
| 19 | + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 20 | + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 | + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
| 23 | + ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 24 | + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 25 | + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 26 | + ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 28 | + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | +*/ |
| 30 | + |
| 31 | +#include <WiFi.h> |
| 32 | +#include <WiFiClient.h> |
| 33 | +#include <WebServer.h> |
| 34 | +#include <ESPmDNS.h> |
| 35 | + |
| 36 | +const char *ssid = "YourSSIDHere"; |
| 37 | +const char *password = "YourPSKHere"; |
| 38 | + |
| 39 | +WebServer server(80); |
| 40 | + |
| 41 | +const int led = 13; |
| 42 | + |
| 43 | +void handleRoot() { |
| 44 | + digitalWrite(led, 1); |
| 45 | + char temp[400]; |
| 46 | + int sec = millis() / 1000; |
| 47 | + int min = sec / 60; |
| 48 | + int hr = min / 60; |
| 49 | + |
| 50 | + snprintf(temp, 400, |
| 51 | + |
| 52 | + "<html>\ |
| 53 | + <head>\ |
| 54 | + <meta http-equiv='refresh' content='5'/>\ |
| 55 | + <title>ESP32 Demo</title>\ |
| 56 | + <style>\ |
| 57 | + body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\ |
| 58 | + </style>\ |
| 59 | + </head>\ |
| 60 | + <body>\ |
| 61 | + <h1>Hello from ESP32!</h1>\ |
| 62 | + <p>Uptime: %02d:%02d:%02d</p>\ |
| 63 | + <img src=\"/test.svg\" />\ |
| 64 | + </body>\ |
| 65 | +</html>", |
| 66 | + |
| 67 | + hr, min % 60, sec % 60 |
| 68 | + ); |
| 69 | + server.send(200, "text/html", temp); |
| 70 | + digitalWrite(led, 0); |
| 71 | +} |
| 72 | + |
| 73 | +void handleNotFound() { |
| 74 | + digitalWrite(led, 1); |
| 75 | + String message = "File Not Found\n\n"; |
| 76 | + message += "URI: "; |
| 77 | + message += server.uri(); |
| 78 | + message += "\nMethod: "; |
| 79 | + message += (server.method() == HTTP_GET) ? "GET" : "POST"; |
| 80 | + message += "\nArguments: "; |
| 81 | + message += server.args(); |
| 82 | + message += "\n"; |
| 83 | + |
| 84 | + for (uint8_t i = 0; i < server.args(); i++) { |
| 85 | + message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; |
| 86 | + } |
| 87 | + |
| 88 | + server.send(404, "text/plain", message); |
| 89 | + digitalWrite(led, 0); |
| 90 | +} |
| 91 | + |
| 92 | +void setup(void) { |
| 93 | + pinMode(led, OUTPUT); |
| 94 | + digitalWrite(led, 0); |
| 95 | + Serial.begin(115200); |
| 96 | + WiFi.mode(WIFI_STA); |
| 97 | + WiFi.begin(ssid, password); |
| 98 | + Serial.println(""); |
| 99 | + |
| 100 | + // Wait for connection |
| 101 | + while (WiFi.status() != WL_CONNECTED) { |
| 102 | + delay(500); |
| 103 | + Serial.print("."); |
| 104 | + } |
| 105 | + |
| 106 | + Serial.println(""); |
| 107 | + Serial.print("Connected to "); |
| 108 | + Serial.println(ssid); |
| 109 | + Serial.print("IP address: "); |
| 110 | + Serial.println(WiFi.localIP()); |
| 111 | + |
| 112 | + if (MDNS.begin("esp32")) { |
| 113 | + Serial.println("MDNS responder started"); |
| 114 | + } |
| 115 | + |
| 116 | + server.on("/", handleRoot); |
| 117 | + server.on("/test.svg", drawGraph); |
| 118 | + server.on("/inline", []() { |
| 119 | + server.send(200, "text/plain", "this works as well"); |
| 120 | + }); |
| 121 | + server.onNotFound(handleNotFound); |
| 122 | + server.begin(); |
| 123 | + Serial.println("HTTP server started"); |
| 124 | +} |
| 125 | + |
| 126 | +void loop(void) { |
| 127 | + server.handleClient(); |
| 128 | +} |
| 129 | + |
| 130 | +void drawGraph() { |
| 131 | + String out = ""; |
| 132 | + char temp[100]; |
| 133 | + out += "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"400\" height=\"150\">\n"; |
| 134 | + out += "<rect width=\"400\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"1\" stroke=\"rgb(0, 0, 0)\" />\n"; |
| 135 | + out += "<g stroke=\"black\">\n"; |
| 136 | + int y = rand() % 130; |
| 137 | + for (int x = 10; x < 390; x += 10) { |
| 138 | + int y2 = rand() % 130; |
| 139 | + sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"1\" />\n", x, 140 - y, x + 10, 140 - y2); |
| 140 | + out += temp; |
| 141 | + y = y2; |
| 142 | + } |
| 143 | + out += "</g>\n</svg>\n"; |
| 144 | + |
| 145 | + server.send(200, "image/svg+xml", out); |
| 146 | +} |
0 commit comments