Last active
April 20, 2023 18:49
-
-
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" | |
| #define DEBUG | |
| //const char* downloadHost = "firebasestorage.googleapis.com"; | |
| const int16_t httpsPort = 443; | |
| //const char* downloadEnding = "?alt=media"; | |
| enum updateResult_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_t checkUpdates(const char* downloadHost, const char* firmwarelink){//OTA_OK, OTA_CONNFAIL, OTA_UPDFAIL, OTA_NOFIRMWARE, OTA_NONEWFIRMWARE | |
| BearSSL::WiFiClientSecure client; | |
| client.setInsecure(); | |
| client.setTimeout(1000); | |
| #ifdef DEBUG | |
| Serial.print("connecting to "); | |
| Serial.println(downloadHost); | |
| #endif | |
| if (!client.connect(downloadHost, httpsPort)) { | |
| #ifdef DEBUG | |
| Serial.println("connection failed"); | |
| #endif | |
| return OTA_CONNFAIL; | |
| }else{ | |
| #ifdef DEBUG | |
| 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"); | |
| #ifdef DEBUG | |
| Serial.println("request sent"); | |
| #endif | |
| while (client.connected()) { | |
| String line = client.readStringUntil('\n'); | |
| if (line.indexOf("404") > 0) { | |
| #ifdef DEBUG | |
| Serial.println("firmware not found"); | |
| Serial.println(line); | |
| #endif | |
| return OTA_NOFIRMWARE; | |
| } | |
| if (line == "\r") { | |
| #ifdef DEBUG | |
| Serial.println("headers received"); | |
| #endif | |
| break; | |
| } | |
| } | |
| #ifdef DEBUG | |
| Serial.println("firmware found"); | |
| #endif | |
| HTTPClient http; | |
| 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)){ | |
| #ifdef DEBUG | |
| Serial.println("http client started"); | |
| #endif | |
| int16_t httpCode = http.GET(); | |
| if (httpCode == HTTP_CODE_OK){ | |
| const int len = http.getSize(); | |
| if (len > 0){ | |
| if (len != ESP.getSketchSize()){ | |
| #ifdef DEBUG | |
| 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(); | |
| return OTA_OK; | |
| }else{ | |
| #ifdef DEBUG | |
| Serial.println("no new firmware found"); | |
| #endif | |
| http.end(); | |
| return OTA_NONEWFIRMWARE; | |
| } | |
| }else{ | |
| #ifdef DEBUG | |
| Serial.println("len < 0"); | |
| #endif | |
| http.end(); | |
| return OTA_UPDFAIL; | |
| } | |
| } | |
| #ifdef DEBUG | |
| Serial.println(); | |
| Serial.print("HTTP_CODE: "); | |
| Serial.print(httpCode); | |
| Serial.println(); | |
| #endif | |
| http.end(); | |
| return OTA_CONNFAIL; | |
| }else{ | |
| #ifdef DEBUG | |
| Serial.println("http client fail"); | |
| #endif | |
| http.end(); | |
| return OTA_CONNFAIL; | |
| } | |
| } | |
| return OTA_CONNFAIL; | |
| } | |
| updateResult_t startUpdate(const char* downloadHost, const char* firmwarelink){//OTA_OK, OTA_CONNFAIL, OTA_UPDFAIL, OTA_NOFIRMWARE, OTA_NONEWFIRMWARE | |
| int size = strlen(downloadHost) + strlen(firmwarelink) + 10; | |
| char link[size]; | |
| snprintf(link, size, "https://%s%s", downloadHost, firmwarelink); | |
| #ifdef DEBUG | |
| Serial.print("Starting OTA from: "); | |
| Serial.println(link); | |
| #endif | |
| BearSSL::WiFiClientSecure client; | |
| client.setInsecure(); | |
| client.setTimeout(1000); | |
| #ifdef DEBUG | |
| Serial.print("connecting to "); | |
| Serial.println(downloadHost); | |
| #endif | |
| if (!client.connect(downloadHost, httpsPort)) { | |
| #ifdef DEBUG | |
| Serial.println("connection failed"); | |
| #endif | |
| return OTA_CONNFAIL; | |
| }else{ | |
| #ifdef DEBUG | |
| Serial.println("connected!"); | |
| #endif | |
| } | |
| auto ret = ESPhttpUpdate.update(client, link); | |
| #ifdef DEBUG | |
| Serial.println("update failed, result: "); | |
| Serial.print((int) ret); | |
| #endif | |
| return OTA_UPDFAIL; | |
| } | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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);
}