1 | //**************************************************************************************************
|
2 | // C O N N E C T W I F I *
|
3 | //**************************************************************************************************
|
4 | // Connect to WiFi using the SSID's available in wifiMulti. *
|
5 | // If only one AP if found in preferences (i.e. wifi_00) the connection is made without *
|
6 | // using wifiMulti. *
|
7 | // If connection fails, an AP is created and the function returns false. *
|
8 | //**************************************************************************************************
|
9 | bool connectwifi()
|
10 | {
|
11 | char* pfs ; // Pointer to formatted string
|
12 | char* pfs2 ; // Pointer to formatted string
|
13 | bool localAP = false ; // True if only local AP is left
|
14 |
|
15 | WifiInfo_t winfo ; // Entry from wifilist
|
16 |
|
17 | WiFi.disconnect() ; // After restart the router could
|
18 | WiFi.softAPdisconnect(true) ; // still keep the old connection
|
19 | if ( wifilist.size() ) // Any AP defined?
|
20 | {
|
21 | if ( wifilist.size() == 1 ) // Just one AP defined in preferences?
|
22 | {
|
23 | winfo = wifilist[0] ; // Get this entry
|
24 | WiFi.begin ( winfo.ssid, winfo.passphrase ) ; // Connect to single SSID found in wifi_xx
|
25 | dbgprint ( "Try WiFi %s", winfo.ssid ) ; // Message to show during WiFi connect
|
26 | }
|
27 | else // More AP to try
|
28 | {
|
29 | wifiMulti.run() ; // Connect to best network
|
30 | }
|
31 | if ( WiFi.waitForConnectResult() != WL_CONNECTED ) // Try to connect
|
32 | {
|
33 | localAP = true ; // Error, setup own AP
|
34 | }
|
35 | }
|
36 | else
|
37 | {
|
38 | localAP = true ; // Not even a single AP defined
|
39 | }
|
40 | if ( localAP ) // Must setup local AP?
|
41 | {
|
42 | dbgprint ( "WiFi Failed! Trying to setup AP with name %s and password %s.", NAME, NAME ) ;
|
43 | WiFi.softAP ( NAME, NAME ) ; // This ESP will be an AP
|
44 | pfs = dbgprint ( "IP = 192.168.4.1" ) ; // Address for AP
|
45 | }
|
46 | else
|
47 | {
|
48 | ipaddress = WiFi.localIP().toString() ; // Form IP address
|
49 | pfs2 = dbgprint ( "Connected to %s", WiFi.SSID().c_str() ) ;
|
50 | tftlog ( pfs2 ) ;
|
51 | pfs = dbgprint ( "IP = %s", ipaddress.c_str() ) ; // String to dispay on TFT
|
52 | }
|
53 | tftlog ( pfs ) ; // Show IP
|
54 | delay ( 3000 ) ; // Allow user to read this
|
55 | return ( localAP == false ) ; // Return result of connection
|
56 | }
|