Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.84 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <WebServer.h>
  3. #include <SPIFFS.h>
  4. #include <WebSocketsServer.h>
  5. #include <cbuf.h>
  6.  
  7. unsigned char incomingByte,length_data,checksum_recv,checksum_calc = 0,id,length_value,value; // variável
  8. unsigned char bufferpayload[64];
  9. int pos=0,j=0,k=0;
  10.  
  11. typedef enum {WaitingStartByte,Framelength,Payload,CheckSum,Value} state_machine_t;
  12. state_machine_t data_state_s;
  13.  
  14. WebServer server;
  15. char* ssid = "AP";
  16. char* password = "";
  17.  
  18. // hold uploaded file
  19. File fsUploadFile;
  20.  
  21. // websockets server for handling messages sent by the client
  22. WebSocketsServer webSocket = WebSocketsServer(81);
  23.  
  24. void setup()
  25. {
  26.   // Format the file system in case it hasn't been done before
  27.   SPIFFS.begin(true);
  28.   // Start access point
  29.   WiFi.softAP(ssid, password);
  30.  
  31.   Serial.begin(115200);
  32.   // Print our IP address
  33.   Serial.println();
  34.   Serial.println("AP running");
  35.   Serial.print("My IP address: ");
  36.   Serial.println(WiFi.softAPIP());
  37.  
  38.   server.on("/",handleIndexFile);
  39.  
  40.   // handle JavaScript files
  41.   server.on("/smoothie.js",handleJS1File);
  42.   server.on("/smoothie-functions.js",handleJS2File);
  43.   server.on("/websocket.js",handleJS3File);
  44.   server.on("/ui.js",handleJS4File);
  45.  
  46.   // handle CSS files
  47.   server.on("/pure-min.css",handleCSS1file);
  48.   server.on("/side-menu.css",handleCSS2file);
  49.  
  50.   // list available files
  51.   server.on("/list", HTTP_GET, handleFileList);
  52.   // handle file upload
  53.   server.on("/upload", HTTP_POST, [](){
  54.     server.send(200, "text/plain", "{\"success\":1}");
  55.   }, handleFileUpload);
  56.  
  57.  
  58.   server.begin();
  59.   webSocket.begin();
  60. }
  61.  
  62. void loop()
  63. {
  64.  
  65.  webSocket.loop();
  66.  server.handleClient();
  67.  //data_SM();
  68.    
  69. }
  70.  
  71. // Update file handling functions for the ESP32
  72. void handleFileUpload() {
  73.   HTTPUpload& upload = server.upload();
  74.   if (upload.status == UPLOAD_FILE_START) {
  75.     String filename = upload.filename;
  76.     if (!filename.startsWith("/")) {
  77.       filename = "/" + filename;
  78.     }
  79.     Serial.print("handleFileUpload Name: "); Serial.println(filename);
  80.     fsUploadFile = SPIFFS.open(filename, "w");
  81.     filename = String();
  82.   } else if (upload.status == UPLOAD_FILE_WRITE) {
  83.     //Serial.print("handleFileUpload Data: "); Serial.println(upload.currentSize);
  84.     if (fsUploadFile) {
  85.       fsUploadFile.write(upload.buf, upload.currentSize);
  86.     }
  87.   } else if (upload.status == UPLOAD_FILE_END) {
  88.     if (fsUploadFile) {
  89.       fsUploadFile.close();
  90.     }
  91.     Serial.print("handleFileUpload Size: "); Serial.println(upload.totalSize);
  92.   }
  93. }
  94.  
  95. void handleIndexFile()
  96. {
  97.   File file = SPIFFS.open("/index.html","r");
  98.   server.streamFile(file, "text/html");
  99.   file.close();
  100. }
  101.  
  102. //Handle JavaScript files
  103. void handleJS1File()
  104. {
  105.   File js = SPIFFS.open("/smoothie.js","r");
  106.   server.streamFile(js, "text/javascript");
  107.   js.close();
  108. }
  109. void handleJS2File()
  110. {
  111.   File js = SPIFFS.open("/smoothie-functions.js","r");
  112.   server.streamFile(js, "text/javascript");
  113.   js.close();
  114. }
  115. void handleJS3File()
  116. {
  117.   File js = SPIFFS.open("/websocket.js","r");
  118.   server.streamFile(js, "text/javascript");
  119.   js.close();
  120. }
  121. void handleJS4File()
  122. {
  123.   File js = SPIFFS.open("/ui.js","r");
  124.   server.streamFile(js, "text/javascript");
  125.   js.close();
  126. }
  127.  
  128. void handleCSS1file()
  129. {
  130.   File css = SPIFFS.open("/pure-min.css","r");
  131.   server.streamFile(css, "text/css");
  132.   css.close();
  133. }
  134. void handleCSS2file()
  135. {
  136.   File css = SPIFFS.open("/side-menu.css","r");
  137.   server.streamFile(css, "text/css");
  138.   css.close();
  139. }
  140. void handleFileList() {
  141.   if (!server.hasArg("dir")) {
  142.     server.send(500, "text/plain", "BAD ARGS");
  143.     return;
  144.   }
  145.  
  146.   String path = server.arg("dir");
  147.   Serial.println("handleFileList: " + path);
  148.  
  149.  
  150.   File root = SPIFFS.open(path);
  151.   path = String();
  152.  
  153.   String output = "[";
  154.   if(root.isDirectory()){
  155.       File file = root.openNextFile();
  156.       while(file){
  157.           if (output != "[") {
  158.             output += ',';
  159.           }
  160.           output += "{\"type\":\"";
  161.           output += (file.isDirectory()) ? "dir" : "file";
  162.           output += "\",\"name\":\"";
  163.           output += String(file.name()).substring(1);
  164.           output += "\"}";
  165.           file = root.openNextFile();
  166.       }
  167.   }
  168.   output += "]";
  169.   server.send(200, "text/json", output);
  170. }
  171.  
  172. bool exists(String path){
  173.   bool yes = false;
  174.   File file = SPIFFS.open(path, "r");
  175.   if(!file.isDirectory()){
  176.     yes = true;
  177.   }
  178.   file.close();
  179.   return yes;
  180. }
  181.  
  182. void data_SM(void){
  183.  
  184.   switch(data_state_s){
  185.    
  186.     case WaitingStartByte:
  187.       checksum_calc = 0;
  188.      
  189.       if (Serial.available() > 0) {
  190.         incomingByte == Serial.read();
  191.         if(incomingByte = 170){
  192.           data_state_s = Framelength;
  193.         }
  194.       }
  195.       break;
  196.  
  197.     case Framelength:
  198.       if (Serial.available() > 0) {
  199.         length_data = Serial.read();
  200.         data_state_s = Payload;
  201.       }
  202.      
  203.       break;
  204.      
  205.     case Payload:
  206.      
  207.       if (Serial.available() > 0) {
  208.                
  209.          bufferpayload[pos] = Serial.read();  //reads payload to buffer
  210.          
  211.          checksum_calc += bufferpayload[pos]; //calculate checksum
  212.          
  213.         if(pos == (length_data-1)){
  214.           pos=0;
  215.           data_state_s = CheckSum;
  216.         }
  217.         else{
  218.           pos++;
  219.           data_state_s = Payload;
  220.         }
  221.       }
  222.      
  223.  
  224.       break;
  225.      
  226.     case CheckSum:
  227.         if (Serial.available() > 0) {
  228.           checksum_recv = Serial.read();
  229.          
  230.         }
  231.  
  232.        
  233.         if(checksum_recv == checksum_calc){   //payload correto
  234.           handlepayload();
  235.           data_state_s = WaitingStartByte;  
  236.         }
  237.        
  238.         else{
  239.           Serial.print("recebido:");
  240.           Serial.println(checksum_recv);
  241.           Serial.print("recebido:");
  242.           Serial.println(checksum_calc);
  243.           Serial.println("checksum não correto");
  244.           data_state_s = WaitingStartByte;  
  245.         }
  246.        
  247.       break;
  248.      
  249.     default:
  250.  
  251.       break;
  252.   }
  253. }
  254.  
  255. void handlepayload(void){
  256.  
  257. String output = "{";
  258. int i = 0;    
  259.                                                                                                                                                                                                                                                                        
  260.   while(i<length_data){                //Percorre o buffer todo
  261.     value = 0;
  262.     id = bufferpayload[i];
  263.     output += "\"";
  264.     output += id;
  265.     output += "\":";
  266.      
  267.     i++;
  268.     length_value = bufferpayload[i];
  269.     i++;
  270.     k=i;
  271.    
  272.     while(k<(i+length_value)){
  273.       value += bufferpayload[k];
  274.       k++;      
  275.     }
  276.    
  277.     output += "\"";
  278.     output += value;
  279.     output += "\"";
  280.     i=k;
  281.    
  282.       if(i != length_data){
  283.       output += ",";  
  284.       }
  285.   }
  286.     output += "}";
  287.    
  288.     webSocket.broadcastTXT(output);
  289.     Serial.println(output);
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement