Make the 'connecting'-screen a bit more friendly by eg telling the user the IP the ESP has connected to.

v0.9.0
Sprite_tm 10 years ago
parent d9c5fd5cfa
commit d088cddd70
  1. 44
      html/wifi/connecting.html
  2. 53
      user/cgiwifi.c
  3. 3
      user/cgiwifi.h
  4. 3
      user/user_main.c

@ -1,7 +1,43 @@
<html>
<head><title>Connecting</title></head>
<META HTTP-EQUIV="refresh" CONTENT="3;URL=wifi.tpl">
<html><head><title>Connecting...</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="140medley.min.js"></script>
<script type="text/javascript">
var xhr=j();
function getStatus() {
xhr.open("GET", "connstatus.cgi");
xhr.onreadystatechange=function() {
if (xhr.readyState==4 && xhr.status>=200 && xhr.status<300) {
var data=JSON.parse(xhr.responseText);
if (data.status=="idle") {
$("#status").innerHTML="Preparing to connect...";
window.setTimeout(getStatus, 1000);
} else if (data.status=="success") {
$("#status").innerHTML="Connected! Got IP "+data.ip+". If you're in the same network, you can access it <a href=\"http://"+data.ip+"/\">here</a>.";
} else if (data.status=="working") {
$("#status").innerHTML="Trying to connect to selected access point...";
window.setTimeout(getStatus, 1000);
} else if (data.status=="fail") {
$("#status").innerHTML="Connection failed. Check password and selected AP.<br /><a href=\"wifi.tpl\">Go Back</a>";
}
}
}
xhr.send();
}
window.onload=function(e) {
getStatus();
};
</script>
</head>
<body>
Connecting to AP now...
<div id="main">
<h2>Connecting to AP...</h2>
<p>Status:<br />
<div id="status">...</div>
</p>
</div>
</body>
</html>

@ -33,7 +33,15 @@ typedef struct {
} ScanResultData;
//Static scan status storage.
ScanResultData cgiWifiAps;
static ScanResultData cgiWifiAps;
#define CONNTRY_IDLE 0
#define CONNTRY_WORKING 1
#define CONNTRY_SUCCESS 2
#define CONNTRY_FAIL 3
//Connection result var
static int connTryStatus=CONNTRY_IDLE;
static ETSTimer resetTimer;
//Callback the code calls when a wlan ap scan is done. Basically stores the result in
//the cgiWifiAps struct.
@ -133,7 +141,6 @@ int ICACHE_FLASH_ATTR cgiWiFiScan(HttpdConnData *connData) {
//Temp store for new ap info.
static struct station_config stconf;
//This routine is ran some time after a connection attempt to an access point. If
//the connect succeeds, this gets the module in STA-only mode.
static void ICACHE_FLASH_ATTR resetTimerCb(void *arg) {
@ -144,27 +151,30 @@ static void ICACHE_FLASH_ATTR resetTimerCb(void *arg) {
wifi_set_opmode(1);
system_restart();
} else {
connTryStatus=CONNTRY_FAIL;
os_printf("Connect fail. Not going into STA-only mode.\n");
//Maybe also pass this through on the webpage?
}
}
//Actually connect to a station. This routine is timed because I had problems
//with immediate connections earlier. It probably was something else that caused it,
//but I can't be arsed to put the code back :P
static void ICACHE_FLASH_ATTR reassTimerCb(void *arg) {
int x;
static ETSTimer resetTimer;
os_printf("Try to connect to AP....\n");
wifi_station_disconnect();
wifi_station_set_config(&stconf);
wifi_station_connect();
x=wifi_get_opmode();
connTryStatus=CONNTRY_WORKING;
if (x!=1) {
//Schedule disconnect/connect
os_timer_disarm(&resetTimer);
os_timer_setfn(&resetTimer, resetTimerCb, NULL);
os_timer_arm(&resetTimer, 4000, 0);
os_timer_arm(&resetTimer, 15000, 0); //time out after 15 secs of trying to connect
}
}
@ -195,7 +205,7 @@ int ICACHE_FLASH_ATTR cgiWiFiConnect(HttpdConnData *connData) {
#ifdef DEMO_MODE
httpdRedirect(connData, "/wifi");
#else
os_timer_arm(&reassTimer, 1000, 0);
os_timer_arm(&reassTimer, 500, 0);
httpdRedirect(connData, "connecting.html");
#endif
return HTTPD_CGI_DONE;
@ -203,7 +213,7 @@ int ICACHE_FLASH_ATTR cgiWiFiConnect(HttpdConnData *connData) {
//This cgi uses the routines above to connect to a specific access point with the
//given ESSID using the given password.
int ICACHE_FLASH_ATTR cgiWifiSetMode(HttpdConnData *connData) {
int ICACHE_FLASH_ATTR cgiWiFiSetMode(HttpdConnData *connData) {
int len;
char buff[1024];
@ -224,6 +234,37 @@ int ICACHE_FLASH_ATTR cgiWifiSetMode(HttpdConnData *connData) {
return HTTPD_CGI_DONE;
}
int ICACHE_FLASH_ATTR cgiWiFiConnStatus(HttpdConnData *connData) {
char buff[1024];
int len;
struct ip_info info;
int st=wifi_station_get_connect_status();
httpdStartResponse(connData, 200);
httpdHeader(connData, "Content-Type", "text/json");
httpdEndHeaders(connData);
if (connTryStatus==CONNTRY_IDLE) {
len=os_sprintf(buff, "{\n \"status\": \"idle\"\n }\n");
} else if (connTryStatus==CONNTRY_WORKING || connTryStatus==CONNTRY_SUCCESS) {
if (st==STATION_GOT_IP) {
wifi_get_ip_info(0, &info);
len=os_sprintf(buff, "{\n \"status\": \"success\",\n \"ip\": \"%d.%d.%d.%d\" }\n",
(info.ip.addr>>0)&0xff, (info.ip.addr>>8)&0xff,
(info.ip.addr>>16)&0xff, (info.ip.addr>>24)&0xff);
//Reset into AP-only mode sooner.
os_timer_disarm(&resetTimer);
os_timer_setfn(&resetTimer, resetTimerCb, NULL);
os_timer_arm(&resetTimer, 1000, 0);
} else {
len=os_sprintf(buff, "{\n \"status\": \"working\"\n }\n");
}
} else {
len=os_sprintf(buff, "{\n \"status\": \"fail\"\n }\n");
}
httpdSend(connData, buff, len);
return HTTPD_CGI_DONE;
}
//Template code for the WLAN page.
int ICACHE_FLASH_ATTR tplWlan(HttpdConnData *connData, char *token, void **arg) {
char buff[1024];

@ -7,6 +7,7 @@ int cgiWiFiScan(HttpdConnData *connData);
int tplWlan(HttpdConnData *connData, char *token, void **arg);
int cgiWiFi(HttpdConnData *connData);
int cgiWiFiConnect(HttpdConnData *connData);
int cgiWifiSetMode(HttpdConnData *connData);
int cgiWiFiSetMode(HttpdConnData *connData);
int cgiWiFiConnStatus(HttpdConnData *connData);
#endif

@ -67,7 +67,8 @@ HttpdBuiltInUrl builtInUrls[]={
{"/wifi/wifiscan.cgi", cgiWiFiScan, NULL},
{"/wifi/wifi.tpl", cgiEspFsTemplate, tplWlan},
{"/wifi/connect.cgi", cgiWiFiConnect, NULL},
{"/wifi/setmode.cgi", cgiWifiSetMode, NULL},
{"/wifi/connstatus.cgi", cgiWiFiConnStatus, NULL},
{"/wifi/setmode.cgi", cgiWiFiSetMode, NULL},
{"*", cgiEspFsHook, NULL}, //Catch-all cgi function for the filesystem
{NULL, NULL, NULL}

Loading…
Cancel
Save