Instantly share code, notes, and snippets.

Embed
What would you like to do?
Network scanner on ESP8266
/**
* ESP8266 Scanner - Scans for networks on the 2.4GHz band and prints them out to the console.
* Program it onto your ESP8266, pull up miniterm, the Serial Monitor, etc, and view a list of
* wireless networks on your computer.
* Requres Arduino JSON (http://arduinojson.org/), downloadable from Library Manager or GitHub.
* Or, set OUTPUT_JSON and consume the serial output with your favorite JSON parser.
*
* Created by Leo Tindall in 2017 for the ESP8266.
* This software is licensed under the GNU General Public License, Version 3.0.
*/
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
// Uncomment to enable JSON format output
//#define OUTPUT_JSON
// Delay between scanning in milliseconds
#define SCAN_DELAY 1000
// The amount of memory to allocate for the static JSON buffer.
// Too large, and the stack will overflow into the heap.
// Too small, and the JSON construction will overflow its bounds.
// Turns out the ESP8266 just doesn't have that much RAM.
// 29000 bytes means about 21000 bytes left for stack.
#define JSON_BUFFER_SIZE 29000
StaticJsonBuffer<JSON_BUFFER_SIZE> jsonBuffer;
char* encTypeToString(int encType) {
if (encType == ENC_TYPE_NONE) {
return "none";
}
if (encType == ENC_TYPE_WEP) {
return "wep";
}
if (encType == ENC_TYPE_TKIP) {
return "wpa_psk";
}
if (encType == ENC_TYPE_CCMP) {
return "wpa2_psk";
}
if (encType == ENC_TYPE_AUTO) {
return "wpa_wpa2_psk";
}
return "wpa2_enterprise";
}
void printScanResultJSON(int networksFound) {
JsonObject& root = jsonBuffer.createObject();
JsonArray& ssidArray = root.createNestedArray("network_descriptions");
root["networks"] = networksFound;
for (int i = 0; i < networksFound; i++)
{
// To prevent watchdog killing us during printing
delay(1);
JsonObject& ssid = ssidArray.createNestedObject();
ssid["essid"] = WiFi.SSID(i);
ssid["bssid"] = WiFi.BSSIDstr(i);
ssid["channel"] = WiFi.channel(i);
ssid["rssi"] = WiFi.RSSI(i);
ssid["security"] = encTypeToString(WiFi.encryptionType(i));
}
root.printTo(Serial);
}
void printScanResult(int networksFound)
{
for (int i = 0; i < networksFound; i++)
{
// To prevent watchdog killing us during printing
delay(1);
Serial.printf("[ESP8266] %d: %s (%s), Ch:%d (%ddBm) %s\n", i + 1, WiFi.SSID(i).c_str(), WiFi.BSSIDstr(i).c_str(), WiFi.channel(i), WiFi.RSSI(i), encTypeToString(WiFi.encryptionType(i)));
}
Serial.println();
}
void setup() {
// Initialize LED pin as an output, so it can be used to inform the user when scanning occurs.
pinMode(LED_BUILTIN, OUTPUT);
// Set up serial communication at 115200 baud.
Serial.begin(115200);
// Print banner to satisfy impatient users.
#ifndef OUTPUT_JSON
Serial.println("[ESP8266] ESP8266 Scanner starting up.");
#endif
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
int nSsids = WiFi.scanNetworks(false, true);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
#ifndef OUTPUT_JSON
printScanResult(nSsids);
#else
printScanResultJSON(nSsids);
#endif
delay(SCAN_DELAY); // wait for a second
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment