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-11-06 03:23:28 +00:00
|
|
|
#include "DebugPort.h"
|
2018-10-27 23:14:34 +00:00
|
|
|
// select which pin will trigger the configuration portal when set to LOW
|
|
|
|
|
|
|
|
WiFiManager wm;
|
|
|
|
|
|
|
|
unsigned int timeout = 120; // seconds to run for
|
|
|
|
unsigned int startTime = millis();
|
|
|
|
bool portalRunning = false;
|
|
|
|
bool startCP = false; // start AP and webserver if true, else start only webserver
|
|
|
|
int TRIG_PIN;
|
2018-10-28 03:03:44 +00:00
|
|
|
bool res;
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-10-27 23:14:34 +00:00
|
|
|
|
2018-10-28 08:59:34 +00:00
|
|
|
void initWifi(int initpin,const char *failedssid, const char *failedpassword)
|
2018-10-27 23:14:34 +00:00
|
|
|
{
|
|
|
|
TRIG_PIN = initpin;
|
2018-11-06 02:25:05 +00:00
|
|
|
pinMode(TRIG_PIN, INPUT);
|
2018-10-28 03:03:44 +00:00
|
|
|
|
2018-11-06 02:25:05 +00:00
|
|
|
//reset settings - wipe credentials for testing
|
|
|
|
//wm.resetSettings();
|
2018-10-28 03:03:44 +00:00
|
|
|
|
2018-11-06 02:25:05 +00:00
|
|
|
// Automatically connect using saved credentials,
|
|
|
|
// if connection fails, it starts an access point with the specified name ( "AutoConnectAP"),
|
|
|
|
// if empty will auto generate SSID, if password is blank it will be anonymous AP (wm.autoConnect())
|
|
|
|
// then goes into a blocking loop awaiting configuration and will return success result
|
|
|
|
wm.setConfigPortalTimeout(20);
|
2018-11-06 17:18:12 +00:00
|
|
|
wm.setConfigPortalBlocking(false);
|
2018-10-28 03:03:44 +00:00
|
|
|
|
2018-11-06 02:25:05 +00:00
|
|
|
res = wm.autoConnect(); // auto generated AP name from chipid
|
2018-10-28 08:59:34 +00:00
|
|
|
|
2018-11-06 02:25:05 +00:00
|
|
|
if(!res) {
|
2018-11-06 03:23:28 +00:00
|
|
|
DebugPort.println("Failed to connect");
|
|
|
|
DebugPort.println("Setting up ESP as AP");
|
2018-11-06 02:25:05 +00:00
|
|
|
WiFi.softAP(failedssid, failedpassword);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
//if you get here you have connected to the WiFi
|
2018-11-06 03:23:28 +00:00
|
|
|
DebugPort.println("connected...yeey :)");
|
|
|
|
DebugPort.println("Ready");
|
|
|
|
DebugPort.print("IP address: ");
|
|
|
|
DebugPort.println(WiFi.localIP());
|
2018-11-06 02:25:05 +00:00
|
|
|
}
|
2018-10-27 23:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void doWiFiManager(){
|
|
|
|
// is auto timeout portal running
|
|
|
|
if(portalRunning){
|
|
|
|
wm.process();
|
|
|
|
if((millis()-startTime) > (timeout*1000)){
|
2018-11-06 03:23:28 +00:00
|
|
|
DebugPort.println("portaltimeout");
|
2018-10-27 23:14:34 +00:00
|
|
|
portalRunning = false;
|
|
|
|
if(startCP){
|
|
|
|
wm.stopConfigPortal();
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
wm.stopWebPortal();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// is configuration portal requested?
|
2018-10-28 03:03:44 +00:00
|
|
|
if(TRIG_PIN == 1 && (!portalRunning)) {
|
2018-10-27 23:14:34 +00:00
|
|
|
if(startCP){
|
2018-11-06 03:23:28 +00:00
|
|
|
DebugPort.println("Button Pressed, Starting Config Portal");
|
2018-10-27 23:14:34 +00:00
|
|
|
wm.setConfigPortalBlocking(false);
|
|
|
|
wm.startConfigPortal();
|
2018-10-28 03:03:44 +00:00
|
|
|
TRIG_PIN = 0; // reset the flag
|
2018-10-27 23:14:34 +00:00
|
|
|
}
|
|
|
|
else{
|
2018-11-06 03:23:28 +00:00
|
|
|
DebugPort.println("Button Pressed, Starting Web Portal");
|
2018-10-27 23:14:34 +00:00
|
|
|
wm.startWebPortal();
|
2018-10-28 03:03:44 +00:00
|
|
|
TRIG_PIN = 0; // reset the flag
|
2018-10-27 23:14:34 +00:00
|
|
|
}
|
|
|
|
portalRunning = true;
|
|
|
|
startTime = millis();
|
|
|
|
}
|
|
|
|
}
|
2018-11-19 19:31:20 +00:00
|
|
|
|
|
|
|
const char* getWifiAddrStr()
|
|
|
|
{
|
|
|
|
return WiFi.localIP().toString().c_str();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
bool isWifiConnected()
|
|
|
|
{
|
|
|
|
return WiFi.status() == WL_CONNECTED;
|
|
|
|
}
|