feat: ota upgrade

This commit is contained in:
s3lph 2024-12-21 22:44:18 +01:00
parent a07dfab533
commit 58fbda6f59
Signed by: s3lph
GPG key ID: 0AA29A52FB33CFB5
2 changed files with 61 additions and 17 deletions

View file

@ -16,4 +16,7 @@ lib_deps =
tzapu/WiFiManager@^2.0.17 tzapu/WiFiManager@^2.0.17
bblanchon/ArduinoJson@^7.1.0 bblanchon/ArduinoJson@^7.1.0
khoih-prog/ESP32_C3_TimerInterrupt@^1.8.0 khoih-prog/ESP32_C3_TimerInterrupt@^1.8.0
build_flags = -Wall -Werror build_flags =
-Wall -Werror
upload_protocol = espota
upload_port = 10.20.0.119

View file

@ -4,6 +4,10 @@
#include <WiFiManager.h> #include <WiFiManager.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <ESP32_C3_TimerInterrupt.h> #include <ESP32_C3_TimerInterrupt.h>
#include <ArduinoOTA.h>
#define BRIGHTNESS_HIGH 255
#define BRIGHTNESS_LOW 64
#define DISP_CLK D7 #define DISP_CLK D7
#define DISP_SER D8 #define DISP_SER D8
@ -136,7 +140,7 @@ void isr_minus() {
} }
} }
bool timer0_isr(void *data) { bool isr_timer0(void *data) {
request_update = true; request_update = true;
return true; return true;
} }
@ -162,9 +166,7 @@ void showNumber(int8_t number, uint8_t bright = 255, bool show_dot01 = false, bo
shiftOut(DISP_SER, DISP_CLK, MSBFIRST, digits10[number/10] | dot10 * show_dot10); shiftOut(DISP_SER, DISP_CLK, MSBFIRST, digits10[number/10] | dot10 * show_dot10);
} }
digitalWrite(DISP_CLK, HIGH); digitalWrite(DISP_CLK, HIGH);
delay(2);
digitalWrite(DISP_CLK, LOW); digitalWrite(DISP_CLK, LOW);
delay(2);
setBrightness(bright); setBrightness(bright);
} }
@ -179,8 +181,8 @@ void showSpinner(uint8_t bright = 255) {
} }
void draw(uint8_t bright = 255, bool show_dot01 = false) { void draw(uint8_t bright = 255, bool show_dot01 = false) {
digitalWrite(LED_OPEN, space_open); analogWrite(LED_OPEN, bright * space_open);
digitalWrite(LED_CLOSE, !space_open); analogWrite(LED_CLOSE, bright * !space_open);
showNumber(counter, bright, show_dot01); showNumber(counter, bright, show_dot01);
} }
@ -189,7 +191,7 @@ void reconnect() {
Serial.print("Reconnecting to WiFi"); Serial.print("Reconnecting to WiFi");
WiFi.reconnect(); WiFi.reconnect();
for (uint16_t i = 0; i < 1000 && WiFi.status() != WL_CONNECTED; ++i) { for (uint16_t i = 0; i < 1000 && WiFi.status() != WL_CONNECTED; ++i) {
showSpinner(64); showSpinner(BRIGHTNESS_HIGH);
delay(50); delay(50);
Serial.print("."); Serial.print(".");
} }
@ -228,29 +230,66 @@ void setup() {
pinMode(DISP_CLK, OUTPUT); pinMode(DISP_CLK, OUTPUT);
digitalWrite(DISP_PWM, HIGH); digitalWrite(DISP_PWM, HIGH);
showNumber(88, 255, true, true); showNumber(88, BRIGHTNESS_HIGH, true, true);
digitalWrite(LED_OPEN, HIGH); analogWrite(LED_OPEN, BRIGHTNESS_HIGH);
digitalWrite(LED_CLOSE, HIGH); analogWrite(LED_CLOSE, BRIGHTNESS_HIGH);
delay(1000);
WiFi.setHostname("spaceapibox"); WiFi.setHostname("spaceapibox");
if (!wifiManager.autoConnect("spaceapibox", "12345678")) { if (!digitalRead(SW_OPEN) && !wifiManager.startConfigPortal("spaceapibox", "12345678")) {
ESP.restart();
while (true);
} else if (!wifiManager.autoConnect("spaceapibox", "12345678")) {
ESP.restart(); ESP.restart();
while (true); while (true);
} }
ITimer0.attachInterruptInterval(60 * 1000 * 1000, timer0_isr); ArduinoOTA
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
} else { // U_SPIFFS
type = "filesystem";
}
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);
})
.onEnd([]() {
Serial.println("\nEnd");
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
})
.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();
ITimer0.attachInterruptInterval(60 * 1000 * 1000, isr_timer0);
edited = false; edited = false;
request_update = true; request_update = true;
draw(16); draw(BRIGHTNESS_LOW);
} }
void loop() { void loop() {
if (just_edited) { if (just_edited) {
just_edited = false; just_edited = false;
draw(128, true); draw(BRIGHTNESS_HIGH, true);
} }
if (edited && millis() - last_edit > 1000) { if (edited && millis() - last_edit > 1000) {
edited = false; edited = false;
Serial.println("Edited!"); Serial.println("Edited!");
//reconnect();
snprintf(buf, 1024, "open value=%d\npeople_now_present value=%d\n", space_open ? 1 : 0, counter); snprintf(buf, 1024, "open value=%d\npeople_now_present value=%d\n", space_open ? 1 : 0, counter);
Serial.print("Sending update: "); Serial.print("Sending update: ");
Serial.println(buf); Serial.println(buf);
@ -262,7 +301,7 @@ void loop() {
request_update = true; request_update = true;
} }
if (request_update) { if (request_update) {
draw(16, true); draw(BRIGHTNESS_LOW, true);
request_update = false; request_update = false;
Serial.println("Requesting update!"); Serial.println("Requesting update!");
reconnect(); reconnect();
@ -291,6 +330,8 @@ void loop() {
} }
// Update display // Update display
draw(16); draw(BRIGHTNESS_LOW);
} }
ArduinoOTA.handle();
} }