@ -5,6 +5,9 @@
# include <DNSServer.h>
# include <ESPAsyncWebServer.h> //https://github.com/me-no-dev/ESPAsyncWebServer using the latest dev version from @me-no-dev
# include <esp_wifi.h> //Used for mpdu_rx_disable android workaround
# include <HttpClient.h>
# include <WiFi.h>
# include <WiFiClient.h>
// Pre reading on the fundamentals of captive portals https://textslashplain.com/2022/06/24/captive-portals/
@ -40,7 +43,7 @@ const char index_html[] PROGMEM = R"=====(
< ! DOCTYPE html >
< html >
< head >
< title > ESP32 WiFi Setup < / title >
< title > WiFi Setup < / title >
< style >
body { background - color : # 06 cc13 ; }
h1 { color : white ; }
@ -61,6 +64,29 @@ const char index_html[] PROGMEM = R"=====(
< / html >
) = = = = = " ;
const char post_connected_html [ ] PROGMEM = R " =====(
< ! DOCTYPE html >
< html >
< head >
< title > 01 OS Setup < / title >
< style >
body { background - color : # 06 cc13 ; }
h1 { color : white ; }
h2 { color : white ; }
< / style >
< meta name = " viewport " content = " width=device-width, initial-scale=1.0 " >
< / head >
< body >
< h1 > 01 OS Setup < / h1 >
< form action = " /submit_01os " method = " post " >
< label for = " server_address " > 01 OS Server Address : < / label > < br >
< input type = " text " id = " server_address " name = " server_address " > < br >
< input type = " submit " value = " Connect " >
< / form >
< / body >
< / html >
) = = = = = " ;
DNSServer dnsServer ;
AsyncWebServer server ( 80 ) ;
@ -126,6 +152,88 @@ void connectToWifi(String ssid, String password)
}
}
// Number of milliseconds to wait without receiving any data before we give up
const int kNetworkTimeout = 30 * 1000 ;
// Number of milliseconds to wait if no data is available before trying again
const int kNetworkDelay = 1000 ;
void connectTo01OS ( String server_address )
{
int err = 0 ;
WiFiClient c ;
HttpClient http ( c ) ;
String domain = server_address . substring ( 0 , server_address . indexOf ( ' : ' ) ) ;
String portStr = server_address . substring ( server_address . indexOf ( ' : ' ) + 1 ) ;
int port = portStr . toInt ( ) ;
Serial . println ( " Connecting to 01OS at " + domain + " : " + port + " /ping " ) ;
err = http . get ( domain . c_str ( ) , port , " /ping " ) ;
//err = http.get("arduino.cc", "/");
if ( err = = 0 )
{
Serial . println ( " Started the ping request " ) ;
err = http . responseStatusCode ( ) ;
if ( err > = 0 )
{
Serial . print ( " Got status code: " ) ;
Serial . println ( err ) ;
err = http . skipResponseHeaders ( ) ;
if ( err > = 0 )
{
int bodyLen = http . contentLength ( ) ;
Serial . print ( " Content length is: " ) ;
Serial . println ( bodyLen ) ;
Serial . println ( ) ;
Serial . println ( " Body returned follows: " ) ;
// Now we've got to the body, so we can print it out
unsigned long timeoutStart = millis ( ) ;
char c ;
// Whilst we haven't timed out & haven't reached the end of the body
while ( ( http . connected ( ) | | http . available ( ) ) & &
( ( millis ( ) - timeoutStart ) < kNetworkTimeout ) )
{
if ( http . available ( ) )
{
c = http . read ( ) ;
// Print out this character
Serial . print ( c ) ;
bodyLen - - ;
// We read something, reset the timeout counter
timeoutStart = millis ( ) ;
}
else
{
// We haven't got any data, so let's pause to allow some to
// arrive
delay ( kNetworkDelay ) ;
}
}
}
else
{
Serial . print ( " Failed to skip response headers: " ) ;
Serial . println ( err ) ;
}
}
else
{
Serial . print ( " Getting response failed: " ) ;
Serial . println ( err ) ;
}
}
else
{
Serial . print ( " Connect failed: " ) ;
Serial . println ( err ) ;
}
}
void setUpWebserver ( AsyncWebServer & server , const IPAddress & localIP )
{
//======================== Webserver ========================
@ -210,7 +318,31 @@ void setUpWebserver(AsyncWebServer &server, const IPAddress &localIP)
connectToWifi ( ssid , password ) ;
// Redirect user or send a response back
request - > send ( 200 , " text/plain " , " Attempting to connect to " + ssid ) ; } ) ;
if ( WiFi . status ( ) = = WL_CONNECTED ) {
String htmlContent = post_connected_html ;
AsyncWebServerResponse * response = request - > beginResponse ( 200 , " text/html " , htmlContent ) ;
response - > addHeader ( " Cache-Control " , " public,max-age=31536000 " ) ; // save this file to cache for 1 year (unless you refresh)
request - > send ( response ) ;
Serial . println ( " Served Post connection HTML Page " ) ;
} else {
request - > send ( 200 , " text/plain " , " Failed to connect to " + ssid ) ;
}
} ) ;
server . on ( " /submit_01os " , HTTP_POST , [ ] ( AsyncWebServerRequest * request )
{
String server_address ;
// Check if SSID parameter exists and assign it
if ( request - > hasParam ( " server_address " , true ) ) {
server_address = request - > getParam ( " server_address " , true ) - > value ( ) ;
}
// Attempt to connect to the Wi-Fi network with these credentials
connectTo01OS ( server_address ) ;
// Redirect user or send a response back
request - > send ( 200 , " text/plain " , " Attempting to connect to 01OS " + server_address ) ; } ) ;
}
void setup ( )
@ -250,11 +382,11 @@ void loop()
delay ( DNS_INTERVAL ) ; // seems to help with stability, if you are doing other things in the loop this may not be needed
// Check WiFi connection status
if ( WiFi . status ( ) = = WL_CONNECTED )
{
//if (WiFi.status() == WL_CONNECTED)
//{
// If connected, you might want to do something, like printing the IP address
Serial . println ( " Connected to WiFi! " ) ;
Serial . println ( " IP Address: " + WiFi . localIP ( ) . toString ( ) ) ;
Serial . println ( " SSID " + WiFi . SSID ( ) ) ;
}
//Serial.println("Connected to WiFi!");
//Serial.println("IP Address: " + WiFi.localIP().toString());
//Serial.println("SSID " + WiFi.SSID());
//}
}