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.
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.
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:
Client named Bob will ask server named Bill for Bill’s IP address
The server code:
The output as the server starts
Again disconnect the server from WebIde and connect to Putty terminal application.
1 Attachment