2018-11-26 11:58:15 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the "bluetoothheater" distribution
|
|
|
|
* (https://gitlab.com/mrjones.id.au/bluetoothheater)
|
|
|
|
*
|
2019-01-14 20:03:35 +00:00
|
|
|
* Copyright (C) 2019 Ray Jones
|
|
|
|
* Copyright (C) 2019 James Clark
|
2018-11-26 11:58:15 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-10-30 08:00:19 +00:00
|
|
|
// Should be working - Jimmy C
|
2018-10-27 23:14:34 +00:00
|
|
|
#include "BTCWifi.h"
|
2018-12-16 07:34:39 +00:00
|
|
|
#include "../Utility/DebugPort.h"
|
2019-01-11 05:40:25 +00:00
|
|
|
#include <DNSServer.h>
|
2019-01-16 09:22:17 +00:00
|
|
|
#include "../OLED/ScreenManager.h"
|
2019-01-12 21:32:13 +00:00
|
|
|
#include "esp_system.h"
|
2019-01-13 21:24:13 +00:00
|
|
|
#include <Preferences.h>
|
2019-03-24 05:48:03 +00:00
|
|
|
#include "../Utility/NVStorage.h"
|
2019-07-07 07:18:38 +00:00
|
|
|
#include "../Libraries/WiFiManager-dev/WiFiManager.h"
|
2019-01-13 21:24:13 +00:00
|
|
|
|
2019-02-26 08:38:30 +00:00
|
|
|
#define USE_AP
|
|
|
|
|
2019-01-13 21:24:13 +00:00
|
|
|
// function to control the behaviour upon reboot if no wifi manager credentials exist
|
|
|
|
// or connection fails
|
|
|
|
void prepBootIntoConfigPortal(bool state);
|
|
|
|
bool shouldBootIntoConfigPortal();
|
2019-01-14 05:48:33 +00:00
|
|
|
void saveParamsCallback();
|
|
|
|
void APstartedCallback(WiFiManager*);
|
2018-10-27 23:14:34 +00:00
|
|
|
|
|
|
|
WiFiManager wm;
|
|
|
|
|
2019-01-14 20:03:35 +00:00
|
|
|
bool isPortalAP = false; // true if config portal is running
|
|
|
|
bool isSTA = false; // true if connected to an access point
|
|
|
|
int TRIG_PIN; // pin that triggers the configuration portal when set to LOW
|
|
|
|
unsigned restartServer = 0; // set to time of portal reconfig - will cause reboot a while later
|
2019-01-18 03:34:32 +00:00
|
|
|
char MACstr[2][20]; // MACstr[0] STA, MACstr[1] = AP
|
|
|
|
int wifiButtonState = 0;
|
2019-07-15 09:56:36 +00:00
|
|
|
unsigned long WifiReconnectHoldoff = 0;
|
2018-10-28 03:03:44 +00:00
|
|
|
|
2019-01-16 09:22:17 +00:00
|
|
|
extern CScreenManager ScreenManager;
|
2018-10-27 23:14:34 +00:00
|
|
|
|
2019-05-20 12:09:59 +00:00
|
|
|
|
2019-01-12 21:32:13 +00:00
|
|
|
bool initWifi(int initpin,const char *failedssid, const char *failedpassword)
|
2018-10-27 23:14:34 +00:00
|
|
|
{
|
2019-05-20 12:09:59 +00:00
|
|
|
|
2018-10-27 23:14:34 +00:00
|
|
|
TRIG_PIN = initpin;
|
2018-12-06 19:47:36 +00:00
|
|
|
pinMode(TRIG_PIN, INPUT_PULLUP);
|
2018-10-28 03:03:44 +00:00
|
|
|
|
2019-01-14 20:03:35 +00:00
|
|
|
// report the MAC addresses - note individual values for STA and AP modes
|
2019-01-12 21:32:13 +00:00
|
|
|
uint8_t MAC[6];
|
|
|
|
esp_read_mac(MAC, ESP_MAC_WIFI_STA);
|
2019-01-18 03:34:32 +00:00
|
|
|
sprintf(MACstr[0], "%02X:%02X:%02X:%02X:%02X:%02X", MAC[0], MAC[1], MAC[2], MAC[3], MAC[4], MAC[5]);
|
2019-05-12 10:15:18 +00:00
|
|
|
DebugPort.printf(" STA MAC address: %s\r\n", MACstr[0]);
|
2019-01-12 21:32:13 +00:00
|
|
|
esp_read_mac(MAC, ESP_MAC_WIFI_SOFTAP);
|
2019-01-18 03:34:32 +00:00
|
|
|
sprintf(MACstr[1], "%02X:%02X:%02X:%02X:%02X:%02X", MAC[0], MAC[1], MAC[2], MAC[3], MAC[4], MAC[5]);
|
2019-05-12 10:15:18 +00:00
|
|
|
DebugPort.printf(" AP MAC address: %s\r\n", MACstr[1]);
|
2019-01-12 21:32:13 +00:00
|
|
|
|
2018-11-06 02:25:05 +00:00
|
|
|
//reset settings - wipe credentials for testing
|
2019-01-11 08:50:27 +00:00
|
|
|
// wm.resetSettings();
|
2018-10-28 03:03:44 +00:00
|
|
|
|
2019-01-13 22:00:43 +00:00
|
|
|
// Automatically connect using saved credentials:
|
|
|
|
// WiFiManager will prepare a link connection, using stored credentials if available.
|
|
|
|
//
|
|
|
|
// NO CREDENTIALS:
|
|
|
|
// Using a stored NV variable, we control the link creation via wm.setEnableConfigPortal():
|
|
|
|
// true - SoftAP is created (SSID = failedssid), and linked to the config portal
|
|
|
|
// false - we need to create a Soft AP, the portal does not start, we provide a web server
|
|
|
|
//
|
|
|
|
// WITH CREDENTIALS:
|
|
|
|
//
|
|
|
|
// Connected to stored AP, AP provides an IP address to use, we are STA (station)
|
|
|
|
// failed to connect to stored AP, using a stored NV variable we control the behaviour via wm.setEnableConfigPortal():
|
|
|
|
// true - SoftAP is created (SSID = failedssid), and linked to the config portal
|
|
|
|
// false - we need to create a Soft AP, the portal does not start, we provide a web server
|
2019-01-10 05:01:52 +00:00
|
|
|
|
2019-01-14 09:54:31 +00:00
|
|
|
DebugPort.println("Attempting to start STA mode (or config portal) via WifiManager...");
|
|
|
|
|
2019-05-20 12:09:59 +00:00
|
|
|
wm.setHostname(failedssid);
|
2019-07-15 09:56:36 +00:00
|
|
|
wm.setDebugOutput(true);
|
2019-01-13 19:59:32 +00:00
|
|
|
wm.setConfigPortalTimeout(20);
|
2018-11-06 17:18:12 +00:00
|
|
|
wm.setConfigPortalBlocking(false);
|
2019-07-15 09:56:36 +00:00
|
|
|
wm.setWiFiAutoReconnect(true);
|
2019-01-13 19:59:32 +00:00
|
|
|
wm.setSaveParamsCallback(saveParamsCallback); // ensure our webserver gets awoken when IP config changes to STA
|
2019-01-14 00:47:13 +00:00
|
|
|
wm.setAPCallback(APstartedCallback);
|
2019-01-13 21:24:13 +00:00
|
|
|
wm.setEnableConfigPortal(shouldBootIntoConfigPortal());
|
2019-01-14 09:54:31 +00:00
|
|
|
//REMOVED - UNSTABLE WHETHER WE GET 192.168.4.1 or 192.168.100.1 ????
|
|
|
|
// REMOVED wm.setAPStaticIPConfig(IPAddress(192, 168, 100, 1), IPAddress(192, 168, 100, 1), IPAddress(255,255,255,0));
|
2019-01-13 19:59:32 +00:00
|
|
|
|
2019-05-20 12:09:59 +00:00
|
|
|
bool res = wm.autoConnect(failedssid, failedpassword); // auto generated AP name from chipid
|
2019-05-12 10:15:18 +00:00
|
|
|
DebugPort.printf("WifiMode after autoConnect = "); DebugPort.println(WiFi.getMode());
|
2018-10-28 08:59:34 +00:00
|
|
|
|
2019-01-14 09:54:31 +00:00
|
|
|
int chnl = 1;
|
|
|
|
bool retval = false;
|
2018-11-06 02:25:05 +00:00
|
|
|
if(!res) {
|
2019-01-14 09:54:31 +00:00
|
|
|
// failed STA mode
|
|
|
|
DebugPort.println("WiFimanager failed STA connection. Setting up AP...");
|
2019-05-11 02:18:06 +00:00
|
|
|
WiFi.disconnect(); // apparently needed for AP only OTA to reboot properly!!!
|
2019-01-14 09:54:31 +00:00
|
|
|
}
|
2018-11-06 02:25:05 +00:00
|
|
|
else {
|
2019-01-14 09:54:31 +00:00
|
|
|
// runs through here if STA connected OK
|
|
|
|
// if you get here you have connected to the WiFi
|
|
|
|
isSTA = true;
|
|
|
|
DebugPort.println("WiFiManager connected in STA mode OK");
|
2019-05-18 08:49:22 +00:00
|
|
|
DebugPort.printf(" STA IP address: %s\r\n", getWifiSTAAddrStr());
|
2019-01-14 09:54:31 +00:00
|
|
|
// must use same radio channel as STA to go to STA+AP, otherwise we drop the STA!
|
|
|
|
chnl = WiFi.channel();
|
2019-05-12 10:15:18 +00:00
|
|
|
DebugPort.println("Now promoting to STA+AP mode...");
|
2019-01-14 09:54:31 +00:00
|
|
|
retval = true;
|
|
|
|
}
|
2019-02-26 08:38:30 +00:00
|
|
|
#ifdef USE_AP
|
2019-01-14 09:54:31 +00:00
|
|
|
// always setup an AP - for STA+AP mode we *must* use the same RF channel as STA
|
|
|
|
DebugPort.println("Starting AP mode");
|
|
|
|
//REMOVED - UNSTABLE WHETHER WE GET 192.168.4.1 or 192.168.100.1 ????
|
|
|
|
// REMOVED WiFi.softAPConfig(IPAddress(192, 168, 100, 1), IPAddress(192, 168, 100, 1), IPAddress(255,255,255,0));
|
2019-05-20 12:09:59 +00:00
|
|
|
WiFi.softAP(failedssid, failedpassword, chnl);
|
2019-01-14 09:54:31 +00:00
|
|
|
WiFi.enableAP(true);
|
2019-05-12 10:15:18 +00:00
|
|
|
DebugPort.printf(" AP SSID: %s\r\n", WiFi.softAPgetHostname());
|
2019-05-18 08:49:22 +00:00
|
|
|
DebugPort.printf(" AP IP address: %s\r\n", getWifiAPAddrStr());
|
2019-05-12 10:15:18 +00:00
|
|
|
DebugPort.printf("WifiMode after initWifi = %d\r\n", WiFi.getMode());
|
2019-02-26 08:38:30 +00:00
|
|
|
#endif
|
2019-01-14 09:54:31 +00:00
|
|
|
|
|
|
|
// even though we may have started in STA mode - start the config portal if demanded via the NV flag
|
|
|
|
if(shouldBootIntoConfigPortal()) {
|
|
|
|
DebugPort.println("Manually starting web portal");
|
|
|
|
wm.startWebPortal();
|
2019-01-14 20:03:35 +00:00
|
|
|
isPortalAP = true; // we started portal, we have to flag it!
|
2018-11-06 02:25:05 +00:00
|
|
|
}
|
2019-05-20 12:09:59 +00:00
|
|
|
|
2019-01-14 09:54:31 +00:00
|
|
|
return retval;
|
2018-10-27 23:14:34 +00:00
|
|
|
}
|
|
|
|
|
2019-01-14 20:03:35 +00:00
|
|
|
// call from main sketch loop()
|
2019-01-14 03:30:41 +00:00
|
|
|
void doWiFiManager()
|
|
|
|
{
|
|
|
|
wm.process();
|
2019-01-13 19:59:32 +00:00
|
|
|
|
2019-07-15 09:56:36 +00:00
|
|
|
/* if(WiFi.status() != WL_CONNECTED) {
|
|
|
|
if(WifiReconnectHoldoff) {
|
|
|
|
long tDelta = millis() - WifiReconnectHoldoff;
|
|
|
|
if(tDelta >= 0) {
|
|
|
|
WifiReconnectHoldoff = 0;
|
|
|
|
WiFi.disconnect();
|
|
|
|
WiFi.mode(WIFI_AP_STA);
|
|
|
|
wifi_config_t conf;
|
|
|
|
esp_wifi_get_config(WIFI_IF_STA, &conf);
|
|
|
|
WiFi.begin((char*)conf.sta.ssid, (char*)conf.sta.password);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
WifiReconnectHoldoff = millis() + 10000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
WifiReconnectHoldoff = 0;
|
|
|
|
}*/
|
|
|
|
|
2019-05-20 12:09:59 +00:00
|
|
|
#if USE_PORTAL_TRIGGER_PIN == 1
|
2019-01-14 20:03:35 +00:00
|
|
|
// manage handling of pin to enter WiFManager config portal
|
|
|
|
// we typically use the BOOT pin for this (pins.h)
|
|
|
|
//
|
|
|
|
// Quick Press (< 1 sec) - enable config portal
|
|
|
|
// > 1 second (< 5 sec) press - disable config portal
|
|
|
|
// > 5 second press - erase credentials, enable config portal
|
2019-01-14 03:30:41 +00:00
|
|
|
static bool pinDown = false;
|
|
|
|
static long pinTime = 0;
|
2019-01-16 09:22:17 +00:00
|
|
|
unsigned long tDelta;
|
|
|
|
|
2019-01-14 03:30:41 +00:00
|
|
|
if(digitalRead(TRIG_PIN) == LOW) {
|
2019-01-18 03:34:32 +00:00
|
|
|
if(!pinDown) {
|
2019-01-14 03:30:41 +00:00
|
|
|
pinTime = millis();
|
2019-01-18 03:34:32 +00:00
|
|
|
ScreenManager.reqUpdate();
|
|
|
|
}
|
2019-01-14 03:30:41 +00:00
|
|
|
pinDown = true;
|
2019-01-16 09:22:17 +00:00
|
|
|
// track hold duration - change OLED Wifi annotation according to length of press
|
|
|
|
tDelta = millis() - pinTime;
|
|
|
|
if(tDelta > 5000)
|
2019-01-18 03:34:32 +00:00
|
|
|
wifiButtonState = 3; // we will show 'ERS' on OLED!
|
2019-01-16 09:22:17 +00:00
|
|
|
else if(tDelta > 1000)
|
2019-01-18 03:34:32 +00:00
|
|
|
wifiButtonState = 2; // we will show 'HTR' on OLED!
|
2019-01-16 09:22:17 +00:00
|
|
|
else
|
2019-01-18 03:34:32 +00:00
|
|
|
wifiButtonState = 1; // we will show 'CFG' on OLED!
|
2019-01-14 03:30:41 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(pinDown) {
|
|
|
|
pinDown = false;
|
2019-01-16 09:22:17 +00:00
|
|
|
tDelta = millis() - pinTime;
|
2019-05-12 10:15:18 +00:00
|
|
|
DebugPort.printf("Wifi config button tDelta = %ld\r\n", tDelta);
|
2019-01-14 20:03:35 +00:00
|
|
|
// > 5 second press?
|
|
|
|
if(tDelta > 5000) {
|
2019-01-14 21:41:54 +00:00
|
|
|
wifiEnterConfigPortal(true, true); // very long press - clear credentials, reboot into portal
|
2019-01-13 21:24:13 +00:00
|
|
|
}
|
2019-01-14 20:03:35 +00:00
|
|
|
// > 1 second press?
|
|
|
|
else if(tDelta > 1000) {
|
2019-01-14 21:41:54 +00:00
|
|
|
wifiEnterConfigPortal(false); // long press - reboot into web server
|
2019-01-14 03:30:41 +00:00
|
|
|
}
|
2019-01-14 20:03:35 +00:00
|
|
|
// > 50ms press?
|
2019-01-14 03:30:41 +00:00
|
|
|
else if(tDelta > 50) {
|
2019-01-14 21:41:54 +00:00
|
|
|
wifiEnterConfigPortal(true); // quick press - reboot into portal
|
2019-01-14 03:30:41 +00:00
|
|
|
}
|
2019-01-14 20:03:35 +00:00
|
|
|
// consider as contact bounce if < 50ms!
|
2019-01-13 19:59:32 +00:00
|
|
|
}
|
2018-10-27 23:14:34 +00:00
|
|
|
}
|
2019-05-20 12:09:59 +00:00
|
|
|
#endif
|
2019-01-13 19:59:32 +00:00
|
|
|
}
|
|
|
|
|
2019-03-24 05:48:03 +00:00
|
|
|
void wifiDisable(long rebootDelay)
|
|
|
|
{
|
2019-06-06 01:32:43 +00:00
|
|
|
sUserSettings settings = NVstore.getUserSettings();
|
|
|
|
settings.enableWifi = 0;
|
|
|
|
NVstore.setUserSettings(settings);
|
2019-03-24 05:48:03 +00:00
|
|
|
NVstore.save();
|
|
|
|
|
|
|
|
DebugPort.println("*** Disabling WiFi ***");
|
|
|
|
|
|
|
|
restartServer = (millis() + rebootDelay) | 1; // prepare to reboot in the future - ensure non zero!
|
|
|
|
|
|
|
|
const char* content[2];
|
|
|
|
content[0] = "WiFi Mode \032 DISABLED";
|
|
|
|
content[1] = "";
|
|
|
|
ScreenManager.showRebootMsg(content, rebootDelay);
|
|
|
|
}
|
|
|
|
|
2019-01-16 09:22:17 +00:00
|
|
|
void wifiEnterConfigPortal(bool state, bool erase, long rebootDelay)
|
2019-01-14 21:41:54 +00:00
|
|
|
{
|
2019-01-16 09:22:17 +00:00
|
|
|
wm.disconnect();
|
2019-03-24 05:48:03 +00:00
|
|
|
|
2019-06-06 01:32:43 +00:00
|
|
|
sUserSettings settings = NVstore.getUserSettings();
|
|
|
|
settings.enableWifi = 1;
|
|
|
|
NVstore.setUserSettings(settings);
|
2019-03-24 05:48:03 +00:00
|
|
|
NVstore.save();
|
|
|
|
|
2019-01-14 21:41:54 +00:00
|
|
|
prepBootIntoConfigPortal(state);
|
|
|
|
|
2019-01-16 09:22:17 +00:00
|
|
|
const char* content[2];
|
|
|
|
|
|
|
|
if(isWifiSTA() && !erase)
|
2019-03-17 07:10:01 +00:00
|
|
|
content[0] = "WiFi Mode \032 STA+AP";
|
2019-01-16 09:22:17 +00:00
|
|
|
else
|
2019-03-17 07:10:01 +00:00
|
|
|
content[0] = "WiFi Mode \032 AP only";
|
2019-01-16 09:22:17 +00:00
|
|
|
|
2019-01-14 21:41:54 +00:00
|
|
|
if(erase) {
|
|
|
|
wm.resetSettings();
|
|
|
|
DebugPort.println("*** Erased wifi credentials ***");
|
|
|
|
}
|
|
|
|
|
2019-01-16 09:22:17 +00:00
|
|
|
if(state) {
|
2019-01-14 21:41:54 +00:00
|
|
|
DebugPort.println("*** Rebooting into config portal ***");
|
2019-03-17 07:10:01 +00:00
|
|
|
content[1] = "Web \032 Config Portal";
|
2019-01-16 09:22:17 +00:00
|
|
|
}
|
|
|
|
else {
|
2019-01-14 21:41:54 +00:00
|
|
|
DebugPort.println("*** Rebooting into web server ***");
|
2019-03-17 07:10:01 +00:00
|
|
|
content[1] = "Web \032 Heater control";
|
2019-01-16 09:22:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
restartServer = (millis() + rebootDelay) | 1; // prepare to reboot in the future - ensure non zero!
|
2019-01-14 21:41:54 +00:00
|
|
|
|
2019-01-16 09:22:17 +00:00
|
|
|
ScreenManager.showRebootMsg(content, rebootDelay);
|
2019-01-14 21:41:54 +00:00
|
|
|
}
|
|
|
|
|
2019-05-16 11:12:29 +00:00
|
|
|
void wifiFactoryDefault()
|
|
|
|
{
|
|
|
|
wm.resetSettings();
|
|
|
|
prepBootIntoConfigPortal(false);
|
2019-06-06 01:32:43 +00:00
|
|
|
sUserSettings settings = NVstore.getUserSettings();
|
|
|
|
settings.enableWifi = 1;
|
|
|
|
NVstore.setUserSettings(settings);
|
|
|
|
NVstore.save();
|
2019-05-16 11:12:29 +00:00
|
|
|
}
|
|
|
|
|
2019-01-14 20:03:35 +00:00
|
|
|
// callback is invoked by WiFiManager after new credentials are saved and verified
|
2019-01-13 19:59:32 +00:00
|
|
|
void saveParamsCallback()
|
|
|
|
{
|
2019-01-16 09:22:17 +00:00
|
|
|
wifiEnterConfigPortal(false); // stop config portal, reboot
|
2019-01-14 00:47:13 +00:00
|
|
|
}
|
|
|
|
|
2019-01-14 20:03:35 +00:00
|
|
|
// callback called if the WiFiManager started the config portal
|
2019-01-14 00:47:13 +00:00
|
|
|
void APstartedCallback(WiFiManager*)
|
|
|
|
{
|
2019-01-14 20:03:35 +00:00
|
|
|
isPortalAP = true; // will add CFG adornment to OLED WiFi icon
|
2018-10-27 23:14:34 +00:00
|
|
|
}
|
2018-11-19 19:31:20 +00:00
|
|
|
|
2019-01-14 09:54:31 +00:00
|
|
|
const char* getWifiAPAddrStr()
|
2018-11-19 19:31:20 +00:00
|
|
|
{
|
2019-06-02 09:19:08 +00:00
|
|
|
IPAddress IPaddr = WiFi.softAPIP(); // use stepping stone - function returns an automatic stack var - LAME!
|
|
|
|
static char APIPaddr[16];
|
|
|
|
sprintf(APIPaddr, "%d.%d.%d.%d", IPaddr[0], IPaddr[1], IPaddr[2], IPaddr[3]);
|
|
|
|
return APIPaddr;
|
2019-01-18 03:34:32 +00:00
|
|
|
}
|
2019-01-14 09:54:31 +00:00
|
|
|
|
|
|
|
const char* getWifiSTAAddrStr()
|
|
|
|
{
|
2019-06-02 09:19:08 +00:00
|
|
|
IPAddress IPaddr = WiFi.localIP(); // use stepping stone - function returns an automatic stack var - LAME!
|
|
|
|
static char STAIPaddr[16];
|
|
|
|
sprintf(STAIPaddr, "%d.%d.%d.%d", IPaddr[0], IPaddr[1], IPaddr[2], IPaddr[3]);
|
|
|
|
return STAIPaddr;
|
2019-01-18 03:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* getWifiAPMACStr()
|
|
|
|
{
|
|
|
|
return MACstr[1];
|
|
|
|
}
|
2018-11-19 19:31:20 +00:00
|
|
|
|
2019-01-18 03:34:32 +00:00
|
|
|
const char* getWifiSTAMACStr()
|
|
|
|
{
|
|
|
|
return MACstr[0];
|
|
|
|
}
|
2018-11-19 19:31:20 +00:00
|
|
|
|
2019-07-11 12:03:27 +00:00
|
|
|
String getSSID()
|
|
|
|
{
|
|
|
|
wifi_config_t conf;
|
|
|
|
esp_wifi_get_config(WIFI_IF_STA, &conf);
|
|
|
|
return String(reinterpret_cast<const char*>(conf.sta.ssid));
|
|
|
|
}
|
|
|
|
|
2018-11-19 19:31:20 +00:00
|
|
|
bool isWifiConnected()
|
|
|
|
{
|
|
|
|
return WiFi.status() == WL_CONNECTED;
|
|
|
|
}
|
2018-12-01 18:25:10 +00:00
|
|
|
|
2018-12-02 06:30:40 +00:00
|
|
|
bool isWifiAP()
|
|
|
|
{
|
2019-01-14 09:54:31 +00:00
|
|
|
int mode = WiFi.getMode();
|
|
|
|
return !isSTA && ((mode & WIFI_MODE_AP) != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isWifiSTA()
|
|
|
|
{
|
2019-01-14 20:03:35 +00:00
|
|
|
return isSTA; // true: STAtion mode link is active
|
2018-12-02 06:30:40 +00:00
|
|
|
}
|
|
|
|
|
2019-01-14 21:41:54 +00:00
|
|
|
bool isWifiConfigPortal()
|
2019-01-14 00:47:13 +00:00
|
|
|
{
|
2019-01-14 20:03:35 +00:00
|
|
|
return isPortalAP; // true: config portal is running
|
2019-01-14 00:47:13 +00:00
|
|
|
}
|
|
|
|
|
2019-01-14 20:03:35 +00:00
|
|
|
// save an NV flag to determine whether config portal should run after reboot
|
2019-01-13 21:24:13 +00:00
|
|
|
void prepBootIntoConfigPortal(bool state)
|
|
|
|
{
|
|
|
|
Preferences NV;
|
|
|
|
NV.begin("user");
|
|
|
|
NV.putBool("bootPortal", state);
|
|
|
|
NV.end();
|
2019-05-12 10:15:18 +00:00
|
|
|
DebugPort.printf("Setting boot config portal if WiFiManager fails = %d\r\n", state);
|
2019-01-13 21:24:13 +00:00
|
|
|
}
|
|
|
|
|
2019-01-14 20:03:35 +00:00
|
|
|
// test the NV flag whether the config portal should run after reboot
|
2019-01-13 21:24:13 +00:00
|
|
|
bool shouldBootIntoConfigPortal()
|
|
|
|
{
|
|
|
|
Preferences NV;
|
|
|
|
NV.begin("user");
|
|
|
|
bool retval = NV.getBool("bootPortal", false);
|
|
|
|
NV.end();
|
2019-05-12 10:15:18 +00:00
|
|
|
DebugPort.printf("Boot config portal if WiFiManager fails = %d\r\n", retval);
|
2019-01-13 21:24:13 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2019-01-18 03:34:32 +00:00
|
|
|
int isWifiButton()
|
|
|
|
{
|
|
|
|
return wifiButtonState;
|
|
|
|
}
|