2018-11-26 11:58:15 +00: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 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>
2018-10-27 23:14:34 +00:00
// select which pin will trigger the configuration portal when set to LOW
2019-01-11 05:40:25 +00:00
# define FAILEDSSID "BTCESP32"
# define FAILEDPASSWORD "thereisnospoon"
2018-10-27 23:14:34 +00:00
WiFiManager wm ;
unsigned int timeout = 120 ; // seconds to run for
unsigned int startTime = millis ( ) ;
2018-12-02 06:30:40 +00:00
bool isAP = false ;
2018-10-27 23:14:34 +00:00
bool portalRunning = false ;
2019-01-11 05:40:25 +00:00
bool startCP = true ; // start AP and webserver if true, else start only webserver
2018-10-27 23:14:34 +00:00
int TRIG_PIN ;
2018-10-28 03:03:44 +00:00
2019-01-11 05:40:25 +00:00
void configModeCallback ( WiFiManager * myWiFiManager ) ;
void saveConfigCallback ( ) ;
2018-10-28 03:03:44 +00:00
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-12-06 19:47:36 +00:00
pinMode ( TRIG_PIN , INPUT_PULLUP ) ;
2018-10-28 03:03:44 +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
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
2019-01-10 05:01:52 +00:00
2019-01-11 08:50:27 +00:00
// wm.setHostname("BTCDieselHeater");
2019-01-11 05:40:25 +00:00
wm . setConfigPortalTimeout ( 120 ) ;
2018-11-06 17:18:12 +00:00
wm . setConfigPortalBlocking ( false ) ;
2019-01-12 01:21:29 +00:00
// wm.setAPCallback(configModeCallback);
2019-01-11 05:40:25 +00:00
// wm.setSaveConfigCallback(saveConfigCallback);
2018-10-28 03:03:44 +00:00
2018-12-02 06:30:40 +00:00
bool res = wm . autoConnect ( ) ; // auto generated AP name from chipid
// bool res = false;
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-12-02 06:30:40 +00:00
isAP = WiFi . softAP ( failedssid , failedpassword ) ;
if ( isAP ) {
2019-01-11 05:40:25 +00:00
WiFi . softAPConfig ( IPAddress ( 192 , 168 , 100 , 1 ) , IPAddress ( 192 , 168 , 100 , 1 ) , IPAddress ( 255 , 255 , 255 , 0 ) ) ;
2018-12-02 06:30:40 +00:00
}
2018-11-06 02:25:05 +00:00
}
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 ( ) ;
2018-11-24 11:03:47 +00:00
long tDelta = millis ( ) - startTime ;
if ( tDelta > ( 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?
2019-01-11 05:40:25 +00:00
// if(TRIG_PIN == 1 && (!portalRunning)) {
if ( digitalRead ( TRIG_PIN ) = = LOW & & ! 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 ( ) ;
2019-01-11 05:40:25 +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 ( ) ;
2019-01-11 05:40:25 +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 ( )
{
2018-12-02 06:30:40 +00:00
if ( isAP )
return WiFi . softAPIP ( ) . toString ( ) . c_str ( ) ;
else
return WiFi . localIP ( ) . toString ( ) . c_str ( ) ;
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 ( )
{
return isAP ;
}
2019-01-11 05:40:25 +00:00
//callback que indica que o ESP entrou no modo AP
void configModeCallback ( WiFiManager * myWiFiManager ) {
DebugPort . println ( " Entered config mode " ) ;
DebugPort . println ( WiFi . softAPIP ( ) ) ; //imprime o IP do AP
DebugPort . println ( myWiFiManager - > getConfigPortalSSID ( ) ) ; //imprime o SSID criado da rede</p><p>}</p><p>//callback que indica que salvamos uma nova rede para se conectar (modo estação)
}
void saveConfigCallback ( ) {
DebugPort . println ( " Should save config " ) ;
DebugPort . println ( WiFi . softAPIP ( ) ) ; //imprime o IP do AP
}