Created
March 14, 2016 13:08
-
-
Save patrickjahns/74707d1d7361124d5fb0 to your computer and use it in GitHub Desktop.
SMING Scan networks polling
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
static BssList networks; | |
static bool scanning = false; | |
void networkScanCompleted(bool succeeded, BssList list) | |
{ | |
if (succeeded) | |
{ | |
networks.clear(); | |
for (int i = 0; i < list.count(); i++) | |
if (!list[i].hidden && list[i].ssid.length() > 0) | |
networks.add(list[i]); | |
} | |
networks.sort([](const BssInfo& a, const BssInfo& b){ return b.rssi - a.rssi; } ); | |
scanning = false; | |
} | |
void onNetworks(HttpRequest &request, HttpResponse &response) | |
{ | |
if(!authenticated(request, response)) return; | |
JsonObjectStream* stream = new JsonObjectStream(); | |
JsonObject& json = stream->getRoot(); | |
bool error = false; | |
if (request.getRequestMethod() == RequestMethod::POST) | |
{ | |
String body = request.getBody(); | |
if ( body == NULL || body.length() > 64) | |
{ | |
json["error"] = "bad request"; | |
return; | |
} | |
else | |
{ | |
StaticJsonBuffer<64> jsonBuffer; | |
JsonObject& root = jsonBuffer.parseObject(body); | |
if (root["cmd"].success()) { | |
if(!scanning) { | |
scanNetworks(); | |
} | |
json["success"] = (bool)true; | |
} else { | |
error = true; | |
json["error"] = "bad request"; | |
} | |
} | |
} else { | |
if(scanning) { | |
json["scanning"] = true; | |
} else { | |
json["scanning"] = false; | |
JsonArray& netlist = json.createNestedArray("available"); | |
for (int i = 0; i < networks.count(); i++) | |
{ | |
if (networks[i].hidden) continue; | |
JsonObject &item = netlist.createNestedObject(); | |
item["id"] = (int)networks[i].getHashId(); | |
item["title"] = networks[i].ssid; | |
item["signal"] = networks[i].rssi; | |
item["encryption"] = networks[i].getAuthorizationMethodName(); | |
//limit to max 25 networks | |
if (i >= 25) break; | |
} | |
} | |
} | |
response.setAllowCrossDomainOrigin("*"); | |
response.sendJsonObject(stream); | |
} | |
void scanNetworks() { | |
scanning = true; | |
WifiStation.startScan(networkScanCompleted); | |
} | |
void startWebServer() | |
{ | |
server.listen(80); | |
server.addPath("/networks", onNetworks); | |
} | |
void init() | |
{ | |
//add your init stuff | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment