@@ -185,9 +185,155 @@ When the time passes, the startConfigPortal function will return and continue th
185185
186186#### On Demand Configuration Portal
187187
188- Example usage
188+ Sample Code
189189
190190``` cpp
191+ #define _ESP_AT_LOGLEVEL_ 0
192+ #define DEBUG_WIFIMGR true //false
193+
194+ #if ( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) )
195+ #if defined(ESP8266_AT_USE_STM32)
196+ #undef ESP8266_AT_USE_STM32
197+ #endif
198+ #define ESP8266_AT_USE_STM32 true
199+ #endif
200+
201+ #if ( defined(ARDUINO_SAM_DUE) || defined(__SAM3X8E__) )
202+ #if defined(ESP8266_AT_USE_SAM_DUE)
203+ #undef ESP8266_AT_USE_SAM_DUE
204+ #endif
205+ #define ESP8266_AT_USE_SAM_DUE true
206+ #endif
207+
208+ #if ( defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRWIFI1010) \
209+ || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_SAMD_MKRFox1200) || defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) \
210+ || defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKRNB1500) || defined(ARDUINO_SAMD_MKRVIDOR4000) || defined(__ SAMD21G18A__ ) \
211+ || defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) )
212+ #if defined(ESP8266_AT_USE_SAMD)
213+ #undef ESP8266_AT_USE_SAMD
214+ #endif
215+ #define ESP8266_AT_USE_SAMD true
216+ #endif
217+
218+ #ifdef CORE_TEENSY
219+ // For Teensy 4.0
220+ #define EspSerial Serial2 //Serial2, Pin RX2 : 7, TX2 : 8
221+ #if defined(__ IMXRT1062__ )
222+ #define BOARD_TYPE "TEENSY 4.0"
223+ #elif ( defined(__ MKL26Z64__ ) || defined(ARDUINO_ARCH_AVR) )
224+ #define BOARD_TYPE "TEENSY LC or 2.0"
225+ #else
226+ #define BOARD_TYPE "TEENSY 3.X"
227+ #endif
228+
229+ #elif (ESP8266_AT_USE_SAMD)
230+ // For SAMD
231+ #define EspSerial Serial1
232+
233+ #if defined(ARDUINO_SAMD_ZERO)
234+ #define BOARD_TYPE "SAMD Zero"
235+ #elif defined(ARDUINO_SAMD_MKR1000)
236+ #define BOARD_TYPE "SAMD MKR1000"
237+ #elif defined(ARDUINO_SAMD_MKRWIFI1010)
238+ #define BOARD_TYPE "SAMD MKRWIFI1010"
239+ #elif defined(ARDUINO_SAMD_NANO_33_IOT)
240+ #define BOARD_TYPE "SAMD NANO_33_IOT"
241+ #elif defined(ARDUINO_SAMD_MKRFox1200)
242+ #define BOARD_TYPE "SAMD MKRFox1200"
243+ #elif ( defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) )
244+ #define BOARD_TYPE "SAMD MKRWAN13X0"
245+ #elif defined(ARDUINO_SAMD_MKRGSM1400)
246+ #define BOARD_TYPE "SAMD MKRGSM1400"
247+ #elif defined(ARDUINO_SAMD_MKRNB1500)
248+ #define BOARD_TYPE "SAMD MKRNB1500"
249+ #elif defined(ARDUINO_SAMD_MKRVIDOR4000)
250+ #define BOARD_TYPE "SAMD MKRVIDOR4000"
251+ #elif defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS)
252+ #define BOARD_TYPE "SAMD ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS"
253+ #elif ( defined(__ SAM3X8E__ ) || (__ SAM3X8E__ ) || (__ CPU_ARC__ ) )
254+ #define BOARD_TYPE "SAMD Board"
255+ #else
256+ #define BOARD_TYPE "SAMD Unknown"
257+ #endif
258+
259+ #elif (ESP8266_AT_USE_SAM_DUE)
260+ // For SAM DUE
261+ #define EspSerial Serial1
262+ #define BOARD_TYPE "SAM DUE"
263+
264+ #elif (ESP8266_AT_USE_STM32)
265+ // For STM32F
266+ #define EspSerial Serial1
267+ #define BOARD_TYPE "STM32F"
268+ #else
269+ // For other boards. Change Serial as necessary
270+ #define EspSerial Serial3
271+ #define BOARD_TYPE "Unknown"
272+ #endif
273+
274+ // Must be before #include <ESP_AT_WiFiManager.h>
275+ #define EEPROM_START 0
276+
277+ #include < ESP_AT_WiFiManager.h> // https://github.com/khoih-prog/ESP_AT_WiFiManager
278+
279+ // Your Mega <-> ESP8266 baud rate:
280+ #define ESP8266_BAUD 115200
281+
282+ // SSID and PW for Config Portal
283+ #ifdef CORE_TEENSY
284+ String ssid = "ESP_AT_ " + String(0x1ABCDEF, HEX);
285+ #else
286+ String ssid = "ESP_AT_ " + String(0xABCDEF, HEX);
287+ #endif
288+
289+ const char * password = " ESP_AT_PW" ;
290+
291+ IPAddress staticAP_IP = IPAddress(192 ,168 ,100 ,1 );
292+
293+ // SSID and PW for your Router
294+ String Router_SSID;
295+ String Router_Pass;
296+
297+ // Onboard LED I/O pin on board
298+ const int LOCAL_PIN_LED = 13 ; // Pin 13, Controls the onboard LED.
299+
300+ #define LED_ON HIGH
301+ #define LED_OFF LOW
302+
303+ void heartBeatPrint (void)
304+ {
305+ static int num = 1;
306+
307+ if (WiFi.status() == WL_CONNECTED)
308+ Serial.print("H"); // H means connected to WiFi
309+ else
310+ Serial.print("F"); // F means not connected to WiFi
311+
312+ if (num == 80)
313+ {
314+ Serial.println();
315+ num = 1;
316+ }
317+ else if (num++ % 10 == 0)
318+ {
319+ Serial.print(" ");
320+ }
321+ }
322+
323+ void check_status()
324+ {
325+ static unsigned long checkstatus_timeout = 0;
326+
327+ //KH
328+ #define HEARTBEAT_INTERVAL 10000L
329+ // Print hearbeat every HEARTBEAT_INTERVAL (10) seconds.
330+ if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0))
331+ {
332+ heartBeatPrint();
333+ checkstatus_timeout = millis() + HEARTBEAT_INTERVAL;
334+ }
335+ }
336+
191337void enterConfigPortal(void)
192338{
193339 //Local intialization. Once its business is done, there is no need to keep it around
@@ -203,7 +349,7 @@ void enterConfigPortal(void)
203349 ESP_AT_wiFiManager.setAPStaticIPConfig(staticAP_IP);
204350
205351 // Set static STA IP
206- ESP_AT_wiFiManager.setSTAStaticIPConfig(IPAddress(192,168,2,114));
352+ ESP_AT_wiFiManager.setSTAStaticIPConfig(IPAddress(192,168,2,114));
207353
208354 //Check if there is stored WiFi router/password credentials.
209355 //If not found, device will remain in configuration mode until switched off via webserver.
@@ -221,7 +367,7 @@ void enterConfigPortal(void)
221367 Serial.println(F("No stored Credentials. No timeout"));
222368
223369 // SSID to uppercase
224- ssid.toUpperCase();
370+ ssid.toUpperCase();
225371
226372 //Starts an AP and goes into a blocking loop awaiting configuration
227373 Serial.println("Start Config Portal, SSID = " + ssid + ", Pass = " + password);
@@ -236,18 +382,68 @@ void enterConfigPortal(void)
236382 digitalWrite(LOCAL_PIN_LED, LED_OFF); // Turn led off as we exit Config Portal
237383}
238384
239- void loop ()
385+ void setup ()
240386{
241- // is configuration portal requested?
242- if ((digitalRead(TRIGGER_PIN) == LOW) || (digitalRead(TRIGGER_PIN2) == LOW))
387+ // put your setup code here, to run once:
388+ // initialize the LED digital pin as an output.
389+ pinMode(LOCAL_PIN_LED, OUTPUT);
390+ digitalWrite(LOCAL_PIN_LED, LED_ON); // turn the LED on by making the voltage LOW to tell us we are in configuration mode.
391+
392+ Serial.begin(115200);
393+
394+ unsigned long startedAt = millis();
395+ delay(200);
396+ Serial.println("\nStart ConfigOnStartup on " + String(BOARD_TYPE));
397+
398+ // initialize serial for ESP module
399+ EspSerial.begin(115200);
400+
401+ // initialize ESP module
402+ WiFi.init(&EspSerial);
403+
404+ // check for the presence of the shield
405+ if (WiFi.status() == WL_NO_SHIELD)
243406 {
244- Serial.println("\nConfig Portal requested.");
245- enterConfigPortal();
407+ Serial.println(F("WiFi shield not present"));
408+ // don't continue
409+ while (true);
246410 }
247-
411+
412+ enterConfigPortal();
413+
414+ // For some unknown reason webserver can only be started once per boot up
415+ // so webserver can not be used again in the sketch.
416+ #define WIFI_CONNECT_TIMEOUT 30000L
417+ #define WHILE_LOOP_DELAY 200L
418+ #define WHILE_LOOP_STEPS (WIFI_CONNECT_TIMEOUT / ( 3 * WHILE_LOOP_DELAY ))
419+
420+ startedAt = millis();
421+
422+ while ( (WiFi.status() != WL_CONNECTED) && (millis() - startedAt < WIFI_CONNECT_TIMEOUT ) )
423+ {
424+ int i = 0;
425+ while((!WiFi.status() || WiFi.status() >= WL_DISCONNECTED) && i++ < WHILE_LOOP_STEPS)
426+ {
427+ delay(WHILE_LOOP_DELAY);
428+ }
429+ }
430+
431+ Serial.print(F("After waiting "));
432+ Serial.print((millis()- startedAt) / 1000);
433+ Serial.print(F(" secs in setup(), connect result is "));
434+
435+ if (WiFi.status() == WL_CONNECTED)
436+ {
437+ Serial.print(F("connected. Local IP: "));
438+ Serial.println(WiFi.localIP());
439+ }
440+ }
441+
442+
443+ void loop()
444+ {
248445 // put your main code here, to run repeatedly
249446 check_status();
250-
251447}
252448```
253449
0 commit comments