Advertisement
iqhtyar2k

Esp8266 WifiAuto

Jan 22nd, 2024
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2.  
  3. const char* ssid = "YourWiFiSSID";
  4. const char* password = "YourWiFiPassword";
  5.  
  6. void setup() {
  7.   Serial.begin(115200);
  8.   WiFi.mode(WIFI_STA);
  9.   WiFi.disconnect();
  10.   delay(100);
  11.  
  12.   Serial.println("Scanning available WiFi networks...");
  13.   int n = WiFi.scanNetworks();
  14.   if (n == 0) {
  15.     Serial.println("No networks found");
  16.   } else {
  17.     Serial.println("Found networks:");
  18.     for (int i = 0; i < n; ++i) {
  19.       // Print SSID and RSSI for each network found
  20.       Serial.print(i + 1);
  21.       Serial.print(": ");
  22.       Serial.print(WiFi.SSID(i));
  23.       Serial.print(" (");
  24.       Serial.print(WiFi.RSSI(i));
  25.       Serial.print(")");
  26.       Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*");
  27.       delay(10);
  28.     }
  29.   }
  30.  
  31.   // Connect to an open WiFi network
  32.   for (int i = 0; i < n; ++i) {
  33.     if (WiFi.encryptionType(i) == ENC_TYPE_NONE) {
  34.       Serial.println("Connecting to open network...");
  35.       WiFi.begin(WiFi.SSID(i), "");
  36.       break;
  37.     }
  38.   }
  39.  
  40.   // Wait for connection
  41.   while (WiFi.status() != WL_CONNECTED) {
  42.     delay(500);
  43.     Serial.print(".");
  44.   }
  45.  
  46.   Serial.println("");
  47.   Serial.println("WiFi connected");
  48.   Serial.println("IP address: ");
  49.   Serial.println(WiFi.localIP());
  50. }
  51.  
  52. void loop() {
  53.   // Do nothing
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement