Try this one with a valid ssid and key and then try it with a bogus ssid. Look for the disconnect message in the second case. Also there are console.log statements sprinkled in to clarify that the code executes in an order that you may not expect.
//ESP32wifi.js
// 6 Feb2018
// http://www.espruino.com/Reference#Wifi
var ssid="myssid";
var key="mypassword";
var port = 8080;
console.log('start');
var wifi = require('Wifi');
console.log("1");
// use browser at your connected IP address
// http://192.168.1.14:8080/
function processRequest (req, res) {
console.log("XXX");
res.writeHead(200);
res.end('<text>Hello World');
}
console.log("2");
var onWiFiConnect=function(){
var IPobject= wifi.getIP();
IPobject= wifi.getIP();
var IP = IPobject.ip;
var MAC = IPobject.mac;
console.log("IP:");
console.log(IP);
console.log("MAC:");
console.log(MAC);
var http = require('http');
http.createServer(processRequest).listen(port);
console.log(`Web server running at http://${wifi.getIP().ip}:${port}`);
// });
};
console.log("3");
wifi.connect(ssid, {password: key},onWiFiConnect());
console.log("4");
wifi.on('disconnected', function(details) {
console.log("disconnected");
});
console.log("5");
console.log(wifi.getIP());
Example output:
1v95 Copyright 2017 G.Williams
Espruino is Open Source. Our work is supported
only by sales of official boards and donations:
http://espruino.com/Donate
>start
1
2
3
IP:
192.168.1.14
MAC:
24:0a:c4:00:97:2a
Web server running at http://192.168.1.14:8080
4
5
{
"ip": "192.168.1.14",
"netmask": "255.255.255.0",
"gw": "192.168.1.1",
"mac": "24:0a:c4:00:97:2a"
}
=undefined
XXX
XXX
>
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Try this one with a valid ssid and key and then try it with a bogus ssid. Look for the disconnect message in the second case. Also there are console.log statements sprinkled in to clarify that the code executes in an order that you may not expect.
Example output: