Commit cc7cc395 authored by Philip Gladstone's avatar Philip Gladstone Committed by Marcel Stör
Browse files

Improve the enduser setup experience by triggering captive portal detection. (#3282)

* Make captive portal detection work on macOS
* Change the default SSID prefix to be NodeMCU
parent 625397d1
...@@ -174,7 +174,7 @@ ...@@ -174,7 +174,7 @@
// If you use the enduser_setup module, then you can also set the default // If you use the enduser_setup module, then you can also set the default
// SSID when this module is running in AP mode. // SSID when this module is running in AP mode.
#define ENDUSER_SETUP_AP_SSID "SetupGadget" #define ENDUSER_SETUP_AP_SSID "NodeMCU"
// I2C software driver partially supports use of GPIO16 (D0) pin for SCL line. // I2C software driver partially supports use of GPIO16 (D0) pin for SCL line.
......
...@@ -92,7 +92,7 @@ static const char http_html_gz_filename[] = "enduser_setup.html.gz"; ...@@ -92,7 +92,7 @@ static const char http_html_gz_filename[] = "enduser_setup.html.gz";
static const char http_html_filename[] = "enduser_setup.html"; static const char http_html_filename[] = "enduser_setup.html";
static const char http_header_200[] = "HTTP/1.1 200 OK\r\nCache-control:no-cache\r\nConnection:close\r\nContent-Type:text/html; charset=utf-8\r\n"; /* Note single \r\n here! */ static const char http_header_200[] = "HTTP/1.1 200 OK\r\nCache-control:no-cache\r\nConnection:close\r\nContent-Type:text/html; charset=utf-8\r\n"; /* Note single \r\n here! */
static const char http_header_204[] = "HTTP/1.1 204 No Content\r\nContent-Length:0\r\nConnection:close\r\n\r\n"; static const char http_header_204[] = "HTTP/1.1 204 No Content\r\nContent-Length:0\r\nConnection:close\r\n\r\n";
static const char http_header_302[] = "HTTP/1.1 302 Moved\r\nLocation: /\r\nContent-Length:0\r\nConnection:close\r\n\r\n"; static const char http_header_302[] = "HTTP/1.1 302 Moved\r\nLocation: http://nodemcu.portal/\r\nContent-Length:0\r\nConnection:close\r\n\r\n";
static const char http_header_302_trying[] = "HTTP/1.1 302 Moved\r\nLocation: /?trying=true\r\nContent-Length:0\r\nConnection:close\r\n\r\n"; static const char http_header_302_trying[] = "HTTP/1.1 302 Moved\r\nLocation: /?trying=true\r\nContent-Length:0\r\nConnection:close\r\n\r\n";
static const char http_header_400[] = "HTTP/1.1 400 Bad request\r\nContent-Length:0\r\nConnection:close\r\n\r\n"; static const char http_header_400[] = "HTTP/1.1 400 Bad request\r\nContent-Length:0\r\nConnection:close\r\n\r\n";
static const char http_header_404[] = "HTTP/1.1 404 Not found\r\nContent-Length:10\r\nConnection:close\r\n\r\nNot found\n"; static const char http_header_404[] = "HTTP/1.1 404 Not found\r\nContent-Length:10\r\nConnection:close\r\n\r\nNot found\n";
...@@ -135,6 +135,7 @@ typedef struct ...@@ -135,6 +135,7 @@ typedef struct
struct tcp_pcb *http_pcb; struct tcp_pcb *http_pcb;
char *http_payload_data; char *http_payload_data;
uint32_t http_payload_len; uint32_t http_payload_len;
char *ap_ssid;
os_timer_t check_station_timer; os_timer_t check_station_timer;
os_timer_t shutdown_timer; os_timer_t shutdown_timer;
int lua_connected_cb_ref; int lua_connected_cb_ref;
...@@ -931,6 +932,8 @@ static err_t streamout_sent (void *arg, struct tcp_pcb *pcb, u16_t len) ...@@ -931,6 +932,8 @@ static err_t streamout_sent (void *arg, struct tcp_pcb *pcb, u16_t len)
{ {
tcp_sent (pcb, 0); tcp_sent (pcb, 0);
deferred_close (pcb); deferred_close (pcb);
free(state->http_payload_data);
state->http_payload_data = NULL;
} }
else else
tcp_arg (pcb, (void *)offs); tcp_arg (pcb, (void *)offs);
...@@ -1123,9 +1126,9 @@ static void enduser_setup_handle_OPTIONS (struct tcp_pcb *http_client, char *dat ...@@ -1123,9 +1126,9 @@ static void enduser_setup_handle_OPTIONS (struct tcp_pcb *http_client, char *dat
int type = 0; int type = 0;
if (strncmp(data, "GET ", 4) == 0) if (strncmp(data, "OPTIONS ", 8) == 0)
{ {
if (strncmp(data + 4, "/aplist", 7) == 0 || strncmp(data + 4, "/setwifi?", 9) == 0 || strncmp(data + 4, "/status.json", 12) == 0) if (strncmp(data + 8, "/aplist", 7) == 0 || strncmp(data + 8, "/setwifi?", 9) == 0 || strncmp(data + 8, "/status.json", 12) == 0)
{ {
enduser_setup_http_serve_header (http_client, json, strlen(json)); enduser_setup_http_serve_header (http_client, json, strlen(json));
return; return;
...@@ -1153,7 +1156,7 @@ static void enduser_setup_handle_POST(struct tcp_pcb *http_client, char* data, s ...@@ -1153,7 +1156,7 @@ static void enduser_setup_handle_POST(struct tcp_pcb *http_client, char* data, s
{ {
case 0: { case 0: {
// all went fine, extract all the form data into a file // all went fine, extract all the form data into a file
enduser_setup_write_file_with_extra_configuration_data(body, bodylength); enduser_setup_write_file_with_extra_configuration_data(body, bodylength);
// redirect user to the base page with the trying flag // redirect user to the base page with the trying flag
enduser_setup_http_serve_header(http_client, http_header_302_trying, LITLEN(http_header_302_trying)); enduser_setup_http_serve_header(http_client, http_header_302_trying, LITLEN(http_header_302_trying));
break; break;
...@@ -1283,7 +1286,7 @@ static void on_scan_done (void *arg, STATUS status) ...@@ -1283,7 +1286,7 @@ static void on_scan_done (void *arg, STATUS status)
const size_t hdr_sz = sizeof (header_fmt) +1 -1; /* +expand %4d, -\0 */ const size_t hdr_sz = sizeof (header_fmt) +1 -1; /* +expand %4d, -\0 */
/* To be able to safely escape a pathological SSID, we need 2*32 bytes */ /* To be able to safely escape a pathological SSID, we need 2*32 bytes */
const size_t max_entry_sz = 27 + 2*32 + 6; /* {"ssid":"","rssi":,"chan":} */ const size_t max_entry_sz = 35 + 2*32 + 9; /* {"ssid":"","rssi":,"chan":,"auth":} */
const size_t alloc_sz = hdr_sz + num_nets * max_entry_sz + 3; const size_t alloc_sz = hdr_sz + num_nets * max_entry_sz + 3;
char *http = calloc (1, alloc_sz); char *http = calloc (1, alloc_sz);
if (!http) if (!http)
...@@ -1319,6 +1322,12 @@ static void on_scan_done (void *arg, STATUS status) ...@@ -1319,6 +1322,12 @@ static void on_scan_done (void *arg, STATUS status)
p += sprintf (p, "%d", wn->channel); p += sprintf (p, "%d", wn->channel);
const char entry_auth[] = ",\"auth\":";
strcpy (p, entry_auth);
p += sizeof (entry_auth) -1;
p += sprintf (p, "%d", wn->authmode);
*p++ = '}'; *p++ = '}';
} }
*p++ = ']'; *p++ = ']';
...@@ -1579,15 +1588,10 @@ static err_t enduser_setup_http_recvcb(void *arg, struct tcp_pcb *http_client, s ...@@ -1579,15 +1588,10 @@ static err_t enduser_setup_http_recvcb(void *arg, struct tcp_pcb *http_client, s
break; break;
} }
} }
else if (strncmp(data + 4, "/generate_204", 13) == 0)
{
/* Convince Android devices that they have internet access to avoid pesky dialogues. */
enduser_setup_http_serve_header(http_client, http_header_204, LITLEN(http_header_204));
}
else else
{ {
ENDUSER_SETUP_DEBUG("serving 404"); // All other URLs redirect to http://nodemcu.portal/ -- this triggers captive portal.
enduser_setup_http_serve_header(http_client, http_header_404, LITLEN(http_header_404)); enduser_setup_http_serve_header(http_client, http_header_302, LITLEN(http_header_302));
} }
} }
else if (strncmp(data, "OPTIONS ", 8) == 0) else if (strncmp(data, "OPTIONS ", 8) == 0)
...@@ -1706,18 +1710,23 @@ static void enduser_setup_ap_start(void) ...@@ -1706,18 +1710,23 @@ static void enduser_setup_ap_start(void)
memset(&(cnf), 0, sizeof(struct softap_config)); memset(&(cnf), 0, sizeof(struct softap_config));
#ifndef ENDUSER_SETUP_AP_SSID #ifndef ENDUSER_SETUP_AP_SSID
#define ENDUSER_SETUP_AP_SSID "SetupGadget" #define ENDUSER_SETUP_AP_SSID "NodeMCU"
#endif #endif
char ssid[] = ENDUSER_SETUP_AP_SSID; if (state->ap_ssid) {
int ssid_name_len = strlen(ssid); strncpy(cnf.ssid, state->ap_ssid, sizeof(cnf.ssid));
memcpy(&(cnf.ssid), ssid, ssid_name_len); cnf.ssid_len = strlen(cnf.ssid);
} else {
char ssid[] = ENDUSER_SETUP_AP_SSID;
int ssid_name_len = strlen(ssid);
memcpy(&(cnf.ssid), ssid, ssid_name_len);
uint8_t mac[6]; uint8_t mac[6];
wifi_get_macaddr(SOFTAP_IF, mac); wifi_get_macaddr(SOFTAP_IF, mac);
cnf.ssid[ssid_name_len] = '_'; cnf.ssid[ssid_name_len] = '_';
sprintf(cnf.ssid + ssid_name_len + 1, "%02X%02X%02X", mac[3], mac[4], mac[5]); sprintf(cnf.ssid + ssid_name_len + 1, "%02X%02X%02X", mac[3], mac[4], mac[5]);
cnf.ssid_len = ssid_name_len + 7; cnf.ssid_len = ssid_name_len + 7;
}
cnf.channel = state == NULL? 1 : state->softAPchannel; cnf.channel = state == NULL? 1 : state->softAPchannel;
cnf.authmode = AUTH_OPEN; cnf.authmode = AUTH_OPEN;
cnf.ssid_hidden = 0; cnf.ssid_hidden = 0;
...@@ -1895,6 +1904,8 @@ static void enduser_setup_free(void) ...@@ -1895,6 +1904,8 @@ static void enduser_setup_free(void)
free_scan_listeners (); free_scan_listeners ();
free(state->ap_ssid);
free(state); free(state);
state = NULL; state = NULL;
} }
...@@ -1999,21 +2010,33 @@ static int enduser_setup_init(lua_State *L) ...@@ -1999,21 +2010,33 @@ static int enduser_setup_init(lua_State *L)
} }
} }
if (!lua_isnoneornil(L, 1)) int argno = 1;
if (lua_isstring(L, argno)) {
/* Get the SSID */
state->ap_ssid = strdup(lua_tostring(L, argno));
argno++;
}
if (!lua_isnoneornil(L, argno))
{ {
lua_pushvalue(L, 1); lua_pushvalue(L, argno);
state->lua_connected_cb_ref = luaL_ref(L, LUA_REGISTRYINDEX); state->lua_connected_cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
} }
if (!lua_isnoneornil(L, 2)) argno++;
if (!lua_isnoneornil(L, argno))
{ {
lua_pushvalue (L, 2); lua_pushvalue (L, argno);
state->lua_err_cb_ref = luaL_ref(L, LUA_REGISTRYINDEX); state->lua_err_cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
} }
if (!lua_isnoneornil(L, 3)) argno++;
if (!lua_isnoneornil(L, argno))
{ {
lua_pushvalue (L, 3); lua_pushvalue (L, argno);
state->lua_dbg_cb_ref = luaL_ref(L, LUA_REGISTRYINDEX); state->lua_dbg_cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
ENDUSER_SETUP_DEBUG("enduser_setup_init: Debug callback has been set"); ENDUSER_SETUP_DEBUG("enduser_setup_init: Debug callback has been set");
} }
......
...@@ -160,7 +160,7 @@ ...@@ -160,7 +160,7 @@
<select id=aplist name=aplist></select> <select id=aplist name=aplist></select>
</div> </div>
<input id=ssid name=wifi_ssid type=text autocorrect=off autocapitalize=none placeholder='Wi-Fi Name' /> <input id=ssid name=wifi_ssid type=text autocorrect=off autocapitalize=none placeholder='Wi-Fi Name' />
<input name=wifi_password type=password autocorrect=off autocapitalize=none autocomplete=off placeholder=Password /> <input id=wifi_password name=wifi_password type=password autocorrect=off autocapitalize=none autocomplete=off placeholder=Password />
<!-- You can add inputs here and have them pop up in your lua code through the file eus_params.lua --> <!-- You can add inputs here and have them pop up in your lua code through the file eus_params.lua -->
<input type="submit" value="Save"/> <input type="submit" value="Save"/>
</form> </form>
...@@ -262,46 +262,51 @@ ...@@ -262,46 +262,51 @@
xhr.send(); xhr.send();
} }
function gotAp(s, json) { function gotAp(s, json) {
var list; var list;
if (s === 200 && json != null) { if (s === 200 && json != null) {
if (typeof json === 'string' && json.length > 0) { if (typeof json === 'string' && json.length > 0) {
list = JSON.parse(json); list = JSON.parse(json);
} else if (typeof json === 'object') { } else if (typeof json === 'object') {
list = json; list = json;
} }
list.sort(function (a, b) { list.sort(function (a, b) {
return b.rssi - a.rssi; return b.rssi - a.rssi;
}); });
var ops = '<option>Select a Network...</option>'; var ops = '<option>Select a Network...</option>';
var seen = {}; var seen = {};
for (var i = 0; i < list.length; ++i) { for (var i = 0; i < list.length; ++i) {
var ssid = list[i].ssid; var ssid = list[i].ssid;
if (!seen[ssid]) { if (!seen[ssid]) {
seen[ssid] = 1; seen[ssid] = 1;
ops += '<option>' + ssid.replace(/&/g, "&amp;").replace(/>/g, "&gt;").replace(/</g, "&lt;") + '</option>'; ops += '<option data-auth=' + list[i].auth + '>' +
} ssid.replace(/&/g, "&amp;").replace(/>/g, "&gt;").replace(/</g, "&lt;") + '</option>';
} }
ap.innerHTML = ops; }
ab.disabled = false; ap.innerHTML = ops;
togAp(null, true); ab.disabled = false;
ab.onclick = togAp; togAp(null, true);
} else { ab.onclick = togAp;
ab.innerText = 'No networks found (' + s + ')'; } else {
ra = to(refrAp, 5); ab.innerText = 'No networks found (' + s + ')';
} ra = to(refrAp, 5);
}
} }
function togAp(ev, force) { function togAp(ev, force) {
if (!force || ap.style.display == 'block') { if (!force || ap.style.display == 'block') {
hide('#dropdown'); hide('#dropdown');
show('#ssid'); show('#ssid');
ab.innerText = 'Scan for Networks'; ab.innerText = 'Scan for Networks';
ab.onclick = refrAp; ab.onclick = refrAp;
} else { } else {
show('#dropdown'); show('#dropdown');
hide('#ssid'); hide('#ssid');
ab.innerText = 'Manual Entry'; ab.innerText = 'Manual Entry';
} }
let pw = $('#wifi_password');
pw.placeholder = "Password";
pw.disabled = false;
pw.required = true;
} }
function refrAp() { function refrAp() {
ab.innerText = 'Searching for networks...'; ab.innerText = 'Searching for networks...';
...@@ -315,13 +320,24 @@ ...@@ -315,13 +320,24 @@
ab.innerText = 'Scan for Networks'; ab.innerText = 'Scan for Networks';
ab.onclick = refrAp; ab.onclick = refrAp;
$('#aplist').onchange = function () { $('#aplist').onchange = function () {
$('#ssid').value = $('#aplist').value; $('#ssid').value = $('#aplist').value;
let pw = $('#wifi_password');
if ($('#aplist').selectedOptions[0].dataset.auth > 0) {
pw.placeholder = "Password";
pw.disabled = false;
pw.required = true;
} else {
pw.placeholder = "Open -- no password";
pw.disabled = true;
pw.required = false;
}
}; };
$('#bk2').onclick = function () { $('#bk2').onclick = function () {
cur('#f1') cur('#f1')
} }
rs = to(refr, 0.5); rs = to(refr, 0.5);
if( trying ) cur("#f3"); if( trying ) cur("#f3");
refrAp();
} }
</script> </script>
</body> </body>
......
...@@ -7,9 +7,11 @@ This module provides a simple way of configuring ESP8266 chips without using a ...@@ -7,9 +7,11 @@ This module provides a simple way of configuring ESP8266 chips without using a
serial interface or pre-programming WiFi credentials onto the chip. serial interface or pre-programming WiFi credentials onto the chip.
After running [`enduser_setup.start()`](#enduser_setupstart), a wireless After running [`enduser_setup.start()`](#enduser_setupstart), a wireless
network named "SetupGadget_XXXXXX" will starting. This prefix can be overridden network named "NodeMCU_XXXXXX" will start. This prefix can be overridden
in `user_config.h` by defining `ENDUSER_SETUP_AP_SSID`. Connect to that SSID in `user_config.h` by defining `ENDUSER_SETUP_AP_SSID` or by supplying the whole SSID to the
and then navigate to the root of any website or to 192.168.4.1. `enduser_setup.start` method. Connect to that SSID and captive portal detection on the client
should automatically open the configuration dialog. If not, then
navigate to the root of any website or to 192.168.4.1.
`http://example.com/` will work, but do not use `.local` domains because it `http://example.com/` will work, but do not use `.local` domains because it
will fail on iOS. A web page similar to the one depicted below will load, will fail on iOS. A web page similar to the one depicted below will load,
allowing the end user to provide their Wi-Fi credentials. allowing the end user to provide their Wi-Fi credentials.
...@@ -76,7 +78,6 @@ print("Wifi device_name: " .. p.device_name) ...@@ -76,7 +78,6 @@ print("Wifi device_name: " .. p.device_name)
|----|------|-----------| |----|------|-----------|
|/|GET|Returns HTML for the web page. Will return the contents of `enduser_setup.html` if it exists on the filesystem, otherwise will return a page embedded into the firmware image.| |/|GET|Returns HTML for the web page. Will return the contents of `enduser_setup.html` if it exists on the filesystem, otherwise will return a page embedded into the firmware image.|
|/aplist|GET|Forces the ESP8266 to perform a site survey across all channels, reporting access points that it can find. Return payload is a JSON array: `[{"ssid":"foobar","rssi":-36,"chan":3}]`| |/aplist|GET|Forces the ESP8266 to perform a site survey across all channels, reporting access points that it can find. Return payload is a JSON array: `[{"ssid":"foobar","rssi":-36,"chan":3}]`|
|/generate_204|GET|Returns a HTTP 204 status (expected by certain Android clients during Wi-Fi connectivity checks)|
|/status|GET|Returns plaintext status description, used by the web page| |/status|GET|Returns plaintext status description, used by the web page|
|/status.json|GET|Returns a JSON payload containing the ESP8266's chip id in hexadecimal format and the status code: 0=Idle, 1=Connecting, 2=Wrong Password, 3=Network not Found, 4=Failed, 5=Success| |/status.json|GET|Returns a JSON payload containing the ESP8266's chip id in hexadecimal format and the status code: 0=Idle, 1=Connecting, 2=Wrong Password, 3=Network not Found, 4=Failed, 5=Success|
|/setwifi|POST|HTML form post for setting the WiFi credentials. Expects HTTP content type `application/x-www-form-urlencoded`. Supports sending and storing additinal configuration parameters (as input fields). Returns the same payload as `/status.json` instead of redirecting to `/`. See also: `/update`.| |/setwifi|POST|HTML form post for setting the WiFi credentials. Expects HTTP content type `application/x-www-form-urlencoded`. Supports sending and storing additinal configuration parameters (as input fields). Returns the same payload as `/status.json` instead of redirecting to `/`. See also: `/update`.|
...@@ -130,9 +131,10 @@ Starts the captive portal. ...@@ -130,9 +131,10 @@ Starts the captive portal.
*Note: Calling start() while EUS is already running is an error, and will result in stop() to be invoked to shut down EUS.* *Note: Calling start() while EUS is already running is an error, and will result in stop() to be invoked to shut down EUS.*
#### Syntax #### Syntax
`enduser_setup.start([onConnected()], [onError(err_num, string)], [onDebug(string)])` `enduser_setup.start([AP_SSID,] [onConnected()], [onError(err_num, string)], [onDebug(string)])`
#### Parameters #### Parameters
- `AP_SSID` the (optional) SSID to use for the AP. This defaults to `NodeMCU_<device id>`.
- `onConnected()` callback will be fired when an IP-address has been obtained, just before the enduser_setup module will terminate itself - `onConnected()` callback will be fired when an IP-address has been obtained, just before the enduser_setup module will terminate itself
- `onError()` callback will be fired if an error is encountered. `err_num` is a number describing the error, and `string` contains a description of the error. - `onError()` callback will be fired if an error is encountered. `err_num` is a number describing the error, and `string` contains a description of the error.
- `onDebug()` callback is disabled by default (controlled by `#define ENDUSER_SETUP_DEBUG_ENABLE` in `enduser_setup.c`). It is intended to be used to find internal issues in the module. `string` contains a description of what is going on. - `onDebug()` callback is disabled by default (controlled by `#define ENDUSER_SETUP_DEBUG_ENABLE` in `enduser_setup.c`). It is intended to be used to find internal issues in the module. `string` contains a description of what is going on.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment