用micropython编写的程序,esp芯片经常连不上网络,路由器是newifi d2刷的openwrt。有时候一个月都正常联网,有时候一天两三次连不上,重启路由器就正常,应该说明不是我的esp8266程序有问题。后来路由器刷过老毛子,esp芯片更是连不上网,必须重启esp芯片。而micropython里边关于wifi的选项就那两项,反复改选项测试没有效果。使用一两年饱受折磨,于是我决定另起炉灶,移植到espruino。
espruino也是很多坑,但目前测试联网正常。espruino没有跟上最新的javascript,连await都不支持。最开始的坑就是webIDE默认模式跟micropython完全不同,默认发送到内存,save()重启reset()后只是软重启,需要load(),并且保存的程序里函数外边的调用不起作用。可以把默认模式改成发送到flash,跟micropython有些相似。
移植过来后,目前定时和温度功能没有。
var zhuangtai={
deng1:false,
deng2:false,
dianruzi:0
};
var Deng1=D4,Deng2=D5,Dianruzi=D14;
function onPageRequest(req, res) {
var uri = url.parse(req.url, true);
if(uri.path=="/"){
res.writeHead(200, {'Content-Type': 'text/html'});
var n = 0;
res.on('drain',function() {
res.write(htm.slice(n,n+512));
n=n+512;
if ((n+512)>=htm.length) res.end(htm.slice(n));
});
res.write('');
}
else if(uri.query.hasOwnProperty("mcu_do")){
res.writeHead(200, {'Content-Type': 'text/html'});
switch(uri.query.mcu_do){
case "1on":
console.log("111guan");
zhuangtai.deng1=true;
digitalWrite(Deng1,1);
break;
case "1of":
digitalWrite(Deng1,0);
zhuangtai.deng1=false;
break;
case "2on":
digitalWrite(Deng2,1);
zhuangtai.deng2=true;
break;
case "2of":
digitalWrite(Deng2,0);
zhuangtai.deng2=false;
break;
case "0da":
zhuangtai.dianruzi=0;
break;
case "1da":
zhuangtai.dianruzi=1;
break;
case "2da":
zhuangtai.dianruzi=2;
break;
case "3da":
zhuangtai.dianruzi=3;
break;
case "4da":
zhuangtai.dianruzi=4;
break;
case "shx":
console.log("shx");
break;
default:
console.log("no");
}
res.end(JSON.stringify(zhuangtai));
}
else{
res.writeHead(404, {'Content-Type': 'text/html'});
res.end('File not found');
}
}
require('http').createServer(onPageRequest).listen(80);
console.log("80");
baduan=8;
function dangwei(){
baduan--;
if(baduan==0){
baduan=8;
digitalWrite(Dianruzi,0);
}
if(baduan==(zhuangtai.dianruzi==0?0:(1<<(4-zhuangtai.dianruzi)))){
digitalWrite(Dianruzi,1);
}
}
var ti = setInterval(dangwei, 3000);
htm=`<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport"content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=yes"/>
<style>
input[type="button"]{margin:10px;width:40px;height:40px;border-radius: 10px;}
</style>
</head>
<script >
function dianji(val)
{
var request = new XMLHttpRequest();
request.open('GET', "?mcu_do="+val,true);
request.onload = function() {
zhuangtai=JSON.parse(this.response)
deng1.style.color=zhuangtai.deng1?"red":"black"
deng2.style.color=zhuangtai.deng2?"red":"black"
if( zhuangtai.dianruzi>0){dianruzi.style.color="red";
dang.innerText= ":"+zhuangtai.dianruzi+"档"}
else {dianruzi.style.color="black"
dang.innerText= ""}
}
request.send();
}
function xiugaidingshi(){
var request = new XMLHttpRequest();
request.open('GET',"?mcu_do=xgd"+(shi.value-0+100)+(fen.value-0+100)+(shi2.value-0+100)+(fen2.value-0+100),true)
request.onload = function(){chenggong.innerHTML="<p>成功</p>"}
request.send();
}
function shuaxin()
{
dianji("shx")
var request = new XMLHttpRequest();
request.open('GET', "?mcu_do=dqd",true);
request.onload = function(){
aa=this.response.split("#")
shijianxianshi.innerText=aa[1]
a=aa[0].split(",")
shi.value=a[0]
fen.value=a[1]
shi2.value=a[2]
fen2.value=a[3]
}
request.send();
}
document.onload=dianji("shx")
</script>
<body style="text-align:center">
<h1 id=deng1 style="color:black">屋里电灯</h1>
<input type="button" onclick="dianji('1on');" value="开" /><span style="margin:20px;"></span>
<input type="button" onclick="dianji('1of');" value="关" />
<h1 id=deng2 style="color:black" >院里电灯</h1>
<input type="button" onclick="dianji('2on');" value="开" /><span style="margin:20px;"></span>
<input type="button" onclick="dianji('2of');" value="关" />
<h1 id=dianruzi style="color:black" >电褥子<span id="dang"></span></h1>
<input type="button" onclick="dianji('1da');" value="一档" />
<input type="button" onclick="dianji('2da');" value="二档" />
<input type="button" onclick="dianji('3da');" value="三档" />
<input type="button" onclick="dianji('4da');" value="四档" />
<br><br>
<input type="button" onclick="dianji('0da');" value="关" />
<br><br><div id="shijianxianshi"></div>
<br>开<input type="number" style="width:60px" value="0" id="shi" />时<input type="number" style="width:60px" value="0" id="fen" />分
<br><br>关<input type="number" style="width:60px" value="0" id="shi2" />时<input type="number" style="width:60px" value="0" id="fen2" />分
<br><br><input type="button" style="width:80px" onclick="xiugaidingshi()" value="修改定时"/>
<br><div id="chenggong"></div><br>
<input type="button" onclick="dianji('wen');" value="温湿度" style="width:60px" />
<br>
<p id="huixian"></p>
</body>
</html>`