Browse Source

improved wifi scan (must be fired now, not automatically any more)

pull/372/head
lumapu 2 years ago
parent
commit
3764aa4ce1
  1. 17
      tools/esp8266/ahoywifi.cpp
  2. 1
      tools/esp8266/ahoywifi.h
  3. 6
      tools/esp8266/app.cpp
  4. 1
      tools/esp8266/app.h
  5. 7
      tools/esp8266/html/api.js
  6. 35
      tools/esp8266/html/setup.html
  7. 4
      tools/esp8266/webApi.cpp

17
tools/esp8266/ahoywifi.cpp

@ -209,24 +209,25 @@ time_t ahoywifi::getNtpTime(void) {
}
//-----------------------------------------------------------------------------
void ahoywifi::scanAvailNetworks(void) {
int n = WiFi.scanComplete();
if(n == -2)
WiFi.scanNetworks(true);
}
//-----------------------------------------------------------------------------
void ahoywifi::getAvailNetworks(JsonObject obj) {
JsonArray nets = obj.createNestedArray("networks");
int n = WiFi.scanComplete();
if(n == -2) {
WiFi.scanNetworks(true);
} else if(n) {
if(n > 0) {
for (int i = 0; i < n; ++i) {
nets[i]["ssid"] = WiFi.SSID(i);
nets[i]["rssi"] = WiFi.RSSI(i);
// TODO: does github workflow use another version of this library?
// ahoywifi.cpp:223:38: error: 'class WiFiClass' has no member named 'isHidden'
//nets[i]["hidden"] = WiFi.isHidden(i) ? true : false;
}
WiFi.scanDelete();
if(WiFi.scanComplete() == -2)
WiFi.scanNetworks(true);
}
}

1
tools/esp8266/ahoywifi.h

@ -30,6 +30,7 @@ class ahoywifi {
bool setupStation(uint32_t timeout);
bool getApActive(void);
time_t getNtpTime(void);
void scanAvailNetworks(void);
void getAvailNetworks(JsonObject obj);
private:

6
tools/esp8266/app.cpp

@ -609,6 +609,12 @@ bool app::getWifiApActive(void) {
}
//-----------------------------------------------------------------------------
void app::scanAvailNetworks(void) {
mWifi->scanAvailNetworks();
}
//-----------------------------------------------------------------------------
void app::getAvailNetworks(JsonObject obj) {
mWifi->getAvailNetworks(obj);

1
tools/esp8266/app.h

@ -68,6 +68,7 @@ class app {
void saveValues(void);
void resetPayload(Inverter<>* iv);
bool getWifiApActive(void);
void scanAvailNetworks(void);
void getAvailNetworks(JsonObject obj);
uint8_t getIrqPin(void) {

7
tools/esp8266/html/api.js

@ -77,6 +77,13 @@ function sel(name, opt, selId) {
return e;
}
function selDelAllOpt(sel) {
var i, l = sel.options.length - 1;
for(i = l; i >= 0; i--) {
sel.remove(i);
}
}
function opt(val, html) {
o = document.createElement('option');
o.value = val;

35
tools/esp8266/html/setup.html

@ -44,9 +44,11 @@
<fieldset>
<legend class="des">WiFi</legend>
<p>Enter the credentials to your prefered WiFi station. After rebooting the device tries to connect with this information.</p>
<label for="scanbtn">Search Networks</label>
<input type="button" name="scanbtn" id="scanbtn" class="btn" value="scan" onclick="scan()"/><br/>
<label for="networks">Avail Networks</label>
<select name="networks" id="networks" onChange="selNet()">
<option value="-1">scanning ...</option>
<option value="-1">not scanned</option>
</select>
<label for="ssid">SSID</label>
<input type="text" name="ssid" class="text"/>
@ -162,6 +164,15 @@
ivHtml(JSON.parse('{"name":"","serial":"","channels":4,"ch_max_power":[0,0,0,0],"ch_name":["","","",""]}'), highestId + 1);
});
function apiCbWifi(obj) {
var e = document.getElementById("networks");
selDelAllOpt(e);
if(obj["success"])
e.appendChild(opt("-1", "scanning ..."))
else
e.appendChild(opt("-1", "Error: " + obj["error"]));
}
function apiCbNtp(obj) {
var e = document.getElementById("apiResultNtp");
if(obj["success"])
@ -186,6 +197,13 @@
getAjax("/api/setup", apiCbNtp, "POST", JSON.stringify(obj));
}
function scan() {
var obj = new Object();
obj.cmd = "scan_wifi";
getAjax("/api/setup", apiCbWifi, "POST", JSON.stringify(obj));
setTimeout(function() {getAjax('/api/setup/networks', listNetworks)}, 7000);
}
function syncTime() {
var obj = new Object();
obj.cmd = "sync_ntp";
@ -402,27 +420,26 @@
parseRadio(root["radio"]);
parseSerial(root["serial"]);
}
window.setInterval("getAjax('/api/setup/networks', listNetworks)", 7000);
}
function listNetworks(root) {
var s = document.getElementById("networks");
selDelAllOpt(s);
if(root["networks"].length > 0) {
var s = document.getElementById("networks");
var i, l = s.options.length - 1;
for(i = l; i >= 0; i--) {
s.remove(i);
}
s.appendChild(opt("-1", "please select network"));
for(i = 0; i < root["networks"].length; i++) {
s.appendChild(opt(root["networks"][i]["ssid"], root["networks"][i]["ssid"] + " (" + root["networks"][i]["rssi"] + " dBm)"));
}
}
else
s.appendChild(opt("-1", "no network found"));
}
function selNet() {
var s = document.getElementById("networks");
var e = document.getElementsByName("ssid")[0];
e.value = s.value;
if(-1 != s.value)
e.value = s.value;
}
hiddenInput = document.getElementById("disclaimer")

4
tools/esp8266/webApi.cpp

@ -456,7 +456,9 @@ bool webApi::setCtrl(DynamicJsonDocument jsonIn, JsonObject jsonOut) {
//-----------------------------------------------------------------------------
bool webApi::setSetup(DynamicJsonDocument jsonIn, JsonObject jsonOut) {
if(F("set_time") == jsonIn[F("cmd")])
if(F("scan_wifi"))
mApp->scanAvailNetworks();
else if(F("set_time") == jsonIn[F("cmd")])
mApp->setTimestamp(jsonIn[F("ts")]);
else if(F("sync_ntp") == jsonIn[F("cmd")])
mApp->setTimestamp(0); // 0: update ntp flag

Loading…
Cancel
Save