boblemaire / asyncHTTPrequest

asynchronous HTTP for ESP using ESPasyncTCP. Works like XMLHTTPrequest in JS.
GNU General Public License v3.0
64 stars 31 forks source link

ESP32 Store prohibited while reading file data from get response of HTTP #21

Closed yogeshwaran774 closed 4 years ago

yogeshwaran774 commented 4 years ago

I am getting a OTA file from server and trying update the firmware but when i don't know how to get the data successfully in buffer. I tried with ESPAsynWebServer in that case there also they are creating a pointer argument and pushing the data whatever they received and it is getting uploaded successfully. File Size-825KB

What could be the issue?

void requestCB(void optParm, asyncHTTPrequest request, int readyState){ if(readyState == 4){ uint8_t data;
size_t len = request->responseLength(); request->responseRead(data,len); //Serial.println(request->responseText()); Serial.println(request->responseLength()); if (!Update.begin(UPDATE_SIZE_UNKNOWN, U_FLASH)) { // Start with max available size Update.printError(Serial); } // if (upload.status == UPLOAD_FILE_START) { // Serial.printf("Update: %s\n", upload.filename.c_str()); // if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size // Update.printError(Serial); // } // } else if (upload.status == UPLOAD_FILE_WRITE) { /
flashing firmware to ESP*/ if (Update.write(data, len) != len) { Update.printError(Serial); } request->setDebug(false); } }

boblemaire commented 4 years ago

I don't have all of the context, but from what's visible: Typically the total file size is sent in a response header, and if so you get it with request->responseLength(). That would avoid using UPDATE_SIZE_UNKNOWN.

More importantly, your buffer is only one byte. You can use a small fixed size buffer and loop on it:

uint8_t buf [256]; uint len; while(request->available()){ size_t len = request->response_read(buf, 256); Update.write(buf, len); }

embellish with error checking and keep track of total bytes read vs responseLength() to indicate end;

yogeshwaran774 commented 4 years ago

I assured my file size in file explorer is 825200 but the available size is 10783 (request->available), while writing it is returning to zero.What might be the issue?

while (request->available()) { size_t len = request->responseRead(data, 256); Serial.print("len : ");Serial.println(len); total_size += len; Serial.print("writing : "); Serial.println(total_size); size_t temp_len = Update.write(data, len); Serial.print("temp_len : ");Serial.println(temp_len); if (temp_len != len) { Update.printError(Serial); }

Output: writing : 10496 temp_len : 0 No Error len : 256 writing : 10752 temp_len : 0 No Error len : 31 writing : 10783 temp_len : 0 No Error

boblemaire commented 4 years ago

I don't see a problem with asyncHTTPrequest here. I think if there is an issue here, it's probably a lack of understanding how it works and how the asynchronous lambda function fits into the picture. I's not really possible to work through this here as a github issue. There are better tools for that.

The primary project in this repo is the IoTaWatt firmware. asyncHTTPrequest was written to use as part of that and published separately FBO others who might want to use it for other purposes. It is in general use for the ESP8266, and still has issues with ESP32, although I think it will probably do what you are looking to do.

I would be willing to review your issue if you register and post the issue on my IotaWatt online discourse forum. The tools there are excellent for this application.

If you do that, upload your program, and I will take a look. I am going to close out this as a support issue as not a definitive problem with asyncHTTPrequest.