Instantly share code, notes, and snippets.
Save Tarik2142/52e2a955787c31e542e5fc7a46223086 to your computer and use it in GitHub Desktop.
esp8266 https OTA from firebase etc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
// esp8266 https OTA functoins | |
// 25.01.2020 Первая версия | |
// 27.02.2020 оптимизирована проверка обновлений, теперь можно работать с любым сайтом. | |
#include"WiFiClientSecure.h" | |
#include"ESP8266httpUpdate.h" | |
#defineDEBUG | |
//const char* downloadHost = "firebasestorage.googleapis.com"; | |
constint16_thttpsPort=443; | |
//const char* downloadEnding = "?alt=media"; | |
enumupdateResult_t :uint8_t {OTA_OK,OTA_CONNFAIL,OTA_UPDFAIL,OTA_NOFIRMWARE,OTA_NONEWFIRMWARE};//0, 1, 2, 3, 4 | |
///v0/b/tarik2142cloud.appspot.com/o/esp8266%2Fota%2Ffirmware.bin?alt=media | |
updateResult_tcheckUpdates(constchar*downloadHost,constchar*firmwarelink){//OTA_OK, OTA_CONNFAIL, OTA_UPDFAIL, OTA_NOFIRMWARE, OTA_NONEWFIRMWARE | |
BearSSL::WiFiClientSecureclient; | |
client.setInsecure(); | |
client.setTimeout(1000); | |
#ifdefDEBUG | |
Serial.print("connecting to "); | |
Serial.println(downloadHost); | |
#endif | |
if (!client.connect(downloadHost,httpsPort)) { | |
#ifdefDEBUG | |
Serial.println("connection failed"); | |
#endif | |
returnOTA_CONNFAIL; | |
}else{ | |
#ifdefDEBUG | |
Serial.println("connected!"); | |
#endif | |
client.print(String("GET ")+firmwarelink+" HTTP/1.1\r\n"+ | |
"Host: "+downloadHost+"\r\n"+ | |
"User-Agent: esp8266\r\n"+ | |
"Connection: close\r\n\r\n"); | |
#ifdefDEBUG | |
Serial.println("request sent"); | |
#endif | |
while (client.connected()) { | |
Stringline=client.readStringUntil('\n'); | |
if (line.indexOf("404")>0) { | |
#ifdefDEBUG | |
Serial.println("firmware not found"); | |
Serial.println(line); | |
#endif | |
returnOTA_NOFIRMWARE; | |
} | |
if (line=="\r") { | |
#ifdefDEBUG | |
Serial.println("headers received"); | |
#endif | |
break; | |
} | |
} | |
#ifdefDEBUG | |
Serial.println("firmware found"); | |
#endif | |
HTTPClienthttp; | |
client.stop(); | |
//const int size = strlen(firmwarelink) + strlen(downloadEnding) + 1; | |
//char link[size]; | |
//snprintf(link, size, "%s%s", firmwarelink, downloadEnding); | |
if (http.begin(client,downloadHost,httpsPort,firmwarelink)){ | |
#ifdefDEBUG | |
Serial.println("http client started"); | |
#endif | |
int16_thttpCode=http.GET(); | |
if (httpCode==HTTP_CODE_OK){ | |
constintlen=http.getSize(); | |
if (len>0){ | |
if (len!=ESP.getSketchSize()){ | |
#ifdefDEBUG | |
Serial.println("found new firmware"); | |
Serial.print("new firmware len = "); | |
Serial.print(len); | |
Serial.println(); | |
Serial.print("old firmware len = "); | |
Serial.print(ESP.getSketchSize()); | |
Serial.println(); | |
#endif | |
http.end(); | |
returnOTA_OK; | |
}else{ | |
#ifdefDEBUG | |
Serial.println("no new firmware found"); | |
#endif | |
http.end(); | |
returnOTA_NONEWFIRMWARE; | |
} | |
}else{ | |
#ifdefDEBUG | |
Serial.println("len < 0"); | |
#endif | |
http.end(); | |
returnOTA_UPDFAIL; | |
} | |
} | |
#ifdefDEBUG | |
Serial.println(); | |
Serial.print("HTTP_CODE: "); | |
Serial.print(httpCode); | |
Serial.println(); | |
#endif | |
http.end(); | |
returnOTA_CONNFAIL; | |
}else{ | |
#ifdefDEBUG | |
Serial.println("http client fail"); | |
#endif | |
http.end(); | |
returnOTA_CONNFAIL; | |
} | |
} | |
returnOTA_CONNFAIL; | |
} | |
updateResult_tstartUpdate(constchar*downloadHost,constchar*firmwarelink){//OTA_OK, OTA_CONNFAIL, OTA_UPDFAIL, OTA_NOFIRMWARE, OTA_NONEWFIRMWARE | |
intsize=strlen(downloadHost)+strlen(firmwarelink)+10; | |
charlink[size]; | |
snprintf(link,size,"https://%s%s",downloadHost,firmwarelink); | |
#ifdefDEBUG | |
Serial.print("Starting OTA from: "); | |
Serial.println(link); | |
#endif | |
BearSSL::WiFiClientSecureclient; | |
client.setInsecure(); | |
client.setTimeout(1000); | |
#ifdefDEBUG | |
Serial.print("connecting to "); | |
Serial.println(downloadHost); | |
#endif | |
if (!client.connect(downloadHost,httpsPort)) { | |
#ifdefDEBUG | |
Serial.println("connection failed"); | |
#endif | |
returnOTA_CONNFAIL; | |
}else{ | |
#ifdefDEBUG | |
Serial.println("connected!"); | |
#endif | |
} | |
autoret=ESPhttpUpdate.update(client,link); | |
#ifdefDEBUG | |
Serial.println("update failed, result: "); | |
Serial.print((int)ret); | |
#endif | |
returnOTA_UPDFAIL; | |
} |
Author
Tarik2142 commentedFeb 27, 2020 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
usage example:
#include "update.h"
const char* host = "firebasestorage.googleapis.com";
const char* url = "/v0/b/tarik2142cloud.appspot.com/o/esp8266%2Fota%2Fled_time.bin?alt=media";
if(checkUpdates(host, url) == OTA_OK){
startUpdate(host, url);
}
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment