feat: ota upgrade
This commit is contained in:
parent
a07dfab533
commit
58fbda6f59
2 changed files with 61 additions and 17 deletions
|
@ -16,4 +16,7 @@ lib_deps =
|
|||
tzapu/WiFiManager@^2.0.17
|
||||
bblanchon/ArduinoJson@^7.1.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
|
||||
|
|
|
@ -4,6 +4,10 @@
|
|||
#include <WiFiManager.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <ESP32_C3_TimerInterrupt.h>
|
||||
#include <ArduinoOTA.h>
|
||||
|
||||
#define BRIGHTNESS_HIGH 255
|
||||
#define BRIGHTNESS_LOW 64
|
||||
|
||||
#define DISP_CLK D7
|
||||
#define DISP_SER D8
|
||||
|
@ -136,7 +140,7 @@ void isr_minus() {
|
|||
}
|
||||
}
|
||||
|
||||
bool timer0_isr(void *data) {
|
||||
bool isr_timer0(void *data) {
|
||||
request_update = 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);
|
||||
}
|
||||
digitalWrite(DISP_CLK, HIGH);
|
||||
delay(2);
|
||||
digitalWrite(DISP_CLK, LOW);
|
||||
delay(2);
|
||||
setBrightness(bright);
|
||||
}
|
||||
|
||||
|
@ -179,8 +181,8 @@ void showSpinner(uint8_t bright = 255) {
|
|||
}
|
||||
|
||||
void draw(uint8_t bright = 255, bool show_dot01 = false) {
|
||||
digitalWrite(LED_OPEN, space_open);
|
||||
digitalWrite(LED_CLOSE, !space_open);
|
||||
analogWrite(LED_OPEN, bright * space_open);
|
||||
analogWrite(LED_CLOSE, bright * !space_open);
|
||||
showNumber(counter, bright, show_dot01);
|
||||
}
|
||||
|
||||
|
@ -189,7 +191,7 @@ void reconnect() {
|
|||
Serial.print("Reconnecting to WiFi");
|
||||
WiFi.reconnect();
|
||||
for (uint16_t i = 0; i < 1000 && WiFi.status() != WL_CONNECTED; ++i) {
|
||||
showSpinner(64);
|
||||
showSpinner(BRIGHTNESS_HIGH);
|
||||
delay(50);
|
||||
Serial.print(".");
|
||||
}
|
||||
|
@ -228,29 +230,66 @@ void setup() {
|
|||
pinMode(DISP_CLK, OUTPUT);
|
||||
digitalWrite(DISP_PWM, HIGH);
|
||||
|
||||
showNumber(88, 255, true, true);
|
||||
digitalWrite(LED_OPEN, HIGH);
|
||||
digitalWrite(LED_CLOSE, HIGH);
|
||||
showNumber(88, BRIGHTNESS_HIGH, true, true);
|
||||
analogWrite(LED_OPEN, BRIGHTNESS_HIGH);
|
||||
analogWrite(LED_CLOSE, BRIGHTNESS_HIGH);
|
||||
delay(1000);
|
||||
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();
|
||||
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;
|
||||
request_update = true;
|
||||
draw(16);
|
||||
draw(BRIGHTNESS_LOW);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (just_edited) {
|
||||
just_edited = false;
|
||||
draw(128, true);
|
||||
draw(BRIGHTNESS_HIGH, true);
|
||||
}
|
||||
if (edited && millis() - last_edit > 1000) {
|
||||
edited = false;
|
||||
Serial.println("Edited!");
|
||||
//reconnect();
|
||||
snprintf(buf, 1024, "open value=%d\npeople_now_present value=%d\n", space_open ? 1 : 0, counter);
|
||||
Serial.print("Sending update: ");
|
||||
Serial.println(buf);
|
||||
|
@ -262,7 +301,7 @@ void loop() {
|
|||
request_update = true;
|
||||
}
|
||||
if (request_update) {
|
||||
draw(16, true);
|
||||
draw(BRIGHTNESS_LOW, true);
|
||||
request_update = false;
|
||||
Serial.println("Requesting update!");
|
||||
reconnect();
|
||||
|
@ -291,6 +330,8 @@ void loop() {
|
|||
}
|
||||
|
||||
// Update display
|
||||
draw(16);
|
||||
draw(BRIGHTNESS_LOW);
|
||||
}
|
||||
|
||||
ArduinoOTA.handle();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue