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 <Arduino.h>
#include <WiFi.h> #include <WiFi.h>
#include <ArduinoOTA.h> #include <ArduinoOTA.h>
#include "simpleota.h"
#define OTA_WIFI_SSID "Chaos-West temp alternative" #define OTA_WIFI_SSID "Chaos-West temp alternative"
#define OTA_WIFI_PASSWORD "" #define OTA_WIFI_PASSWORD ""
#define OTA_WAIT_TIMEOUT 1000 // in 0.1s increments -> 10s #define OTA_WAIT_TIMEOUT 1000 // in 0.1s increments -> 10s
void normal_loop();
void normal_setup(); void normal_setup();
void checkOTA(); void checkOTA();
bool initOTA(); bool initOTA();
@ -34,6 +34,7 @@ void checkOTA() {
while (WiFi.waitForConnectResult() != WL_CONNECTED) { while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("no OTA WiFi found, proceed normal boot"); Serial.println("no OTA WiFi found, proceed normal boot");
otaMode = NONE; otaMode = NONE;
WiFi.disconnect();
return; return;
} }
@ -41,28 +42,22 @@ void checkOTA() {
} }
void normal_setup() { 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() { void setup() {
Serial.begin(115200); Serial.begin(115200);
delay(1000);
Serial.println(); Serial.println();
Serial.println("Initializing..."); Serial.println("Initializing");
checkOTA(); checkOTA();
if(!initOTA()) { //initialize ota when ota enabled if(initOTA()) { //initialize ota if ota enabled
normal_setup(); return; //if ota do nothing else for setup
} }
Serial.println("Booting normal");
} }
bool initOTA() { bool initOTA() {
@ -108,8 +103,7 @@ bool initOTA() {
} }
void loop() { bool ota_loop() {
if (otaMode != NONE) { if (otaMode != NONE) {
ArduinoOTA.handle(); ArduinoOTA.handle();
@ -129,17 +123,22 @@ void loop() {
} }
} }
} }
return true;
} else { } else {
normal_loop(); return false;
} }
} }
void normal_loop() { void loop() {
if (ota_loop()) {
return; //just wait for ota
}
static unsigned long last_print=0; static unsigned long last_print=0;
if (millis()-last_print>1000) { if (millis()-last_print>1000) {
last_print=millis(); last_print=millis();
Serial.println("Loop"); 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