I'm trying to implement some code I found on the net for doing updates to the Arduino via HTTP. The author advises putting the bin file along with a text file containing the version number in an HTTP accessible folder. The Arduino then downloads the contents of the version file, converts it to an integer, and compares it to the version number of the current firmware, before deciding whether to download the update.
I'm hitting a snag at conversion from string to integer. If I use
String firmwareVersionAvailableString = httpClient.getString();
and
long firmwareVersionAvailable = firmwareVersionAvailableString.toInt();
I get a zero for the integer.
I tried to troubleshoot by checking the string with
Serial.println( firmwareVersionAvailableString );
before conversion and everything looks okay. I also checked the conversion by bypassing the HTTP and manually entering the string with
String firmwareVersionAvailableString = "2020010101";
and the string converts fine in that case.
With those options being eliminated, I'm guessing that there's something in my text file that's goofing up the string in a way I can't see. But I have no idea what it could be. Any help would be appreciated.
The original code is at Self-updating OTA firmware for ESP8266 – OppoverBakke
My version of the code is
void otaUpdateCheck() {
String firmwareURL = String( SECRET_otaUpdateLocation );
firmwareURL.concat( unitFirmwareCode );
String firmwareVersionURL = firmwareURL;
firmwareVersionURL.concat( ".version" );
Serial.print( "checking for firmware updates for " );
Serial.println( unitFirmwareCode );
Serial.print( "at " );
Serial.println( firmwareVersionURL );
Serial.println();
HTTPClient httpClient;
httpClient.begin( clientWiFiOTAUpdate, firmwareVersionURL );
int httpCode = httpClient.GET();
if( httpCode == 200 ) {
String firmwareVersionAvailableString = httpClient.getString();
long firmwareVersionAvailable = firmwareVersionAvailableString.toInt();
Serial.print( "current firmware version: " );
Serial.println( firmwareVersionCurrent );
Serial.print( "available firmware version: " );
Serial.println( firmwareVersionAvailable );
Serial.println();
if( firmwareVersionAvailable > firmwareVersionCurrent ) {
Serial.println( "new version available" );
Serial.println( "performing update" );
String firmwareImageURL = firmwareURL;
firmwareImageURL.concat( ".bin" );
#ifdef ESP8266
t_httpUpdate_return ret = ESPhttpUpdate.update( clientWiFiOTAUpdate, firmwareImageURL );
switch(ret) {
case HTTP_UPDATE_FAILED:
Serial.printf("HTTP_UPDATE_FAILD Error (%d): %s", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
break;
case HTTP_UPDATE_NO_UPDATES:
Serial.println("HTTP_UPDATE_NO_UPDATES");
break;
}
#endif
#ifdef ESP32
t_httpUpdate_return ret = httpUpdate.update( clientWiFiOTAUpdate, firmwareImageURL );
switch(ret) {
case HTTP_UPDATE_FAILED:
Serial.printf("HTTP_UPDATE_FAILD Error (%d): %s", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str());
break;
case HTTP_UPDATE_NO_UPDATES:
Serial.println("HTTP_UPDATE_NO_UPDATES");
break;
}
#endif
}
else {
Serial.println( "already latest version" );
Serial.println( "skipping update" );
}
Serial.println();
}
else {
Serial.println( "firmware version check failed" );
Serial.print( "HTTP response code " );
Serial.println( httpCode );
Serial.println();
}
httpClient.end();
}