v12345vtm

spiffs-ajax

May 4th, 2018
683
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.39 KB | None | 0 0
  1. //https://www.youtube.com/user/v12345vtm
  2.  
  3.  
  4. #include <ESP8266WiFi.h>
  5. #include <WiFiClient.h>
  6. #include <ESP8266WiFiMulti.h>
  7. #include <ESP8266mDNS.h>
  8. #include <ESP8266WebServer.h>
  9. #include <FS.h> // Include the SPIFFS library
  10.  
  11. ESP8266WiFiMulti wifiMulti; // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti'
  12.  
  13. ESP8266WebServer server(80); // Create a webserver object that listens for HTTP request on port 80
  14. String versie = "inofile = pantiltjvcvirtualjoy3ajax";
  15. String getContentType(String filename); // convert the file extension to the MIME type
  16. bool handleFileRead(String path); // send the right file to the client (if it exists)
  17. String st;
  18. String content;
  19. String slider = "slider";
  20. String testje = "0";
  21.  
  22.  
  23. void setup() {
  24. yield();
  25. Serial.begin(115200); // Start the Serial communication to send messages to the computer
  26. delay(10);
  27. Serial.println('\n'); //
  28.  
  29.  
  30. // add Wi-Fi networks you want to connect to
  31. wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2");
  32. wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3");
  33.  
  34. Serial.println("Connecting ...");
  35. int i = 0;
  36. while (wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
  37. delay(100);
  38. Serial.print('.');
  39. yield();
  40. }
  41. Serial.println(versie);
  42. Serial.print("Connected to ");
  43. Serial.println(WiFi.SSID()); // Tell us what network we're connected to
  44. Serial.print("IP address:\t");
  45. Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
  46.  
  47. if (MDNS.begin("esp8266")) { // Start the mDNS responder for esp8266.local
  48. Serial.println("mDNS responder started");
  49. } else {
  50. Serial.println("Error setting up MDNS responder!");
  51. }
  52.  
  53. SPIFFS.begin(); // Start the SPI Flash Files System
  54.  
  55. server.onNotFound([]() { // If the client requests any URI
  56. if (!handleFileRead(server.uri())) // send it if it exists
  57. server.send(404, "text/plain", "404: Not Found"); // otherwise, respond with a 404 (Not Found) error
  58. });
  59.  
  60. server.begin(); // Actually start the server
  61. Serial.println("HTTP server started");
  62. launchWeb();
  63.  
  64. //Onboard LED port Direction output
  65. pinMode(5,OUTPUT);
  66. }
  67.  
  68.  
  69. ///////////
  70. void launchWeb() {
  71. server.on("/test", []() {
  72. IPAddress ip = WiFi.softAPIP();
  73. String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
  74. content = "<!DOCTYPE HTML>\r\n<html>Hello from ESP8266 at accespoint ";
  75. content += ipStr;
  76. content += "<p>";
  77. content += "<br>";
  78. content += versie;
  79. content += "<br>";
  80. content += "<br> ultrasoon = ";
  81. // content += distance;
  82. content += "<br>";
  83. content += "<input type='range' min='1' max='100' value='50' class='slider' id='myRange'>";
  84.  
  85. content += "<label>ssid op accespoint: </label><input name='testje' length=32 value='";
  86. content += "accespoint";
  87. content += "' >";
  88.  
  89. content += " <a href=""";
  90. content += "/ultrasoonmeting";
  91. content += """ target=""_blank""> live meting ultrasoon</a> " ;
  92.  
  93.  
  94. content += " <a href=""";
  95. content += "/cleareeprom";
  96. content += """ target=""_blank""> clear de eprom</a> " ;
  97. content += st;
  98. content += "</p><form method='get' action='/test'>";
  99. content += "<input type='range' min='1' max='100' value='50' class='slider' name='slider'>";
  100.  
  101. content += "<label>testhier: </label><input name='testje' length=32><input name='pass' length=64><input type='submit'></form>";
  102. content += "</html>";
  103. server.send(200, "text/html", content);
  104.  
  105.  
  106. testje = server.arg("testje");
  107. slider = server.arg("slider");
  108.  
  109. Serial.print ("eerste respons =");
  110. Serial.print (slider);
  111. Serial.print (" ");
  112. Serial.println(testje);
  113.  
  114.  
  115. });
  116.  
  117. ////////
  118. server.on("/setLEDxy", handleXY);
  119.  
  120. server.on("/setLED", handleLED);
  121.  
  122. }//stop lounchwebroutine
  123. ///////////
  124. void handleXY() {
  125. String ledState = "ACK from ESP32";
  126. String t_state = server.arg("LEDstatexy"); //Refer xhttp.open("GET", "setLED?LEDstate="+led, true);
  127. Serial.println("virt ajax joy");
  128. Serial.println(t_state);
  129. if(t_state == "1")
  130. {
  131. digitalWrite(5,HIGH); //LED ON
  132. ledState = "ONxy"; //Feedback parameter
  133. }
  134.  
  135.  
  136. server.send(200, "text/plane", ledState); //Send web page
  137. }
  138.  
  139.  
  140. ////////////
  141.  
  142. void handleLED() {
  143. String ledState = "OFF";
  144. String t_state = server.arg("LEDstate"); //Refer xhttp.open("GET", "setLED?LEDstate="+led, true);
  145. Serial.println(t_state);
  146. if(t_state == "1")
  147. {
  148. digitalWrite(5,HIGH); //LED ON
  149. ledState = "ON"; //Feedback parameter
  150. }
  151. else
  152. {
  153. digitalWrite(5,LOW); //LED OFF
  154. ledState = "OFF"; //Feedback parameter
  155. }
  156.  
  157. server.send(200, "text/plane", ledState); //Send web page
  158. }
  159.  
  160.  
  161.  
  162.  
  163. //////////
  164. void leeswaarde()
  165. {
  166. testje = server.arg("testje");
  167. slider = server.arg("slider");
  168. Serial.print ("tweede respons =");
  169. server.send(200, "text/html", content);//we tonen pagina test
  170. }
  171. /////
  172.  
  173.  
  174. void loop(void) {
  175. server.handleClient();
  176. yield();
  177. delay(1);
  178. // Serial.print(testje);
  179. // Serial.print(" ");
  180. // Serial.println(slider);
  181. }
  182.  
  183. String getContentType(String filename) { // convert the file extension to the MIME type
  184. if (filename.endsWith(".html")) return "text/html";
  185. else if (filename.endsWith(".css")) return "text/css";
  186. else if (filename.endsWith(".js")) return "application/javascript";
  187. else if (filename.endsWith(".ico")) return "image/x-icon";
  188. return "text/plain";
  189. }
  190.  
  191. bool handleFileRead(String path) { // send the right file to the client (if it exists)
  192. Serial.println("handleFileRead: " + path);
  193. if (path.endsWith("/")) path += "index.html"; // If a folder is requested, send the index file
  194. String contentType = getContentType(path); // Get the MIME type
  195. if (SPIFFS.exists(path)) {
  196.  
  197. Serial.println("\tThe file exists");// If the file exists
  198. File file = SPIFFS.open(path, "r"); // Open it
  199. size_t sent = server.streamFile(file, contentType); // And send it to the client
  200. file.close(); // Then close the file again
  201. return true;
  202. }
  203. Serial.println("\tFile Not Found");
  204. return false; // If the file doesn't exist, return false
  205. }
Advertisement
Add Comment
Please, Sign In to add comment