Created
February 26, 2019 13:18
-
-
Save mykeels/bc5eeb7de660bf6e9ac512274f150cc1 to your computer and use it in GitHub Desktop.
Arduino -> ESP8266 Wifi Module program to list available WiFi networks
This file contains 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
// See https://github.com/bportaluri/WiFiEsp | |
// See blog post https://medium.com/@mykeels/connect-to-wifi-with-arduino-9eee4b02d904 | |
#include <WiFiEsp.h> | |
#ifndef HAVE_HWSERIAL1 | |
#include <SoftwareSerial.h> | |
SoftwareSerial wSerial(6, 7); | |
#endif | |
void setup() { | |
Serial.begin(9600); | |
wSerial.begin(115200); | |
WiFi.init(&wSerial); | |
while (WiFi.status() == WL_NO_SHIELD) { | |
Serial.println("WiFi shield not present"); | |
delay(1000); | |
} | |
} | |
void loop() { | |
Serial.println(); | |
Serial.println("Scanning available networks ..."); | |
listNetworks(); | |
delay(10000); | |
} | |
void listNetworks() { | |
// scan for nearby networks | |
int numSsid = WiFi.scanNetworks(); | |
while (numSsid == -1) { | |
Serial.println("Couldn't get a wifi connection"); | |
delay(3000); | |
numSsid = WiFi.scanNetworks(); | |
} | |
// print the list of networks seen | |
Serial.print("Number of available networks:"); | |
Serial.println(numSsid); | |
// print the network number and name for each network found | |
for (int thisNet = 0; thisNet < numSsid; thisNet++) { | |
Serial.print(thisNet); | |
Serial.print(") "); | |
Serial.print(WiFi.SSID(thisNet)); | |
Serial.print("\tSignal: "); | |
Serial.print(WiFi.RSSI(thisNet)); | |
Serial.print(" dBm"); | |
Serial.print("\tEncryption: "); | |
printEncryptionType(WiFi.encryptionType(thisNet)); | |
} | |
} | |
void printEncryptionType(int thisType) { | |
// read the encryption type and print out the name | |
switch (thisType) { | |
case ENC_TYPE_WEP: | |
Serial.print("WEP"); | |
break; | |
case ENC_TYPE_WPA_PSK: | |
Serial.print("WPA_PSK"); | |
break; | |
case ENC_TYPE_WPA2_PSK: | |
Serial.print("WPA2_PSK"); | |
break; | |
case ENC_TYPE_WPA_WPA2_PSK: | |
Serial.print("WPA_WPA2_PSK"); | |
break; | |
case ENC_TYPE_NONE: | |
Serial.print("None"); | |
break; | |
} | |
Serial.println(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment