Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
15 lines (15 sloc) 7.45 KB
function z(a){return{ip:a.charCodeAt(0)+"."+a.charCodeAt(1)+"."+a.charCodeAt(2)+"."+a.charCodeAt(3),port:a.charCodeAt(5)<<8|a.charCodeAt(4),len:a.charCodeAt(7)<<8|a.charCodeAt(6)}}function D(a){var b=a.indexOf(":");if(0>b)return a;var c=a.substring(5,b).split(",");c[1]|=0;var d=a.length-(b+1),e=c[0];if(v[e]){var l=(c[2]||"0.0.0.0").split(".").map(function(a){return 0|a}),A=0|c[3];g[e]+=String.fromCharCode(l[0],l[1],l[2],l[3],A&255,A>>8,d&255,d>>8)}if(d>=c[1])return g[e]+=a.substr(b+1,
c[1]),a.substr(b+c[1]+1);g[e]+=a.substr(b+1,d);f.getData(c[1]-d,function(a){g[e]+=a});return""}function w(a,b){if(b)return a(b);f.cmd("AT+CWMODE="+p+"\r\n",1E3,function(b){"no change"!=b&&"OK"!=b&&"WIFI DISCONNECT"!=b?a("CWMODE failed: "+(b?b:"Timeout")):a(null)})}function q(a){var b=a[0];void 0===e[b]&&e[5]?e[b]="Accept":"Wait"==e[b]?e[b]=!0:f.cmd("AT+CIPCLOSE="+b+"\r\n",1E3,function(){e[b]=void 0})}function r(a){e[a[0]]=""!=g[a[0]]?"DataClose":void 0}function u(a,b){var c=0==p;p|=a;c?("1v91"==process.version?
(x.reset(),t.setup(115200,{rx:A3,tx:A2})):t.setup(115200,{rx:A3,tx:A2,cts:x}),f=require("AT").connect(t),f.register("+IPD",D),f.registerLine("0,CONNECT",q),f.registerLine("1,CONNECT",q),f.registerLine("2,CONNECT",q),f.registerLine("3,CONNECT",q),f.registerLine("4,CONNECT",q),f.registerLine("0,CLOSED",r),f.registerLine("1,CLOSED",r),f.registerLine("2,CLOSED",r),f.registerLine("3,CLOSED",r),f.registerLine("4,CLOSED",r),f.registerLine("WIFI CONNECTED",function(){n|=k.CLIENT;exports.emit("associated")}),
f.registerLine("WIFI GOT IP",function(){exports.emit("connected")}),f.registerLine("WIFI DISCONNECTED",function(){n&=~k.CLIENT;exports.emit("disconnected")}),exports.at=f,require("NetworkJS").create(B),f.cmd("\r\nAT+RST\r\n",1E4,function l(a){if("ready"==a||"Ready."==a)setTimeout(function(){f.cmd("ATE0\r\n",1E3,function F(a){if("ATE0"==a)return F;"OK"==a?f.cmd("AT+CIPDINFO=1\r\n",1E3,function(a){if("OK"!=a)return b("CIPDINFO failed: "+(a?a:"Timeout"));f.cmd("AT+CIPMUX=1\r\n",1E3,function(a){if("OK"!=
a)return b("CIPMUX failed: "+(a?a:"Timeout"));f.cmd("AT+UART_CUR=115200,8,1,0,2\r\n",500,function(a){if("OK"!=a)return b("UART_CUR failed: "+(a?a:"Timeout"));w(b)})})}):b("ATE0 failed: "+(a?a:"Timeout"))})},500);else if(void 0===a)b("No 'ready' after AT+RST");else return l}),digitalWrite(G,1),digitalWrite(y,1)):w(b)}function C(a,b){b=b||function(){};(p&=~a)?w(b,null):(t.removeAllListeners(),f=void 0,exports.at=void 0,digitalWrite(y,0),e=[],setTimeout(b,1))}var G=A13,y=A14,x=A15,t=Serial2;digitalWrite(y,
0);var k={CLIENT:1,AP:2},H=["open","wep","wpa_psk","wpa2_psk","wpa_wpa2_psk"],p=0,n=0,f,e=[],v=[],g=["","","","",""],B={create:function(a,b,c){if(!f||!n)return-1;if(void 0===a&&2!=c)return d=5,e[d]="Wait",g[d]="",f.cmd("AT+CIPSERVER=1,"+b+"\r\n",1E4,function(a){"OK"==a?e[d]=!0:(e[d]=void 0,setTimeout(function(){throw Error("CIPSERVER failed ("+(a?a:"Timeout")+")");},0))}),5;for(var d=0;void 0!==e[d];)d++;if(5<=d)return-7;g[d]="";e[d]="Wait";var h;2==c?(b?h="AT+CIPSTART="+d+',"UDP","255.255.255.255",'+
b+","+b+",2\r\n":e[d]="UDP",v[d]=!0):(h="AT+CIPSTART="+d+',"TCP",'+JSON.stringify(a)+","+b+"\r\n",delete v[d]);h&&f.cmd(h,1E4,function m(a){if("ALREADY CONNECTED"==a)return m;if("OK"!=a||!0!==e[d])e[d]=-6});return d},close:function(a){"Wait"==e[a]?e[a]="WaitClose":void 0!==e[a]&&(0>e[a]||"UDP"==e[a]?e[a]=void 0:f.cmd((5==a?"AT+CIPSERVER=0":"AT+CIPCLOSE="+a)+"\r\n",1E3,function(){e[a]=void 0}))},accept:function(){for(var a=0;5>a;a++)if("Accept"==e[a])return e[a]=!0,a;return-1},recv:function(a,b){if(g[a]){if(g[a].length>
b){var c=g[a].substr(0,b);g[a]=g[a].substr(b)}else c=g[a],g[a]="","DataClose"==e[a]&&(e[a]=void 0);return c}return 0>e[a]?e[a]:e[a]?"":-1},send:function(a,b,c){if(!f)return-1;if(f.isBusy()||"Wait"==e[a])return 0;if(0>e[a])return e[a];if(!e[a])return-1;if("UDP"==e[a])return c=z(b),e[a]="Wait",f.cmd("AT+CIPSTART="+a+',"UDP","'+c.ip+'",'+c.port+","+c.port+",2\r\n",1E4,function(b){"OK"!=b&&(e[a]=-6)}),0;var d=b.length,h="";2==c&&(c=z(b),h=',"'+c.ip+'",'+c.port,b=b.substr(8,c.len),d=8+c.len);f.cmd("AT+CIPSEND="+
a+","+b.length+h+"\r\n",2E3,function m(d){if("OK"==d)f.register("> ",function(a){f.unregister("> ");f.write(b);return a.substr(2)});else if(d!="Recv "+b.length+" bytes"&&"busy s..."!=d){"SEND OK"==d?("WaitClose"==e[a]&&B.close(a),e[a]=!0):(e[a]=void 0,f.unregister("> "));return}return m});e[a]="Wait";return d}};exports.connect=function(a,b,c){var d="";c=c||function(){};void 0!==b.password&&(d=b.password);u(k.CLIENT,function(b){if(b)return c(b);f.cmd("AT+CWJAP="+JSON.stringify(a)+","+JSON.stringify(d)+
"\r\n",2E4,function m(a){if(0<=["WIFI DISCONNECT","WIFI CONNECTED","WIFI GOT IP","+CWJAP:1"].indexOf(a))return m;"OK"!=a?setTimeout(c,0,"WiFi connect failed: "+(a?a:"Timeout")):setTimeout(c,0,null)})})};exports.disconnect=function(a){C(k.CLIENT,a)};exports.startAP=function(a,b,c){c=c||function(){};b=b||{};if(!b.password||8>b.password.length)throw Error("Password must be at least 8 characters");var d=b.password?"3":"0";if(b.authMode&&(d={open:0,wpa:2,wpa2:3,wpa_wpa2:4}[b.authMode],void 0===d))throw Error("Unknown authMode "+
b.authMode);void 0===b.channel&&(b.channel=5);u(k.AP,function(e){if(e)return c(e);f.cmd("AT+CWSAP="+JSON.stringify(a)+","+JSON.stringify(b.password)+","+b.channel+","+d+"\r\n",5E3,function(a){"OK"!=a?c("CWSAP failed: "+(a?a:"Timeout")):(n|=k.AP,c(null))})})};exports.stopAP=function(a){n&=~k.AP;C(k.AP,a)};exports.scan=function(a){var b=[];u(k.CLIENT,function(c){if(c)return a(c);f.cmdReg("AT+CWLAP\r\n",5E3,"+CWLAP:",function(a){a=a.slice(8,-1).split(",");b.push({ssid:JSON.parse(a[1]),authMode:H[a[0]],
rssi:parseInt(a[2]),mac:JSON.parse(a[3]),channel:JSON.parse(a[4])})},function(){a(null,b)})})};exports.getIP=function(a){var b={};f.cmd("AT+CIFSR\r\n",1E3,function h(d){if(void 0===d)a("Timeout");else{if("+CIFSR:STAIP"==d.substr(0,12))b.ip=d.slice(14,-1);else if("+CIFSR:STAMAC"==d.substr(0,13))b.mac=d.slice(15,-1);else if("OK"==d){a(null,b);return}return h}})};exports.setIP=function(a,b){if("object"==typeof a&&a.ip){var c=[JSON.stringify(a.ip)];a.gw&&(c.push(JSON.stringify(a.gw)),c.push(JSON.stringify(a.netmask||
"255.255.255.0")));c="AT+CIPSTA_CUR="+c.join(",")+"\r\n";var d=3E3}else c="AT+CWDHCP_CUR=1,1\r\n",d=2E4;f.cmd(c,d,function(a){if("OK"==a)b(null);else return b("setIP failed: "+(a?a:"Timeout"))})};exports.getAPIP=function(a){var b={};f.cmd("AT+CIPAP_CUR?\r\n",1E3,function h(d){if(void 0===d)a("Timeout");else if("OK"==d)f.cmd("AT+CIPAPMAC_CUR?\r\n",1E3,function m(d){if(void 0===d)a("Timeout");else if("OK"==d)a(null,b);else return"+CIPAPMAC_CUR"==d.substr(0,14)&&(b.mac=JSON.parse(d.substr(10))),m});
else return"+CIPAP_CUR"==d.substr(0,10)&&(d=d.split(":"),"gateway"==d[1]&&"gw"==d[1],b[d[1]]=JSON.parse(d[2])),h})};exports.setAPIP=function(a,b){var c=[JSON.stringify(a.ip)];a.gw&&(c.push(JSON.stringify(a.gw)),c.push(JSON.stringify(a.netmask||"255.255.255.0")));f.cmd("AT+CIPAP_CUR="+c.join(",")+"\r\n",3E3,function(a){if("OK"==a)b(null);else return b("setAPIP failed: "+(a?a:"Timeout"))})};exports.setHostname=function(a,b){u(k.CLIENT,function(c){if(c)return b(c);f.cmd("AT+CWHOSTNAME="+JSON.stringify(a)+
"\r\n",500,function(a){b("OK"==a?null:a)})})};exports.ping=function(a,b){var c;f.cmd('AT+PING="'+a+'"\r\n',1E3,function l(a){if(a&&"+"==a[0])return c=a.substr(1),l;"OK"==a?b(c):b()})};exports.turbo=function(a,b){var c=a?!0===a?921600:a:115200;f.cmd("AT+UART_CUR="+c+",8,1,0,2\r\n",500,function(a){"OK"!=a?b&&b("Baud rate switch to "+c+" failed: "+(a?a:"Timeout")):(t.setup(c,{rx:A3,tx:A2,cts:x}),b&&b(null))})};exports.debug=function(){return{wifiMode:p,connected:n,socks:e,sockData:g}}
You can’t perform that action at this time.