|
| 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 <ESP8266WiFi.h> |
| 32 | +#include <WiFiClient.h> |
| 33 | +#include <ESP8266WebServer.h> |
| 34 | +#include <ESP8266mDNS.h> |
| 35 | + |
| 36 | +const char *ssid = "YourSSIDHere"; |
| 37 | +const char *password = "YourPSKHere"; |
| 38 | +MDNSResponder mdns; |
| 39 | + |
| 40 | +ESP8266WebServer server ( 80 ); |
| 41 | + |
| 42 | +const int led = 13; |
| 43 | + |
| 44 | +void handleRoot() { |
| 45 | + digitalWrite ( led, 1 ); |
| 46 | + char temp[400]; |
| 47 | + int sec = millis() / 1000; |
| 48 | + int min = sec / 60; |
| 49 | + int hr = min / 60; |
| 50 | + |
| 51 | + snprintf ( temp, 400, |
| 52 | + |
| 53 | +"<html>\ |
| 54 | + <head>\ |
| 55 | + <meta http-equiv='refresh' content='5'/>\ |
| 56 | + <title>ESP8266 Demo</title>\ |
| 57 | + <style>\ |
| 58 | + body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\ |
| 59 | + </style>\ |
| 60 | + </head>\ |
| 61 | + <body>\ |
| 62 | + <h1>Hello from ESP8266!</h1>\ |
| 63 | + <p>Uptime: %02d:%02d:%02d</p>\ |
| 64 | + <img src=\"/test.svg\" />\ |
| 65 | + </body>\ |
| 66 | +</html>", |
| 67 | + |
| 68 | + hr, min % 60, sec % 60 |
| 69 | + ); |
| 70 | + server.send ( 200, "text/html", temp ); |
| 71 | + digitalWrite ( led, 0 ); |
| 72 | +} |
| 73 | + |
| 74 | +void handleNotFound() { |
| 75 | + digitalWrite ( led, 1 ); |
| 76 | + String message = "File Not Found\n\n"; |
| 77 | + message += "URI: "; |
| 78 | + message += server.uri(); |
| 79 | + message += "\nMethod: "; |
| 80 | + message += ( server.method() == HTTP_GET ) ? "GET" : "POST"; |
| 81 | + message += "\nArguments: "; |
| 82 | + message += server.args(); |
| 83 | + message += "\n"; |
| 84 | + |
| 85 | + for ( uint8_t i = 0; i < server.args(); i++ ) { |
| 86 | + message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n"; |
| 87 | + } |
| 88 | + |
| 89 | + server.send ( 404, "text/plain", message ); |
| 90 | + digitalWrite ( led, 0 ); |
| 91 | +} |
| 92 | + |
| 93 | +void setup ( void ) { |
| 94 | + pinMode ( led, OUTPUT ); |
| 95 | + digitalWrite ( led, 0 ); |
| 96 | + Serial.begin ( 115200 ); |
| 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 ( "esp8266", WiFi.localIP() ) ) { |
| 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 | + mdns.update(); |
| 128 | + server.handleClient(); |
| 129 | +} |
| 130 | + |
| 131 | +void drawGraph() { |
| 132 | + String out = ""; |
| 133 | + char temp[100]; |
| 134 | + out += "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"400\" height=\"150\">\n"; |
| 135 | + out += "<rect width=\"400\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"1\" stroke=\"rgb(0, 0, 0)\" />\n"; |
| 136 | + out += "<g stroke=\"black\">\n"; |
| 137 | + int y = rand() % 130; |
| 138 | + for (int x = 10; x < 390; x+= 10) { |
| 139 | + int y2 = rand() % 130; |
| 140 | + sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"1\" />\n", x, 140 - y, x + 10, 140 - y2); |
| 141 | + out += temp; |
| 142 | + y = y2; |
| 143 | + } |
| 144 | + out += "</g>\n</svg>\n"; |
| 145 | + |
| 146 | + server.send ( 200, "image/svg+xml", out); |
| 147 | +} |
0 commit comments