Hi @Aleksandrs see the attached file:
I hope it gets you started.
>echo(0);
To run in left pane enter test();
To save for boot and run enter save(); in left pane
=undefined
>save();
=undefined
Erasing Flash.....
Writing..................
Compressed 81600 bytes to 16091
Checking...
Done!
Start
Start connection process
Try again
Reset the ESP8266
Connecting to WiFi
IP= 192.168.1.3
null
Get
Get
The code:
//alex2.js
//1 Oct 2016
//var dht = require("DHT22").connect(D0); // GPIO4
var lastDhtData ="DHT reading goes here";
var testcount=0;
//espruino board with ESP8266
//PICO with ESP8266
//IP=192.168.1.4
//Use the following in the browser
//http://192.168.1.4:8080/
//IP=192.168.1.3
//Use the following in the browser
//http://192.168.1.3:8080/
//var Hardware=0; //Espruino board
var Hardware =1; //PICO
var SSID="ssid";
var key= "passcode";
var Serial;
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)return 1;// throw err;
console.log("Reset the ESP8266");
wifi.reset(function(err) {
if (err)return 1;// throw err;
console.log("Connecting to WiFi");
wifi.connect(SSID,key, function(err) {
if (err)return 1;//throw err;
wifi.getIP(function(l,ip){
console.log("IP= ",ip,"\n\r"+l);
// Now you can do something,
////////////////////////////////////////////////////////////
serveHTML();
////////////////////////////////////////////////////////////
});//end getIP
});//end connect SSID
});//end reset
});//end conect serial
}//end test
////////////////////////////////////////////////////////////
function serveHTML(){
var http=require("http").createServer(onPageRequest).listen(8080);
}//end serveFile
var pdata="";
function onPageRequest(req, res) {
//console.log("Req= ",req);
//console.log("Header",req.headers);
if (req.method=="POST") {
console.log("Post");
doPost(req,res);
}else{
doGet(req,res);
}//endif
}//end on PageRequest
var KMtext1="<html><body style=\"text-align:center;margin-left:auto;margin-right:auto;\"> \r\n<h1>";
var KMtext2="</h1>\r\n</body> \r\n</html> \r\n";
function doGet(req,res){
var a = url.parse(req.url, true);
console.log("Get");
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(KMtext1);
res.write(lastDhtData);
res.write(KMtext2);
res.end("");
}
function doPost1(req,res){
//todo read posted data, compose reply and send it
}
////////////////////////////////////////////////////////////
// Get data from sensor every 1 minute
setInterval(function(){
/* dht.read(function (a) {
lastDhtData = a.temp.toString()+","+a.rh.toString();
});
*/
testcount++;
lastDhtData= "data= "+testcount;
},1000); //60000);
E.on('init', function() {
var x;
console.log("Start");
x=test();
if(x!==0)console.log("Try again");//error seen
if(x===0) console.log("Exit Seen");
});
console.log("To run in left pane enter test();");
console.log("To save for boot and run enter save(); in left pane");
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.
Hi @Aleksandrs see the attached file:
I hope it gets you started.
The code: