//ServeHello4.js 7Jan2018
//http//192.168.1.6:8080
//espruino board with ESP8266
//PICO with ESP8266
//var Hardware=0; //Espruino board
var Hardware =1; //PICO
// list of Wifi and passwords
var hotspots=[
{SSID:"faux",key:"a6a69"},
{SSID:"faux2",key:"a6a70"}
];
var hotspot=1;
console.log(hotspots.length);
var Serial;
var Startagain=0;
var myinterval;
function test(){
if(Hardware===1)Serial=Serial2;
if(Hardware===0)Serial=Serial4;
if(Hardware===1){
digitalWrite(B9,1); // enable on Pico Shim V2
Serial.setup(115200, { rx: A3, tx : A2 }); //Pico
}
if(Hardware===0)Serial.setup(115200, { rx: C11, tx : C10 });
//espruino board
console.log("Start connection process");
var wifi = require("ESP8266WiFi_0v25").connect(Serial, function(err) {
if (err){Startagain=1;return 1;}
console.log("Reset the ESP8266");
wifi.reset(function(err) {
if (err){Startagain=1;return 1;}
console.log("Try Connecting to WiFi ",hotspots[hotspot].SSID);
wifi.connect(hotspots[hotspot].SSID,hotspots[hotspot].key, function(err) {console.log(err);
if (err){Startagain=1;return 1;
}
wifi.getIP(function(l,ip){
if (err){Startagain=1;return;}
console.log("IP= ",ip,"\n\r"+l);
console.log("Wi-Fi Connected");
clearInterval( myinterval);
var http = require("http");
//do a server
http.createServer(function (req, res) {
res.writeHead(200);
res.end("Hello World");
}).listen(8080); //end server
});//end getIP
console.log("end getIP");
});//end connect
console.log("end connect");
});//end reset
console.log("end reset");
});//end require
console.log("end require");
return 0;
}//end test
function ccc(){
var x=0;
console.log("Start");
x=test();
console.log("end test",x);
if(x!==0){
console.log("Try again");//error seen
}
}//end cccc
myinterval=setInterval(function () {
console.log("Test for error");
if(Startagain){
Startagain=0;
hotspot++;
if(hotspot>=hotspots.length)hotspot=0;
console.log(hotspots[hotspot].SSID);
ccc();
}//end of errr
}, 2000);
ccc();
The left pane output, note faux2 is not in range, but faux is
>2
Start
Start connection process
end require
end test 0
=undefined
Reset the ESP8266
end reset
Test for error
Try Connecting to WiFi faux2
end connect
Test for error
Test for error
Test for error
Test for error
Test for error
Test for error
Test for error
Test for error
WiFi connect failed: FAIL
Test for error
faux
Start
Start connection process
end require
end test 0
Reset the ESP8266
end reset
Test for error
Try Connecting to WiFi faux
end connect
Test for error
Test for error
Test for error
null
end getIP
IP= 192.168.1.9
null
Wi-Fi Connected
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.
Cleaning it up a bit:
The code
The left pane output, note faux2 is not in range, but faux is
1 Attachment