var n = 4, chk=5*1024;
var tdata=new Uint8Array(chk);
var i;
for(i=0;i<1024;i++){
tdata[i]=0x30;
tdata[i+1024]=0x31;
tdata[i+1024*2]=0x32;
tdata[i+1024*3]=0x33;
tdata[i+1024*4]=0x34;
}//next i
var page = '<html><body><script>var ws;setTimeout(function(){';
page += 'ws = new WebSocket("ws://" + location.host + "/my_websocket", "protocolOne");';
page += 'ws.onmessage = function (event) { document.write("MSG:"+event.data); };';
page += 'setTimeout(function() { ws.send("Hello to Espruino!"); }, 1000);';
page += '},1000);</script></body></html>';
function onPageRequest(req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(page);
}
//////
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;}
console.log("Reset the ESP8266");
wifi.reset(function(err) {
if (err){Startagain=1;return;}
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;
}
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 server = require('ws').createServer(onPageRequest);
server.listen(8080);
server.on("websocket", function(ws) {
ws.on('message',function(msg) { print("[WS] "+JSON.stringify(msg)); });
var t=getTime();
ws.send(E.toString(tdata));
print("n=",n,"t:",(getTime()-t).toFixed(3),"sec");
});
});//end getIP
console.log("end getIP");
});//end connect
console.log("end connect");
});//end reset
console.log("end reset");
});//end require
console.log("end require");
}//end test
1v95 Copyright 2017 G.Williams
>2
>Start connection process
end require
=undefined
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
n= 4 t: 0.029 sec
[WS] "Hello to Espruino!"
>
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 using websockets
Webserver method
Websockets