move ota stuff into better functions

This commit is contained in:
interfisch 2022-07-25 12:53:27 +02:00
parent ba3a0acbce
commit 799f21ab56
3 changed files with 39 additions and 35 deletions

View File

@ -1,12 +1,12 @@
#include <Arduino.h>
#include <WiFi.h>
#include <ArduinoOTA.h>
#include "simpleota.h"
#define OTA_WIFI_SSID "Chaos-West temp alternative"
#define OTA_WIFI_PASSWORD ""
#define OTA_WAIT_TIMEOUT 1000 // in 0.1s increments -> 10s
void normal_loop();
void normal_setup();
void checkOTA();
bool initOTA();
@ -34,6 +34,7 @@ void checkOTA() {
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("no OTA WiFi found, proceed normal boot");
otaMode = NONE;
WiFi.disconnect();
return;
}
@ -41,28 +42,22 @@ void checkOTA() {
}
void normal_setup() {
Serial.println("Booting normal");
WiFi.disconnect();
WiFi.mode(WIFI_STA);
WiFi.disconnect();
Serial.printf("wifi channel: %d\n", WiFi.channel());
}
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println();
Serial.println("Initializing...");
Serial.println("Initializing");
checkOTA();
if(!initOTA()) { //initialize ota when ota enabled
normal_setup();
if(initOTA()) { //initialize ota if ota enabled
return; //if ota do nothing else for setup
}
Serial.println("Booting normal");
}
bool initOTA() {
@ -108,38 +103,42 @@ bool initOTA() {
}
void loop() {
bool ota_loop() {
if (otaMode != NONE) {
ArduinoOTA.handle();
if (otaMode != NONE) {
ArduinoOTA.handle();
if(otaMode == WAITING) {
static long mil = millis();
static boolean huehott = false;
if(otaMode == WAITING) {
static long mil = millis();
static boolean huehott = false;
if(millis() - mil > 100) {
huehott = !huehott;
mil = millis();
if(millis() - mil > 100) {
huehott = !huehott;
mil = millis();
otaWaitCounter++;
if(otaWaitCounter >= OTA_WAIT_TIMEOUT) {
Serial.println("OTA wait timeout, proceeding normal boot");
otaMode = NONE;
normal_setup();
}
}
}
otaWaitCounter++;
if(otaWaitCounter >= OTA_WAIT_TIMEOUT) {
Serial.println("OTA wait timeout, proceeding normal boot");
otaMode = NONE;
normal_setup();
}
}
}
return true;
} else {
normal_loop();
return false;
}
}
void normal_loop() {
void loop() {
if (ota_loop()) {
return; //just wait for ota
}
static unsigned long last_print=0;
if (millis()-last_print>1000) {
last_print=millis();
Serial.println("Loop");
}
}

1
src/simpleota.cpp Normal file
View File

@ -0,0 +1 @@
#include "simpleota.h"

4
src/simpleota.h Normal file
View File

@ -0,0 +1,4 @@
#ifndef simpleota_h
#define simpleota_h
#endif