Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <WebServer.h>
- #include <SPIFFS.h>
- #include <WebSocketsServer.h>
- #include <cbuf.h>
- unsigned char incomingByte,length_data,checksum_recv,checksum_calc = 0,id,length_value,value; // variável
- unsigned char bufferpayload[64];
- int pos=0,j=0,k=0;
- typedef enum {WaitingStartByte,Framelength,Payload,CheckSum,Value} state_machine_t;
- state_machine_t data_state_s;
- WebServer server;
- char* ssid = "AP";
- char* password = "";
- // hold uploaded file
- File fsUploadFile;
- // websockets server for handling messages sent by the client
- WebSocketsServer webSocket = WebSocketsServer(81);
- void setup()
- {
- // Format the file system in case it hasn't been done before
- SPIFFS.begin(true);
- // Start access point
- WiFi.softAP(ssid, password);
- Serial.begin(115200);
- // Print our IP address
- Serial.println();
- Serial.println("AP running");
- Serial.print("My IP address: ");
- Serial.println(WiFi.softAPIP());
- server.on("/",handleIndexFile);
- // handle JavaScript files
- server.on("/smoothie.js",handleJS1File);
- server.on("/smoothie-functions.js",handleJS2File);
- server.on("/websocket.js",handleJS3File);
- server.on("/ui.js",handleJS4File);
- // handle CSS files
- server.on("/pure-min.css",handleCSS1file);
- server.on("/side-menu.css",handleCSS2file);
- // list available files
- server.on("/list", HTTP_GET, handleFileList);
- // handle file upload
- server.on("/upload", HTTP_POST, [](){
- server.send(200, "text/plain", "{\"success\":1}");
- }, handleFileUpload);
- server.begin();
- webSocket.begin();
- }
- void loop()
- {
- webSocket.loop();
- server.handleClient();
- //data_SM();
- }
- // Update file handling functions for the ESP32
- void handleFileUpload() {
- HTTPUpload& upload = server.upload();
- if (upload.status == UPLOAD_FILE_START) {
- String filename = upload.filename;
- if (!filename.startsWith("/")) {
- filename = "/" + filename;
- }
- Serial.print("handleFileUpload Name: "); Serial.println(filename);
- fsUploadFile = SPIFFS.open(filename, "w");
- filename = String();
- } else if (upload.status == UPLOAD_FILE_WRITE) {
- //Serial.print("handleFileUpload Data: "); Serial.println(upload.currentSize);
- if (fsUploadFile) {
- fsUploadFile.write(upload.buf, upload.currentSize);
- }
- } else if (upload.status == UPLOAD_FILE_END) {
- if (fsUploadFile) {
- fsUploadFile.close();
- }
- Serial.print("handleFileUpload Size: "); Serial.println(upload.totalSize);
- }
- }
- void handleIndexFile()
- {
- File file = SPIFFS.open("/index.html","r");
- server.streamFile(file, "text/html");
- file.close();
- }
- //Handle JavaScript files
- void handleJS1File()
- {
- File js = SPIFFS.open("/smoothie.js","r");
- server.streamFile(js, "text/javascript");
- js.close();
- }
- void handleJS2File()
- {
- File js = SPIFFS.open("/smoothie-functions.js","r");
- server.streamFile(js, "text/javascript");
- js.close();
- }
- void handleJS3File()
- {
- File js = SPIFFS.open("/websocket.js","r");
- server.streamFile(js, "text/javascript");
- js.close();
- }
- void handleJS4File()
- {
- File js = SPIFFS.open("/ui.js","r");
- server.streamFile(js, "text/javascript");
- js.close();
- }
- void handleCSS1file()
- {
- File css = SPIFFS.open("/pure-min.css","r");
- server.streamFile(css, "text/css");
- css.close();
- }
- void handleCSS2file()
- {
- File css = SPIFFS.open("/side-menu.css","r");
- server.streamFile(css, "text/css");
- css.close();
- }
- void handleFileList() {
- if (!server.hasArg("dir")) {
- server.send(500, "text/plain", "BAD ARGS");
- return;
- }
- String path = server.arg("dir");
- Serial.println("handleFileList: " + path);
- File root = SPIFFS.open(path);
- path = String();
- String output = "[";
- if(root.isDirectory()){
- File file = root.openNextFile();
- while(file){
- if (output != "[") {
- output += ',';
- }
- output += "{\"type\":\"";
- output += (file.isDirectory()) ? "dir" : "file";
- output += "\",\"name\":\"";
- output += String(file.name()).substring(1);
- output += "\"}";
- file = root.openNextFile();
- }
- }
- output += "]";
- server.send(200, "text/json", output);
- }
- bool exists(String path){
- bool yes = false;
- File file = SPIFFS.open(path, "r");
- if(!file.isDirectory()){
- yes = true;
- }
- file.close();
- return yes;
- }
- void data_SM(void){
- switch(data_state_s){
- case WaitingStartByte:
- checksum_calc = 0;
- if (Serial.available() > 0) {
- incomingByte == Serial.read();
- if(incomingByte = 170){
- data_state_s = Framelength;
- }
- }
- break;
- case Framelength:
- if (Serial.available() > 0) {
- length_data = Serial.read();
- data_state_s = Payload;
- }
- break;
- case Payload:
- if (Serial.available() > 0) {
- bufferpayload[pos] = Serial.read(); //reads payload to buffer
- checksum_calc += bufferpayload[pos]; //calculate checksum
- if(pos == (length_data-1)){
- pos=0;
- data_state_s = CheckSum;
- }
- else{
- pos++;
- data_state_s = Payload;
- }
- }
- break;
- case CheckSum:
- if (Serial.available() > 0) {
- checksum_recv = Serial.read();
- }
- if(checksum_recv == checksum_calc){ //payload correto
- handlepayload();
- data_state_s = WaitingStartByte;
- }
- else{
- Serial.print("recebido:");
- Serial.println(checksum_recv);
- Serial.print("recebido:");
- Serial.println(checksum_calc);
- Serial.println("checksum não correto");
- data_state_s = WaitingStartByte;
- }
- break;
- default:
- break;
- }
- }
- void handlepayload(void){
- String output = "{";
- int i = 0;
- while(i<length_data){ //Percorre o buffer todo
- value = 0;
- id = bufferpayload[i];
- output += "\"";
- output += id;
- output += "\":";
- i++;
- length_value = bufferpayload[i];
- i++;
- k=i;
- while(k<(i+length_value)){
- value += bufferpayload[k];
- k++;
- }
- output += "\"";
- output += value;
- output += "\"";
- i=k;
- if(i != length_data){
- output += ",";
- }
- }
- output += "}";
- webSocket.broadcastTXT(output);
- Serial.println(output);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement