Created
August 27, 2016 19:17
-
-
Save dustinbrownman/22591a6601b9a409107631df9b654a5a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| var wifi = require("Wifi"); | |
| var http = require("http"); | |
| function setupAccessPoint() { | |
| wifi.disconnect(); | |
| wifi.startAP("Setup", { password: "password" }, createServer); | |
| wifi.save(); | |
| } | |
| function createServer(err) { | |
| if (err === null) { | |
| http.createServer(onPageRequest).listen(8080); | |
| } | |
| } | |
| function onPageRequest(request, response) { | |
| var template = | |
| "<html>" + | |
| "<head></head>" + | |
| "<body>" + | |
| "<h3>Enter your wifi credentials</h3>" + | |
| "<form action='/auth'>" + | |
| "<label for='ssid'>SSID</lable>" + | |
| "<input type='text' name='ssid'>" + | |
| "<label for='password'>Password</lable>" + | |
| "<input type='password' name='password'>" + | |
| "<button type='submit'>Submit</button>" + | |
| "</form>" + | |
| "</body>" + | |
| "</html>"; | |
| var parseUrl = url.parse(request.url, true); | |
| var query = parseUrl.query; | |
| if (parseUrl.pathname === "/auth") { | |
| var ssid = query["ssid"].replace("+", " "); // because url.parse preserves `+` for some reason | |
| var password = query["password"].replace("+", " "); | |
| connectToWifi(ssid, password, function() { | |
| response.writeHead(200, { 'Content-Type': 'text/html' }); | |
| response.end("Successful connection!"); | |
| wifi.stopAP(); | |
| wifi.save(); | |
| }); | |
| } else { | |
| response.writeHead(200, { 'Content-Type': 'text/html' }); | |
| response.end(template); | |
| } | |
| } | |
| function connectToWifi(ssid, password, onSuccessCallback) { | |
| wifi.connect(ssid, { password: password }, function(err) { | |
| if (err === null) { | |
| onSuccessCallback(); | |
| } | |
| }); | |
| } | |
| setupAccessPoint(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment