esp32-upload-download-multipart-http icon indicating copy to clipboard operation
esp32-upload-download-multipart-http copied to clipboard

Example fails on ESP32 with divide by zero error

Open Daemach opened this issue 5 years ago • 8 comments

Thank you very much for providing this library. It is exactly what I needed to finish an IoT project. Unfortunately, the upload portion which is the part I need, is not working on an ESP-32 with 1.02-rc2 Arduino code. Here is the result of the code (integer divide by zero error) - Can you help, please? You obviously very good at this and I would appreciate the help immensely:

.... WiFi connected IP address: 192.168.2.104 Initializing SD card...initialization done. 100 done downloading Guru Meditation Error: Core 1 panic'ed (IntegerDivideByZero). Exception was unhandled. Core 1 register dump: PC : 0x400d1f87 PS : 0x00060630 A0 : 0x800d1c2c A1 : 0x3ffb1c30
A2 : 0x3ffb1f77 A3 : 0x00000000 A4 : 0x00000000 A5 : 0x00000000
A6 : 0x400d1a4c A7 : 0x3f4011bc A8 : 0x800d1f78 A9 : 0x400d1a28
A10 : 0x00000000 A11 : 0x3ffb1c58 A12 : 0x3ffb1d48 A13 : 0x00000000
A14 : 0x00000063 A15 : 0xff000000 SAR : 0x00000009 EXCCAUSE: 0x00000006
EXCVADDR: 0x00000000 LBEG : 0x4000c2e0 LEND : 0x4000c2f6 LCOUNT : 0xffffffff

Backtrace: 0x400d1f87:0x3ffb1c30 0x400d1c29:0x3ffb1f60 0x400d69b3:0x3ffb1fb0 0x400889bd:0x3ffb1fd0

Rebooting...

And here is the code I used - I had to modify it slightly from the example file to get it to run.

#include "UDHttp.h"
#include "FS.h"
#include "SD.h"
#include "SPI.h"

const char* ssid     = "mySSID";
const char* password = "myPW";

File root;
//these callbacks will be invoked to read and write data to sdcard
//and process response
//and showing progress 
int responsef(uint8_t *buffer, int len){
  Serial.printf("%s\n", buffer);
  return 0;
}

int rdataf(uint8_t *buffer, int len){
  //read file to upload
  if (root.available()) {
    return root.read(buffer, len);
  }
  return 0;
}

int wdataf(uint8_t *buffer, int len){
  //write downloaded data to file
  return root.write(buffer, len);
}

void progressf(int percent){
  Serial.printf("%d\n", percent);
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
    
  Serial.print("Initializing SD card...");
  if (!SD.begin(33)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  SD.remove("test.pdf");
  {
    UDHttp udh;
    //open file on sdcard to write
    root = SD.open("/test.pdf", FILE_WRITE);
    if (!root) {
       Serial.println("can not open file!");
       return;
    }
    //download the file from url
    udh.download("http://www.smart-words.org/linking-words/linking-words.pdf", wdataf, progressf);
    root.close();
    Serial.printf("done downloading\n");
  }
  {
    UDHttp udh;
    //open file on sdcard to read
    root = SD.open("/test.pdf");
    if (!root) {
       Serial.println("can not open file!");
       return;
    }
    //upload downloaded file to local server
    udh.upload("http://d.thcguard.com:80/dbugUpload.cfm", "test.pdf", root.size(), rdataf, progressf, responsef);
    root.close();
    Serial.printf("done uploading\n");
  }
}

Daemach avatar Apr 13 '19 19:04 Daemach

Hi friend,

Could you seperate the problem come from upload or download. I think the problem come from download in the code to check progress.

Best regards,

On Sun, 14 Apr 2019, 02:15 Daemach, [email protected] wrote:

Thank you very much for providing this library. It is exactly what I needed to finish an IoT project. Unfortunately, the upload portion which is the part I need, is not working on an ESP-32 with 1.02-rc2 Arduino code. Here is the result of the code (integer divide by zero error) - Can you help, please? You obviously very good at this and I would appreciate the help immensely:

.... WiFi connected IP address: 192.168.2.104 Initializing SD card...initialization done. 100 done downloading Guru Meditation Error: Core 1 panic'ed (IntegerDivideByZero). Exception was unhandled. Core 1 register dump: PC : 0x400d1f87 PS : 0x00060630 A0 : 0x800d1c2c A1 : 0x3ffb1c30 A2 : 0x3ffb1f77 A3 : 0x00000000 A4 : 0x00000000 A5 : 0x00000000 A6 : 0x400d1a4c A7 : 0x3f4011bc A8 : 0x800d1f78 A9 : 0x400d1a28 A10 : 0x00000000 A11 : 0x3ffb1c58 A12 : 0x3ffb1d48 A13 : 0x00000000 A14 : 0x00000063 A15 : 0xff000000 SAR : 0x00000009 EXCCAUSE: 0x00000006 EXCVADDR: 0x00000000 LBEG : 0x4000c2e0 LEND : 0x4000c2f6 LCOUNT : 0xffffffff

Backtrace: 0x400d1f87:0x3ffb1c30 0x400d1c29:0x3ffb1f60 0x400d69b3:0x3ffb1fb0 0x400889bd:0x3ffb1fd0

Rebooting...

And here is the code I used - I had to modify it slightly from the example file to get it to run.

#include "UDHttp.h" #include "FS.h" #include "SD.h" #include "SPI.h"

const char* ssid = "mySSID"; const char* password = "myPW";

File root; //these callbacks will be invoked to read and write data to sdcard //and process response //and showing progress int responsef(uint8_t *buffer, int len){ Serial.printf("%s\n", buffer); return 0; }

int rdataf(uint8_t *buffer, int len){ //read file to upload if (root.available()) { return root.read(buffer, len); } return 0; }

int wdataf(uint8_t *buffer, int len){ //write downloaded data to file return root.write(buffer, len); }

void progressf(int percent){ Serial.printf("%d\n", percent); }

void setup() { // put your setup code here, to run once: Serial.begin(115200);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }

Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP());

Serial.print("Initializing SD card..."); if (!SD.begin(33)) { Serial.println("initialization failed!"); return; } Serial.println("initialization done."); SD.remove("test.pdf"); { UDHttp udh; //open file on sdcard to write root = SD.open("/test.pdf", FILE_WRITE); if (!root) { Serial.println("can not open file!"); return; } //download the file from url udh.download("http://www.smart-words.org/linking-words/linking-words.pdf", wdataf, progressf); root.close(); Serial.printf("done downloading\n"); } { UDHttp udh; //open file on sdcard to read root = SD.open("/test.pdf"); if (!root) { Serial.println("can not open file!"); return; } //upload downloaded file to local server udh.upload("http://d.thcguard.com:80/dbugUpload.cfm", "test.pdf", root.size(), rdataf, progressf, responsef); root.close(); Serial.printf("done uploading\n"); } }

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/nhatuan84/esp-upload-download-file-http/issues/2, or mute the thread https://github.com/notifications/unsubscribe-auth/AEjfLotMceEOKrCV93jOQpTU4nBOKy7Tks5vgiztgaJpZM4cuOPY .

nhatuan84 avatar Apr 15 '19 03:04 nhatuan84

The error does come from download. I commented that out because I don't need it. I just need upload, but upload not working. Here's what I get on the server - it looks like no file is being sent, just POST headers: https://www.screencast.com/t/vQAuFQp5kz

Here is the output of the serial monitor: .... WiFi connected IP address: 192.168.2.104 Initializing SD card...initialization done. Beginning Upload 0 done uploading

It is a 50K text file.

There are some compilation errors - maybe this is the reason?

C:\Users\Daemach\Dropbox\Private\Arduino\Sketches\esp32udhttp\esp32udhttp.ino: In function 'void setup()':

C:\Users\Daemach\Dropbox\Private\Arduino\Sketches\esp32udhttp\esp32udhttp.ino:81:111: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

 udh.upload("http://d.thcguard.com:80/index.cfm", "20190412.log", root.size(), rdataf, progressf, responsef);

C:\Users\Daemach\Dropbox\Private\Arduino\Sketches\libraries\UDHttp\src\UDHttp.cpp: In member function 'int UDHttp::upload(char*, char*, int, DataCb, ProgressCb, DataCb)':

C:\Users\Daemach\Dropbox\Private\Arduino\Sketches\libraries\UDHttp\src\UDHttp.cpp:103:17: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

 char *key = "aHR0cDovL3d3dy5pb3RzaGFyaW5nLmNvbQ==";

Daemach avatar Apr 15 '19 21:04 Daemach

So you may check whether the reading process from sdcard. Or refer this: http://www.iotsharing.com/2017/12/how-to-debugging-errors-using-arduino-esp-exception-decoder.html?m=1

On Tue, 16 Apr 2019, 04:49 Daemach, [email protected] wrote:

The error does come from download. I commented that out because I don't need it. I just need upload, but upload not working. Here's what I get on the server - it looks like no file is being sent, just POST headers: https://www.screencast.com/t/vQAuFQp5kz

Here is the output of the serial monitor: .... WiFi connected IP address: 192.168.2.104 Initializing SD card...initialization done. Beginning Upload 0 done uploading

It is a 50K text file.

There are some compilation errors - maybe this is the reason?

C:\Users\Daemach\Dropbox\Private\Arduino\Sketches\esp32udhttp\esp32udhttp.ino: In function 'void setup()':

C:\Users\Daemach\Dropbox\Private\Arduino\Sketches\esp32udhttp\esp32udhttp.ino:81:111: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

udh.upload("http://d.thcguard.com:80/index.cfm", "20190412.log", root.size(), rdataf, progressf, responsef);

C:\Users\Daemach\Dropbox\Private\Arduino\Sketches\libraries\UDHttp\src\UDHttp.cpp: In member function 'int UDHttp::upload(char*, char*, int, DataCb, ProgressCb, DataCb)':

C:\Users\Daemach\Dropbox\Private\Arduino\Sketches\libraries\UDHttp\src\UDHttp.cpp:103:17: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

char *key = "aHR0cDovL3d3dy5pb3RzaGFyaW5nLmNvbQ==";

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/nhatuan84/esp-upload-download-file-http/issues/2#issuecomment-483431749, or mute the thread https://github.com/notifications/unsubscribe-auth/AEjfLq9DEKxQpGvS070UidFdXrYe0ddbks5vhPPlgaJpZM4cuOPY .

nhatuan84 avatar Apr 16 '19 02:04 nhatuan84

That is a very useful tool. Thanks for providing it. After commenting out the download section it's no longer erroring. I'm only interested in the upload portion which is not erroring, but is not working either.

I'm using your code and it's not returning a "can not open file!" error so it must be opening the file. It's also passing the correct root.size() to the upload function. It's just that no actual read is taking place and no data is showing up in the POST.

Daemach avatar Apr 16 '19 15:04 Daemach

I'm sorry, I figured out the problem with the file. It is now transferring but the webserver is giving a 404 because the POST headers are not correct. I am using:

udh.upload("http://d.thcguard.com/index.cfm", "20190412.log", root.size(), rdataf, progressf, responsef);

The post header should say POST /index.cfm HTTP/1.1 But it says POST d.thcguard.com/index.cfm HTTP/1.1

Daemach avatar Apr 16 '19 16:04 Daemach

udh.upload("http://d.thcguard.com:80/index.cfm", "test.pdf", myFile.size(), rdataf, progressf, responsef); (added port) doesn't work either...

See https://www.w3schools.com/tags/ref_httpmethods.asp

The data sent to the server with POST is stored in the request body of the HTTP request:

POST /test/demo_form.php HTTP/1.1 Host: w3schools.com name1=value1&name2=value2

Daemach avatar Apr 16 '19 16:04 Daemach

Hello, I am trying to execute the code above and apparently it works, the client claims to have sent the file but the server does not receive it. The debug is attached. Can anyone tell me what may be happening? is this debug `correct?

10:53:47.724 -> [I][WiFiClient.cpp:512] connected(): Unexpected: RES: 0, ERR: 5 10:53:47.758 -> [V][WebServer.cpp:286] handleClient(): New client 10:53:47.758 -> [I][WiFiClient.cpp:512] connected(): Unexpected: RES: 0, ERR: 5 10:53:47.758 -> [V][Parsing.cpp:113] _parseRequest(): method: POST url: http://192.168.0.118:80/upload.php search: 10:53:47.758 -> [V][Parsing.cpp:146] _parseRequest(): headerName: Host 10:53:47.758 -> [V][Parsing.cpp:147] _parseRequest(): headerValue: 192.168.0.118:80 10:53:47.758 -> [V][Parsing.cpp:146] _parseRequest(): headerName: Connection 10:53:47.793 -> [V][Parsing.cpp:147] _parseRequest(): headerValue: keep-alive 10:53:47.793 -> [V][Parsing.cpp:146] _parseRequest(): headerName: Accept 10:53:47.793 -> [V][Parsing.cpp:147] _parseRequest(): headerValue: / 10:53:47.793 -> [V][Parsing.cpp:146] _parseRequest(): headerName: Content-Length 10:53:47.793 -> [V][Parsing.cpp:147] _parseRequest(): headerValue: 244 10:53:47.793 -> [V][Parsing.cpp:146] _parseRequest(): headerName: Expect 10:53:47.793 -> [V][Parsing.cpp:147] _parseRequest(): headerValue: 10:53:47.828 -> [V][Parsing.cpp:146] _parseRequest(): headerName: Content-Type 10:53:47.828 -> [V][Parsing.cpp:147] _parseRequest(): headerValue: multipart/form-data; boundary=------------------------aHR0cDovL3d3dy5pb3RzaGFyaW5nLmNvbQ== 10:53:47.828 -> [V][Parsing.cpp:247] _parseArguments(): args: 10:53:47.828 -> [V][Parsing.cpp:348] _parseForm(): Parse Form: Boundary: ------------------------aHR0cDovL3d3dy5pb3RzaGFyaW5nLmNvbQ== Length: 244 10:53:47.862 -> [V][Parsing.cpp:382] _parseForm(): PostArg FileName: test.txt 10:53:47.862 -> [V][Parsing.cpp:387] _parseForm(): PostArg Name: data'; filename='test.txt' 10:53:47.862 -> [V][Parsing.cpp:398] _parseForm(): PostArg Type: application/octet-stream 10:53:47.862 -> [V][Parsing.cpp:425] _parseForm(): Start File: test.txt Type: application/octet-stream 10:53:47.862 -> [D][WiFiClient.cpp:509] connected(): Disconnected: RES: 0, ERR: 128

rbbahia avatar May 01 '20 13:05 rbbahia

The key does not correspond to some standard)) Symbols "==" must be removed

"char * key ="aHR0cDovL3d3dy5pb3RzaGFyaW5nLmNvbQ==";

replace to

 "char * key ="aHR0cDovL3d3dy5pb3RzaGFyaW5nLmNvbQww";

Jekahome avatar Jul 17 '20 12:07 Jekahome