DrRandom

wifi scan request

Jul 27th, 2021
809
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. server.on("/scan", HTTP_GET, [](AsyncWebServerRequest *request){
  2.   String json = "[";
  3.   int n = WiFi.scanComplete();
  4.   if(n == -2){
  5.     WiFi.scanNetworks(true);
  6.   } else if(n){
  7.     for (int i = 0; i < n; ++i){
  8.       if(i) json += ",";
  9.       json += "{";
  10.       json += "\"rssi\":"+String(WiFi.RSSI(i));
  11.       json += ",\"ssid\":\""+WiFi.SSID(i)+"\"";
  12.       json += ",\"bssid\":\""+WiFi.BSSIDstr(i)+"\"";
  13.       json += ",\"channel\":"+String(WiFi.channel(i));
  14.       json += ",\"secure\":"+String(WiFi.encryptionType(i));
  15.       json += ",\"hidden\":"+String(WiFi.isHidden(i)?"true":"false");
  16.       json += "}";
  17.     }
  18.     WiFi.scanDelete();
  19.     if(WiFi.scanComplete() == -2){
  20.       WiFi.scanNetworks(true);
  21.     }
  22.   }
  23.   json += "]";
  24.   request->send(200, "application/json", json);
  25.   json = String();
  26. });
RAW Paste Data