Files
Patient-counter-esp8266/src/myOTA.cpp
T

77 lines
2.4 KiB
C++

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <networkCredentials.h>
#include <version.h>
#include <ArduinoOTA.h>
#include <ESP8266httpUpdate.h>
#include <WiFiClientSecure.h>
void setupOTALocal() {
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
ArduinoOTA.onStart([]() {
Serial.println("Start");
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void handleOTALocal() {
ArduinoOTA.handle();
}
void handleOTARemote() {
WiFiClient client;
const uint8_t fingerprint[20] = {
0xe9, 0x4e, 0x54, 0xa9, 0x30, 0x86, 0x3d, 0x53, 0x5b, 0xa0, 0xd2, 0xd3, 0xa5, 0xdd, 0x0d, 0xe3, 0xbd, 0xa8, 0xc9, 0xc2,
};
// client.setFingerprint(fingerprint);
// client.setInsecure();
const char* host = "www.github.com";
const int httpsPort = 80;
Serial.println(host);
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
} else {
Serial.println("connection worked");
}
t_httpUpdate_return ret = ESPhttpUpdate.update(client, "http://github.com/bendtherules/Patient-counter-esp8266/raw/main ", VERSION_SHORT);
switch(ret) {
case HTTP_UPDATE_FAILED:
Serial.print("[update] Update failed. Reason - ");
Serial.println(ESPhttpUpdate.getLastErrorString());
break;
case HTTP_UPDATE_NO_UPDATES:
Serial.println("[update] Update no Update.");
break;
case HTTP_UPDATE_OK:
Serial.println("[update] Update ok."); // may not be called since we reboot the ESP
break;
}
}