diff --git a/esp3d/bridge.cpp b/esp3d/bridge.cpp index 8c917b42..dfa9e51d 100644 --- a/esp3d/bridge.cpp +++ b/esp3d/bridge.cpp @@ -27,6 +27,29 @@ WiFiServer * data_server; WiFiClient serverClients[MAX_SRV_CLIENTS]; #endif +#ifdef TCP_IP_DATA_FEATURE +void BRIDGE::send2TCP(const __FlashStringHelper *data){ + String tmp = data; + BRIDGE::send2TCP(tmp.c_str()); +} +void BRIDGE::send2TCP(String data){ + BRIDGE::send2TCP(data.c_str()); +} +void BRIDGE::send2TCP(const char * data) +{ + for(uint8_t i = 0; i < MAX_SRV_CLIENTS; i++) { + if (serverClients[i] && serverClients[i].connected()) { + serverClients[i].write(data, strlen(data)); + delay(0); + } + } +} +#endif + +void debug_esp(String st){ + BRIDGE::send2TCP(st); +} + bool BRIDGE::processFromSerial2TCP() { uint8_t i; @@ -40,7 +63,7 @@ bool BRIDGE::processFromSerial2TCP() for(i = 0; i < MAX_SRV_CLIENTS; i++) { if (serverClients[i] && serverClients[i].connected()) { serverClients[i].write(sbuf, len); - delay(1); + delay(0); } } #endif diff --git a/esp3d/bridge.h b/esp3d/bridge.h index 41c500e0..d3de9a13 100644 --- a/esp3d/bridge.h +++ b/esp3d/bridge.h @@ -18,11 +18,10 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "config.h" - #ifndef BRIDGE_H #define BRIDGE_H - +#include +#include "config.h" #ifdef TCP_IP_DATA_FEATURE extern WiFiServer * data_server; #endif @@ -33,7 +32,9 @@ public: static bool processFromSerial2TCP(); #ifdef TCP_IP_DATA_FEATURE static void processFromTCP2Serial(); + static void send2TCP(const __FlashStringHelper *data); + static void send2TCP(String data); + static void send2TCP(const char * data); #endif }; - #endif diff --git a/esp3d/command.cpp b/esp3d/command.cpp index a0b6a6b5..bbc76d72 100644 --- a/esp3d/command.cpp +++ b/esp3d/command.cpp @@ -80,12 +80,12 @@ bool COMMAND::isadmin(String & cmd_params) String adminpassword; String sadminPassword; if (!CONFIG::read_string(EP_ADMIN_PWD, sadminPassword , MAX_LOCAL_PASSWORD_LENGTH)) { - LOG("ERROR getting admin\n") + LOG("ERROR getting admin\r\n") sadminPassword=FPSTR(DEFAULT_ADMIN_PWD); } adminpassword = get_param(cmd_params,"pwd=", true); if (!sadminPassword.equals(adminpassword)) { - LOG("Not allowed \n") + LOG("Not allowed\r\n") return false; } else { return true; @@ -97,6 +97,7 @@ void COMMAND::execute_command(int cmd,String cmd_params) //manage parameters byte mode = 254; String parameter; + LOG("Execute Command\r\n") switch(cmd) { //STA SSID //[ESP100][pwd=] @@ -273,6 +274,9 @@ void COMMAND::execute_command(int cmd,String cmd_params) Serial.print(cmd_params); Serial.println(currentIP); Serial.print("\r\n"); + LOG(cmd_params) + LOG(currentIP) + LOG("\r\n") } break; //Get hostname @@ -286,6 +290,9 @@ void COMMAND::execute_command(int cmd,String cmd_params) Serial.print(cmd_params); Serial.println(shost); Serial.print("\r\n"); + LOG(cmd_params) + LOG(shost) + LOG("\r\n") } break; @@ -297,7 +304,7 @@ void COMMAND::execute_command(int cmd,String cmd_params) parameter = get_param(cmd_params,"P", false); LOG("Pin:") LOG(parameter) - LOG("\n") + LOG("\r\n") if (parameter == "") { Serial.println(INCORRECT_CMD_MSG); } else { @@ -308,21 +315,26 @@ void COMMAND::execute_command(int cmd,String cmd_params) parameter = get_param(cmd_params,"V", false); //it is a get if (parameter == "") { - parameter = get_param(cmd_params,"PULLUP=", false); - if (parameter == "YES"){ - //GPIO16 is different than others - if (pin <16) { - LOG("Set as input pull up\n") - pinMode(pin, INPUT_PULLUP); - } else { - LOG("Set as input pull down 16\n") - pinMode(pin, INPUT_PULLDOWN_16); + //this is to not set pin mode + parameter = get_param(cmd_params,"RAW=", false); + if (parameter !="YES") + { + parameter = get_param(cmd_params,"PULLUP=", false); + if (parameter == "YES"){ + //GPIO16 is different than others + if (pin <16) { + LOG("Set as input pull up\r\n") + pinMode(pin, INPUT_PULLUP); + } else { + LOG("Set as input pull down 16\r\n") + pinMode(pin, INPUT_PULLDOWN_16); + } + }else { + LOG("Set as input\r\n") + pinMode(pin, INPUT); + } + delay(100); } - }else { - LOG("Set as input\n") - pinMode(pin, INPUT); - } - delay(100); int value = digitalRead(pin); LOG("Read:"); Serial.println(String(value)); @@ -335,7 +347,7 @@ void COMMAND::execute_command(int cmd,String cmd_params) delay(10); LOG("Set:") LOG(String((value == 0)?LOW:HIGH)) - LOG("\n") + LOG("\r\n") digitalWrite(pin, (value == 0)?LOW:HIGH); } else { Serial.println(INCORRECT_CMD_MSG); @@ -477,9 +489,12 @@ void COMMAND::execute_command(int cmd,String cmd_params) } } -void COMMAND::check_command(String buffer) +void COMMAND::check_command(String buffer, bool handlelockserial) { String buffer2; + LOG("Check Command:") + LOG(buffer) + LOG("\r\n") //if direct access to SDCard no need to handle the M20 command answer #ifndef DIRECT_SDCARD_FEATURE static bool bfileslist=false; @@ -487,9 +502,11 @@ void COMMAND::check_command(String buffer) //if SD list is not on going if (!bfileslist) { //check if command is a start of SD File list - int filesstart = buffer.indexOf("Begin file list"); + LOG("No File list ongoing\r\n") + int filesstart = buffer.indexOf("egin file list"); //yes it is file list starting to be displayed if (filesstart>-1) { + LOG("Found start File list\r\n") //init time out start_list = millis(); //set file list started @@ -497,7 +514,7 @@ void COMMAND::check_command(String buffer) //clear current list web_interface->fileslist.clear(); //block any new output to serial from ESP to avoid pollution - (web_interface->blockserial) = true; + if (handlelockserial)(web_interface->blockserial) = true; return; } #endif @@ -634,17 +651,19 @@ void COMMAND::check_command(String buffer) #endif #ifndef DIRECT_SDCARD_FEATURE } else { //listing file is on going + LOG("File list is ongoing\r\n") //check if we are too long if ((millis()-start_list)>30000) { //timeout in case of problem bfileslist=false; - (web_interface->blockserial) = false; //release serial - LOG("Time out\n"); + if(handlelockserial)(web_interface->blockserial) = false; //release serial + LOG("Time out\r\n"); } else { //check if this is the end - if (buffer.indexOf("End file list")>-1) { + if (buffer.indexOf("nd file list")>-1) { + LOG("End File list detected\r\n") bfileslist=false; - (web_interface->blockserial) = false; - LOG("End list\n"); + if(handlelockserial)(web_interface->blockserial) = false; + LOG("End list\r\n"); } else { //Serial.print(buffer); //add list to buffer @@ -652,7 +671,7 @@ void COMMAND::check_command(String buffer) LOG(String(web_interface->fileslist.size())); LOG(":"); LOG(buffer); - LOG('\n'); + LOG("\r\n"); } } } diff --git a/esp3d/command.h b/esp3d/command.h index 6d27ca20..c6f74f45 100644 --- a/esp3d/command.h +++ b/esp3d/command.h @@ -30,7 +30,7 @@ public: static void read_buffer_serial(uint8_t *b, size_t len); static void read_buffer_serial(uint8_t b); static void read_buffer_tcp(uint8_t b); - static void check_command(String buffer); + static void check_command(String buffer,bool handlelockserial = true); static void execute_command(int cmd,String cmd_params); static String get_param(String & cmd_params, const char * id, bool withspace = false); static bool isadmin(String & cmd_params); diff --git a/esp3d/config.cpp b/esp3d/config.cpp index d50705be..3129a38a 100644 --- a/esp3d/config.cpp +++ b/esp3d/config.cpp @@ -27,7 +27,7 @@ extern "C" { void CONFIG::esp_restart() { - LOG("Restarting\n") + LOG("Restarting\r\n") Serial.flush(); delay(500); Serial.swap(); @@ -233,7 +233,7 @@ bool CONFIG::read_string(int pos, char byte_buffer[], int size_max) { //check if parameters are acceptable if (size_max==0 || pos+size_max+1 > EEPROM_SIZE || byte_buffer== NULL) { - LOG("Error read string\n") + LOG("Error read string\r\n") return false; } EEPROM.begin(EEPROM_SIZE); @@ -260,7 +260,7 @@ bool CONFIG::read_string(int pos, String & sbuffer, int size_max) { //check if parameters are acceptable if (size_max==0 || pos+size_max+1 > EEPROM_SIZE ) { - LOG("Error read string\n") + LOG("Error read string\r\n") return false; } byte b = 13; // non zero for the while loop below @@ -286,7 +286,7 @@ bool CONFIG::read_buffer(int pos, byte byte_buffer[], int size_buffer) { //check if parameters are acceptable if (size_buffer==0 || pos+size_buffer > EEPROM_SIZE || byte_buffer== NULL) { - LOG("Error read buffer\n") + LOG("Error read buffer\r\n") return false; } int i=0; @@ -305,7 +305,7 @@ bool CONFIG::read_byte(int pos, byte * value) { //check if parameters are acceptable if (pos+1 > EEPROM_SIZE) { - LOG("Error read byte\n") + LOG("Error read byte\r\n") return false; } EEPROM.begin(EEPROM_SIZE); @@ -348,7 +348,7 @@ bool CONFIG::write_string(int pos, const char * byte_buffer) break; } if (size_buffer==0 || pos+size_buffer+1 > EEPROM_SIZE || size_buffer > maxsize || byte_buffer== NULL) { - LOG("Error write string\n") + LOG("Error write string\r\n") return false; } //copy the value(s) @@ -369,7 +369,7 @@ bool CONFIG::write_buffer(int pos, const byte * byte_buffer, int size_buffer) { //check if parameters are acceptable if (size_buffer==0 || pos+size_buffer > EEPROM_SIZE || byte_buffer== NULL) { - LOG("Error write buffer\n") + LOG("Error write buffer\r\n") return false; } EEPROM.begin(EEPROM_SIZE); @@ -387,7 +387,7 @@ bool CONFIG::write_byte(int pos, const byte value) { //check if parameters are acceptable if (pos+1 > EEPROM_SIZE) { - LOG("Error write byte\n") + LOG("Error write byte\r\n") return false; } EEPROM.begin(EEPROM_SIZE); @@ -806,5 +806,8 @@ void CONFIG::print_config() #ifdef DEBUG_OUTPUT_SERIAL Serial.println(F("serial")); #endif +#ifdef DEBUG_OUTPUT_TCP + Serial.println(F("TCP")); +#endif #endif } diff --git a/esp3d/config.h b/esp3d/config.h index e1a26e9c..07e1236c 100644 --- a/esp3d/config.h +++ b/esp3d/config.h @@ -26,7 +26,7 @@ #define SMOOTHIEWARE 4 //FIRMWARE_TARGET: the targeted FW, can be REPETIER (Original Repetier)/ REPETIER4DV (Repetier for Davinci) / MARLIN (Marlin)/ SMOOTHIEWARE (Smoothieware) -#define FIRMWARE_TARGET SMOOTHIEWARE +#define FIRMWARE_TARGET REPETIER //number of clients allowed to use data port at once #define MAX_SRV_CLIENTS 1 @@ -58,7 +58,7 @@ #define TCP_IP_DATA_FEATURE //RECOVERY_FEATURE: allow to use GPIO2 pin as hardware reset for EEPROM, add 8s to boot time to let user to jump GPIO2 to GND -#define RECOVERY_FEATURE +//#define RECOVERY_FEATURE #ifdef RECOVERY_FEATURE //pin used to reset setting @@ -92,22 +92,28 @@ //#define DEBUG_OUTPUT_SPIFFS //#define DEBUG_OUTPUT_SD //#define DEBUG_OUTPUT_SERIAL +//#define DEBUG_OUTPUT_TCP -#include +//store performance result in storestring variable : info_msg / status_msg +//#define DEBUG_PERFORMANCE +#define DEBUG_PERF_VARIABLE (web_interface->info_msg) #ifdef DEBUG_ESP3D #ifdef DEBUG_OUTPUT_SPIFFS -#define LOG(string) {FSFILE logfile = SPIFFS.open("/log.txt", "a+");logfile.print(string);logfile.close();} -#else -#ifdef SDCARD_FEATURE -#ifdef DEBUG_OUTPUT_SD -#define LOG(string) {if(CONFIG::hasSD()){LOCKSD() File logfile = SD.open("/log.txt", "a+");logfile.print(string);logfile.close();RELEASESD()}} -#else -#define LOG(string) {Serial.print(string);} + /*#ifdef SDCARD_FEATURE + #ifndef FS_NO_GLOBALS + #define FS_NO_GLOBALS + #endif + #endif + #include */ + #define LOG(string) {FSFILE logfile = SPIFFS.open("/log.txt", "a+");logfile.print(string);logfile.close();} #endif -#else -#define LOG(string) {Serial.print(string);} +#ifdef DEBUG_OUTPUT_SERIAL + #define LOG(string) {Serial.print(string);} #endif +#ifdef DEBUG_OUTPUT_TCP + #include "bridge.h" + #define LOG(string) {BRIDGE::send2TCP(string);} #endif #else #define LOG(string) {} @@ -137,7 +143,7 @@ extern "C" { } #include "wifi.h" //version and sources location -#define FW_VERSION "0.8.50" +#define FW_VERSION "0.9.70" #define REPOSITORY "https://github.com/luc-github/ESP3D" diff --git a/esp3d/data/printer.tpl b/esp3d/data/printer.tpl index 701a461e..a111079c 100644 --- a/esp3d/data/printer.tpl +++ b/esp3d/data/printer.tpl @@ -83,6 +83,7 @@ $INCLUDE[css2.inc]$
+ @@ -383,9 +384,19 @@ function expand_collapse(flag, targetpin,targetdiv){ var XYfeedrate=$XY_FEEDRATE$; var Zfeedrate=$Z_FEEDRATE$; var Efeedrate=$E_FEEDRATE$; -function Sendcommand(commandtxt){ + +function Sendcommand(commandtxt, showresult=false){ var xmlhttp = new XMLHttpRequest(); -var url = "http://$WEB_ADDRESS$/CMD?COM="+encodeURIComponent(commandtxt);; +var url = "/command?plain="+encodeURIComponent(commandtxt);; +if (showresult){ + xmlhttp.onreadystatechange = function() { + if (xmlhttp.readyState == 4 && xmlhttp.status === 200) { + var textarea = document.getElementById("logwindow"); + textarea.innerHTML = textarea.innerHTML + xmlhttp.responseText; + textarea.scrollTop = textarea.scrollHeight; + } + } +} xmlhttp.open("GET", url, true); xmlhttp.send(); } @@ -407,7 +418,7 @@ Sendcommand(cmd + document.getElementById("numberinput"+item).value); } function Sendcustomcommand(){ var cmd = document.getElementById("cmd").value; -if (cmd.trim().length > 0) Sendcommand(cmd); +if (cmd.trim().length > 0) Sendcommand(cmd,true); document.getElementById("cmd").value=""; } function OnclickEmergency(){ @@ -847,11 +858,5 @@ Sendcommand("M23 " + currentpath + filename); delay(100); Sendcommand("M24");} } - - - - - - $INCLUDE[footer.inc]$ diff --git a/esp3d/esp3d.ino b/esp3d/esp3d.ino index 6e881e80..3233ea8c 100644 --- a/esp3d/esp3d.ino +++ b/esp3d/esp3d.ino @@ -63,7 +63,7 @@ void setup() #ifdef DEBUG_ESP3D Serial.begin(DEFAULT_BAUD_RATE); delay(2000); - LOG("\nDebug Serial set\n") + LOG("\r\nDebug Serial set\r\n") #endif //WiFi.disconnect(); WiFi.mode(WIFI_OFF); @@ -79,17 +79,17 @@ void setup() if ( CONFIG::read_buffer(EP_BAUD_RATE, (byte *)&baud_rate , INTEGER_LENGTH)&&CONFIG::read_buffer(EP_WEB_PORT, (byte *)&(wifi_config.iweb_port) , INTEGER_LENGTH)&&CONFIG::read_buffer(EP_DATA_PORT, (byte *)&(wifi_config.idata_port) , INTEGER_LENGTH)) { //check if baud value is one of allowed ones if ( ! (baud_rate==9600 || baud_rate==19200 ||baud_rate==38400 ||baud_rate==57600 ||baud_rate==115200 ||baud_rate==230400 ||baud_rate==250000) ) { - LOG("Error for EEPROM baud rate\n") + LOG("Error for EEPROM baud rate\r\n") breset_config=true; //baud rate is incorrect =>reset settings } if (wifi_config.iweb_port<1 ||wifi_config.iweb_port>65001 || wifi_config.idata_port <1 || wifi_config.idata_port >65001) { breset_config=true; //out of range =>reset settings - LOG("Error for EEPROM port values\n") + LOG("Error for EEPROM port values\r\n") } } else { breset_config=true; //cannot access to config settings=> reset settings - LOG("Error no EEPROM access\n") + LOG("Error no EEPROM access\r\n") } //reset is requested @@ -110,14 +110,14 @@ void setup() CONFIG::esp_restart(); } #if defined(DEBUG_ESP3D) && defined(DEBUG_OUTPUT_SERIAL) - LOG("\n"); + LOG("\r\n"); delay(500); Serial.flush(); #endif //setup serial Serial.begin(baud_rate); delay(1000); - LOG("Serial Set\n"); + LOG("Serial Set\r\n"); wifi_config.baud_rate=baud_rate; //Update is done if any so should be Ok SPIFFS.begin(); @@ -183,7 +183,7 @@ void setup() #ifdef NETBIOS_FEATURE NBNS.begin(shost.c_str()); #endif - LOG("Setup Done\n"); + LOG("Setup Done\r\n"); } diff --git a/esp3d/webinterface.cpp b/esp3d/webinterface.cpp index 20ad5546..ef1d994f 100644 --- a/esp3d/webinterface.cpp +++ b/esp3d/webinterface.cpp @@ -213,8 +213,10 @@ const char KEY_IS_DEFAULT_MODE [] PROGMEM = "$IS_DEFAULT_MODE$"; bool WEBINTERFACE_CLASS::processTemplate(const char * filename, STORESTRINGS_CLASS & KeysList , STORESTRINGS_CLASS & ValuesList ) { + LOG("process template\r\n") if(KeysList.size() != ValuesList.size()) { //Sanity check - Serial.print("Error"); + Serial.print("M117 Error"); + LOG("Error\r\n") return false; } @@ -346,9 +348,9 @@ bool WEBINTERFACE_CLASS::processTemplate(const char * filename, STORESTRINGS_CL //send header with calculated size header_sent=true; web_interface->WebServer.setContentLength(CONTENT_LENGTH_UNKNOWN); - web_interface->WebServer.send(200); web_interface->WebServer.sendHeader("Content-Type","text/html"); web_interface->WebServer.sendHeader("Cache-Control","no-cache"); + web_interface->WebServer.send(200); } //send data web_interface->WebServer.sendContent(buffer2send); @@ -359,7 +361,7 @@ bool WEBINTERFACE_CLASS::processTemplate(const char * filename, STORESTRINGS_CL //reset line sLine=""; //add a delay for safety for WDT - delay(1); + delay(0); } } else { //EOF is reached //close current file @@ -390,6 +392,7 @@ bool WEBINTERFACE_CLASS::processTemplate(const char * filename, STORESTRINGS_CL } //close line web_interface->WebServer.sendContent(""); + LOG("Process template done\r\n") return true; } @@ -605,7 +608,7 @@ void handle_web_interface_home() struct softap_config apconfig; struct ip_info info; uint8_t mac [WL_MAC_ADDR_LENGTH]; - + LOG("request /HOME\r\n") //login web_interface->GeLogin(KeysList, ValuesList,web_interface->is_authenticated()); @@ -917,6 +920,7 @@ void handle_web_interface_home() //need to clean to speed up memory recovery KeysList.clear(); ValuesList.clear(); + LOG("request /HOME done\r\n") } void handle_web_interface_configSys() @@ -1312,10 +1316,10 @@ void handle_web_interface_configAP() //Default mode ? if (web_interface->WebServer.hasArg("DEFAULT_MODE") ) { default_mode=AP_MODE; - LOG("Set AP Mode\n") + LOG("Set AP Mode\r\n") } else { default_mode=CLIENT_MODE; - LOG("Set Station mode\n") + LOG("Set Station mode\r\n") } //phy mode phy_mode_buf = byte(web_interface->WebServer.arg("NETWORK").toInt()); @@ -1345,10 +1349,10 @@ void handle_web_interface_configAP() //Static IP ? if (web_interface->WebServer.hasArg("STATIC_IP") ) { static_ip_buf=STATIC_IP_MODE; - LOG("Set Static\n") + LOG("Set Static\r\n") } else { static_ip_buf=DHCP_MODE; - LOG("Set DHCP\n") + LOG("Set DHCP\r\n") } //IP @@ -1686,10 +1690,10 @@ void handle_web_interface_configSTA() //Default mode ? if (web_interface->WebServer.hasArg("DEFAULT_MODE") ) { default_mode=CLIENT_MODE; - LOG("Set STA mode\n") + LOG("Set STA mode\r\n") } else { default_mode=AP_MODE; - LOG("Set AP mode\n") + LOG("Set AP mode\r\n") } //Static IP ? if (web_interface->WebServer.hasArg("STATIC_IP") ) { @@ -2384,7 +2388,7 @@ void SPIFFSFileupload() #define NB_RETRY 5 #define MAX_RESEND_BUFFER 128 - +//SD file upload by serial void SDFileupload() { static char buffer_line[MAX_RESEND_BUFFER]; //if need to resend @@ -2397,9 +2401,14 @@ void SDFileupload() if(web_interface->is_authenticated() == LEVEL_GUEST) { web_interface->_upload_status=UPLOAD_STATUS_CANCELLED; Serial.println("M117 SD upload failed"); - LOG("SD upload failed\n"); + LOG("SD upload failed\r\n"); return; } +#ifdef DEBUG_PERFORMANCE +static uint32_t startupload; +static uint32_t write_time; +static size_t filesize; +#endif //retrieve current file id HTTPUpload& upload = (web_interface->WebServer).upload(); //Upload start @@ -2414,8 +2423,13 @@ void SDFileupload() previous = 0; web_interface->_upload_status= UPLOAD_STATUS_ONGOING; Serial.println("M117 Uploading..."); +#ifdef DEBUG_PERFORMANCE + startupload = millis(); + write_time = 0; + filesize = 0; +#endif LOG(String(upload.filename)); - LOG("\n"); + LOG("\r\n"); //command to pritnter to start print String filename = "M28 " + upload.filename; Serial.println(filename); @@ -2447,6 +2461,10 @@ void SDFileupload() //upload is on going with data coming by 2K blocks } else if((upload.status == UPLOAD_FILE_WRITE) && (com_error == false)) { //if com error no need to send more data to serial web_interface->_upload_status= UPLOAD_STATUS_ONGOING; +#ifdef DEBUG_PERFORMANCE + filesize+=upload.currentSize; + uint32_t startwrite = millis(); +#endif for (int pos = 0; pos < upload.currentSize; pos++) { //parse full post data if (buffer_size < MAX_RESEND_BUFFER-1) { //raise error/handle if overbuffer - copy is space available //remove/ignore every comment to save transfert time and avoid over buffer issues @@ -2494,7 +2512,7 @@ void SDFileupload() response = (const char*)sbuf; LOG("Retry:"); LOG(String(retry)); - LOG("\n"); + LOG("\r\n"); LOG(response); //if buffer contain ok or wait - it means command is pass if ((response.indexOf("wait")>-1)||(response.indexOf("ok")>-1)) { @@ -2527,7 +2545,7 @@ void SDFileupload() //if even after the number of retry still have error - then we are in error if (!success) { //raise error - LOG("Error detected\n"); + LOG("Error detected\r\n"); LOG(response); com_error = true; } @@ -2554,11 +2572,14 @@ void SDFileupload() } } else { //raise error - LOG("\nlong line detected\n"); + LOG("\r\nlong line detected\r\n"); LOG(buffer_line); com_error = true; } } +#ifdef DEBUG_PERFORMANCE + write_time += (millis()-startwrite); +#endif //Upload end //************** } else if(upload.status == UPLOAD_FILE_END) { @@ -2603,7 +2624,7 @@ void SDFileupload() } if (!success) { //raise error - LOG("Error detected 2\n"); + LOG("Error detected 2\r\n"); LOG(response); com_error = true; } @@ -2623,13 +2644,19 @@ void SDFileupload() //resend M29 command to close file on SD as first command may be lost Serial.print("\r\nM29\r\n"); Serial.flush(); +#ifdef DEBUG_PERFORMANCE + uint32_t endupload = millis(); + DEBUG_PERF_VARIABLE.add(String(endupload-startupload).c_str()); + DEBUG_PERF_VARIABLE.add(String(write_time).c_str()); + DEBUG_PERF_VARIABLE.add(String(filesize).c_str()); +#endif if (com_error) { - LOG("with error\n"); + LOG("with error\r\n"); web_interface->_upload_status=UPLOAD_STATUS_CANCELLED; Serial.println("M117 SD upload failed"); Serial.flush(); } else { - LOG("with success\n"); + LOG("with success\r\n"); web_interface->_upload_status=UPLOAD_STATUS_SUCCESSFUL; Serial.println("M117 SD upload done"); Serial.flush(); @@ -2637,7 +2664,7 @@ void SDFileupload() //Upload cancelled //************** } else { //UPLOAD_FILE_ABORTED - LOG("Error, Something happened\n"); + LOG("Error, Something happened\r\n"); com_error = true; web_interface->_upload_status=UPLOAD_STATUS_CANCELLED; buffer_size=0; @@ -2664,7 +2691,7 @@ void WebUpdateUpload() if(web_interface->is_authenticated() != LEVEL_ADMIN) { web_interface->_upload_status=UPLOAD_STATUS_CANCELLED; Serial.println("M117 Update failed"); - LOG("SD Update failed\n"); + LOG("SD Update failed\r\n"); return; } //get current file ID @@ -2907,7 +2934,7 @@ void handleSDFileList() if (web_interface->is_authenticated() == LEVEL_GUEST) { return; } - LOG("List SD FILES\n") + LOG("List SD FILES\r\n") String path="/"; String sstatus="Ok"; uint32_t totalspace = 0; @@ -2924,51 +2951,51 @@ void handleSDFileList() } //check if query need some action if(web_interface->WebServer.hasArg("action")) { - LOG("action requested\n") + LOG("action requested\r\n") //delete a file if(web_interface->WebServer.arg("action") == "delete" && web_interface->WebServer.hasArg("filename")) { - LOG("delete requested\n") + LOG("delete requested\r\n") String filename; String shortname = web_interface->WebServer.arg("filename"); shortname.replace("/",""); filename = path + web_interface->WebServer.arg("filename"); filename.replace("//","/"); LOG(shortname) - LOG("\n") + LOG("\r\n") LOG(filename) - LOG("\n") + LOG("\r\n") LOG(sstatus) - LOG("\n") + LOG("\r\n") } //delete a directory if(web_interface->WebServer.arg("action") == "deletedir" && web_interface->WebServer.hasArg("filename")) { - LOG("deletedir requested\n") + LOG("deletedir requested\r\n") String filename; String shortname = web_interface->WebServer.arg("filename"); shortname.replace("/",""); filename = path + web_interface->WebServer.arg("filename"); filename.replace("//","/"); LOG(shortname) - LOG("\n") + LOG("\r\n") LOG(filename) - LOG("\n") + LOG("\r\n") LOG(sstatus) - LOG("\n") + LOG("\r\n") } //create a directory if(web_interface->WebServer.arg("action")=="createdir" && web_interface->WebServer.hasArg("filename")) { String filename; - LOG("createdir requested\n") + LOG("createdir requested\r\n") filename = path + web_interface->WebServer.arg("filename") ; String shortname = web_interface->WebServer.arg("filename"); shortname.replace("/",""); filename.replace("//","/"); LOG(shortname) - LOG("\n") + LOG("\r\n") LOG(filename) - LOG("\n") + LOG("\r\n") LOG(sstatus) - LOG("\n") + LOG("\r\n") } } @@ -2976,16 +3003,16 @@ void handleSDFileList() #ifndef DIRECT_SDCARD_FEATURE //if action is processing do not build list, but no need Serial for Direct SDCard Support if ((web_interface->blockserial)) { - LOG("Wait, blocking\n"); + LOG("Wait, blocking\r\n"); jsonfile+="\"status\":\"processing\",\"mode\":\"serial\"}"; } else #endif { jsonfile+="\"files\":["; - LOG("No Blocking \n"); - LOG("JSON File\n"); + LOG("No Blocking\r\n"); + LOG("JSON File\r\n"); LOG(String(web_interface->fileslist.size())); - LOG(" entries\n"); + LOG(" entries\r\n"); String sname; for (int i=0; ifileslist.size(); i++) { if (i>0) { @@ -3031,7 +3058,7 @@ void handleSDFileList() //nothing to add jsonfile+=""; } - LOG("\n"); + LOG("\r\n"); #endif jsonfile+="\"}"; } @@ -3046,7 +3073,7 @@ void handleSDFileList() jsonfile+= "\""; jsonfile+= "}"; - LOG("JSON done\n"); + LOG("JSON done\r\n"); } path = String(); web_interface->WebServer.sendHeader("Cache-Control", "no-cache"); @@ -3070,24 +3097,34 @@ void handle_not_found() String pathWithGz = path + ".gz"; LOG("request:") LOG(path) - LOG("\n") + LOG("\r\n") +#ifdef DEBUG_ESP3D + int nb = web_interface->WebServer.args(); + for (int i = 0 ; i < nb;i++){ + LOG(web_interface->WebServer.argName(i)) + LOG(":") + LOG(web_interface->WebServer.arg(i)) + LOG("\r\n") + + } +#endif LOG("type:") LOG(contentType) - LOG("\n") - if(SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)) { - if(SPIFFS.exists(pathWithGz)) { - path = pathWithGz; + LOG("\r\n") + if(SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)) { + if(SPIFFS.exists(pathWithGz)) { + path = pathWithGz; + } + FSFILE file = SPIFFS.open(path, "r"); + web_interface->WebServer.streamFile(file, contentType); + file.close(); + return; + } else { + page_not_found = true; } - FSFILE file = SPIFFS.open(path, "r"); - web_interface->WebServer.streamFile(file, contentType); - file.close(); - return; - } else { - page_not_found = true; - } if (page_not_found ) { - LOG("Page not found it \n") + LOG("Page not found it \r\n") if (SPIFFS.exists("/404.tpl")) { STORESTRINGS_CLASS KeysList ; STORESTRINGS_CLASS ValuesList ; @@ -3297,31 +3334,50 @@ void handle_restart() web_interface->restartmodule=true; } -void handle_web_command() -{ +#define MAX_TRY 2000 +void handle_web_command(){ if (web_interface->is_authenticated() == LEVEL_GUEST) { + web_interface->WebServer.send(200,"text/plain","Not allowed, log in first!"); return; - } - //check we have proper parameter - if (web_interface->WebServer.hasArg("COM")) { - String scmd; - //decode command - scmd = web_interface->WebServer.arg("COM"); - scmd.trim(); - //give an ack - we need to be polite, right ? - web_interface->WebServer.send(200,"text/plain","Ok"); + } + String buffer2send = ""; + LOG(String (web_interface->WebServer.args())) + LOG(" Web command\r\n") +#ifdef DEBUG_ESP3D + int nb = web_interface->WebServer.args(); + for (int i = 0 ; i < nb;i++){ + LOG(web_interface->WebServer.argName(i)) + LOG(":") + LOG(web_interface->WebServer.arg(i)) + LOG("\r\n") + } +#endif + String cmd; + int count ; + if (web_interface->WebServer.hasArg("plain") || web_interface->WebServer.hasArg("commandText")){ + if (web_interface->WebServer.hasArg("plain")) cmd = web_interface->WebServer.arg("plain"); + else cmd = web_interface->WebServer.arg("commandText"); + LOG("Web Command:") + LOG(cmd) + LOG("\r\n") + } else { + LOG("invalid argument\r\n") + web_interface->WebServer.send(200,"text/plain","Invalid command"); + return; + } //if it is for ESP module [ESPXXX] - int ESPpos = scmd.indexOf("[ESP"); + cmd.trim(); + int ESPpos = cmd.indexOf("[ESP"); if (ESPpos>-1) { //is there the second part? - int ESPpos2 = scmd.indexOf("]",ESPpos); + int ESPpos2 = cmd.indexOf("]",ESPpos); if (ESPpos2>-1) { //Split in command and parameters - String cmd_part1=scmd.substring(ESPpos+4,ESPpos2); + String cmd_part1=cmd.substring(ESPpos+4,ESPpos2); String cmd_part2=""; //is there space for parameters? - if (ESPpos2blockserial) == false) { - Serial.println(scmd); - } + //block every query + web_interface->blockserial = true; + LOG("Block Serial\r\n") + //empty the serial buffer and incoming data + LOG("Start PurgeSerial\r\n") + while(Serial.available()){ + BRIDGE::processFromSerial2TCP(); + delay(1); + } + LOG("End PurgeSerial\r\n") + web_interface->WebServer.setContentLength(CONTENT_LENGTH_UNKNOWN); + web_interface->WebServer.send(200); + web_interface->WebServer.sendHeader("Content-Type","text/plain"); + web_interface->WebServer.sendHeader("Cache-Control","no-cache"); + //send command + LOG(String(cmd.length())) + LOG("Start PurgeSerial\r\n") + while(Serial.available()){ + BRIDGE::processFromSerial2TCP(); + delay(1); + } + LOG("End PurgeSerial\r\n") + LOG("Send Command\r\n") + Serial.println(cmd); + count = 0; + String current_buffer; + String current_line; + int pos; + String tmp; + bool datasent = false; + //pickup the list + while (count < MAX_TRY){ + //give some time between each buffer + if (Serial.available()){ + count = 0; + size_t len = Serial.available(); + uint8_t sbuf[len+1]; + //read buffer + Serial.readBytes(sbuf, len); + //change buffer as string + sbuf[len]='\0'; + //add buffer to current one if any + current_buffer += (char * ) sbuf; + while (current_buffer.indexOf("\n") !=-1){ + //remove the possible "\r" + current_buffer.replace("\r",""); + pos = current_buffer.indexOf("\n"); + //get line + current_line = current_buffer.substring(0,current_buffer.indexOf("\n")); + //if line is command acck - just exit so save the time out period + if ((current_line.indexOf("ok" ) == 0) && (current_line.length() == 2)) + { + count = MAX_TRY; + LOG("Found ok\r\n") + break; + } + //get the line and transmit it + LOG(current_line) + LOG("\r\n") + //check command + COMMAND::check_command(current_line,false); + buffer2send +=current_line; + buffer2send +="\n"; + if (buffer2send.length() > 1200) { + web_interface->WebServer.sendContent(buffer2send); + buffer2send = ""; + datasent = true; + } + //current remove line from buffer + tmp = current_buffer.substring(current_buffer.indexOf("\n")+1,current_buffer.length()); + current_buffer = tmp; + } + } else delay(1); + count++; + } + //to be sure connection close + if (buffer2send.length() > 0) { + web_interface->WebServer.sendContent(buffer2send); + datasent = true; + } + if (!datasent)web_interface->WebServer.sendContent(" \r\n"); + web_interface->WebServer.sendContent(""); + LOG("Start PurgeSerial\r\n") + while(Serial.available()){ + BRIDGE::processFromSerial2TCP(); + delay(1); + } + LOG("End PurgeSerial\r\n") + web_interface->blockserial = false; + LOG("Release Serial\r\n") + } } - } } #ifdef SSDP_FEATURE @@ -3358,13 +3502,19 @@ WEBINTERFACE_CLASS::WEBINTERFACE_CLASS (int port):WebServer(port) WebServer.on("/STATUS",HTTP_ANY, handle_web_interface_status); WebServer.on("/SETTINGS",HTTP_ANY, handle_web_settings); WebServer.on("/PRINTER",HTTP_ANY, handle_web_interface_printer); - WebServer.on("/CMD",HTTP_ANY, handle_web_command); + WebServer.on("/command",HTTP_ANY, handle_web_command); +#if FIRMWARE_TARGET == SMOOTHIEWARE + WebServer.on("/command_silent",HTTP_ANY, handle_web_command); +#endif WebServer.on("/RESTART",HTTP_GET, handle_restart); #ifdef WEB_UPDATE_FEATURE WebServer.on("/UPDATE",HTTP_ANY, handleUpdate,WebUpdateUpload); #endif WebServer.on("/FILES", HTTP_ANY, handleFileList,SPIFFSFileupload); WebServer.on("/SDFILES", HTTP_ANY, handleSDFileList,SDFileupload); +#if FIRMWARE_TARGET == SMOOTHIEWARE + WebServer.on("/upload", HTTP_ANY, handleSDFileList,SDFileupload); +#endif #ifdef AUTHENTICATION_FEATURE WebServer.on("/LOGIN", HTTP_ANY, handle_login); WebServer.on("/PASSWORD", HTTP_ANY, handle_password); diff --git a/esp3d/wifi.cpp b/esp3d/wifi.cpp index adcfa6a9..fd142d71 100644 --- a/esp3d/wifi.cpp +++ b/esp3d/wifi.cpp @@ -111,7 +111,7 @@ bool WIFI_CONFIG::Setup(bool force_ap) //system_update_cpu_freq(SYS_CPU_160MHZ); //set the sleep mode if (!CONFIG::read_byte(EP_SLEEP_MODE, &bflag )) { - LOG("Error read Sleep mode\n") + LOG("Error read Sleep mode\r\n") return false; } WiFi.setSleepMode ((WiFiSleepType_t)bflag); @@ -121,7 +121,7 @@ bool WIFI_CONFIG::Setup(bool force_ap) } else { //AP or client ? if (!CONFIG::read_byte(EP_WIFI_MODE, &bmode ) ) { - LOG("Error read wifi mode\n") + LOG("Error read wifi mode\r\n") return false; } } @@ -130,7 +130,7 @@ bool WIFI_CONFIG::Setup(bool force_ap) } //this is AP mode if (bmode==AP_MODE) { - LOG("Set AP mode\n") + LOG("Set AP mode\r\n") if(!CONFIG::read_string(EP_AP_SSID, sbuf , MAX_SSID_LENGTH)) { return false; } @@ -142,65 +142,65 @@ bool WIFI_CONFIG::Setup(bool force_ap) Serial.println(sbuf); LOG("SSID ") LOG(sbuf) - LOG("\n") + LOG("\r\n") //DHCP or Static IP ? if (!CONFIG::read_byte(EP_AP_IP_MODE, &bflag )) { - LOG("Error IP mode\n") + LOG("Error IP mode\r\n") return false; } LOG("IP Mode: ") LOG(CONFIG::intTostr(bflag)) - LOG("\n") + LOG("\r\n") if (bflag==STATIC_IP_MODE) { byte ip_buf[4]; - LOG("Static mode\n") + LOG("Static mode\r\n") //get the IP LOG("IP value:") if (!CONFIG::read_buffer(EP_AP_IP_VALUE,ip_buf , IP_LENGTH)) { - LOG("Error\n") + LOG("Error\r\n") return false; } IPAddress local_ip (ip_buf[0],ip_buf[1],ip_buf[2],ip_buf[3]); LOG(local_ip.toString()) - LOG("\nGW value:") + LOG("\r\nGW value:") //get the gateway if (!CONFIG::read_buffer(EP_AP_GATEWAY_VALUE,ip_buf , IP_LENGTH)) { - LOG("Error\n") + LOG("Error\r\n") return false; } IPAddress gateway (ip_buf[0],ip_buf[1],ip_buf[2],ip_buf[3]); LOG(gateway.toString()) - LOG("\nMask value:") + LOG("\r\nMask value:") //get the mask if (!CONFIG::read_buffer(EP_AP_MASK_VALUE,ip_buf , IP_LENGTH)) { - LOG("Error Mask value\n") + LOG("Error Mask value\r\n") return false; } IPAddress subnet (ip_buf[0],ip_buf[1],ip_buf[2],ip_buf[3]); LOG(subnet.toString()) - LOG("\n") + LOG("\r\n") //apply according active wifi mode - LOG("Set IP\n") + LOG("Set IP\r\n") WiFi.softAPConfig( local_ip, gateway, subnet); delay(100); } - LOG("Disable STA\n") + LOG("Disable STA\r\n") WiFi.enableSTA(false); delay(100); - LOG("Set AP\n") + LOG("Set AP\r\n") //setup Soft AP WiFi.mode(WIFI_AP); delay(50); WiFi.softAP(sbuf, pwd); delay(100); - LOG("Set phy mode\n") + LOG("Set phy mode\r\n") //setup PHY_MODE if (!CONFIG::read_byte(EP_AP_PHY_MODE, &bflag )) { return false; } WiFi.setPhyMode((WiFiPhyMode_t)bflag); delay(100); - LOG("Get current config\n") + LOG("Get current config\r\n") //get current config struct softap_config apconfig; wifi_softap_get_config(&apconfig); @@ -228,7 +228,7 @@ bool WIFI_CONFIG::Setup(bool force_ap) delay(1000); } } else { - LOG("Set STA mode\n") + LOG("Set STA mode\r\n") if(!CONFIG::read_string(EP_STA_SSID, sbuf , MAX_SSID_LENGTH)) { return false; } @@ -240,7 +240,7 @@ bool WIFI_CONFIG::Setup(bool force_ap) Serial.println(sbuf); LOG("SSID ") LOG(sbuf) - LOG("\n") + LOG("\r\n") if (!CONFIG::read_byte(EP_STA_IP_MODE, &bflag )) { return false; }