11/* ***************************************************************************************************************************
2- * AutoConnect.ino
3- * WiFi/Credentials Manager for Teensy, SAM DUE, SAMD, STM32, etc. boards running `ESP8266 AT-command` shields
4- *
5- * ESP_AT_WiFiManager is a library for the Teensy, SAM DUE, SAMD, STM32, etc. boards running `ESP8266 AT-command` shields
6- * (https://github.com/esp8266/Arduino) to enable easy configuration and reconfiguration of WiFi, etc. credentials using a Captive Portal
7- * inspired by:
8- * http://www.esp8266.com/viewtopic.php?f=29&t=2520
9- * https://github.com/chriscook8/esp-arduino-apboot
10- * https://github.com/esp8266/Arduino/blob/master/libraries/DNSServer/examples/CaptivePortalAdvanced/
11- *
12- * Forked from Tzapu https://github.com/tzapu/WiFiManager
13- * and from Ken Taylor https://github.com/kentaylor
14- *
15- * Built by Khoi Hoang https://github.com/khoih-prog/ESP_AT_WiFiManager
16- * Licensed under MIT license
17- * Version: 1.0.0
18- *
19- * Version Modified By Date Comments
20- * ------- ----------- ---------- -----------
21- * 1.0.0 K Hoang 08/03/2020 Initial coding
2+ AutoConnect.ino
3+ WiFi/Credentials Manager for Teensy, SAM DUE, SAMD, nRF52, STM32, etc. boards running `ESP8266/ESP32-AT-command` shields
4+
5+ ESP_AT_WiFiManager is a library for the Teensy, SAM DUE, SAMD, nRF52, STM32, etc. boards running `ESP8266/ESP32-AT-command` shields
6+ (https://github.com/esp8266/Arduino) to enable easy configuration and reconfiguration of WiFi, etc. credentials using a Captive Portal
7+
8+ Inspired by:
9+ http://www.esp8266.com/viewtopic.php?f=29&t=2520
10+ https://github.com/chriscook8/esp-arduino-apboot
11+ https://github.com/esp8266/Arduino/blob/master/libraries/DNSServer/examples/CaptivePortalAdvanced/
12+
13+ Based on and modified from Tzapu https://github.com/tzapu/WiFiManager
14+ and from Ken Taylor https://github.com/kentaylor
15+
16+ Built by Khoi Hoang https://github.com/khoih-prog/ESP_AT_WiFiManager
17+ Licensed under MIT license
18+ Version: 1.0.1
19+
20+ Version Modified By Date Comments
21+ ------- ----------- ---------- -----------
22+ 1.0.0 K Hoang 08/03/2020 Initial coding
23+ 1.0.1 K Hoang 22/06/2020 Add support to nRF52 boards, such as AdaFruit Feather nRF52832, NINA_B30_ublox, etc.
2224 *****************************************************************************************************************************/
2325#define _ESP_AT_LOGLEVEL_ 0
2426#define DEBUG_WIFIMGR true // false
2527
2628#if ( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) )
27- #if defined(ESP8266_AT_USE_STM32)
28- #undef ESP8266_AT_USE_STM32
29- #endif
30- #define ESP8266_AT_USE_STM32 true
29+ #if defined(STM32F0)
30+ #error STMF0 not supported
3131#endif
32+ #if defined(ESP8266_AT_USE_STM32)
33+ #undef ESP8266_AT_USE_STM32
34+ #endif
35+ #define ESP8266_AT_USE_STM32 true
3236
33- #if ( defined(ARDUINO_SAM_DUE) || defined(__SAM3X8E__) )
34- #if defined(ESP8266_AT_USE_SAM_DUE)
35- #undef ESP8266_AT_USE_SAM_DUE
36- #endif
37- #define ESP8266_AT_USE_SAM_DUE true
37+ #elif ( defined(ARDUINO_SAM_DUE) || defined(__SAM3X8E__) )
38+ #if defined(ESP8266_AT_USE_SAM_DUE)
39+ #undef ESP8266_AT_USE_SAM_DUE
3840#endif
41+ #define ESP8266_AT_USE_SAM_DUE true
3942
40- #if ( defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRWIFI1010) \
41- || defined (ARDUINO_SAMD_NANO_33_IOT) || defined (ARDUINO_SAMD_MKRFox1200) || defined (ARDUINO_SAMD_MKRWAN1300) || defined (ARDUINO_SAMD_MKRWAN1310) \
42- || defined (ARDUINO_SAMD_MKRGSM1400) || defined (ARDUINO_SAMD_MKRNB1500) || defined (ARDUINO_SAMD_MKRVIDOR4000) || defined (__SAMD21G18A__) \
43- || defined (ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) )
44- #if defined(ESP8266_AT_USE_SAMD)
45- #undef ESP8266_AT_USE_SAMD
46- #endif
47- #define ESP8266_AT_USE_SAMD true
43+ #elif ( defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRWIFI1010) \
44+ || defined (ARDUINO_SAMD_NANO_33_IOT) || defined (ARDUINO_SAMD_MKRFox1200) || defined (ARDUINO_SAMD_MKRWAN1300) || defined (ARDUINO_SAMD_MKRWAN1310) \
45+ || defined (ARDUINO_SAMD_MKRGSM1400) || defined (ARDUINO_SAMD_MKRNB1500) || defined (ARDUINO_SAMD_MKRVIDOR4000) || defined (__SAMD21G18A__) \
46+ || defined (ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined (__SAMD21E18A__) || defined (__SAMD51__) || defined (__SAMD51J20A__) || defined (__SAMD51J19A__) \
47+ || defined (__SAMD51G19A__) || defined (__SAMD21G18A__) )
48+ #if defined(ESP8266_AT_USE_SAMD)
49+ #undef ESP8266_AT_USE_SAMD
4850#endif
51+ #define ESP8266_AT_USE_SAMD true
4952
50- #ifdef CORE_TEENSY
51- // For Teensy 4.0
52- #define EspSerial Serial2 // Serial2, Pin RX2 : 7, TX2 : 8
53- #if defined(__IMXRT1062__)
54- #define BOARD_TYPE " TEENSY 4.0"
55- #elif ( defined(__MKL26Z64__) || defined(ARDUINO_ARCH_AVR) )
56- #define BOARD_TYPE " TEENSY LC or 2.0"
57- #else
58- #define BOARD_TYPE " TEENSY 3.X"
59- #endif
60-
61- #elif (ESP8266_AT_USE_SAMD)
53+ #elif ( defined(NRF52840_FEATHER) || defined(NRF52832_FEATHER) || defined(NRF52_SERIES) || defined(ARDUINO_NRF52_ADAFRUIT) || \
54+ defined (NRF52840_FEATHER_SENSE) || defined(NRF52840_ITSYBITSY) || defined(NRF52840_CIRCUITPLAY) || defined(NRF52840_CLUE) || \
55+ defined(NRF52840_METRO) || defined(NRF52840_PCA10056) || defined(PARTICLE_XENON) || defined(NINA_B302_ublox) || defined(NINA_B112_ublox) )
56+ #if defined(ESP8266_AT_USE_NRF528XX)
57+ #undef ESP8266_AT_USE_NRF528XX
58+ #endif
59+ #define ESP8266_AT_USE_NRF528XX true
60+
61+ #endif
62+
63+ #if (ESP8266_AT_USE_NRF528XX)
64+
65+ #if defined(NRF52840_FEATHER)
66+ #define BOARD_TYPE " NRF52840_FEATHER"
67+ #elif defined(NRF52832_FEATHER)
68+ #define BOARD_TYPE " NRF52832_FEATHER"
69+ #elif defined(NRF52840_FEATHER_SENSE)
70+ #define BOARD_TYPE " NRF52840_FEATHER_SENSE"
71+ #elif defined(NRF52840_ITSYBITSY)
72+ #define BOARD_TYPE " NRF52840_ITSYBITSY"
73+ #elif defined(NRF52840_CIRCUITPLAY)
74+ #define BOARD_TYPE " NRF52840_CIRCUITPLAY"
75+ #elif defined(NRF52840_CLUE)
76+ #define BOARD_TYPE " NRF52840_CLUE"
77+ #elif defined(NRF52840_METRO)
78+ #define BOARD_TYPE " NRF52840_METRO"
79+ #elif defined(NRF52840_PCA10056)
80+ #define BOARD_TYPE " NRF52840_PCA10056"
81+ #elif defined(NINA_B302_ublox)
82+ #define BOARD_TYPE " NINA_B302_ublox"
83+ #elif defined(NINA_B112_ublox)
84+ #define BOARD_TYPE " NINA_B112_ublox"
85+ #elif defined(PARTICLE_XENON)
86+ #define BOARD_TYPE " PARTICLE_XENON"
87+ #elif defined(ARDUINO_NRF52_ADAFRUIT)
88+ #define BOARD_TYPE " ARDUINO_NRF52_ADAFRUIT"
89+ #else
90+ #define BOARD_TYPE " nRF52 Unknown"
91+ #endif
92+
93+ #define EspSerial Serial1
94+
95+ #elif (ESP8266_AT_USE_SAMD)
6296// For SAMD
63- #define EspSerial Serial1
64-
65- #if defined(ARDUINO_SAMD_ZERO)
66- #define BOARD_TYPE " SAMD Zero"
67- #elif defined(ARDUINO_SAMD_MKR1000)
68- #define BOARD_TYPE " SAMD MKR1000"
69- #elif defined(ARDUINO_SAMD_MKRWIFI1010)
70- #define BOARD_TYPE " SAMD MKRWIFI1010"
71- #elif defined(ARDUINO_SAMD_NANO_33_IOT)
72- #define BOARD_TYPE " SAMD NANO_33_IOT"
73- #elif defined(ARDUINO_SAMD_MKRFox1200)
74- #define BOARD_TYPE " SAMD MKRFox1200"
75- #elif ( defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) )
76- #define BOARD_TYPE " SAMD MKRWAN13X0"
77- #elif defined(ARDUINO_SAMD_MKRGSM1400)
78- #define BOARD_TYPE " SAMD MKRGSM1400"
79- #elif defined(ARDUINO_SAMD_MKRNB1500)
80- #define BOARD_TYPE " SAMD MKRNB1500"
81- #elif defined(ARDUINO_SAMD_MKRVIDOR4000)
82- #define BOARD_TYPE " SAMD MKRVIDOR4000"
83- #elif defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS)
84- #define BOARD_TYPE " SAMD ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS"
85- #elif ( defined(__SAM3X8E__) || (__SAM3X8E__) || (__CPU_ARC__) )
86- #define BOARD_TYPE " SAMD Board"
87- #else
88- #define BOARD_TYPE " SAMD Unknown"
89- #endif
90-
91- #elif (ESP8266_AT_USE_SAM_DUE)
97+ #define EspSerial Serial1
98+
99+ #if defined(ARDUINO_SAMD_ZERO)
100+ #define BOARD_TYPE " SAMD Zero"
101+ #elif defined(ARDUINO_SAMD_MKR1000)
102+ #define BOARD_TYPE " SAMD MKR1000"
103+ #elif defined(ARDUINO_SAMD_MKRWIFI1010)
104+ #define BOARD_TYPE " SAMD MKRWIFI1010"
105+ #elif defined(ARDUINO_SAMD_NANO_33_IOT)
106+ #define BOARD_TYPE " SAMD NANO_33_IOT"
107+ #elif defined(ARDUINO_SAMD_MKRFox1200)
108+ #define BOARD_TYPE " SAMD MKRFox1200"
109+ #elif ( defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) )
110+ #define BOARD_TYPE " SAMD MKRWAN13X0"
111+ #elif defined(ARDUINO_SAMD_MKRGSM1400)
112+ #define BOARD_TYPE " SAMD MKRGSM1400"
113+ #elif defined(ARDUINO_SAMD_MKRNB1500)
114+ #define BOARD_TYPE " SAMD MKRNB1500"
115+ #elif defined(ARDUINO_SAMD_MKRVIDOR4000)
116+ #define BOARD_TYPE " SAMD MKRVIDOR4000"
117+ #elif defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS)
118+ #define BOARD_TYPE " SAMD ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS"
119+ #elif defined(ADAFRUIT_ITSYBITSY_M4_EXPRESS)
120+ #define BOARD_TYPE " SAMD ADAFRUIT_ITSYBITSY_M4_EXPRESS"
121+ #elif defined(__SAMD21E18A__)
122+ #define BOARD_TYPE " SAMD21E18A"
123+ #elif defined(__SAMD21G18A__)
124+ #define BOARD_TYPE " SAMD21G18A"
125+ #elif defined(__SAMD51G19A__)
126+ #define BOARD_TYPE " SAMD51G19A"
127+ #elif defined(__SAMD51J19A__)
128+ #define BOARD_TYPE " SAMD51J19A"
129+ #elif defined(__SAMD51J20A__)
130+ #define BOARD_TYPE " SAMD51J20A"
131+ #elif defined(__SAM3X8E__)
132+ #define BOARD_TYPE " SAM3X8E"
133+ #elif defined(__CPU_ARC__)
134+ #define BOARD_TYPE " CPU_ARC"
135+ #elif defined(__SAMD51__)
136+ #define BOARD_TYPE " SAMD51"
137+ #else
138+ #define BOARD_TYPE " SAMD Unknown"
139+ #endif
140+
141+ #elif (ESP8266_AT_USE_SAM_DUE)
92142// For SAM DUE
93- #define EspSerial Serial1
94- #define BOARD_TYPE " SAM DUE"
95-
96- #elif (ESP8266_AT_USE_STM32)
143+ #define EspSerial Serial1
144+ #define BOARD_TYPE " SAM DUE"
145+
146+ #elif (ESP8266_AT_USE_STM32)
97147// For STM32F
98- #define EspSerial Serial1
99- #define BOARD_TYPE " STM32F"
148+ #define EspSerial Serial1
149+ #define BOARD_TYPE " STM32F"
150+
100151#else
101152// For other boards. Change Serial as necessary
102153#define EspSerial Serial3
103154#define BOARD_TYPE " Unknown"
104- #endif
105155
106- // Must be before #include <ESP_AT_WiFiManager.h>
156+ #endif // ESP8266_AT_USE_NRF528XX
157+
158+ // Must be before #include <ESP_AT_WiFiManager.h>
107159#define EEPROM_START 0
108160
109161#include < ESP_AT_WiFiManager.h> // https://github.com/khoih-prog/ESP_AT_WiFiManager
@@ -120,7 +172,7 @@ String ssid = "ESP_AT_" + String(0xABCDEF, HEX);
120172
121173const char * password = " ESP_AT_PW" ;
122174
123- IPAddress staticAP_IP = IPAddress(192 ,168 ,100 ,1 );
175+ IPAddress staticAP_IP = IPAddress(192 , 168 , 100 , 1 );
124176
125177// SSID and PW for your Router
126178String Router_SSID;
@@ -171,48 +223,48 @@ void enterConfigPortal(void)
171223 ESP_AT_WiFiManager ESP_AT_wiFiManager;
172224
173225 ESP_AT_wiFiManager.setDebugOutput (true );
174-
226+
175227 ESP_AT_wiFiManager.setMinimumSignalQuality (-1 );
176228
177229 ESP_AT_wiFiManager.setAPChannel (1 );
178230
179231 // Default AP IP is 192.168.4.1. Uncomment to use different AP IP
180232 ESP_AT_wiFiManager.setAPStaticIPConfig (staticAP_IP);
181-
233+
182234 // Set static STA IP
183- ESP_AT_wiFiManager.setSTAStaticIPConfig (IPAddress (192 ,168 ,2 , 114 ));
184-
235+ ESP_AT_wiFiManager.setSTAStaticIPConfig (IPAddress (192 , 168 , 2 , 114 ));
236+
185237 // Check if there is stored WiFi router/password credentials.
186238 // If not found, device will remain in configuration mode until switched off via webserver.
187239 Serial.println (" Opening Config Portal." );
188-
240+
189241 Router_SSID = ESP_AT_wiFiManager.WiFi_SSID ();
190242 Router_Pass = ESP_AT_wiFiManager.WiFi_Pass ();
191-
243+
192244 if (Router_SSID != " " )
193245 {
194246 ESP_AT_wiFiManager.setConfigPortalTimeout (60 ); // If no access point name has been previously entered disable timeout.
195247 Serial.println (F (" Got stored Credentials. Timeout 60s" ));
196248 }
197249 else
198250 Serial.println (F (" No stored Credentials. No timeout" ));
199-
200- // SSID to uppercase
201- ssid.toUpperCase ();
202-
251+
252+ // SSID to uppercase
253+ ssid.toUpperCase ();
254+
203255 // Starts an AP and goes into a blocking loop awaiting configuration
204256 Serial.println (" Start Config Portal, SSID = " + ssid + " , Pass = " + password);
205-
257+
206258 digitalWrite (LOCAL_PIN_LED, LED_ON); // Turn led on as we enter Config Portal
207-
208- if (!ESP_AT_wiFiManager.startConfigPortal ((const char *) ssid.c_str (), password))
259+
260+ if (!ESP_AT_wiFiManager.startConfigPortal ((const char *) ssid.c_str (), password))
209261 Serial.println (F (" Not connected to WiFi but continuing anyway." ));
210- else
262+ else
211263 Serial.println (F (" WiFi connected...yeey" ));
212-
264+
213265 digitalWrite (LOCAL_PIN_LED, LED_OFF); // Turn led off as we exit Config Portal
214266
215- // Get Router SSID and PASS from EEPROM, then open Config portal AP named "ESP_AT_XXXXXX_AutoConnectAP" and PW "ESP_AT_PW"
267+ // Get Router SSID and PASS from EEPROM, then open Config portal AP named "ESP_AT_XXXXXX_AutoConnectAP" and PW "ESP_AT_PW"
216268 // 1) If got stored Credentials, Config portal timeout is 60s
217269 // 2) If no stored Credentials, stay in Config portal until get WiFi Credentials
218270 if (!ESP_AT_wiFiManager.autoConnect (ssid.c_str (), password))
@@ -228,20 +280,20 @@ void setup()
228280 // initialize the LED digital pin as an output.
229281 pinMode (LOCAL_PIN_LED, OUTPUT);
230282 digitalWrite (LOCAL_PIN_LED, LED_ON); // turn the LED on by making the voltage LOW to tell us we are in configuration mode.
231-
283+
232284 Serial.begin (115200 );
233-
234- delay ( 500 );
285+ while (!Serial);
286+
235287 Serial.println (" \n Start AutoConnect on " + String (BOARD_TYPE));
236288
237289 // initialize serial for ESP module
238290 EspSerial.begin (115200 );
239-
291+
240292 // initialize ESP module
241- WiFi.init (&EspSerial);
242-
293+ WiFi.init (&EspSerial);
294+
243295 // check for the presence of the shield
244- if (WiFi.status () == WL_NO_SHIELD)
296+ if (WiFi.status () == WL_NO_SHIELD)
245297 {
246298 Serial.println (F (" WiFi shield not present" ));
247299 // don't continue
0 commit comments