fixed a lot of bugs with wifi connecting

pull/111/merge
Ondřej Hruška 7 years ago
parent 80721c1715
commit 32ba7dc0d7
  1. 2
      Makefile
  2. 14
      html_orig/lang/en.php
  3. 70
      html_orig/pages/cfg_wifi_conn.php
  4. 46
      user/cgi_wifi.c

@ -65,7 +65,7 @@ LIBS += esphttpd
# compiler flags using during compilation of source files # compiler flags using during compilation of source files
CFLAGS = -Os -ggdb -std=gnu99 -Werror -Wpointer-arith -Wundef -Wall -Wl,-EL -fno-inline-functions \ CFLAGS = -Os -ggdb -std=gnu99 -Werror -Wpointer-arith -Wundef -Wall -Wl,-EL -fno-inline-functions \
-nostdlib -mlongcalls -mtext-section-literals -D__ets__ -DICACHE_FLASH \ -nostdlib -mlongcalls -mtext-section-literals -D__ets__ -DICACHE_FLASH \
-Wno-address -Wno-unused -DHTTPD_MAX_BACKLOG_SIZE=4096 -DADMIN_PASSWORD=$(ADMIN_PASSWORD) \ -Wno-address -Wno-unused -DHTTPD_MAX_BACKLOG_SIZE=8192 -DADMIN_PASSWORD=$(ADMIN_PASSWORD) \
-DGIT_HASH='"$(shell git rev-parse --short HEAD)"' -DGIT_HASH='"$(shell git rev-parse --short HEAD)"'
# linker flags used to generate the main object file # linker flags used to generate the main object file

@ -105,10 +105,20 @@ return [
'wifi.conn.status' => 'Status:', 'wifi.conn.status' => 'Status:',
'wifi.conn.back_to_config' => 'Back to WiFi config', 'wifi.conn.back_to_config' => 'Back to WiFi config',
'wifi.conn.telemetry_lost' => 'Telemetry lost, something went wrong. Try again...', 'wifi.conn.telemetry_lost' => 'Telemetry lost; something went wrong, or your device disconnected.',
'wifi.conn.explain_android_sucks' => '
If you\'re configuring ESPTerm via a smartphone, or were connected
from another external network, your device may lose connection and this
progress indicator won\'t work. Please wait a while (~ 15 seconds),
then check if the connection succeeded.',
'wifi.conn.explain_reset' => '
To force enable the built-in AP, hold the BOOT
button until the blue LED starts flashing. Hold the button longer (until the LED
flashes rapidly) for a "factory reset".',
'wifi.conn.disabled' =>"Station mode is disabled.", 'wifi.conn.disabled' =>"Station mode is disabled.",
'wifi.conn.idle' =>"Idle, not connected and with no IP.", 'wifi.conn.idle' =>"Idle, not connected and has no IP.",
'wifi.conn.success' => "Connected! Received IP ", 'wifi.conn.success' => "Connected! Received IP ",
'wifi.conn.working' => "Connecting to selected AP", 'wifi.conn.working' => "Connecting to selected AP",
'wifi.conn.fail' => "Connection failed, check settings & try again. Cause: ", 'wifi.conn.fail' => "Connection failed, check settings & try again. Cause: ",

@ -5,9 +5,15 @@
<a href="<?= e(url('cfg_wifi')) ?>" id="backbtn" class="button"><?= tr('wifi.conn.back_to_config') ?></a> <a href="<?= e(url('cfg_wifi')) ?>" id="backbtn" class="button"><?= tr('wifi.conn.back_to_config') ?></a>
</div> </div>
<div class="Box">
<p><?= tr('wifi.conn.explain_android_sucks') ?></p>
<p><?= tr('wifi.conn.explain_reset') ?></p>
</div>
<script> <script>
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
var abortTmeo; var abortTmeo;
var failCounter = 0;
var messages = <?= json_encode([ var messages = <?= json_encode([
'disabled' => tr('wifi.conn.disabled'), 'disabled' => tr('wifi.conn.disabled'),
@ -17,33 +23,63 @@
'fail' => tr('wifi.conn.fail'), 'fail' => tr('wifi.conn.fail'),
]) ?>; ]) ?>;
function onFail() {
$("#status").html(<?= json_encode(tr('wifi.conn.telemetry_lost')) ?>);
$('.anim-dots').addClass('hidden');
}
function getStatus() { function getStatus() {
xhr.open("GET", 'http://'+_root+'<?= url('wifi_connstatus', true) ?>'); xhr.open("GET", 'http://'+_root+'<?= url('wifi_connstatus', true) ?>');
xhr.onreadystatechange = function () { xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status >= 200 && xhr.status < 300) { if (xhr.readyState == 4) {
clearTimeout(abortTmeo); if (xhr.status == 200) {
var data = JSON.parse(xhr.responseText); clearTimeout(abortTmeo);
var done = false;
var msg = messages[data.status] || '...'; try {
if (data.status == 'success') msg += data.ip; var data = JSON.parse(xhr.responseText);
if (data.status == 'fail') msg += data.cause; var done = false;
var msg = messages[data.status] || '...';
$("#status").html(msg);
if (data.status == 'success') {
if (done) { msg += data.ip;
// $('#backbtn').removeClass('hidden'); done = true;
$('.anim-dots').addClass('hidden'); }
if (data.status == 'fail') {
msg += data.cause;
done = true;
}
$("#status").html(msg);
if (done) {
// $('#backbtn').removeClass('hidden');
$('.anim-dots').addClass('hidden');
} else {
// ask again after a short delay
window.setTimeout(getStatus, 1000);
}
} catch(e) {
failCounter++;
console.log(e);
// repeat
if (failCounter > 5) {
onFail();
}
else {
window.setTimeout(getStatus, 1000);
}
}
} else { } else {
window.setTimeout(getStatus, 1000); onFail();
} }
} }
}; };
// XHR timeout
abortTmeo = setTimeout(function () { abortTmeo = setTimeout(function () {
xhr.abort(); xhr.abort();
$("#status").html(<?= json_encode(tr('wifi.conn.telemetry_lost')) ?>); onFail();
// $('#backbtn').removeClass('hidden');
$('.anim-dots').addClass('hidden');
}, 4000); }, 4000);
xhr.send(); xhr.send();

@ -193,6 +193,11 @@ httpd_cgi_state ICACHE_FLASH_ATTR cgiWiFiScan(HttpdConnData *connData)
int len; int len;
char buff[256]; char buff[256];
if (connData->conn == NULL) {
//Connection aborted. Clean up.
return HTTPD_CGI_DONE;
}
// auto-turn on STA // auto-turn on STA
if ((wificonf->opmode & STATION_MODE) == 0) { if ((wificonf->opmode & STATION_MODE) == 0) {
wificonf->opmode |= STATION_MODE; wificonf->opmode |= STATION_MODE;
@ -265,6 +270,13 @@ httpd_cgi_state ICACHE_FLASH_ATTR cgiWiFiConnStatus(HttpdConnData *connData)
char buff[100]; char buff[100];
struct ip_info info; struct ip_info info;
buff[0] = 0; // avoid unitialized read
if (connData->conn == NULL) {
//Connection aborted. Clean up.
return HTTPD_CGI_DONE;
}
httpdStartResponse(connData, 200); httpdStartResponse(connData, 200);
httpdHeader(connData, "Content-Type", "application/json"); httpdHeader(connData, "Content-Type", "application/json");
httpdEndHeaders(connData); httpdEndHeaders(connData);
@ -276,6 +288,7 @@ httpd_cgi_state ICACHE_FLASH_ATTR cgiWiFiConnStatus(HttpdConnData *connData)
} }
STATION_STATUS st = wifi_station_get_connect_status(); STATION_STATUS st = wifi_station_get_connect_status();
dbg("CONN STATE = %d", st);
switch(st) { switch(st) {
case STATION_IDLE: case STATION_IDLE:
sprintf(buff, "{\"status\": \"idle\"}"); // unclear when this is used sprintf(buff, "{\"status\": \"idle\"}"); // unclear when this is used
@ -301,9 +314,14 @@ httpd_cgi_state ICACHE_FLASH_ATTR cgiWiFiConnStatus(HttpdConnData *connData)
wifi_get_ip_info(STATION_IF, &info); wifi_get_ip_info(STATION_IF, &info);
sprintf(buff, "{\"status\": \"success\", \"ip\": \""IPSTR"\"}", GOOD_IP2STR(info.ip.addr)); sprintf(buff, "{\"status\": \"success\", \"ip\": \""IPSTR"\"}", GOOD_IP2STR(info.ip.addr));
break; break;
default:
sprintf(buff, "{\"status\": \"working\", \"wtf\": \"state = %d\"}", st);
break;
} }
tplSend(connData, buff, -1); httpdSend(connData, buff, -1);
return HTTPD_CGI_DONE; return HTTPD_CGI_DONE;
} }
@ -336,6 +354,9 @@ httpd_cgi_state ICACHE_FLASH_ATTR cgiWiFiSetParams(HttpdConnData *connData)
return HTTPD_CGI_DONE; return HTTPD_CGI_DONE;
} }
bool sta_turned_on = false;
bool sta_ssid_pw_changed = false;
// ---- WiFi opmode ---- // ---- WiFi opmode ----
if (GET_ARG("opmode")) { if (GET_ARG("opmode")) {
@ -366,6 +387,7 @@ httpd_cgi_state ICACHE_FLASH_ATTR cgiWiFiSetParams(HttpdConnData *connData)
if (enable) { if (enable) {
wificonf->opmode |= STATION_MODE; wificonf->opmode |= STATION_MODE;
sta_turned_on = true;
} else { } else {
wificonf->opmode &= ~STATION_MODE; wificonf->opmode &= ~STATION_MODE;
} }
@ -463,6 +485,7 @@ httpd_cgi_state ICACHE_FLASH_ATTR cgiWiFiSetParams(HttpdConnData *connData)
info("Setting station SSID to: \"%s\"", buff); info("Setting station SSID to: \"%s\"", buff);
strncpy_safe(wificonf->sta_ssid, buff, SSID_LEN); strncpy_safe(wificonf->sta_ssid, buff, SSID_LEN);
wifi_change_flags.sta = true; wifi_change_flags.sta = true;
sta_ssid_pw_changed = true;
} }
} }
@ -474,12 +497,13 @@ httpd_cgi_state ICACHE_FLASH_ATTR cgiWiFiSetParams(HttpdConnData *connData)
info("Setting station password to: \"%s\"", buff); info("Setting station password to: \"%s\"", buff);
strncpy_safe(wificonf->sta_password, buff, PASSWORD_LEN); strncpy_safe(wificonf->sta_password, buff, PASSWORD_LEN);
wifi_change_flags.sta = true; wifi_change_flags.sta = true;
sta_ssid_pw_changed = true;
} }
} }
if (redir_url_buf[strlen(SET_REDIR_ERR)] == 0) { if (redir_url_buf[strlen(SET_REDIR_ERR)] == 0) {
// All was OK // All was OK
info("Set WiFi params - success, applying in 1000 ms"); info("Set WiFi params - success, applying in 2000 ms");
// Settings are applied only if all was OK // Settings are applied only if all was OK
// //
@ -492,9 +516,19 @@ httpd_cgi_state ICACHE_FLASH_ATTR cgiWiFiSetParams(HttpdConnData *connData)
// If user connects via the Station IF, they may not even notice the connection reset. // If user connects via the Station IF, they may not even notice the connection reset.
os_timer_disarm(&timer); os_timer_disarm(&timer);
os_timer_setfn(&timer, applyWifiSettingsLaterCb, NULL); os_timer_setfn(&timer, applyWifiSettingsLaterCb, NULL);
os_timer_arm(&timer, 1000, false); os_timer_arm(&timer, 2000, false);
if ((sta_ssid_pw_changed || sta_turned_on)
&& wificonf->opmode != SOFTAP_MODE
&& wificonf->sta_ssid[0] != 0) {
// User wants to connect
httpdRedirect(connData, SET_REDIR_SUC); info("User wants to connect to SSID, redirecting to ConnStatus page.");
httpdRedirect(connData, "/cfg/wifi/connecting");
}
else {
httpdRedirect(connData, SET_REDIR_SUC);
}
} else { } else {
warn("Some WiFi settings did not validate, asking for correction"); warn("Some WiFi settings did not validate, asking for correction");
// Some errors, appended to the URL as ?err= // Some errors, appended to the URL as ?err=
@ -558,7 +592,7 @@ httpd_cgi_state ICACHE_FLASH_ATTR tplWlan(HttpdConnData *connData, char *token,
// For display of our current SSID // For display of our current SSID
connectStatus = wifi_station_get_connect_status(); connectStatus = wifi_station_get_connect_status();
x = wifi_get_opmode(); x = wifi_get_opmode();
if (x == SOFTAP_MODE || connectStatus != STATION_GOT_IP) { if (x == SOFTAP_MODE || connectStatus != STATION_GOT_IP || wificonf->opmode == SOFTAP_MODE) {
strcpy(buff, ""); strcpy(buff, "");
} }
else { else {
@ -571,7 +605,7 @@ httpd_cgi_state ICACHE_FLASH_ATTR tplWlan(HttpdConnData *connData, char *token,
x = wifi_get_opmode(); x = wifi_get_opmode();
connectStatus = wifi_station_get_connect_status(); connectStatus = wifi_station_get_connect_status();
if (x == SOFTAP_MODE || connectStatus != STATION_GOT_IP) { if (x == SOFTAP_MODE || connectStatus != STATION_GOT_IP || wificonf->opmode == SOFTAP_MODE) {
strcpy(buff, ""); strcpy(buff, "");
} }
else { else {

Loading…
Cancel
Save