Move WiFi setup to OTA app

This commit is contained in:
Correl Roush 2023-01-28 21:51:39 -05:00
parent 3a96227231
commit 0b25d08d66

View file

@ -16,6 +16,42 @@ public:
class OTA: public App {
public:
OTA() {
M5.Display.print("Connecting to WiFi");
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
M5.Display.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
M5.Display.print(".");
}
M5.Display.println("");
M5.Display.print("Connected to ");
M5.Display.println(WIFI_SSID);
M5.Display.print("IP address: ");
M5.Display.println(WiFi.localIP());
M5.Display.print("Syncing time");
configTzTime(NTP_TIMEZONE, NTP_SERVER1, NTP_SERVER2, NTP_SERVER3);
while (sntp_get_sync_status() != SNTP_SYNC_STATUS_COMPLETED) {
M5.Display.print('.');
delay(1000);
}
M5.Display.println("");
time_t t = time(nullptr)+1; // Advance one second.
while (t > time(nullptr)); /// Synchronization in seconds
M5.Rtc.setDateTime( gmtime( &t ) );
server = new AsyncWebServer(80);
server->on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", "Hi! I am ESP32.");
});
AsyncElegantOTA.begin(server); // Start ElegantOTA
server->begin();
M5.Display.println("HTTP server started");
M5.Display.setEpdMode(epd_text);
M5.Display.startWrite();
M5.Display.clearDisplay(TFT_WHITE);
@ -29,33 +65,23 @@ public:
"http://%s/update\n",
WiFi.localIP().toString());
button_power.initButton(&M5.Display, M5.Display.width() / 2, M5.Display.height() / 2, 200, 100, TFT_BLACK, TFT_LIGHTGRAY, TFT_BLACK, "Power", 3, 3);
button_power.drawButton();
M5.Display.endWrite();
}
App::Choices loop() {
auto t = M5.Touch.getDetail();
if (t.wasPressed()) {
if (button_power.contains(t.x, t.y)) {
button_power.press(true);
} else {
button_power.press(false);
}
} else {
button_power.press(false);
}
if (button_power.justReleased()) {
M5.Display.clearDisplay(TFT_WHITE);
M5.Power.powerOff();
}
return App::OTA;
}
void shutdown() {
M5.Display.printf("Disconnecting WiFi...");
M5.Display.print("Stopping web server...");
server->end();
delete server;
M5.Display.println("ok");
M5.Display.print("Disconnecting WiFi...");
WiFi.disconnect();
M5.Display.println("ok");
delay(2000);
}
protected:
LGFX_Button button_power;
AsyncWebServer* server;
};
class System: public App {
@ -177,7 +203,6 @@ public:
}
};
AsyncWebServer server(80);
App* app;
App::Choices current_app;
@ -189,41 +214,6 @@ void setup(void) {
M5.Display.setCursor(0, M5.Display.height() / 4);
M5.Display.println("Starting up...");
M5.Display.print("Connecting to WiFi");
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
M5.Display.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
M5.Display.print(".");
}
M5.Display.println("");
M5.Display.print("Connected to ");
M5.Display.println(WIFI_SSID);
M5.Display.print("IP address: ");
M5.Display.println(WiFi.localIP());
M5.Display.print("Syncing time");
configTzTime(NTP_TIMEZONE, NTP_SERVER1, NTP_SERVER2, NTP_SERVER3);
while (sntp_get_sync_status() != SNTP_SYNC_STATUS_COMPLETED) {
M5.Display.print('.');
delay(1000);
}
M5.Display.println("");
time_t t = time(nullptr)+1; // Advance one second.
while (t > time(nullptr)); /// Synchronization in seconds
M5.Rtc.setDateTime( gmtime( &t ) );
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", "Hi! I am ESP32.");
});
AsyncElegantOTA.begin(&server); // Start ElegantOTA
server.begin();
M5.Display.println("HTTP server started");
app = new Clock;
current_app = App::Clock;
}