wallart/network.cpp

64 lines
1.4 KiB
C++
Raw Permalink Normal View History

2022-07-17 22:22:16 -06:00
#include <FastLED.h>
#include <WiFiManager.h>
2023-12-06 10:50:31 -07:00
#include <WiFiManagerTz.h>
2022-07-17 22:22:16 -06:00
#include <esp_wifi.h>
2023-12-08 20:37:55 -07:00
#include <esp_sntp.h>
2023-01-06 12:50:46 -07:00
#include <HTTPClient.h>
2022-07-17 22:22:16 -06:00
#include <WiFiClientSecure.h>
#include "network.h"
WiFiManager wfm;
2023-09-04 16:30:21 -06:00
void network_reset() {
2023-12-06 10:50:31 -07:00
Serial.println("Resetting network");
2023-09-04 16:30:21 -06:00
wfm.resetSettings();
}
2023-12-08 20:37:55 -07:00
bool time_was_accurate_once = false;
bool clock_is_set() {
return time_was_accurate_once;
}
void on_time_available(struct timeval *t) {
2023-12-06 10:50:31 -07:00
struct tm timeInfo;
getLocalTime(&timeInfo, 1000);
Serial.println(&timeInfo, "%A, %B %d %Y %H:%M:%S %Z %z ");
2023-12-08 20:37:55 -07:00
time_was_accurate_once = true;
2023-12-06 10:50:31 -07:00
}
2022-07-17 22:22:16 -06:00
void network_setup(char *password) {
2023-12-06 10:50:31 -07:00
String hostname = "WallArt";
WiFiManagerNS::NTP::onTimeAvailable(&on_time_available);
2024-06-09 21:53:19 -06:00
WiFiManagerNS::init(&wfm, nullptr);
2022-07-17 22:22:16 -06:00
std::vector<const char *> menu = {"wifi", "info", "custom", "param", "sep", "update", "restart", "exit"};
2023-12-06 10:50:31 -07:00
wfm.setMenu(menu);
2022-07-17 22:22:16 -06:00
wfm.setConfigPortalBlocking(false);
wfm.setHostname(hostname);
wfm.autoConnect(hostname.c_str(), password);
}
bool connected() {
return WiFi.status() == WL_CONNECTED;
}
2023-12-06 10:50:31 -07:00
bool timeConfigured = false;
2022-07-17 22:22:16 -06:00
void pause(uint32_t dwMs) {
2023-12-06 10:50:31 -07:00
if (connected() && !timeConfigured) {
WiFiManagerNS::configTime();
timeConfigured = true;
}
if (!digitalRead(RESET_PIN)) {
network_reset();
}
2022-07-17 22:22:16 -06:00
for (uint32_t t = 0; t < dwMs; t += 10) {
wfm.process();
2022-07-18 07:42:52 -06:00
digitalWrite(LED_BUILTIN, !connected());
2022-07-17 22:22:16 -06:00
delay(10);
}
}