Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  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. #ifndef STASSID
  37. #define STASSID "MikroTik-D31"
  38. #define STAPSK "mikrobus"
  39. #endif
  40.  
  41. const char *ssid = STASSID;
  42. const char *password = STAPSK;
  43.  
  44. ESP8266WebServer server(80);
  45.  
  46. const int led = 13;
  47.  
  48. void handleRoot() {
  49. digitalWrite(led, 1);
  50. char temp[400];
  51. int sec = millis() / 1000;
  52. int min = sec / 60;
  53. int hr = min / 60;
  54.  
  55. snprintf(temp, 400,
  56.  
  57. "<html>\
  58. <head>\
  59. <meta http-equiv='refresh' content='5'/>\
  60. <title>Hi Guys</title>\
  61. <style>\
  62. body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
  63. </style>\
  64. </head>\
  65. <body>\
  66. <h1>Welcome To The Game</h1>\
  67. <h2>Všichni jste irelevantní</h2>\
  68. <h3>Sbohem</h3>\
  69. <b>Tučný text</b>, <i>skloněný text</i> a <u>podtržení</u>.\
  70. <p>Uptime: %02d:%02d:%02d</p>\
  71. <img src=\"/test.svg\" />\
  72. </body>\
  73. </html>",
  74.  
  75. hr, min % 60, sec % 60
  76. );
  77. server.send(200, "text/html", temp);
  78. digitalWrite(led, 0);
  79. }
  80.  
  81. void handleNotFound() {
  82. digitalWrite(led, 1);
  83. String message = "File Not Found\n\n";
  84. message += "URI: ";
  85. message += server.uri();
  86. message += "\nMethod: ";
  87. message += (server.method() == HTTP_GET) ? "GET" : "POST";
  88. message += "\nArguments: ";
  89. message += server.args();
  90. message += "\n";
  91.  
  92. for (uint8_t i = 0; i < server.args(); i++) {
  93. message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  94. }
  95.  
  96. server.send(404, "text/plain", message);
  97. digitalWrite(led, 0);
  98. }
  99.  
  100. void setup(void) {
  101. pinMode(led, OUTPUT);
  102. digitalWrite(led, 0);
  103. Serial.begin(115200);
  104. WiFi.mode(WIFI_STA);
  105. WiFi.begin(ssid, password);
  106. Serial.println("");
  107.  
  108. // Wait for connection
  109. while (WiFi.status() != WL_CONNECTED) {
  110. delay(500);
  111. Serial.print(".");
  112. }
  113.  
  114. Serial.println("");
  115. Serial.print("Connected to ");
  116. Serial.println(ssid);
  117. Serial.print("IP address: ");
  118. Serial.println(WiFi.localIP());
  119.  
  120. if (MDNS.begin("esp8266")) {
  121. Serial.println("MDNS responder started");
  122. }
  123.  
  124. server.on("/", handleRoot);
  125. server.on("/test.svg", drawGraph);
  126. server.on("/inline", []() {
  127. server.send(200, "text/plain", "this works as well");
  128. });
  129. server.onNotFound(handleNotFound);
  130. server.begin();
  131. Serial.println("HTTP server started");
  132. }
  133.  
  134. void loop(void) {
  135. server.handleClient();
  136. MDNS.update();
  137. }
  138.  
  139. void drawGraph() {
  140. String out = "";
  141. char temp[100];
  142. out += "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"400\" height=\"150\">\n";
  143. out += "<rect width=\"400\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"1\" stroke=\"rgb(0, 0, 0)\" />\n";
  144. out += "<g stroke=\"black\">\n";
  145. int y = rand() % 130;
  146. for (int x = 10; x < 390; x += 10) {
  147. int y2 = rand() % 130;
  148. sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"1\" />\n", x, 140 - y, x + 10, 140 - y2);
  149. out += temp;
  150. y = y2;
  151. }
  152. out += "</g>\n</svg>\n";
  153.  
  154. server.send(200, "image/svg+xml", out);
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement