Skip to content

Commit 8a2c398

Browse files
committed
Implement check sum for Serial upload as mandatory for Marlin
Add 2 new [ESPXXX] command as check sum helper * Send GCode with check sum caching right line numbering [ESP600]<gcode> * Send line checksum [ESP601]<line> Implemented only for Sync WebServer at this moment, so Async is disabled in Travis
1 parent f24dfe9 commit 8a2c398

File tree

6 files changed

+305
-134
lines changed

6 files changed

+305
-134
lines changed

.travis.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ script:
6262
- build_sketch $TRAVIS_BUILD_DIR/examples/basicesp3d/basicesp3d.ino
6363
- arduino --board esp32:esp32:esp32 --save-prefs
6464
- build_sketch $TRAVIS_BUILD_DIR/examples/basicesp3d/basicesp3d.ino
65-
- sed -i "s/\/\/#define ASYNCWEBSERVER /#define ASYNCWEBSERVER/g" $HOME/arduino_ide/libraries/ESP3D/src/config.h
66-
- arduino --board esp8266com:esp8266:generic --save-prefs
67-
- arduino --get-pref sketchbook.path
68-
- build_sketch $TRAVIS_BUILD_DIR/examples/basicesp3d/basicesp3d.ino
69-
- arduino --board esp32:esp32:esp32 --save-prefs
70-
- build_sketch $TRAVIS_BUILD_DIR/examples/basicesp3d/basicesp3d.ino
65+
# - sed -i "s/\/\/#define ASYNCWEBSERVER /#define ASYNCWEBSERVER/g" $HOME/arduino_ide/libraries/ESP3D/src/config.h
66+
# - arduino --board esp8266com:esp8266:generic --save-prefs
67+
# - arduino --get-pref sketchbook.path
68+
# - build_sketch $TRAVIS_BUILD_DIR/examples/basicesp3d/basicesp3d.ino
69+
# - arduino --board esp32:esp32:esp32 --save-prefs
70+
# - build_sketch $TRAVIS_BUILD_DIR/examples/basicesp3d/basicesp3d.ino
7171

7272
notifications:
7373
email:

docs/Commands.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ if authentication is on, need admin password for RESET, RESTART and SAFEMODE
136136
[ESP555]<password>pwd=<admin password>
137137
if no password set it use default one
138138

139+
* Send GCode with check sum caching right line numbering
140+
[ESP600]<gcode>
141+
142+
* Send line checksum
143+
[ESP601]<line>
144+
139145
* Read SPIFFS file and send each line to serial
140146
[ESP700]<filename>
141147

src/command.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ String COMMAND::buffer_tcp;
4949
#define INCORRECT_CMD_MSG (output == WEB_PIPE)?F("Error: Incorrect Command"):F("Incorrect Cmd")
5050
#define OK_CMD_MSG (output == WEB_PIPE)?F("ok"):F("Cmd Ok")
5151

52+
extern uint8_t Checksum(const char * line, uint16_t lineSize);
53+
extern bool sendLine2Serial (String & line, int32_t linenb, int32_t* newlinenb);
54+
5255
String COMMAND::get_param (String & cmd_params, const char * id, bool withspace)
5356
{
5457
static String parameter;
@@ -1403,6 +1406,47 @@ bool COMMAND::execute_command (int cmd, String cmd_params, tpipe output, level_a
14031406
break;
14041407
}
14051408
#endif
1409+
//[ESP600]<gcode>
1410+
case 600: { //send GCode with check sum caching right line numbering
1411+
//be sure serial is locked
1412+
if ( (web_interface->blockserial) ) {
1413+
break;
1414+
}
1415+
int32_t linenb = 1;
1416+
cmd_params.trim() ;
1417+
if (sendLine2Serial (cmd_params, linenb, &linenb))ESPCOM::println (OK_CMD_MSG, output, espresponse);
1418+
else { //it may failed because of skip if repetier so let's reset numbering first
1419+
if ( ( CONFIG::GetFirmwareTarget() == REPETIER4DV) || (CONFIG::GetFirmwareTarget() == REPETIER) ) {
1420+
//reset numbering
1421+
String cmd = "M110 N0";
1422+
if (sendLine2Serial (cmd, -1, NULL)){
1423+
linenb = 1;
1424+
//if success let's try again to send the command
1425+
if (sendLine2Serial (cmd_params, linenb, &linenb))ESPCOM::println (OK_CMD_MSG, output, espresponse);
1426+
else {
1427+
ESPCOM::println (ERROR_CMD_MSG, output, espresponse);
1428+
response = false;
1429+
}
1430+
} else {
1431+
ESPCOM::println (ERROR_CMD_MSG, output, espresponse);
1432+
response = false;
1433+
}
1434+
} else {
1435+
1436+
ESPCOM::println (ERROR_CMD_MSG, output, espresponse);
1437+
response = false;
1438+
}
1439+
}
1440+
}
1441+
break;
1442+
//[ESP601]<line>
1443+
case 601: { //send line checksum
1444+
cmd_params.trim();
1445+
int8_t chk = Checksum(cmd_params.c_str(),cmd_params.length());
1446+
String schecksum = "Checksum: " + String(chk);
1447+
ESPCOM::println (schecksum, output, espresponse);
1448+
}
1449+
break;
14061450
//[ESP700]<filename>
14071451
case 700: { //read local file
14081452
//be sure serial is locked

src/config.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*/
2020

2121
//version and sources location
22-
#define FW_VERSION "2.0.0.c20"
22+
#define FW_VERSION "2.0.0.c21"
2323
#define REPOSITORY "https://github.com/luc-github/ESP3D"
2424

2525
//Customize ESP3D ////////////////////////////////////////////////////////////////////////
@@ -71,10 +71,10 @@
7171
#define DIRECT_PIN_FEATURE
7272

7373
//ESP_OLED_FEATURE: allow oled screen output
74-
#define ESP_OLED_FEATURE
74+
//#define ESP_OLED_FEATURE
7575

7676
//DHT_FEATURE: send update of temperature / humidity based on DHT 11/22
77-
#define DHT_FEATURE
77+
//#define DHT_FEATURE
7878

7979
//AUTHENTICATION_FEATURE: protect pages by login password
8080
//#define AUTHENTICATION_FEATURE
@@ -83,7 +83,7 @@
8383
#define WS_DATA_FEATURE
8484

8585
//TIMESTAMP_FEATURE: Time stamp feature on direct SD files
86-
#define TIMESTAMP_FEATURE
86+
//#define TIMESTAMP_FEATURE
8787

8888
//Extra features /////////////////////////////////////////////////////////////////////////
8989

@@ -216,7 +216,7 @@ using fs::File;
216216
#endif
217217
#ifdef DEBUG_OUTPUT_TCP
218218
#include "espcom.h"
219-
#define LOG(string) {ESPCOM::send2TCP(string, true);}
219+
#define LOG(string) {ESPCOM::send2TCP(string, false);}
220220
#define DEBUG_PIPE TCP_PIPE
221221
#endif
222222
#else

src/syncwebserver.cpp

Lines changed: 82 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length
101101

102102

103103
extern bool deleteRecursive(String path);
104-
extern void CloseSerialUpload (bool iserror, String & filename);
105-
extern bool sendLine2Serial (String & line);
106-
104+
extern void CloseSerialUpload (bool iserror, String & filename, int32_t linenb);
105+
extern bool sendLine2Serial (String & line, int32_t linenb, int32_t* newlinenb);
106+
extern bool purge_serial();
107107

108108
const uint8_t PAGE_404 [] PROGMEM = "<HTML>\n<HEAD>\n<title>Redirecting...</title> \n</HEAD>\n<BODY>\n<CENTER>Unknown page : $QUERY$- you will be redirected...\n<BR><BR>\nif not redirected, <a href='http://$WEB_ADDRESS$'>click here</a>\n<BR><BR>\n<PROGRESS name='prg' id='prg'></PROGRESS>\n\n<script>\nvar i = 0; \nvar x = document.getElementById(\"prg\"); \nx.max=5; \nvar interval=setInterval(function(){\ni=i+1; \nvar x = document.getElementById(\"prg\"); \nx.value=i; \nif (i>5) \n{\nclearInterval(interval);\nwindow.location.href='/';\n}\n},1000);\n</script>\n</CENTER>\n</BODY>\n</HTML>\n\n";
109109
const uint8_t PAGE_CAPTIVE [] PROGMEM = "<HTML>\n<HEAD>\n<title>Captive Portal</title> \n</HEAD>\n<BODY>\n<CENTER>Captive Portal page : $QUERY$- you will be redirected...\n<BR><BR>\nif not redirected, <a href='http://$WEB_ADDRESS$'>click here</a>\n<BR><BR>\n<PROGRESS name='prg' id='prg'></PROGRESS>\n\n<script>\nvar i = 0; \nvar x = document.getElementById(\"prg\"); \nx.max=5; \nvar interval=setInterval(function(){\ni=i+1; \nvar x = document.getElementById(\"prg\"); \nx.value=i; \nif (i>5) \n{\nclearInterval(interval);\nwindow.location.href='/';\n}\n},1000);\n</script>\n</CENTER>\n</BODY>\n</HTML>\n\n";
@@ -559,7 +559,6 @@ void SPIFFSFileupload()
559559
if(upload.status == UPLOAD_FILE_START) {
560560
String upload_filename = upload.filename;
561561
String sizeargname = upload_filename + "S";
562-
if (web_interface->web_server.hasArg ("plain") ) Serial.println("Yes");
563562
if (upload_filename[0] != '/') filename = "/" + upload_filename;
564563
else filename = upload.filename;
565564
//according User or Admin the root is different as user is isolate to /user when admin has full access
@@ -1152,6 +1151,7 @@ void handle_serial_SDFileList()
11521151
//SD file upload by serial
11531152
void SDFile_serial_upload()
11541153
{
1154+
static int32_t lineNb =-1;
11551155
static String current_line;
11561156
static bool is_comment = false;
11571157
static String current_filename;
@@ -1174,93 +1174,70 @@ void SDFile_serial_upload()
11741174
//Upload start
11751175
//**************
11761176
if(upload.status == UPLOAD_FILE_START) {
1177+
LOG("Upload Start\r\n")
1178+
String command = "M29";
1179+
String resetcmd = "M110 N0";
1180+
if (CONFIG::GetFirmwareTarget() == SMOOTHIEWARE)resetcmd = "N0 M110";
1181+
lineNb=1;
1182+
//close any ongoing upload and get current line number
1183+
if(!sendLine2Serial (command,1, &lineNb)){
1184+
//it can failed for repetier
1185+
if ( ( CONFIG::GetFirmwareTarget() == REPETIER4DV) || (CONFIG::GetFirmwareTarget() == REPETIER) ) {
1186+
if(!sendLine2Serial (command,-1, NULL)){
1187+
LOG("Start Upload failed")
1188+
web_interface->_upload_status= UPLOAD_STATUS_FAILED;
1189+
return;
1190+
}
1191+
} else {
1192+
LOG("Start Upload failed")
1193+
web_interface->_upload_status= UPLOAD_STATUS_FAILED;
1194+
return;
1195+
}
1196+
}
1197+
//Reset line numbering
1198+
if(!sendLine2Serial (resetcmd,-1, NULL)){
1199+
LOG("Reset Numbering failed")
1200+
web_interface->_upload_status= UPLOAD_STATUS_FAILED;
1201+
return;
1202+
}
1203+
lineNb=1;
11771204
//need to lock serial out to avoid garbage in file
11781205
(web_interface->blockserial) = true;
11791206
current_line ="";
1180-
is_comment = false;
1181-
String response;
1182-
web_interface->_upload_status= UPLOAD_STATUS_ONGOING;
1207+
current_filename = upload.filename;
1208+
is_comment = false;
1209+
String response;
11831210
ESPCOM::println (F ("Uploading..."), PRINTER_PIPE);
1211+
//Clear all serial
11841212
ESPCOM::flush (DEFAULT_PRINTER_PIPE);
1185-
LOG("Clear Serial\r\n");
1186-
if(ESPCOM::available(DEFAULT_PRINTER_PIPE)) {
1187-
ESPCOM::bridge();
1188-
CONFIG::wait(1);
1189-
}
1190-
//command to pritnter to start print
1191-
String command = "M28 " + upload.filename;
1192-
LOG(command);
1193-
LOG("\r\n");
1194-
ESPCOM::println (command, DEFAULT_PRINTER_PIPE);
1195-
ESPCOM::flush (DEFAULT_PRINTER_PIPE);
1196-
current_filename = upload.filename;
1197-
CONFIG::wait (500);
1198-
uint32_t timeout = millis();
1199-
bool done = false;
1200-
while (!done) { //time out is 2000ms
1201-
CONFIG::wait(0);
1202-
//if there is something in serial buffer
1203-
size_t len = ESPCOM::available(DEFAULT_PRINTER_PIPE);
1204-
//get size of buffer
1205-
if (len > 0) {
1206-
CONFIG::wait(0);
1207-
uint8_t * sbuf = (uint8_t *)malloc(len+1);
1208-
if(!sbuf){
1209-
ESPCOM::println (F ("SD upload rejected"), PRINTER_PIPE);
1210-
LOG("SD upload rejected\r\n");
1211-
LOG("Need to stop");
1212-
#if defined ( ARDUINO_ARCH_ESP8266)
1213-
web_interface->web_server.client().stopAll();
1214-
#else
1215-
web_interface->web_server.client().stop();
1216-
#endif
1217-
return ;
1218-
}
1219-
//read buffer
1220-
ESPCOM::readBytes (DEFAULT_PRINTER_PIPE, sbuf, len);
1221-
//convert buffer to zero end array
1222-
sbuf[len] = '\0';
1223-
//use string because easier to handle
1224-
response = (const char*) sbuf;
1225-
LOG (response);
1226-
//if there is a wait it means purge is done
1227-
if (response.indexOf ("wait") > -1) {
1228-
LOG ("Exit start writing\r\n");
1229-
done = true;
1230-
free(sbuf);
1231-
break;
1232-
}
1233-
//it is first command if it is failed no need to continue
1234-
//and resend command won't help
1235-
if (response.indexOf ("Resend") > -1 || response.indexOf ("failed") > -1) {
1236-
web_interface->blockserial = false;
1237-
LOG ("Error start writing\r\n");
1238-
web_interface->_upload_status = UPLOAD_STATUS_FAILED;
1239-
#if defined ( ARDUINO_ARCH_ESP8266)
1240-
web_interface->web_server.client().stopAll();
1241-
#else
1242-
web_interface->web_server.client().stop();
1243-
#endif
1244-
free(sbuf);
1245-
return;
1246-
}
1247-
free(sbuf);
1248-
}
1249-
if ( (millis() - timeout) > SERIAL_CHECK_TIMEOUT) {
1250-
done = true;
1251-
}
1213+
purge_serial();
1214+
//besure nothing left again
1215+
purge_serial();
1216+
command = "M28 " + upload.filename;
1217+
//send start upload
1218+
//no correction allowed because it means reset numbering was failed
1219+
if (sendLine2Serial(command, lineNb, NULL)){
1220+
CONFIG::wait(1200);
1221+
//additional purge, in case it is slow to answer
1222+
purge_serial();
1223+
web_interface->_upload_status= UPLOAD_STATUS_ONGOING;
1224+
LOG("Creation Ok\r\n")
1225+
1226+
} else {
1227+
web_interface->_upload_status= UPLOAD_STATUS_FAILED;
1228+
LOG("Creation failed\r\n");
12521229
}
12531230
//Upload write
12541231
//**************
12551232
//upload is on going with data coming by 2K blocks
12561233
} else if(upload.status == UPLOAD_FILE_WRITE) { //if com error no need to send more data to serial
1257-
web_interface->_upload_status= UPLOAD_STATUS_ONGOING;
1234+
if (web_interface->_upload_status == UPLOAD_STATUS_ONGOING) {
12581235
for (int pos = 0; pos < upload.currentSize; pos++) { //parse full post data
12591236
//feed watchdog
12601237
CONFIG::wait(0);
12611238
//it is a comment
12621239
if (upload.buf[pos] == ';') {
1263-
LOG ("Comment\n")
1240+
LOG ("Comment\r\n")
12641241
is_comment = true;
12651242
}
12661243
//it is an end line
@@ -1271,10 +1248,11 @@ void SDFile_serial_upload()
12711248
if (current_line.length() < 126) {
12721249
//do we have something in buffer ?
12731250
if (current_line.length() > 0 ) {
1274-
current_line += "\r\n";
1275-
if (!sendLine2Serial (current_line) ) {
1251+
lineNb++;
1252+
if (!sendLine2Serial (current_line, lineNb, NULL) ) {
1253+
//fup.println("[Log]Write Error");
12761254
LOG ("Error over buffer\n")
1277-
CloseSerialUpload (true, current_filename);
1255+
CloseSerialUpload (true, current_filename,lineNb);
12781256
#if defined ( ARDUINO_ARCH_ESP8266)
12791257
web_interface->web_server.client().stopAll();
12801258
#else
@@ -1291,7 +1269,8 @@ void SDFile_serial_upload()
12911269
} else {
12921270
//error buffer overload
12931271
LOG ("Error over buffer\n")
1294-
CloseSerialUpload (true, current_filename);
1272+
lineNb++;
1273+
CloseSerialUpload (true, current_filename, lineNb);
12951274
#if defined ( ARDUINO_ARCH_ESP8266)
12961275
web_interface->web_server.client().stopAll();
12971276
#else
@@ -1304,7 +1283,8 @@ void SDFile_serial_upload()
13041283
current_line += char (upload.buf[pos]); //copy current char to buffer to send/resend
13051284
} else {
13061285
LOG ("Error over buffer\n")
1307-
CloseSerialUpload (true, current_filename);
1286+
lineNb++;
1287+
CloseSerialUpload (true, current_filename, lineNb);
13081288
#if defined ( ARDUINO_ARCH_ESP8266)
13091289
web_interface->web_server.client().stopAll();
13101290
#else
@@ -1314,15 +1294,28 @@ void SDFile_serial_upload()
13141294
}
13151295
}
13161296
}
1297+
} else {
1298+
LOG ("Error upload\n")
1299+
web_interface->_upload_status = UPLOAD_STATUS_FAILED;
1300+
lineNb++;
1301+
CloseSerialUpload (true, current_filename, lineNb);
1302+
#if defined ( ARDUINO_ARCH_ESP8266)
1303+
web_interface->web_server.client().stopAll();
1304+
#else
1305+
web_interface->web_server.client().stop();
1306+
#endif
1307+
return;
1308+
}
13171309
//Upload end
13181310
//**************
1319-
} else if(upload.status == UPLOAD_FILE_END) {
1311+
} else if(upload.status == UPLOAD_FILE_END && web_interface->_upload_status == UPLOAD_STATUS_ONGOING) {
13201312
//if last part does not have '\n'
13211313
if (current_line.length() > 0) {
1322-
current_line += "\r\n";
1323-
if (!sendLine2Serial (current_line) ) {
1314+
lineNb++;
1315+
if (!sendLine2Serial (current_line, lineNb, NULL) ) {
13241316
LOG ("Error sending buffer\n")
1325-
CloseSerialUpload (true, current_filename);
1317+
lineNb++;
1318+
CloseSerialUpload (true, current_filename, lineNb);
13261319
#if defined ( ARDUINO_ARCH_ESP8266)
13271320
web_interface->web_server.client().stopAll();
13281321
#else
@@ -1332,12 +1325,14 @@ void SDFile_serial_upload()
13321325
}
13331326
}
13341327
LOG ("Upload finished ");
1335-
CloseSerialUpload (false, current_filename);
1328+
lineNb++;
1329+
CloseSerialUpload (false, current_filename, lineNb);
13361330
//Upload cancelled
13371331
//**************
13381332
} else { //UPLOAD_FILE_ABORTED
13391333
LOG("Error, Something happened\r\n");
1340-
CloseSerialUpload (true, current_filename);
1334+
lineNb++;
1335+
CloseSerialUpload (true, current_filename, lineNb);
13411336
#if defined ( ARDUINO_ARCH_ESP8266)
13421337
web_interface->web_server.client().stopAll();
13431338
#else

0 commit comments

Comments
 (0)