Ein WLAN-Netzfinder mit einem ESP8266.
//------------------------------------------------------------------------------------------------------------ // WiFi Netzwerk-Scanner Demoprogramm mit ESP8266 ESP-01 //------------------------------------------------------------------------------------------------------------ //(c) Roland Elmiger, HB9GAA // // Version 1.0B // // Enwickelt am : 26.05.2015 // Letzter Update : 14.09.2015 A. Bieri http://www.esp8266.com/viewtopic.php?f=29&t=4612 //------------------------------------------------------------------------------------------------------------
#include "ESP8266WiFi.h"
const char* WiFiSSID = "xxxxxx"; const char* WiFiPSK = "password"; // Host const char* host = "www.example.com"; const int httpPort = 80;
// Initialisierung ...
//------------------------------------------------------------------------------------------------------------
void setup()
{
uart_div_modify(0, UART_CLK_FREQ / 115200); //damit Serial.printf verwendet werden kann
Serial.begin(115200);
WiFi.mode(WIFI_STA); //WiFi als Station definieren
WiFi.disconnect(); //und eine ev. bestehende Verbindung trennen
delay(100);
Serial.printf("Initialisierung beendet\r\n");
connectWiFi();
Serial.printf("Verbindung aufgebaut\r\n");
connectTCP();
Serial.println("Stream geschlossen");
}
//------------------------------------------------------------------------------------------------------------
void connectWiFi()
{
// WiFI.begin([ssid], [passkey]) initiates a WiFI connection
// to the stated [ssid], using the [passkey] as a WPA, WPA2,
// or WEP passphrase.
WiFi.begin(WiFiSSID, WiFiPSK);
// Use the WiFi.status() function to check if the ESP8266
// is connected to a WiFi network.
Serial.println("Im Netz registrieren");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi verbunden");
Serial.println("IP Addresse: ");
Serial.println(WiFi.localIP());
}
//------------------------------------------------------------------------------------------------------------
void connectTCP()
{
// connect to a web server and read stream
// first open connection
Serial.print("Connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// This will send the request to the server
client.print(String("GET /") + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
delay(10);
Serial.print("client.status(): ");
Serial.println(client.status());
Serial.print("client.available(): ");
Serial.println(client.available());
Serial.print("client.connected(): ");
Serial.println(client.connected());
// Read all the lines of the reply from server and print them to Serial
while(client.connected()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
delay(5000);
}
// Hauptprogramm ...
//------------------------------------------------------------------------------------------------------------
void loop()
{
Serial.printf("\r\nScan starten ... ");
int n = WiFi.scanNetworks(); //WiFi.scanNetworks gibt die Anzahl gefundene AP zurück
Serial.printf("Scan beendet\n\n");
if (n == 0)
Serial.printf("keine Netzwerke gefunden\r\n");
else
{
Serial.printf("%2u Netzwerke gefunden (* = ungesichertes Netzwerk)\n---------------------------------------------------\n", n);
for (int i = 0; i < n; ++i) //SSID und RSSI fuer jedes gefundene Netzwerk ausgeben
{
Serial.printf("%2u: %s\t(%i dBm)%s ", i+1, (char*)WiFi.SSID(i), (char*)WiFi.RSSI(i)); printEncryptionType(WiFi.encryptionType(i));
}
}
delay(5000); // etwas warten bis zum naechsten Scann
}
// Verschlüsselungstyp ausgeben
//------------------------------------------------------------------------------------------------------------
void printEncryptionType(int thisType)
{
switch (thisType)
{
case ENC_TYPE_WEP:
Serial.printf("WEP\n");
break;
case ENC_TYPE_TKIP:
Serial.printf("WPA\n");
break;
case ENC_TYPE_CCMP:
Serial.printf("WPA2\n");
break;
case ENC_TYPE_NONE:
Serial.printf("*\n");
break;
case ENC_TYPE_AUTO:
Serial.printf("Auto\n");
break;
}
}