2018-11-26 12:58:15 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the "bluetoothheater" distribution
|
|
|
|
* (https://gitlab.com/mrjones.id.au/bluetoothheater)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018 James Clark
|
|
|
|
*
|
|
|
|
* 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 09:00:19 +01:00
|
|
|
// Should be working - Jimmy C
|
2018-10-28 01:14:34 +02:00
|
|
|
#include "BTCWifi.h"
|
2018-12-16 08:34:39 +01:00
|
|
|
#include "../Utility/DebugPort.h"
|
2019-01-11 06:40:25 +01:00
|
|
|
#include <DNSServer.h>
|
2019-01-13 22:24:13 +01:00
|
|
|
|
2019-01-12 22:32:13 +01:00
|
|
|
#include "esp_system.h"
|
2019-01-13 22:24:13 +01:00
|
|
|
#include <Preferences.h>
|
|
|
|
|
|
|
|
// function to control the behaviour upon reboot if no wifi manager credentials exist
|
|
|
|
// or connection fails
|
|
|
|
void prepBootIntoConfigPortal(bool state);
|
|
|
|
bool shouldBootIntoConfigPortal();
|
2019-01-12 22:32:13 +01:00
|
|
|
|
2018-10-28 01:14:34 +02:00
|
|
|
|
2019-01-11 06:40:25 +01:00
|
|
|
#define FAILEDSSID "BTCESP32"
|
|
|
|
#define FAILEDPASSWORD "thereisnospoon"
|
|
|
|
|
2018-10-28 01:14:34 +02:00
|
|
|
WiFiManager wm;
|
2019-01-12 22:32:13 +01:00
|
|
|
extern void stopWebServer();
|
2019-01-13 20:59:32 +01:00
|
|
|
extern void initWebServer();
|
|
|
|
void saveParamsCallback();
|
2019-01-14 01:47:13 +01:00
|
|
|
void APstartedCallback(WiFiManager*);
|
2018-10-28 01:14:34 +02:00
|
|
|
|
|
|
|
unsigned int timeout = 120; // seconds to run for
|
|
|
|
unsigned int startTime = millis();
|
2019-01-14 01:47:13 +01:00
|
|
|
bool isPortalAP = false;
|
2018-12-02 07:30:40 +01:00
|
|
|
bool isAP = false;
|
2018-10-28 01:14:34 +02:00
|
|
|
bool portalRunning = false;
|
2019-01-13 20:59:32 +01:00
|
|
|
bool startCP = true;//true; // start AP and webserver if true, else start only webserver
|
2019-01-13 22:24:13 +01:00
|
|
|
int TRIG_PIN; // which pin triggers the configuration portal when set to LOW
|
|
|
|
|
2019-01-13 20:59:32 +01:00
|
|
|
unsigned startServer = 0;
|
2018-10-28 04:03:44 +01:00
|
|
|
|
2018-10-28 01:14:34 +02:00
|
|
|
|
2019-01-12 22:32:13 +01:00
|
|
|
bool initWifi(int initpin,const char *failedssid, const char *failedpassword)
|
2018-10-28 01:14:34 +02:00
|
|
|
{
|
|
|
|
TRIG_PIN = initpin;
|
2018-12-06 20:47:36 +01:00
|
|
|
pinMode(TRIG_PIN, INPUT_PULLUP);
|
2018-10-28 04:03:44 +01:00
|
|
|
|
2019-01-12 22:32:13 +01:00
|
|
|
uint8_t MAC[6];
|
|
|
|
esp_read_mac(MAC, ESP_MAC_WIFI_STA);
|
|
|
|
char msg[64];
|
|
|
|
sprintf(msg, "STA MAC address: %02X:%02X:%02X:%02X:%02X:%02X", MAC[0], MAC[1], MAC[2], MAC[3], MAC[4], MAC[5]);
|
|
|
|
DebugPort.println(msg);
|
|
|
|
esp_read_mac(MAC, ESP_MAC_WIFI_SOFTAP);
|
2019-01-14 04:30:41 +01:00
|
|
|
sprintf(msg, " AP MAC address: %02X:%02X:%02X:%02X:%02X:%02X", MAC[0], MAC[1], MAC[2], MAC[3], MAC[4], MAC[5]);
|
2019-01-12 22:32:13 +01:00
|
|
|
DebugPort.println(msg);
|
|
|
|
|
2018-11-06 03:25:05 +01:00
|
|
|
//reset settings - wipe credentials for testing
|
2019-01-11 09:50:27 +01:00
|
|
|
// wm.resetSettings();
|
2018-10-28 04:03:44 +01:00
|
|
|
|
2019-01-13 23:00:43 +01: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 06:01:52 +01:00
|
|
|
|
2019-01-13 20:59:32 +01:00
|
|
|
wm.setHostname(failedssid);
|
|
|
|
wm.setConfigPortalTimeout(20);
|
2018-11-06 18:18:12 +01:00
|
|
|
wm.setConfigPortalBlocking(false);
|
2019-01-13 20:59:32 +01:00
|
|
|
wm.setSaveParamsCallback(saveParamsCallback); // ensure our webserver gets awoken when IP config changes to STA
|
2019-01-14 01:47:13 +01:00
|
|
|
wm.setAPCallback(APstartedCallback);
|
2019-01-13 22:24:13 +01:00
|
|
|
wm.setEnableConfigPortal(shouldBootIntoConfigPortal());
|
2019-01-13 23:00:43 +01:00
|
|
|
wm.setAPStaticIPConfig(IPAddress(192, 168, 100, 1), IPAddress(192, 168, 100, 1), IPAddress(255,255,255,0));
|
2019-01-13 20:59:32 +01:00
|
|
|
|
|
|
|
bool res = wm.autoConnect(failedssid, failedpassword); // auto generated AP name from chipid
|
2018-12-02 07:30:40 +01:00
|
|
|
// bool res = false;
|
2018-10-28 09:59:34 +01:00
|
|
|
|
2018-11-06 03:25:05 +01:00
|
|
|
if(!res) {
|
2019-01-14 01:47:13 +01:00
|
|
|
// runs through here if we need to start our own soft AP to run THE web page
|
2018-11-06 04:23:28 +01:00
|
|
|
DebugPort.println("Failed to connect");
|
|
|
|
DebugPort.println("Setting up ESP as AP");
|
2019-01-13 23:00:43 +01:00
|
|
|
// We need to start the soft AP
|
|
|
|
// - wifimanger has done most of the work, but has been left us high and dry :-)
|
2019-01-14 01:47:13 +01:00
|
|
|
WiFi.softAPConfig(IPAddress(192, 168, 100, 1), IPAddress(192, 168, 100, 1), IPAddress(255,255,255,0));
|
|
|
|
delay(100);
|
2019-01-13 21:16:45 +01:00
|
|
|
isAP = WiFi.softAP(failedssid, failedpassword);
|
2019-01-14 01:47:13 +01:00
|
|
|
DebugPort.print("AP IP address: "); DebugPort.println(WiFi.softAPIP());
|
2019-01-12 22:32:13 +01:00
|
|
|
return false;
|
2018-11-06 03:25:05 +01:00
|
|
|
}
|
|
|
|
else {
|
2019-01-14 01:47:13 +01:00
|
|
|
// runs through here is STA or portal config AP mode
|
2018-11-06 03:25:05 +01:00
|
|
|
//if you get here you have connected to the WiFi
|
2018-11-06 04:23:28 +01:00
|
|
|
DebugPort.println("connected...yeey :)");
|
|
|
|
DebugPort.println("Ready");
|
2019-01-14 01:47:13 +01:00
|
|
|
if(isPortal())
|
|
|
|
DebugPort.print("AP IP address: ");
|
|
|
|
else
|
|
|
|
DebugPort.print("STA IP address: ");
|
2018-11-06 04:23:28 +01:00
|
|
|
DebugPort.println(WiFi.localIP());
|
2018-11-06 03:25:05 +01:00
|
|
|
}
|
2019-01-12 22:32:13 +01:00
|
|
|
return true;
|
2018-10-28 01:14:34 +02:00
|
|
|
}
|
|
|
|
|
2019-01-14 04:30:41 +01:00
|
|
|
void doWiFiManager()
|
|
|
|
{
|
|
|
|
wm.process();
|
2019-01-13 20:59:32 +01:00
|
|
|
|
2019-01-14 04:30:41 +01:00
|
|
|
if(startServer) {
|
|
|
|
long tDelta = millis() - startServer;
|
|
|
|
if(tDelta > 10000) {
|
|
|
|
startServer = 0;
|
|
|
|
initWebServer();
|
|
|
|
ESP.restart();
|
2019-01-13 20:59:32 +01:00
|
|
|
}
|
2019-01-14 04:30:41 +01:00
|
|
|
}
|
2019-01-13 20:59:32 +01:00
|
|
|
|
2019-01-14 04:30:41 +01:00
|
|
|
static bool pinDown = false;
|
|
|
|
static long pinTime = 0;
|
|
|
|
if(digitalRead(TRIG_PIN) == LOW) {
|
|
|
|
if(!pinDown)
|
|
|
|
pinTime = millis();
|
|
|
|
pinDown = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(pinDown) {
|
|
|
|
pinDown = false;
|
|
|
|
unsigned long tDelta = millis() - pinTime;
|
|
|
|
DebugPort.print("Wifi config button tDelta = "); DebugPort.println(tDelta);
|
|
|
|
if(tDelta > 5000) { // > 5 second press
|
|
|
|
prepBootIntoConfigPortal(true); // very long press - clear credentials, boot into portal
|
|
|
|
wm.resetSettings();
|
|
|
|
DebugPort.println("*** Clearing credentials and rebooting into portal ***");
|
|
|
|
delay(1000);
|
|
|
|
ESP.restart();
|
2019-01-13 22:24:13 +01:00
|
|
|
}
|
2019-01-14 04:30:41 +01:00
|
|
|
else if(tDelta > 1000) { // > 1 second press
|
|
|
|
prepBootIntoConfigPortal(false); // long press - boot into SoftAP
|
|
|
|
DebugPort.println("*** Rebooting into web server ***");
|
|
|
|
delay(1000);
|
|
|
|
ESP.restart();
|
|
|
|
}
|
|
|
|
else if(tDelta > 50) {
|
|
|
|
prepBootIntoConfigPortal(true); // short press - boot into Portal
|
|
|
|
DebugPort.println("*** Rebooting into config portal ***");
|
|
|
|
delay(1000);
|
|
|
|
ESP.restart();
|
|
|
|
}
|
|
|
|
// contact bounce otherwise!
|
2019-01-13 20:59:32 +01:00
|
|
|
}
|
2018-10-28 01:14:34 +02:00
|
|
|
}
|
2019-01-13 20:59:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void saveParamsCallback()
|
|
|
|
{
|
|
|
|
startServer = millis();
|
2019-01-14 01:47:13 +01:00
|
|
|
prepBootIntoConfigPortal(false); // ensure we fall back to SoftAP with our web page in future
|
2019-01-14 04:30:41 +01:00
|
|
|
isPortalAP = false;
|
|
|
|
isAP = false;
|
2019-01-14 01:47:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void APstartedCallback(WiFiManager*)
|
|
|
|
{
|
|
|
|
isPortalAP = true;
|
2018-10-28 01:14:34 +02:00
|
|
|
}
|
2018-11-19 20:31:20 +01:00
|
|
|
|
|
|
|
const char* getWifiAddrStr()
|
|
|
|
{
|
2018-12-02 07:30:40 +01:00
|
|
|
if(isAP)
|
|
|
|
return WiFi.softAPIP().toString().c_str();
|
|
|
|
else
|
|
|
|
return WiFi.localIP().toString().c_str();
|
2018-11-19 20:31:20 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
bool isWifiConnected()
|
|
|
|
{
|
|
|
|
return WiFi.status() == WL_CONNECTED;
|
|
|
|
}
|
2018-12-01 19:25:10 +01:00
|
|
|
|
2018-12-02 07:30:40 +01:00
|
|
|
bool isWifiAP()
|
|
|
|
{
|
|
|
|
return isAP;
|
|
|
|
}
|
|
|
|
|
2019-01-14 01:47:13 +01:00
|
|
|
bool isPortal()
|
|
|
|
{
|
|
|
|
return isPortalAP;
|
|
|
|
}
|
|
|
|
|
2019-01-13 22:24:13 +01:00
|
|
|
|
|
|
|
void prepBootIntoConfigPortal(bool state)
|
|
|
|
{
|
|
|
|
Preferences NV;
|
|
|
|
NV.begin("user");
|
|
|
|
NV.putBool("bootPortal", state);
|
|
|
|
NV.end();
|
2019-01-14 01:47:13 +01:00
|
|
|
DebugPort.print("Setting boot config portal if WiFiManager not configured = "); DebugPort.println(state);
|
2019-01-13 22:24:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool shouldBootIntoConfigPortal()
|
|
|
|
{
|
|
|
|
Preferences NV;
|
|
|
|
NV.begin("user");
|
|
|
|
bool retval = NV.getBool("bootPortal", false);
|
|
|
|
NV.end();
|
|
|
|
DebugPort.print("Boot config portal if WiFiManager not configured = "); DebugPort.println(retval);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|