Advertisement
Guest User

Untitled

a guest
Sep 19th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.81 KB | None | 0 0
  1. #include <errno.h>
  2. #include <limits.h>
  3. #include <time.h>
  4.  
  5. #include <stdio.h>
  6. #include <ctype.h>
  7. #include <string.h>
  8. #include <stdint.h>
  9. #include <Arduino.h>
  10. #include <ESP8266WebServer.h>
  11. #include <ESP8266mDNS.h>
  12. #include <FS.h>
  13. #include <ESP8266HTTPUpdateServer.h>
  14.  
  15. #include "web_server.h"
  16.  
  17. ESP8266WebServer server(80);
  18. ESP8266HTTPUpdateServer httpUpdater;
  19.  
  20. const char* update_path = "/firmware";
  21. const char* update_username = "admin";
  22. const char* update_password = "admin";
  23.  
  24.  
  25.  
  26. JsonCommandsBuffer JsonResults;
  27.  
  28. bool isConnected = false;
  29.  
  30. String getContentType(String filename){
  31.   if(server.hasArg("download")) return "application/octet-stream";
  32.   else if(filename.endsWith(".htm")) return "text/html";
  33.   else if(filename.endsWith(".html")) return "text/html";
  34.   else if(filename.endsWith(".json")) return "text/json";  
  35.   else if(filename.endsWith(".css")) return "text/css";
  36.   else if(filename.endsWith(".js")) return "application/javascript";
  37.   else if(filename.endsWith(".gz")) return "application/x-gzip";
  38.   return "text/plain";
  39. }
  40.  
  41.  
  42. void handleRoot() {  
  43.   server.send(200, "text/plain", "hello from esp82633!");  
  44. }
  45.  
  46. File fsUploadFile;
  47.  
  48. bool handleFileRead(String path){
  49.  
  50.   if(path.endsWith("/")) path += "index.htm";
  51.   String contentType = getContentType(path);
  52.   String pathWithGz = path + ".gz";
  53.   if(SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)){
  54.     if(SPIFFS.exists(pathWithGz))
  55.       path += ".gz";
  56.     File file = SPIFFS.open(path, "r");
  57.     size_t sent = server.streamFile(file, contentType);
  58.     file.close();
  59.     return true;
  60.   }
  61.   return false;
  62. }
  63.  
  64. void handleFileUpload(){
  65.   if(server.uri() != "/edit") return;
  66.   HTTPUpload& upload = server.upload();
  67.   if(upload.status == UPLOAD_FILE_START){
  68.     String filename = upload.filename;
  69.     if(!filename.startsWith("/")) filename = "/"+filename;
  70.     fsUploadFile = SPIFFS.open(filename, "w");
  71.     filename = String();
  72.   } else if(upload.status == UPLOAD_FILE_WRITE){
  73.     if(fsUploadFile)
  74.       fsUploadFile.write(upload.buf, upload.currentSize);
  75.   } else if(upload.status == UPLOAD_FILE_END){
  76.     if(fsUploadFile)
  77.       fsUploadFile.close();
  78.   }
  79. }
  80.  
  81. void handleWebServerRequests()
  82. {
  83.   if(isConnected)
  84.   {
  85.      server.handleClient();
  86.   }
  87. }
  88.  
  89. bool initWebServer()
  90. {
  91.   // Wait for connection
  92.   int ledVal = LOW;  
  93.   Serial.print("INFO:IP address: ");
  94.   Serial.println(WiFi.localIP());
  95.  
  96.   if (MDNS.begin("device2")) {
  97.     Serial.println("MDNS responder started");
  98.   }
  99.  
  100.   MDNS.addService("http", "tcp", 80); // Announce esp tcp service on port 8080
  101.   server.on("/", handleRoot);
  102.  
  103.  
  104.   server.on("/reloadconfig", HTTP_GET, []() {              
  105.     if(realoadConfiguration(&Serial))
  106.         server.send ( 200, "text/json", "{\"success\":\"true\"}" );
  107.      else
  108.         server.send ( 200, "text/json", "{\"success\":\"false\"}" );
  109.   });
  110.  
  111.   server.on("/devset", HTTP_GET, []() {              
  112.     if (server.hasArg("device") == false) { //Check if body received      
  113.       server.send(200, "text/json", "{ \"success\":\"false\", \"error\" : \"Device not set\"}");
  114.       return;
  115.     }
  116.  
  117.     if (server.hasArg("value") == false) {      
  118.       server.send(200, "text/json", "{\"success\":\"false\", \"error\" : \"Statuc not set\"}");
  119.       return;
  120.     }
  121.  
  122.    
  123.     Serial.print(F("devset "));
  124.     Serial.print(server.arg("device"));
  125.     Serial.print(F(" "));
  126.     Serial.println(server.arg("value"));
  127.     server.send ( 200, "text/json", "{\"success\":\"true\"}" );
  128.   });
  129.  
  130.   server.on("/status", HTTP_GET, []() {              
  131.     String res = "[";    
  132.     for(int i = 0; i < JsonResults.size(); i++)
  133.     {      
  134.       if(i > 0) res += ",";        
  135.       res += JsonResults[i];      
  136.     }
  137.  
  138.     res += "]";
  139.  
  140.     JsonResults.clear();
  141.     server.send ( 200, "text/json", res );
  142.   });
  143.  
  144.   server.on("/devsreq", HTTP_GET, []() {              
  145.    
  146.     if (server.hasArg("ids") == false) { //Check if body received      
  147.       server.send(200, "text/json", "{\"success\":\"false\", \"error\" = 'Device not set'}");
  148.       return;
  149.     }
  150.    
  151.     Serial.print("devsget ");
  152.     Serial.println(server.arg("ids"));
  153.     server.send ( 200, "text/json", "{\"success\":\"true\"}" );
  154.   });
  155.  
  156.   server.on("/inline", []() {
  157.     Serial.println("inline request");
  158.     server.send(200, "text/plain", "this works as well");
  159.   });
  160.  
  161.  server.on("/edit", HTTP_GET, [](){
  162.     if(!handleFileRead("/edit.htm")) server.send(404, "text/plain", "FileNotFound");
  163.   });
  164.  
  165.   server.on("/edit", HTTP_POST, [](){ server.send(200, "text/plain", ""); }, handleFileUpload);
  166.  
  167.   server.onNotFound([](){
  168.     if(!handleFileRead(server.uri()))
  169.       server.send(404, "text/plain", "FileNotFound");
  170.   });
  171.  
  172.  
  173.   httpUpdater.setup(&server, update_path, update_username, update_password);
  174.   server.begin();  
  175.   isConnected = true;
  176.   return true;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement