Finally I got a first version up and running (at least in my environment) which
initializes ESP8266
connects to wireless
gets IP adress
reads html from a server
listens to a port
To get it running, you have to change mySSID and myPassWD in attached code.
Give it some time, about 20 secs, and you should then get: >echo(0); =undefined init true connect:true getIP: true 192.168.1.2 IP in your network can be different readUrl: true Copyright.... source of servo.js ....
***} ***
Simple test for listening is to open a browser and open "http://192.168.1.2:88?hugo=27
then you should get a line : listen: /?hugo=27
function ESP8266(s,bd,rx,tx){
var sp = s, rdata = "", wText = "",cb,timr,command = "",me = this;
me.dbg = false;
sp.setup(bd,{rx:rx,tx:tx});
function sendCommandWaitFor(cmd,waitText,waitFor,callback,skipCommand){
var rdata = '', timr,scmd = 'AT+' + cmd + '\r',p;
timr = setTimeout(function(){
sp.removeAllListeners('data');
callback(false,rdata);
},waitFor);
sp.on('data',function(d){
rdata += d;
if(skipCommand){
p = rdata.indexOf(scmd + '\r\n');
if(p >= 0){
rdata = rdata.substr(p + scmd.length + 2);
}
}
if(rdata.indexOf(waitText) >=0){
clearTimeout(timr);
sp.removeAllListeners('data');
callback(true,rdata);
}
});
sp.print(scmd);
}
function sendCommandWait(cmd,waitFor,callback){
sp.print('AT+' + cmd + '\r');
setTimeout(function(){ callback(true,""); });
}
function connect(ssid,passwd,callback){
sendCommandWait('CWMODE=1',1000,function(f,d){
sendCommandWaitFor('CWJAP="' + ssid + '","' +passwd + '"','OK',5000,function(f,d){
setTimeout(function(){
if(f){sendCommandWait('CIPMUX=0',1000,callback);}
else{callback(false,d);}
},5000);
});
});
}
function getIP(callback){
sendCommandWaitFor("CIFSR","\r\n",2000,callback,true);
}
function init(callback){
sendCommandWaitFor('RST','ready',5000,function(d,f){callback(d,f);});
}
function readUrl(host,adr,port,callback){
var cmd,rdata,timr,p;
sendCommandWaitFor('CIPSTART="TCP","' + host + '",' + port,'Linked',1000,
function(f,d){
if(f){
cmd ='GET ' + adr + ' HTTP/1.0\r\nHost:' + host + '\r\n\r\n';
sendCommandWaitFor('CIPSEND=' + cmd.length,'>',10000,function(f,d){
timr = setTimeout(function(){
sp.removeAllListeners('data');
callback(false,rdata);
},10000);
sp.on('data',function(d){
rdata += d;
p = rdata.indexOf("Unlink\r\n");
if(p >= 0){
sp.removeAllListeners('data');
clearTimeout(timr);
callback(f,extractData(rdata));
}
});
sp.print(cmd);
});
}
});
}
function extractData(d){
var p,r = "",l;
p = d.indexOf('+IPD,');
while(p >= 0){
d = d.substr(p + 5);
p = d.indexOf(':');
r += d.substr(p + 1,parseInt(d.substr(0,p),10));
p = d.indexOf('+IPD,');
}
p = r.indexOf("Content-Length");
l = parseInt(r.substr(p,r.substr(p).indexOf('\r\n')).split(":")[1],10);
r = r.substr(r.length - l);
return r;
}
function extractListen(d){
var p,r = "";
p = d.indexOf("+IPD,");
if(p >= 0){
r = d.substr(p).split(" ")[1];
}
return r;
}
function listen(port,handler){
var rdata = "",p,q;
sendCommandWait("CIPMUX=1",1000,function(f,d){
sendCommandWaitFor("CIPSERVER=1," + port,'OK',2000,function(d,f){
if(d){
sp.on('data',function(d){
rdata += d;
p = rdata.indexOf("\r\n");
if(p >= 0){
q = extractListen(rdata.substr(0,p));
if(q !== ""){ handler(q);}
rdata = rdata.substr(p + 2);
}
});
}
});
});
}
me.connect = connect;
me.getIP = getIP;
me.init = init;
me.readUrl = readUrl;
me.listen = listen;
}
var esp = new ESP8266(Serial4,115200,C11,C10);
var mySSID = "changeThis", myPassWD = "changeThis";
esp.init(function(f,d){
console.log("init",f);
esp.connect(mySSID,myPassWD,function(f,d){
console.log("connect:",f);
esp.getIP(function(d,f){
console.log("getIP:",d,f);
//esp.readUrl("192.168.1.3","/Espruino/test.htm",80,function(f,d){
esp.readUrl("http://www.espruino.com","/modules/servo.js",80,function(f,d){
console.log("readUrl:",f,d);
esp.listen(88,function(d){console.log("listen:",d);});
});
});
});
});
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.
Finally I got a first version up and running (at least in my environment) which
listens to a port
To get it running, you have to change mySSID and myPassWD in attached code.
Give it some time, about 20 secs, and you should then get:
>echo(0);
=undefined
init true
connect:true
getIP: true 192.168.1.2 IP in your network can be different
readUrl: true Copyright.... source of servo.js
....
***} ***
Simple test for listening is to open a browser and open "http://192.168.1.2:88?hugo=27
then you should get a line :
listen: /?hugo=27