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>
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.
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.
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)
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
UDP_HTTPserver2.js
3 Attachments