//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
>
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.
The UDP/TCP client
The WebIde screen:
And the server screen:
1 Attachment