I've been trying to make a Espruino webserver that would work with an ESP8266, and be able to serve static files and dynamically generated data.
I've run into a problem though....
It serves the first request just fine.
Subsequent requests make the ESP's activity LED blink, but nothing is printed to the espruino console, and the browser hangs at "waiting for 192.168.2.102..."
It's as if the callback stops getting called after the first request. Are things getting fouled up, leaving the ESP waiting for a response that the Espruino doesn't know it needs to give?
In any event, after getting into this state, the ESP8266 needs to be reset. If we can't keep it from doing this, we need to take the CC3k approach and wire in reset, and use it liberally.
Serial4.setup(9600, { rx: C11, tx : C10 });
var http = require("http");
var wifi = require("ESP8266WiFi").connect(Serial4, function(err) {
if (err) throw err;
wifi.reset(function(err) {
if (err) throw err;
console.log("Connecting to WiFi");
wifi.connect(wifi.config.ssid,wifi.config.pass, function(err) {
if (err) throw err;
console.log("Connected");
wifi.getIP(function(err,ip){wifi.config.ip=ip;});
setTimeout(wifi.userinit.bind(wifi),15000);
});
});
});
wifi.config={ssid:"TwilightZone", pass:"(snip)", port:80};
wifi.fpfx="html"; //file prefix for serving files off SD card;
wifi.userinit= function() { //set up the server.
console.log("setting up server on "+this.config.ip+":"+this.config.port);
this.server=require('http').createServer(this.onRequest.bind(this)).listen(this.config.port);
};
wifi.onRequest=function (req, res) {
var par=url.parse(req.url,true);
var q=par.query;
var nam=par.pathname;
var l=nam.indexOf("/");
nam=nam.slice(l);
var rd=this.procreq(nam,q);
res.writeHead(rd.code,rd.head?rd.head:{'Content-Type': 'text/plain'});
if (!rd.file) {
res.write(rd.body);
res.end();
} else {
rd.file.pipe(res);
res.end();
}
};
wifi.procreq = function (path,query) {
var paths=path.split(".");
var rd={};
rd.code=404;
rd.body="";
// code goes here
console.log(paths[1]);
switch (paths[1]) {
case "json":
if (this.json.hasOwnProperty(paths[0].slice(1)))
{
rd=this.json[paths[0].slice(1)](path,query);
} else {
rd.body="json "+paths[0].slice(1)+" not found";
}
break;
case "cmd":
rd=runCMD(path,query);
break;
default:
var f = E.openFile(wifi.fpfx+"/"+path, "r");
if (f==undefined) {
rd.body="File "+path+" not found";
} else {
rd.code=200;
rd.file=f;
}
}
return rd;
};
wifi.json={};
wifi.json.status= function (path,query) {
return {code:200,body:'{gtg:true,dtf:false,missiles:["armed","armed","repair","mothballed"]}'};
};
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.
I've been trying to make a Espruino webserver that would work with an ESP8266, and be able to serve static files and dynamically generated data.
I've run into a problem though....
It serves the first request just fine.
Subsequent requests make the ESP's activity LED blink, but nothing is printed to the espruino console, and the browser hangs at "waiting for 192.168.2.102..."
It's as if the callback stops getting called after the first request. Are things getting fouled up, leaving the ESP waiting for a response that the Espruino doesn't know it needs to give?
In any event, after getting into this state, the ESP8266 needs to be reset. If we can't keep it from doing this, we need to take the CC3k approach and wire in reset, and use it liberally.
at.debug() output below.