esp32webserver2.ino 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #include <WiFiClient.h>
  31. #include <ESP32WebServer.h>
  32. #include <WiFi.h>
  33. #include <ESPmDNS.h>
  34. const char* ssid = "mieu mieu 01";
  35. const char* password = "09471919479";
  36. ESP32WebServer server ( 80 );
  37. const int led = 13;
  38. void handleRoot() {
  39. digitalWrite ( led, 1 );
  40. char temp[400];
  41. int sec = millis() / 1000;
  42. int min = sec / 60;
  43. int hr = min / 60;
  44. snprintf ( temp, 400,
  45. "<html>\
  46. <head>\
  47. <meta http-equiv='refresh' content='5'/>\
  48. <title>ESP32 Demo</title>\
  49. <style>\
  50. body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
  51. </style>\
  52. </head>\
  53. <body>\
  54. <h1>Hello from ESP32!</h1>\
  55. <p>Uptime: %02d:%02d:%02d</p>\
  56. <img src=\"/test.svg\" />\
  57. </body>\
  58. </html>",
  59. hr, min % 60, sec % 60
  60. );
  61. server.send ( 200, "text/html", temp );
  62. digitalWrite ( led, 0 );
  63. }
  64. void handleNotFound() {
  65. digitalWrite ( led, 1 );
  66. String message = "File Not Found\n\n";
  67. message += "URI: ";
  68. message += server.uri();
  69. message += "\nMethod: ";
  70. message += ( server.method() == HTTP_GET ) ? "GET" : "POST";
  71. message += "\nArguments: ";
  72. message += server.args();
  73. message += "\n";
  74. for ( uint8_t i = 0; i < server.args(); i++ ) {
  75. message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n";
  76. }
  77. server.send ( 404, "text/plain", message );
  78. digitalWrite ( led, 0 );
  79. }
  80. void setup ( void ) {
  81. pinMode ( led, OUTPUT );
  82. digitalWrite ( led, 0 );
  83. Serial.begin ( 115200 );
  84. WiFi.begin ( ssid, password );
  85. Serial.println ( "" );
  86. // Wait for connection
  87. while ( WiFi.status() != WL_CONNECTED ) {
  88. delay ( 500 );
  89. Serial.print ( "." );
  90. }
  91. Serial.println ( "" );
  92. Serial.print ( "Connected to " );
  93. Serial.println ( ssid );
  94. Serial.print ( "IP address: " );
  95. Serial.println ( WiFi.localIP() );
  96. if ( MDNS.begin ( "esp32" ) ) {
  97. Serial.println ( "MDNS responder started" );
  98. }
  99. server.on ( "/", handleRoot );
  100. server.on ( "/test.svg", drawGraph );
  101. server.on ( "/inline", []() {
  102. server.send ( 200, "text/plain", "this works as well" );
  103. } );
  104. server.onNotFound ( handleNotFound );
  105. server.begin();
  106. Serial.println ( "HTTP server started" );
  107. }
  108. void loop ( void ) {
  109. server.handleClient();
  110. }
  111. void drawGraph() {
  112. String out = "";
  113. char temp[100];
  114. out += "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"400\" height=\"150\">\n";
  115. out += "<rect width=\"400\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"1\" stroke=\"rgb(0, 0, 0)\" />\n";
  116. out += "<g stroke=\"black\">\n";
  117. int y = rand() % 130;
  118. for (int x = 10; x < 390; x+= 10) {
  119. int y2 = rand() % 130;
  120. sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"1\" />\n", x, 140 - y, x + 10, 140 - y2);
  121. out += temp;
  122. y = y2;
  123. }
  124. out += "</g>\n</svg>\n";
  125. server.send ( 200, "image/svg+xml", out);
  126. }