Update wifi.ino
This commit is contained in:
parent
d849f34cd3
commit
9666c3ef44
59
wifi.ino
59
wifi.ino
@ -6,6 +6,8 @@
|
|||||||
#include <WiFiManager.h>
|
#include <WiFiManager.h>
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
|
#define PRODUCT_NAME "TAKEONE"
|
||||||
|
|
||||||
// OLED display setup (on I2C bus 0)
|
// OLED display setup (on I2C bus 0)
|
||||||
#define OLED_SDA 18
|
#define OLED_SDA 18
|
||||||
#define OLED_SCL 19
|
#define OLED_SCL 19
|
||||||
@ -26,24 +28,36 @@ char mqtt_port[6] = "8080";
|
|||||||
char api_token[34] = "YOUR_API_TOKEN";
|
char api_token[34] = "YOUR_API_TOKEN";
|
||||||
bool shouldSaveConfig = false;
|
bool shouldSaveConfig = false;
|
||||||
|
|
||||||
|
// WiFi loss detection
|
||||||
|
unsigned long wifiLostTime = 0;
|
||||||
|
bool portalOpened = false;
|
||||||
|
|
||||||
|
// Button reset
|
||||||
|
#define BOOT_BUTTON 0 // Usually GPIO 0 on ESP32
|
||||||
|
|
||||||
void saveConfigCallback() {
|
void saveConfigCallback() {
|
||||||
|
|
||||||
shouldSaveConfig = true;
|
shouldSaveConfig = true;
|
||||||
display.clearDisplay();
|
display.clearDisplay();
|
||||||
display.setCursor(0, 0);
|
display.setCursor(0, 0);
|
||||||
display.println("Saving config...");
|
display.println("Saving config...");
|
||||||
display.display();
|
display.display();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void showStatus(const String& line1, const String& line2 = "", const String& line3 = "") {
|
void showStatus(const String& line1, const String& line2 = "", const String& line3 = "") {
|
||||||
|
|
||||||
display.clearDisplay();
|
display.clearDisplay();
|
||||||
display.setCursor(0, 0);
|
display.setCursor(0, 0);
|
||||||
display.println(line1);
|
display.println(line1);
|
||||||
if (!line2.isEmpty()) display.println(line2);
|
if (!line2.isEmpty()) display.println(line2);
|
||||||
if (!line3.isEmpty()) display.println(line3);
|
if (!line3.isEmpty()) display.println(line3);
|
||||||
display.display();
|
display.display();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
|
||||||
// Start both I2C buses
|
// Start both I2C buses
|
||||||
@ -89,7 +103,7 @@ void setup() {
|
|||||||
wifiManager.addParameter(&custom_api_token);
|
wifiManager.addParameter(&custom_api_token);
|
||||||
|
|
||||||
showStatus("Connecting WiFi...");
|
showStatus("Connecting WiFi...");
|
||||||
if (!wifiManager.autoConnect("AutoConnectAP", "password")) {
|
if (!wifiManager.autoConnect(PRODUCT_NAME)) {
|
||||||
showStatus("WiFi Connect", "Failed!", "Restarting...");
|
showStatus("WiFi Connect", "Failed!", "Restarting...");
|
||||||
delay(3000);
|
delay(3000);
|
||||||
ESP.restart();
|
ESP.restart();
|
||||||
@ -116,9 +130,48 @@ void setup() {
|
|||||||
// Show final info (truncate to fit OLED)
|
// Show final info (truncate to fit OLED)
|
||||||
String ipStr = WiFi.localIP().toString();
|
String ipStr = WiFi.localIP().toString();
|
||||||
if (ipStr.length() > 15) ipStr = ipStr.substring(0, 15);
|
if (ipStr.length() > 15) ipStr = ipStr.substring(0, 15);
|
||||||
showStatus("WiFi OK: " + ipStr, "MQTT: " + String(mqtt_server), "Port: " + String(mqtt_port));
|
showStatus("IPAD: " + ipStr, "MQTT: " + String(mqtt_server), "Port: " + String(mqtt_port));
|
||||||
|
|
||||||
|
// Button setup
|
||||||
|
pinMode(BOOT_BUTTON, INPUT_PULLUP);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
// You can use I2C_Two for other sensors/devices here
|
|
||||||
|
// WiFi loss detection
|
||||||
|
if (WiFi.status() != WL_CONNECTED) {
|
||||||
|
if (wifiLostTime == 0) {
|
||||||
|
wifiLostTime = millis();
|
||||||
|
} else if (millis() - wifiLostTime > 10000 && !portalOpened) {
|
||||||
|
showStatus("WiFi lost", "Opening portal...");
|
||||||
|
WiFiManager wifiManager;
|
||||||
|
portalOpened = true;
|
||||||
|
wifiManager.startConfigPortal(PRODUCT_NAME);
|
||||||
|
portalOpened = false;
|
||||||
|
wifiLostTime = 0;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
wifiLostTime = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Button hold check
|
||||||
|
static unsigned long buttonPressedTime = 0;
|
||||||
|
static bool buttonActionTaken = false;
|
||||||
|
|
||||||
|
if (digitalRead(BOOT_BUTTON) == LOW) {
|
||||||
|
if (buttonPressedTime == 0) {
|
||||||
|
buttonPressedTime = millis();
|
||||||
|
} else if (millis() - buttonPressedTime > 5000 && !buttonActionTaken) {
|
||||||
|
showStatus("Erasing WiFi", "Opening portal...");
|
||||||
|
WiFi.disconnect(true);
|
||||||
|
WiFiManager wifiManager;
|
||||||
|
wifiManager.resetSettings();
|
||||||
|
wifiManager.startConfigPortal("AutoConnectAP", "12345678");
|
||||||
|
buttonActionTaken = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
buttonPressedTime = 0;
|
||||||
|
buttonActionTaken = false;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user