I'm working on a device in which I'm trying to do Firmware Over the air update of esp32-wroom module,
but i'm using another 4G-LTE module from Quictel (EC200S) to download the bin file from server.
everything looks fine but in the last it gives me error to update as >>Error Occurred. Error #: 6
here is the ota Function i'm using.
please help
- bool FirmwareVersionCheck()
- {
- bool value;
- int httpCode ;
- String fwurl = "";
- fwurl = URL_fw_Version;
- Serial.println(fwurl);
- Serial.println();
- if (WiFi.status() != WL_CONNECTED)
- {
- Network_Check_GSM();
- if (GSM_Network_STS == true)
- {
- Serial2.write("AT+QIACT?\r\n");
- delay(100);
- input = Serial2.readString();
- if (input.indexOf("OK") >= 0)
- {
- Serial.println("GPRS CONNECT SUCCESSFULLY");
- Serial.println();
- input = Serial2.readString();
- input.remove(0);
- Serial2.print("AT+QHTTPURL=83,30\r\n");
- delay(100);
- input = Serial2.readString();
- if (input.indexOf("CONNECT") >= 0)
- {
- Serial2.print(fwurl);
- delay(100);
- input = Serial2.readString();
- if (input.indexOf("OK") >= 0)
- {
- Serial.println("URL CONNECT SUCCESSFULLY");
- Serial.println();
- input = Serial2.readString();
- input.remove(0);
- int count = 0;
- Serial2.write("AT+QHTTPGET=30\r\n");
- input = Serial2.readString();
- while (input.indexOf("+QHTTPGET") < 0)
- {
- Serial.println("WAITING FOR GET COMMAND RESPONSE");
- Serial.println();
- if (count >= 5)
- {
- value = false;
- break;
- }
- count++;
- input = Serial2.readString();
- }
- String GetResponse = input.substring(input.indexOf("+QHTTPGET") + 13, input.indexOf("+QHTTPGET") + 16);
- Serial.print("GET RESPONSE IS:");
- Serial.println(GetResponse);
- Serial.println();
- if (GetResponse == "200")
- {
- Serial.println("GET RESPONSE IS OK");
- Serial.println();
- input = Serial2.readString();
- input.remove(0);
- Serial2.write("AT+QHTTPREAD=30\r\n");
- while (!Serial2.available())
- {
- }
- while (Serial2.available())
- {
- String line = Serial2.readStringUntil('\n');
- Serial.print("line is:" + line); Serial.println();
- line.trim();// remove white/empty space from the line.
- if (!line.length())// if the the line is empty,this is end of headers,//break the while and feed the remaining `Serial2` to the Update.writeStream()
- {
- Serial.println("This Line Was empty");//headers ended
- httpResponseLineCount ++;
- Serial.println("httpResponseLineCount is :" + String(httpResponseLineCount));
- if (httpResponseLineCount == 2)
- {
- Serial.println("This Line Was empty Two Times");//headers ended
- Serial.println("Breaking While() Now");
- httpResponseLineCount = 0;
- break;
- }
- }
- if (line.startsWith("HTTP/1.1"))// Check if the HTTP Response is 200, else break and Exit Update.
- {
- if (line.indexOf("200") < 0)
- {
- Serial.println("Got a non 200 status code from server. Exiting OTA Update.");
- break;
- }
- }
- if (line.startsWith("Content-Length: "))// extract headers here, Start with content length
- {
- contentLength = atol((getHeaderValue(line, "Content-Length: ")).c_str());
- Serial.println("Got " + String(contentLength) + " bytes from server");
- }
- if (line.startsWith("Content-Type: "))// Next, the content type
- {
- String contentType = getHeaderValue(line, "Content-Type: ");
- Serial.println("Got " + contentType + " payload.");
- if (contentType == "text/plain")
- {
- isValidContentType = true;
- }
- }
- }//-------------------------------------while
- Serial.println("contentLength : " + String(contentLength) + ", isValidContentType : " + String(isValidContentType));
- if (contentLength && isValidContentType)// check contentLength and content type
- {
- String GetVersion = Serial2.readStringUntil('#');
- GetVersion = GetVersion.substring(GetVersion.indexOf("$") + 1, GetVersion.indexOf("#"));
- Serial.println("Found Firmware Version is:" + GetVersion);
- if (GetVersion == FirmwareVer)
- {
- Serial.println("Device is already on the latest Firmware Version:" + GetVersion );
- value = false;
- }
- if (GetVersion != FirmwareVer)
- {
- Serial.println("This is a New Firmware Version" + GetVersion );
- value = true;
- }
- }
- else
- {
- Serial.println("There was no content in the response");
- Serial2.flush();
- value = false;
- }
- }
- input = Serial2.readString();
- input.remove(0);
- Serial2.write("AT+QHTTPSTOP\r\n");
- delay(100);
- input = Serial2.readString();
- input.remove(0);
- }
- else
- {
- Serial.println("HTTP RESPONSE ERROR");
- Serial.println();
- input.remove(0);
- value = false;
- }
- }
- else
- {
- Serial.println("URL CONNECT FAILED");
- Serial.println();
- value = false;
- }
- }
- else
- {
- Serial.println("GPRS CONNECT FAILED");
- Serial.println();
- value = false;
- }
- }
- else if (GSM_Network_STS == false)
- {
- value = false;
- }
- }
- if (WiFi.status() == WL_CONNECTED)
- {
- WiFiClient * client = new WiFiClient;
- if (client)
- {
- HTTPClient http;
- if (http.begin( * client, fwurl))
- {
- httpCode = http.GET();
- Serial.print("HTTP_Code Recieved : ");
- Serial.print(httpCode);
- Serial.println();
- delay(1000);
- if (httpCode == HTTP_CODE_OK) // if version received
- {
- payload = http.getString();
- payload = payload.substring(payload.indexOf("$") + 1, payload.indexOf("#"));
- Serial.println("Found Firmware Version is: " + payload );
- Serial.println();
- if (payload == FirmwareVer)
- {
- Serial.printf("\nDevice is already on the latest Firmware Version:%s\n", payload);
- Serial.println();
- value = false;
- }
- else if (payload != FirmwareVer)
- {
- Serial.println("This is a New Firmware Version: " + payload);
- Serial.println();
- value = true;
- }
- }
- else
- {
- Serial.print("Error in Downloading version file:");
- Serial.println(httpCode);
- Serial.println();
- value = false;
- }
- http.end();
- }
- delete client;
- }
- }
- return value;
- }
- //----------------------------------------------------------------------------------
- bool DoFirmwareUpdate()
- {
- Serial.println("<<< ATTENTION !!! DO NOT TURN OFF THE DEVICE... >>>"); Serial.println();
- Serial.println("Updating the Device Firmware Now!"); Serial.println();
- Serial.println("Please Wait untill the Device Restarts"); Serial.println();
- bool value;
- String fwurl = "";
- fwurl = URL_fw_Bin;
- Serial.println(fwurl); Serial.println();
- if (WiFi.status() != WL_CONNECTED)
- {
- Network_Check_GSM();
- if (GSM_Network_STS == true)
- {
- Serial2.write("AT+QIACT?\r\n");
- delay(100);
- input = Serial2.readString();
- if (input.indexOf("OK") >= 0)
- {
- Serial.println("GPRS CONNECT SUCCESSFULLY");
- Serial.println();
- input = Serial2.readString();
- input.remove(0);
- Serial2.print("AT+QHTTPURL=79,30\r\n");
- delay(100);
- input = Serial2.readString();
- if (input.indexOf("CONNECT") >= 0)
- {
- Serial2.print(fwurl);
- delay(100);
- input = Serial2.readString();
- if (input.indexOf("OK") >= 0)
- {
- Serial.println("URL CONNECT SUCCESSFULLY");
- Serial.println();
- input = Serial2.readString();
- input.remove(0);
- int count = 0;
- Serial2.write("AT+QHTTPGET=30\r\n");
- input = Serial2.readString();
- while (input.indexOf("+QHTTPGET") < 0)
- {
- Serial.println("WAITING FOR GET COMMAND RESPONSE");
- Serial.println();
- if (count >= 5)
- {
- break;
- }
- count++;
- input = Serial2.readString();
- }
- String GetResponse = input.substring(input.indexOf("+QHTTPGET") + 13, input.indexOf("+QHTTPGET") + 16);
- Serial.print("GET RESPONSE IS:");
- Serial.println(GetResponse);
- Serial.println();
- if (GetResponse == "200")
- {
- Serial.println("GET RESPONSE IS OK");
- Serial.println();
- input = Serial2.readString();
- input.remove(0);
- httpResponseLineCount = 0;
- Serial2.write("AT+QHTTPREAD=30\r\n");
- while (!Serial2.available())
- {
- }
- while (Serial2.available())
- {
- // read line till /n
- String line = Serial2.readStringUntil('\n');
- // remove space, to check if the line is end of headers
- Serial.print("line:");
- Serial.println(line);
- line.trim();
- // if the the line is empty,
- // this is end of headers
- // break the while and feed the
- // remaining `client` to the
- // Update.writeStream();
- if (!line.length())
- {
- Serial.println("This Line Was empty one times");//headers ended
- Serial.println("httpResponseLineCount is :" + String(httpResponseLineCount));
- if (httpResponseLineCount == 1)
- {
- Serial.println("This Line Was empty two times");//headers ended
- Serial.println("Breaking While() Now");
- httpResponseLineCount = 0;
- break; // and get the OTA started
- }
- httpResponseLineCount ++;
- //break; // and get the OTA started
- }
- // Check if the HTTP Response is 200
- // else break and Exit Update
- if (line.startsWith("HTTP/1.1"))
- {
- if (line.indexOf("200") < 0)
- {
- Serial.println("Got a non 200 status code from server. Exiting OTA Update.");
- break;
- }
- }
- // extract headers here
- // Start with content length
- if (line.startsWith("Content-Length: "))
- {
- contentLength = atol((getHeaderValue(line, "Content-Length: ")).c_str());
- Serial.println("Got " + String(contentLength) + " bytes from server");
- }
- // Next, the content type
- if (line.startsWith("Content-Type: "))
- {
- String contentType = getHeaderValue(line, "Content-Type: ");
- Serial.println("Got " + contentType + " payload.");
- if (contentType == "application/octet-stream")
- {
- isValidContentType = true;
- }
- }
- }//-------------------------------------while
- Serial.println("contentLength : " + String(contentLength) + ", isValidContentType : " + String(isValidContentType));
- // check contentLength and content type
- if (contentLength && isValidContentType)
- {
- // Check if there is enough to OTA Update
- bool canBegin = Update.begin(contentLength, 0, 2, 1, NULL);
- // If yes, begin
- if (canBegin)
- {
- Serial.println("Begin OTA. This may take 2 - 5 mins to complete. Things might be quite for a while.. Patience!");
- // No activity would appear on the Serial monitor
- // So be patient. This may take 2 - 5mins to complete
- size_t written = Update.writeStream(Serial2);
- if (written == contentLength)
- {
- Serial.println("Written : " + String(written) + " successfully");
- }
- else
- {
- Serial.println("Written only : " + String(written) + "/" + String(contentLength) + ". Retry?" );
- // retry??
- // execOTA();
- }
- if (Update.end())
- {
- Serial.println("OTA done!");
- if (Update.isFinished())
- {
- Serial.println("Update successfully completed. Rebooting.");
- ESP.restart();
- }
- else
- {
- Serial.println("Update not finished? Something went wrong!");
- }
- }
- else
- {
- Serial.println("Error Occurred. Error #: " + String(Update.getError()));
- }
- }
- else
- {
- // not enough space to begin OTA
- // Understand the partitions and
- // space availability
- Serial.println("Not enough space to begin OTA");
- Serial2.flush();
- }
- }
- else
- {
- Serial.println("There was no content in the response");
- Serial2.flush();
- }
- }
- input = Serial2.readString();
- input.remove(0);
- Serial2.write("AT+QHTTPSTOP\r\n");
- delay(100);
- input = Serial2.readString();
- input.remove(0);
- }
- else
- {
- Serial.println("HTTP RESPONSE ERROR");
- Serial.println();
- input.remove(0);
- value = false;
- }
- }
- else
- {
- Serial.println("URL CONNECT FAILED");
- Serial.println();
- value = false;
- }
- }
- else
- {
- Serial.println("GPRS CONNECT FAILED");
- Serial.println();
- value = false;
- }
- input = Serial2.readString();
- input.remove(0);
- Serial2.write("AT+QIDEACT=1\r\n");
- delay(100);
- input = Serial2.readString();
- input.remove(0);
- }
- else if (GSM_Network_STS == false)
- {
- value = false;
- }
- }
- if (WiFi.status() == WL_CONNECTED)
- {
- WiFiClient client;
- t_httpUpdate_return ret = httpUpdate.update(client, URL_fw_Bin);
- switch (ret)
- {
- case HTTP_UPDATE_FAILED:
- Serial.printf("HTTP_UPDATE_FAILD Error (%d): %s\n", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str());
- break;
- case HTTP_UPDATE_NO_UPDATES:
- Serial.println("HTTP_UPDATE_NO_UPDATES");
- break;
- case HTTP_UPDATE_OK:
- Serial.println("HTTP_UPDATE_OK");
- break;
- }
- }
- }
- //----------------------------------------------------------------------------------
- String getHeaderValue(String header, String headerName)
- {
- return header.substring(strlen(headerName.c_str()));
- }
- //----------------------------------------------------------------------------------