-
-
- AT+MDNS – Set MDNS function
ESP8266 softAP mode can not support MDNS. Do not contain special characters (for example, "."
character), or use a protocol name (for example, "http"), when defining "host_name" and
"server_name" for MDNS.
AT+WPS – Set WPS function
Example
AT+CWMODE=1
AT+WPS=1
Command AT+WPS=
Response OK or ERROR
Parameter
1 : start WPS function
0 : stop WPS function
AT+MDNS – Set MDNS function
Example AT+MDNS=1, "espressif", "iot", 8080
Command AT+MDNS=, , ,
Response OK or ERROR
Parameter
1 : enable MDNS function
0 : disable MDNS function
MDNS host name
MDNS server name
MDNS server port
http://bbs.espressif.com/viewtopic.php?t=1714
https://cdn.sparkfun.com/assets/learn_tutorials/4/0/3/4A-ESP8266AT_Instruction_SetEN_v0.30.pdf
https://espressif.com/en/support/download/sdks-demos
https://espressif.com/en/support/download/overview?keys=&field_type_tid%5B%5D=14
http://bbs.espressif.com/ - AT+MDNS – Set MDNS function
-
What is mDNS?
It gives a name to your ESP8266 server that the Apple Bonjour browser add in can use to access your server.
https://www.youtube.com/watch?v=G_gCfi3MdoU
In this example code I named the server “bob”.
I downloaded and installed Bonjour from this site and installed it on my Windows 7 system.
http://download.cnet.com/Bonjour-for-Windows/3000-18507_4-93550.html
On the IE browser command line I enter
“bob.local”
Or
http://bob.local:8080///MDNS_HTTPserver1.js //5 OCT 2016 //espruino board with ESP8266 //PICO with ESP8266 //var Hardware=0; //Espruino board var Hardware =1; //PICO var SSID="ssid"; var key= "router passcode"; var Serial; var Wifi; // "bob" is the server name // in IE browser with Bonjour installed // bob.local // http://bob.local:8080/ var mDNS="AT+MDNS=1,\"bob\",\"http\",8080\r\n"; function mysend(a){ Wifi.at.cmd(a, 1000, function(d){console.log(d+"xxx");}); } //var ddata=""; 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; Wifi=wifi; console.log("Reset the ESP8266"); wifi.reset(function(err) { if (err)return 1;// throw err; Wifi.at.cmd("AT+CWMODE_CUR=3\r\n", 1000, function(d){console.log(d+"xx1");}); Wifi.at.cmd("AT+CIPMUX=1\r\n", 1000, function(d){console.log(d+"xx2");}); 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"); console.log("WiFi Connected "); mysend(mDNS); //HTTP or Network Server goes here serveHTML(); //////////////////////////////////////////////////////////// });//end wifi.getIP });//end wifi.connectSSID });//end wifi.reset });//end wifi.connect }//end test function serveHTML(){ var http=require("http").createServer(onPageRequest).listen(8080); }//end serveFile 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 pdata=""; function doPost(req,res){ //todo }//end doPost //Simple Web Page var KMtext="<html><body style=\"text-align:center;margin-left:auto;margin-right:auto;\"> \r\n<h1>mDNS Demo. bob</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(KMtext); res.end(""); } var x; console.log("Start"); x=test(); if(x!==0)console.log("Try again");//error seen if(x===0) console.log("Exit Seen");
>echo(0); Start Start connection process Try again =undefined Reset the ESP8266 Connecting to WiFi OKxx1 OKxx2 IP= 192.168.1.3 WiFi Connected OKxxx Get >
https://en.wikipedia.org/wiki/Multicast_DNS
Problems with older ESP8266 SDKs
http://internetofhomethings.com/homethings/?p=426Update: The issues I had with the ESP8266 SDK Version 1.0 have been fixed with the Version 1.0.1 release dated 24 April 2015. And now, Version 1.1.1 has been released with even more enhancements.
-
The UDP/TCP client
//UDP_TCPclient1.js //2 Oct 2016 //espruino board with ESP8266 //PICO with ESP8266 //var BroadIP="255.255.255.255"; var BroadIP="192.168.1.255"; //var LocalIP; var UDPport=1234; var TCPport=9988; //var Hardware=0; //Espruino board var Hardware =1; //PICO var SSID="ssid"; var key= "router passcode"; var Serial; var Wifi; function UDPmsg(a,b,c,d){ this.ClientName=a; this.ClientIP=b; this.ServerName=c; this.ServerIP=d; } var UDP1=new UDPmsg("Bob",0,"Bill",0); var UDP2;//=new UDPmsg("",0,"",0); var ServerIP=0; var ddata=""; 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("UDP").connect(Serial, function(err) { if (err)return 1;// throw err; Wifi=wifi; console.log("Reset the ESP8266"); wifi.reset(function(err) { if (err)return 1;// throw err; Wifi.at.cmd("AT+CWMODE_CUR=3\r\n", 1000, function(d){console.log(d+"xx1");}); Wifi.at.cmd("AT+CIPMUX=1\r\n", 1000, function(d){console.log(d+"xx2");}); 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"); UDP1.ClientIP=ip; console.log("Wi-Fi Connected"); // Setup UDP Client wifi.SetUDP(); var client = require("net").connect({host: BroadIP, port: UDPport,protocolVersion: 17}, function() { console.log('client connected'); console.log(UDP1.ClientIP); client.write(JSON.stringify(UDP1)); client.on('data', function(data) { ddata+=data; if(ddata.charAt(ddata.length-1)==='}'){ UDP2=JSON.parse(ddata); console.log(">"+ddata); ddata=""; if((UDP1.ClientName===UDP2.ClientName)&& (UDP1.ServerName===UDP2.ServerName)){ ServerIP=UDP2.ServerIP; //HTTP or network client goes here menu(); }else{ }//endif }//endif ddata=} });//end client on data client.on('end', function() { console.log('client disconnected'); });//client on end //client.end("); });//end client.net });//end wif.getIP //////////////////////////////////////////////////////////// });//end wifi.connect SSID });//end wifi.reset });//end wifi.connect serial }//end test //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx function sendit(msg){ var reply=""; Wifi.SetTCP(); var client = require("net").connect({host: ServerIP, port: TCPport}, function() { // console.log('client connected'); client.write(msg); client.on('data', function(data) { reply+=data; console.log(">"+JSON.stringify(data)); }); client.on('end', function() { // console.log('client disconnected'); return reply; }); }); } //////////////////////////////////////////////////////////////////////// var sst=""; //Menus menu=function(){ USB.print(" \n\r"); USB.print("Select using digit and return key\n\r"); USB.print("1 LED Off\n\r"); USB.print("2 LED On\n\r"); USB.print("0 Exit\n\r"); }; parsecmd=function(){ USB.print("\n\r"); switch(this.sst.charAt(0)){ case "1"://LED off sst=""; USB.print("LED Off\n\r"); sendit("LED Off"); break; case "2"://LED On USB.print("LED On\n\r"); sst=""; sendit("LED On"); break; case"0"://Exit LoopbackA.print("USB.setConsole();\n\r"); sst=""; USB.print("Exit\n\r"); break; default: menu(); break; }//end switch sst };//end parsecmd //input cmd from terminal,send it to parsecmd() USB.on('data', function (data) { var i; sst+=data; USB.print(data); if(sst.length>0) if(sst.charAt(sst.length-1)==="\r")parsecmd(); }); //Espruino replies here LoopbackA.on('data',function(data){ USB.print(data); //sending data to terminal }); ///////////////////////// setTimeout(function () { var x; console.log("Start"); LoopbackB.setConsole(); x=test(); if(x!==0)console.log("Try again");//error seen if(x===0) console.log("Exit Seen"); }, 1000);
The WebIde screen:
>echo(0); =undefined Start -> LoopbackB <- USB Start connection process Try again Reset the ESP8266 Connecting to WiFi OKxx1 OKxx2 IP= 192.168.1.4 Wi-Fi Connected Client client connected 192.168.1.4 Send 0 {"ClientName":"Bob","ClientIP":"192.168.1.4","ServerName":"Bill","ServerIP":0} > Select using digit and return key 1 LED Off 2 LED On 0 Exit >{"ClientName":"Bob","ClientIP":"192.168.1.4","ServerName":"Bill","ServerIP":"192.168.1.3"} >1 LED Off Client Send 1 LED Off >2 LED On Client Send 1 LED On >0 Exit <- LoopbackB =undefined =undefined >USB.setConsole(); -> USB
And the server screen:
>{"ClientName":"Bob","ClientIP":"192.168.1.4","ServerName":"Bill","ServerIP":0} Send 0 {"ClientName":"Bob","ClientIP":"192.168.1.4","ServerName":"Bill","ServerIP":"192.168.1.3"} Socket accept 1 "LED O" undefined Close false LED O Socket accept 1 "ff" undefined Close false ff Socket accept 1 "LED On" undefined Close false LED On >
-
Use UDP to allow clients to find the server IP address
The UDP is used with a broadcast IP to allow a named client to locate a named server’s IP address.
The message sent via UDP:function UDPmsg(a,b,c,d){ this.ClientName=a; this.ClientIP=b; this.ServerName=c; this.ServerIP=d; } var UDP1=new UDPmsg("Bob",0,"Bill",0);
Client named Bob will ask server named Bill for Bill’s IP address
The server code:
//UDP_TCPserver1.js //2 Oct 2016 //espruino board with ESP8266 //PICO with ESP8266 //var BroadIP="255.255.255.255"; var BroadIP="192.168.1.255"; //var Hardware=0; //Espruino board var Hardware =1; //PICO var SSID="ssid"; var key= "router passcode"; var Serial; var Wifi; //var LocalIP; var UDPport=1234; var TCPport=9988; function UDPmsg(a,b,c,d){ this.ClientName=a; this.ClientIP=b; this.ServerName=c; this.ServerIP=d; } var UDP1=new UDPmsg("Bob",0,"Bill",0); var UDP2=new UDPmsg("",0,"",0); var ddata=""; 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("UDP").connect(Serial, function(err) { //var wifi = require("ESP8266WiFi_0v25").connect(Serial, function(err) { if (err)return 1;// throw err; Wifi=wifi; console.log("Reset the ESP8266"); wifi.reset(function(err) { if (err)return 1;// throw err; Wifi.at.cmd("AT+CWMODE_CUR=3\r\n", 1000, function(d){console.log(d+"xx1");}); Wifi.at.cmd("AT+CIPMUX=1\r\n", 1000, function(d){console.log(d+"xx2");}); 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"); UDP1.ServerIP=ip; console.log("LocalIP= ",UDP1.ServerIP); console.log("WiFi Connected "); //start the UDP listener setupUDP(); //HTTP or Network Server goes here TCPserve(); //////////////////////////////////////////////////////////// });//end wifi.getIP });//end wifi.connectSSID });//end wifi.reset });//end wifi.connect }//end test function setupUDP(){ Wifi.SetUDP(); var client = require("net").connect({host:BroadIP,port:UDPport ,protocolVersion: 17}, function() { console.log('client connected'); client.write(JSON.stringify(UDP1)); client.on('data', function(data){ ddata+=data; if(ddata.charAt(ddata.length-1)==='}'){ UDP2=JSON.parse(ddata); console.log(">"+ddata); if(UDP1.ServerName===UDP2.ServerName){ UDP2.ServerIP=UDP1.ServerIP; client.write(JSON.stringify(UDP2)); ddata=""; }//endif }//endif });//end client on data client.on('close',function(){console.log("client Close");}); client.on('end', function() { console.log('client disconnected'); });//end client on end //client.end(); });//end UDP client connect }//end setupUDP function TCPserve(){ var data; Wifi.SetTCP(); var server = require("net").createServer(function(c) { // A new client as connected // c.write("Hello"); /* c.on('data', function(data) { console.log(">"+JSON.stringify(data)); }); */ c.on('close',function(d){ console.log("Close "+d); data=c.read(c.available()); console.log(data); c.write(data); if(data==="LED Off"){ digitalWrite(B12,0); //B12 is green LED } if(data==="LED On"){ digitalWrite(B12,1); } }); c.end(); }); server.listen(TCPport); }//end TCPserve var x; console.log("Start"); x=test(); if(x!==0)console.log("Try again");//error seen if(x===0) console.log("Exit Seen");
The output as the server starts
>echo(0); Start Start connection process Try again =undefined Reset the ESP8266 Connecting to WiFi OKxx1 OKxx2 IP= 192.168.1.3 LocalIP= 192.168.1.3 WiFi Connected Client Server client connected Send 0 {"ClientName":"Bob","ClientIP":0,"ServerName":"Bill","ServerIP":"192.168.1.3"} >
Again disconnect the server from WebIde and connect to Putty terminal application.
-
Modify ESP8266WiFi_0v25.js to do UDP and TCP
Add UDPflag and CIPstartSTR.var at; var UDPflag=0; var CIPstartStr=""; var socks = [];
Modify the code to CIPstart a client in the create: function
create : function(host, port) { /* Create a socket and return its index, host is a string, port is an integer. If host isn't defined, create a server socket */ if (host===undefined) { console.log("Server"); sckt = MAXSOCKETS; socks[sckt] = "Wait"; sockData[sckt] = ""; at.cmd("AT+CIPSERVER=1,"+port+"\r\n", 10000, function(d) { if (d=="OK") { socks[sckt] = true; } else { socks[sckt] = undefined; setTimeout(function() { throw new Error("CIPSERVER failed ("+(d?d:"Timeout")+")"); }, 0); } }); return MAXSOCKETS; } else { console.log("Client"); var sckt = 0; while (socks[sckt]!==undefined) sckt++; // find free socket if (sckt>=MAXSOCKETS) throw new Error("No free sockets"); socks[sckt] = "Wait"; sockData[sckt] = ""; if(UDPflag){//UDP client CIPstartStr='AT+CIPSTART='+sckt+',"UDP",'+JSON.stringify(host)+','+port+','+port+',0\r\n'; }else{//TCP client CIPstartStr='AT+CIPSTART='+sckt+',"TCP",'+JSON.stringify(host)+','+port+'\r\n'; }//end else at.cmd(CIPstartStr,10000, function cb(d) {
Add code to set and reset the UDP flag
"SetUDP":function(){ UDPflag=1; }, "SetTCP" : function(){ UDPflag=0; }, "getIP" : function(callback) {
UDP.js placed in modules directory of WebIDE project.
AT commands for UDP
https://github.com/espressif/ESP8266_AT/wiki/at_example_0020000903
https://gist.github.com/mokogobo/3f4d2f074305d4d84344https://cdn.sparkfun.com/assets/learn_tutorials/4/0/3/4A-ESP8266__AT_Instruction_Set__EN_v0.30.pdf
AT+CIPSTART – Establish TCP connection or register UDP port, start connection
Example
AT+CIPSTART="TCP","192.168.101.110",1000
Single connection
(+CIPMUX=0)
AT+CIPSTART=,,[,,]
[,]
Multiple connection
(+CIPMUX=1)
AT+CIPSTART=,,,[,,]
[,]
Response
OK
or
ERROR
If connection already exists, returns
ALREAY CONNECT
Parameters
ID of the connection (0~4), for multi-connect
string, "TCP" or "UDP"
string, remote IP
string, remote port
[]
for UDP only
[] In UDP transparent transmission, it has to be 0.
[] 0
: destination peer entity of UDP will not change.
[] 1
: destination peer entity of UDP can change once.
[] 2
: destination peer entity of UDP is allowed to change.
Note: [] can only be used when [] is set.
[]
default 0. unit: 500 milliseconds.
[] 0
: disable TCP keep-alive
[] 1 ~ 7200
: TCP keep-alive intervalAT+CIPSTART – Function 2: R
egister UDP port
Example
AT+CIPSTART="UDP", "192.168.101.110", 1000, 1002,
2
Single connection
(AT
+CIPMUX=0)
AT+CIPSTART=, , [, ,
]
Multiple connection
(AT+CIPMUX=1
)
AT+CIPSTART=, , , [, , ]
Response
OK or
ERROR
If connection already exists, returns
ALREADY CONNECT -
Setup a TCP client
This code sets up the TCP client on port 9988.
The console is redirected to the Loopback to allow the WebIDE to act as a simple menu system.
You will need to edit the hardware flag , ssid and router passcode.
Additionally you have to hardcode the IP address of the server. (we will fix this with UDP later)//netclient3.js //2 Oct 2016 //espruino board with ESP8266 //PICO with ESP8266 var IP="192.168.1.3"; var Sport=9988; //var Hardware=0; //Espruino board var Hardware =1; //PICO var SSID="ssid"; var key= "router 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);}); console.log("Wi-Fi Connected"); // Now you can do something, menu(); //////////////////////////////////////////////////////////// }); }); }); }//end test //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx function sendit(msg){ var reply=""; var client = require("net").connect({host: IP, port: Sport}, function() { // console.log('client connected'); client.write(msg); client.on('data', function(data) { reply+=data; console.log(">"+JSON.stringify(data)); }); client.on('end', function() { // console.log('client disconnected'); return reply; }); }); } //////////////////////////////////////////////////////////////////////// var sst=""; //Menus menu=function(){ USB.print(" \n\r"); USB.print("Select using digit and return key\n\r"); USB.print("1 LED Off\n\r"); USB.print("2 LED On\n\r"); USB.print("0 Exit\n\r"); }; parsecmd=function(){ USB.print("\n\r"); switch(this.sst.charAt(0)){ case "1"://LED off sst=""; USB.print("LED Off\n\r"); sendit("LED Off"); break; case "2"://LED On USB.print("LED On\n\r"); sst=""; sendit("LED On"); break; case"0"://Exit LoopbackA.print("USB.setConsole();\n\r"); sst=""; USB.print("Exit\n\r"); break; default: menu(); break; }//end switch sst };//end parsecmd //input cmd from terminal,send it to parsecmd() USB.on('data', function (data) { var i; sst+=data; USB.print(data); if(sst.length>0) if(sst.charAt(sst.length-1)==="\r")parsecmd(); }); //Espruino replies here LoopbackA.on('data',function(data){ USB.print(data); //sending data to terminal }); ///////////////////////// setTimeout(function () { var x; console.log("Start"); LoopbackB.setConsole(); x=test(); if(x!==0)console.log("Try again");//error seen if(x===0) console.log("Exit Seen"); }, 1000);
And the WebIde output:
>echo(0); =undefined Start -> LoopbackB <- USB Start connection process Try again Reset the ESP8266 Connecting to WiFi > Select using digit and return key 1 LED Off 2 LED On 0 Exit Wi-Fi Connected IP= 192.168.1.4 null >1 LED Off 2 LED On 1 LED Off 0 Exit <- LoopbackB =undefined =undefined >USB.setConsole(); -> USB
Notice that the 0 Exit restores the console to the USB port.
The Server output on the Putty terminal:Close false LED Off Close false LED On Close false LED Off >
-
- Setup a TCP network server
- Setup a TCP client
- Modify ESP8266WiFi_0v25.js to do UDP and TCP
- Use UDP to allow clients to find the server IP address
The UDP/TCP client.
Setup a TCP network server
The TCP server and client code are based on the examples listed under the Sockets section of
http://www.espruino.com/Internet
The following code sets up a TCP server on port 9988.
You will need to edit:
//var Hardware=0; //Espruino board
var Hardware =1; //PICO
var SSID="ssid";
var key= "router passcode";
When a client connects two commands are accepted “LED On” and “LED Off”
//netServer3.js //2 Oct 2016 //espruino board with ESP8266 //PICO with ESP8266 var Sport=9988; //var Hardware=0; //Espruino board var Hardware =1; //PICO var SSID="ssid"; var key= "router passcode"; var Serial; var MyIP=""; 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);MyIP=ip; console.log("Wi-Fi Connected"); // Now you can do something, serve(); //////////////////////////////////////////////////////////// }); }); }); }); }//end test var x; console.log("Start"); x=test(); if(x!==0)console.log("Try again");//error seen if(x===0) console.log("Exit Seen"); //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx function serve(){ var data; var server = require("net").createServer(function(c) { // A new client has connected // c.write("Hello"); /* c.on('data', function(data) { console.log(">"+JSON.stringify(data)); }); */ c.on('close',function(d){ console.log("Close "+d); data=c.read(c.available()); console.log(data); c.write(data); if(data==="LED Off"){ digitalWrite(B12,0); //B12 is green LED } if(data==="LED On"){ digitalWrite(B12,1); } }); c.end(); }); server.listen(Sport); }
The TCP server output:
>echo(0); Start Start connection process Try again =undefined Reset the ESP8266 Connecting to WiFi IP= 192.168.1.3 null Wi-Fi Connected >
At this point I disconnect the WebIDE from the TCP server hardware and reconnect using putty.
Make a note of the server IP addess. - Setup a TCP network server
-
Try the example at the end of this link to see if the pin is correct.
http://crufti.com/getting-started-with-espruino-on-esp8266/
var led = new Pin(4); var toggle=1; setInterval(function() { toggle=!toggle; digitalWrite(led, toggle); }, 500); //You should see the LED connected to D2 blinking:
There is also a DH22 module that you could try in the software require(DH22).
-
-
http://www.espruino.com/EspruinoESP8266
Use this link to make sure you are naming the pin correctly in the call function.
var dht = require("DHT11").connect(D14);
-
Hi @Aleksandrs
I don't have a DH11.
I would suggest disconnecting the DH11 and using an LED and resistor in series.
Use digitalWrite(D14,0) and digitalWrite(D14,1) and see if you have the correct pinhttp://crufti.com/getting-started-with-espruino-on-esp8266/
These links may be of use to you.
-
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");
-
(As another user)
Did you load the program into the device and then in the left pane of the WebIDE type in
save();
?
http://www.espruino.com/Reference#l__global_save -
You can do both UDP and TCP at the same time.
Wifi.at.cmd("AT+CWMODE_CUR=3\r\n", 1000, function(d){console.log(d+"xx1");});
You call SetUDP and then setup a network with protocol 17 for UDP.
Then you call SetTCP and setup either HTTP or a TCP network.wifi.SetUDP(); var client = require("net").connect({host: BroadIP, port: UDPport,protocolVersion: 17}, function() {
Use the UDP to obtain the IP address of the server and then
wifi.SetTCP(); getReply(console.log); ... function getReply(callback) { var options = { host: ServerIP, port: '8080', path:"/", method:'GET', headers: { } }; console.log("getReply"); require("http").request(options, function(res) { var d = ""; res.on('data', function(data) { d+= data; }); res.on('close', function(data) { var j = JSON.parse(d); console.log("d= ",d);//console.log("j= ",j); if (callback) callback((j&&j.result)?j.result:undefined); }); }).end(); }//end getReply
On the server side
wifi.SetUDP(); var client = require("net").connect({host:BroadIP,port:UDPport ,protocolVersion: 17}, function() { console.log('client connected'); client.write(JSON.stringify(UDP1)); client.on('data', function(data){ ddata+=data; if(ddata.charAt(ddata.length-1)==='}'){ UDP2=JSON.parse(ddata); console.log(">"+ddata); if(UDP1.ServerName===UDP2.ServerName){ UDP2.ServerIP=UDP1.ServerIP; client.write(JSON.stringify(UDP2)); ddata=""; }//endif }//endif });//end client on data client.on('close',function(){console.log("client Close");}); client.on('end', function() { console.log('client disconnected'); });//end client on end //client.end(); });//end UDP client connect });//end wifi.getIP //HTTP or Network Server goes here wifi.SetTCP(); serveHTML(); ////////////////// ..... function serveHTML(){ var http=require("http").createServer(onPageRequest).listen(8080); }//end serveFile 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
-
Have a look at this for UDP.
http://forum.espruino.com/conversations/293517/
SetTCP() SetUDP() set a flag and are used before setting up HTTP and Net -
Attached is some code to try for TCP comunications.
You will need to edit the SSID and key to configure your router access.
On one Pico start the server code and note the IP address it will display.
Disconnect from WebIDE and connect with a Terminal program such as Putty.
On a second Pico load the client code using WebIde.
In addition to the SSID and key edit the IP variable to match the server IP address
You can also change the port in both server and client code as it is 1234 in this code. -
Attached file contains code to serve HTML. It does both a Get and Post method.
You will need to edit the SSID and router password.
Adjust the serial port stuff to fit your hardware.The IP address will display in the WebIDE
Mine was 192.168.3
I used it in the Browser as follows:You will need to modify the doGet() function at the bottom to serve your content.
function doGet(req,res){ var a = url.parse(req.url, true); console.log("Get"); res.writeHead(200, {'Content-Type': 'text/html'}); res.write(KMtext); res.end(""); }
-
You have implemented a house full of IoT devices and have a happy automated life. The power goes off and when it comes back on the DCHP in your router reassigns IP addresses and your IoTs can’t find each other.
These programs show how to implement UDP (User Datagram Protocol) in Espruino and use the broadcast IP address 255.255.255.255 to allow the IoT’s to discover the IP addresses.Place UDP.js in the project modules folder.
Edit the UDP_HTTPserver2.js and UDP_HTTPclient3.js files and insert your router SSID and password.
var SSID="ssis";
var key= "router password";The ESP8266WiFi_0v25.js module was amended into UDP.js
var wifi = require("UDP").connect(Serial, function(err) {
//var wifi = require("ESP8266WiFi_0v25").connect(Serial, function(err) {
The send function is currently set to print the socket number and text.These variables were added: var UDPflag=1; var CIPstartStr=""; The client code was modified var netCallbacks = { create : function(host, port) { … } else { console.log("Client"); var sckt = 0; while (socks[sckt]!==undefined) sckt++; // find free socket if (sckt>=MAXSOCKETS) throw new Error("No free sockets"); socks[sckt] = "Wait"; sockData[sckt] = ""; if(UDPflag){//UDP client CIPstartStr='AT+CIPSTART='+sckt+',"UDP",'+JSON.stringify(host)+','+port+','+port+',0\r\n'; }else{//TCP client CIPstartStr='AT+CIPSTART='+sckt+',"TCP",'+JSON.stringify(host)+','+port+'\r\n'; }//end else at.cmd(CIPstartStr,10000, function cb(d) { And two commands were added, "SetUDP":function(){ UDPflag=1; }, "SetTCP" : function(){ UDPflag=0; }, "getIP" : function(callback) {
UDP_HTTPserver2.js
This uses the broadcast IP of 255.255.255.255 to listen for a message from a client.
The message identifies the client, the client’s IP and the server name.
If the server name refers to this server program then a UDP broadcast message is sent that supplies the IP address of this server.
An HTTP server is also implemented. If you run the program, a browser can access the simple web page. (The browser needs the IP address)>echo(0); Start Start connection process Try again =undefined Reset the ESP8266 Connecting to WiFi OKxx1 OKxx2 Server IP= 192.168.1.3 LocalIP= 192.168.1.3 WiFi Connected Client client connected Send 0 {"ClientName":"Bob","ClientIP":0,"ServerName":"Bill","ServerIP":"192.168.1.3"}
UDP_HTTPclient3.js
This program uses UDP to broadcast a message containing the client name, client IP and the server name that is needed to start a connection. The program listens for the reply message and if the names match it sends an HTTP Get request to the server at the server IP returned in the UDP message. The result of the HTTP Get request are displayed>echo(0); Start Start connection process Try again =undefined Reset the ESP8266 Connecting to WiFi OKxx1 OKxx2 IP= 192.168.1.4 Wi-Fi Connected Client client connected 192.168.1.4 Send 0 {"ClientName":"Bob","ClientIP":"192.168.1.4","ServerName":"Bill","ServerIP":0} >{"ClientName":"Bob","ClientIP":"192.168.1.4","ServerName":"Bill","ServerIP":"192.168.1.3"} getReply Client Send 1 GET / HTTP/1.0 User-Agent: Espruino 1v86 Connection: close Host: 192.168.1.3:8080 d= <html><body style="text-align:center;margin-left:auto;margin-right:auto;"> <h1>UDP Demo.</h1> </body> </html>
UDP_HTTPserver2.js
>{"ClientName":"Bob","ClientIP":"192.168.1.4","ServerName":"Bill","ServerIP":0} Send 0 {"ClientName":"Bob","ClientIP":"192.168.1.4","ServerName":"Bill","ServerIP":"192.168.1.3"} Socket accept 1 "GET / " undefined Get Send 1 HTTP/1.0 200 OK Server: Espruino 1v86 Content-Type: text/html <html><body style="text-align:center;margin-left:auto;margin-right:auto;"> <h1>UDP Demo.</h1> </body> </html> >
-
I found a solution:
Use a SetInterval and wait for the IP address to show up.wifi.getIP(function(l,ip){console.log("IP= ",ip,"\n\r"+l);xIP=ip;}); id=setInterval(function () { console.log("Hello World"); if(xIP!==undefined){ console.log("xIP= ",xIP); clearInterval(id); console.log("Wi-Fi Connected "); // Now you can do something, var client = require("net").connect({host: IP, port: 1234,protocolVersion: 17}, function() { console.log('client connected'); client.write("Jello"+xIP); client.on('data', function(data) { console.log(">"+JSON.stringify(data)+"xjello"); client.write("Reply"+xIP); }); client.on('end', function() { console.log('client disconnected'); }); //client.end("UTLEY"); }); } }, 1000);
And the output:
>echo(0); Start Start connection process Try again =undefined Reset the ESP8266 Connecting to WiFi OKxx1 OKxx2 IP= 192.168.1.3 null Hello World xIP= 192.168.1.3 Wi-Fi Connected Client client connected Send 0 Jello192.168.1.3 >
And the Billy Bob device
>echo(0); Start Start connection process Try again =undefined Reset the ESP8266 Connecting to WiFi OKxx1 OKxx2 Wi-Fi Connected Client client connected IP= 192.168.1.4 null "192.168.1.4" Send 0 XX 192.168.1.4Jello >"Reply192.168.1.3"xjello > Disconnected
I expect to post the UDP in a few days after some code cleanup.
It modifies the ESP8266WiFi_0v25 module to allow UDP.
var wifi = require("UDP").connect(Serial, function(err) {
//var wifi = require("ESP8266WiFi_0v25").connect(Serial, function(err) { -
The assignment MyIP=ip; is done in the callback. That's what's puzzling. The console.log(ip) is also done in the callback before the assignment and displays the IP address. It's likely a timing issue but still puzzling.
I've got UDP working (not this example code). I broadcast using IP=255.255.255.255 and send a keyword and local IP address. If the keyword matches, the other end broadcasts using a keyword and it's local IP address. So I need the local IP address from the getIP function to finish this.
Msg1: Hi I'm Billy Bob at IPxxx, where are you Sally?
Msg2: Hi I'm Sally at IPyyy.
Then Billy Bob can use IPyyy to contact Sally.
Right now I'm hard coding the local IP addresses, but that defeats the purpose since the next time my router resets the IP addresses will be reassigned. -
PICO shimmed to ESP8266.
I use wifi.getIP() and print the resulting IP address, then assign it to a global variable.
Printing the global variable MyIP doesn’t work.
Later typing MyIP; into the left pane I get the IP address.
So what’s going on and is there a solution?//MyIP.js //26Sep 2016 //espruino board with ESP8266 //PICO with ESP8266 //var Hardware=0; //Espruino board var Hardware =1; //PICO var SSID="ssis"; var key= "routerpw"; var Serial; var MyIP=""; 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);MyIP=ip;}); console.log("Wi-Fi Connected"); console.log("MyIP= ",MyIP,"XXX"); console.log("MyIP= "+MyIP+"XXX"); // Now you can do something, var server = require("net").createServer(function(c) { // A new client as connected c.write("Hello"); c.on('data', function(data) { console.log(">"+JSON.stringify(data)); }); c.end(); }); server.listen(1234); }); }); }); }//end test var x; console.log("Start"); x=test(); if(x!==0)console.log("Try again");//error seen if(x===0) console.log("Exit Seen");
The output:
>echo(0); Start Start connection process Try again =undefined Reset the ESP8266 Connecting to WiFi Wi-Fi Connected MyIP= XXX MyIP= XXX IP= 192.168.1.3 null >MyIP; ="192.168.1.3" >
-
This link might help. Note it uses a 0.9 duty cycle.
http://www.espruino.com/Pico+Infrared
Since you don't have a scope you might try this one liner to see the LED flashing.
analogWrite(A5,0.5,{freq:0.5});
It uses a lower frequency so you can see the flashes.I did this one liner to flash the Green LED on a Pico. This is not PWM pin so you have to use the software PWM. PWM pins use the Timer peripherals built into the ARM chip.
analogWrite(B12,0.5,{freq:0.5,soft:true});
Latest AT document 2.0
https://espressif.com/sites/default/files/documentation/4a-esp8266_at_instruction_set_en.pdf