Created
May 15, 2023 15:58
-
-
Save kakopappa/4a7807b4cd4e9e3c5e84003b7cb5b2e6 to your computer and use it in GitHub Desktop.
Example for Arduino ESP8266, ESP32 WiFi connect with max tries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <ESP8266WiFi.h> | |
| #define WIFI_SSID "MY SSID" | |
| #define WIFI_PASSWORD "MY PASS" | |
| bool isConnected() { | |
| return (WiFi.status() == WL_CONNECTED); | |
| } | |
| void connectToWiFi(int max_tries = 20, int pause = 500) { | |
| int i = 0; | |
| WiFi.mode(WIFI_STA); | |
| #if defined(ARDUINO_ARCH_ESP8266) | |
| WiFi.forceSleepWake(); | |
| delay(200); | |
| #endif | |
| WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | |
| do { | |
| delay(pause); | |
| Serial.print("."); | |
| } while (!isConnected() || i++ < max_tries); | |
| WiFi.setAutoReconnect(true); | |
| WiFi.persistent(true); | |
| } | |
| void setup() | |
| { | |
| Serial.begin(115200); | |
| connectToWiFi(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment