An ESP8266 based webserver and a web-app.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
2.9 KiB

  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266WebServer.h>
  4. #include <ESP8266mDNS.h>
  5. #include "FS.h"
  6. ESP8266WebServer server(80);
  7. void mainLoadRoute()
  8. {
  9. SPIFFS.begin();
  10. File f = SPIFFS.open("/index.html", "r");
  11. server.streamFile(f, "text/html");
  12. int filesize = f.size();
  13. SPIFFS.end();
  14. String WebString = "";
  15. WebString += "HTTP/1.1 200 OK\r\n";
  16. WebString += "Content-Type: text/html; charset=utf-8\r\n";
  17. WebString += "Content-Length: " + String(filesize) + "\r\n";
  18. WebString += "\r\n";
  19. server.send(200, "text/html", WebString);
  20. }
  21. void patternLoadRoute(const char *patternFilename)
  22. {
  23. SPIFFS.begin();
  24. File f = SPIFFS.open(patternFilename, "r");
  25. server.streamFile(f, "image/png");
  26. int filesize = f.size();
  27. SPIFFS.end();
  28. String WebString = "";
  29. WebString += "HTTP/1.1 200 OK\r\n";
  30. WebString += "Content-Type: image/png\r\n";
  31. WebString += "Content-Length: " + String(filesize) + "\r\n";
  32. WebString += "\r\n";
  33. server.send(200, "text/html", WebString);
  34. }
  35. void patternLoad1Route()
  36. {
  37. patternLoadRoute("/pattern_1.png");
  38. }
  39. void patternLoad2Route()
  40. {
  41. patternLoadRoute("/pattern_2.png");
  42. }
  43. void patternLoad3Route()
  44. {
  45. patternLoadRoute("/pattern_3.png");
  46. }
  47. void patternLoad4Route()
  48. {
  49. patternLoadRoute("/pattern_4.png");
  50. }
  51. void patternLoad5Route()
  52. {
  53. patternLoadRoute("/pattern_5.png");
  54. }
  55. void patternAction1Route()
  56. {
  57. }
  58. void patternAction2Route()
  59. {
  60. }
  61. void patternAction3Route()
  62. {
  63. }
  64. void patternAction4Route()
  65. {
  66. }
  67. void patternAction5Route()
  68. {
  69. }
  70. void notFoundRoute()
  71. {
  72. String message = "File Not Found\n\n";
  73. message += "URI: ";
  74. message += server.uri();
  75. message += "\nMethod: ";
  76. message +=(server.method() == HTTP_GET) ? "GET" : "POST";
  77. message += "\nArguments: ";
  78. message += server.args();
  79. message += "\n";
  80. for(uint8_t i = 0; i < server.args(); i++) {
  81. message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  82. }
  83. server.send(404, "text/plain", message);
  84. }
  85. void setup(void)
  86. {
  87. Serial.begin(115200);
  88. WiFi.begin(ssid, password);
  89. Serial.println("");
  90. // Wait for connection
  91. while(WiFi.status() != WL_CONNECTED) {
  92. delay(500);
  93. Serial.print(".");
  94. }
  95. Serial.println("");
  96. Serial.print("Connected to ");
  97. Serial.println(ssid);
  98. Serial.print("IP address: ");
  99. Serial.println(WiFi.localIP());
  100. if(MDNS.begin("esp8266")) {
  101. Serial.println("MDNS responder started");
  102. }
  103. server.on("/", mainLoadRoute);
  104. server.on("/data/pattern_1.png", patternLoad1Route);
  105. server.on("/data/pattern_2.png", patternLoad2Route);
  106. server.on("/data/pattern_3.png", patternLoad3Route);
  107. server.on("/data/pattern_4.png", patternLoad4Route);
  108. server.on("/data/pattern_5.png", patternLoad5Route);
  109. server.on("/pattern/1", patternAction1Route);
  110. server.on("/pattern/2", patternAction2Route);
  111. server.on("/pattern/3", patternAction3Route);
  112. server.on("/pattern/4", patternAction4Route);
  113. server.on("/pattern/5", patternAction5Route);
  114. server.onNotFound(notFoundRoute);
  115. server.begin();
  116. }
  117. void loop(void)
  118. {
  119. server.handleClient();
  120. }