v12345vtm

sketch

May 4th, 2018
912
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266WiFiMulti.h>
  4. #include <ESP8266mDNS.h>
  5. #include <ESP8266WebServer.h>
  6. #include <FS.h> // Include the SPIFFS library
  7.  
  8. ESP8266WiFiMulti wifiMulti; // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti'
  9.  
  10. ESP8266WebServer server(80); // Create a webserver object that listens for HTTP request on port 80
  11.  
  12. String getContentType(String filename); // convert the file extension to the MIME type
  13. bool handleFileRead(String path); // send the right file to the client (if it exists)
  14.  
  15. void setup() {
  16. yield();
  17. Serial.begin(115200); // Start the Serial communication to send messages to the computer
  18. delay(10);
  19. Serial.println('\n'); //
  20.  
  21.  
  22. // add Wi-Fi networks you want to connect to
  23. wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2");
  24. wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3");
  25.  
  26. Serial.println("Connecting ...");
  27. int i = 0;
  28. while (wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
  29. delay(100);
  30. Serial.print('.');
  31. yield();
  32. }
  33. Serial.println('\n');
  34. Serial.print("vitual joystick spiffs pantiltjvcvirtualjoy1 --Connected to ");
  35. Serial.println(WiFi.SSID()); // Tell us what network we're connected to
  36. Serial.print("IP address:\t");
  37. Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
  38.  
  39. if (MDNS.begin("esp8266")) { // Start the mDNS responder for esp8266.local
  40. Serial.println("mDNS responder started");
  41. } else {
  42. Serial.println("Error setting up MDNS responder!");
  43. }
  44.  
  45. SPIFFS.begin(); // Start the SPI Flash Files System
  46.  
  47. server.onNotFound([]() { // If the client requests any URI
  48. if (!handleFileRead(server.uri())) // send it if it exists
  49. server.send(404, "text/plain", "404: Not Found"); // otherwise, respond with a 404 (Not Found) error
  50. });
  51.  
  52. server.begin(); // Actually start the server
  53. Serial.println("HTTP server started");
  54. }
  55.  
  56. void loop(void) {
  57. server.handleClient();
  58. yield();
  59. }
  60.  
  61. String getContentType(String filename) { // convert the file extension to the MIME type
  62. if (filename.endsWith(".html")) return "text/html";
  63. else if (filename.endsWith(".css")) return "text/css";
  64. else if (filename.endsWith(".js")) return "application/javascript";
  65. else if (filename.endsWith(".ico")) return "image/x-icon";
  66. return "text/plain";
  67. }
  68.  
  69. bool handleFileRead(String path) { // send the right file to the client (if it exists)
  70. Serial.println("handleFileRead: " + path);
  71. if (path.endsWith("/")) path += "index.html"; // If a folder is requested, send the index file
  72. String contentType = getContentType(path); // Get the MIME type
  73. if (SPIFFS.exists(path)) { // If the file exists
  74. File file = SPIFFS.open(path, "r"); // Open it
  75. size_t sent = server.streamFile(file, contentType); // And send it to the client
  76. file.close(); // Then close the file again
  77. return true;
  78. }
  79. Serial.println("\tFile Not Found");
  80. return false; // If the file doesn't exist, return false
  81. }
Advertisement
Add Comment
Please, Sign In to add comment