How to make two ESP8266 communicate through WiFi with Espruino and JavaScript

In this article we will make two ESP8266 communicate using the WiFi network. We will use Espruino and some JavaScript code.

Setup the WiFi

Espruino offers a WiFi module that let us to connect to a WiFi network.

The code below will connect our ESP to the WiFi network and will print the information about the obtained IP plus other network information:

var wifi = require("Wifi");
wifi.connect("***WIFI*SSID***", {password:"***WIFI*PASSWORD***"}, function(err) {
  console.log("connected? err = ", err, " info = ", wifi.getIP());
});

The result should be:
 _____                 _
|   __|___ ___ ___ _ _|_|___ ___
|   __|_ -| . |  _| | | |   | . |
|_____|___|  _|_| |___|_|_|_|___|
          |_| http://espruino.com
 1v93 Copyright 2016 G.Williams
Espruino is Open Source. Our work is supported
only by sales of official boards and donations:
http://espruino.com/Donate
Flash map 4MB:512/512, manuf 0xe0 chip 0x4016
>
=undefined
connected? err =  null  info =  {
  "ip": "192.168.1.106",
  "netmask": "255.255.255.0",
  "gw": "192.168.1.1",
  "mac": "5c:cf:7f:f8:53:5a"
 }

Please take note of the IP for both the devices, currently there are no other way to discover each other.

Now both the devices are connected to the WiFi network. Now we can make them talk each other. In this example we elect one board server and the other client. The client board will start a TCP communication toward the server board. Once connected they will able to exchange messages.

Setup the server

The server first setup the WiFi connection, then creates a socket server on port 1234 for receiving connection from the clients. When a connection is established it will print the messages coming from the client and will close the connection saying “I’m a server! Goodbye.”.

function handleClient(socket) {
  console.log('client connected');

  socket.on('error', console.log);

  socket.on('data', function(d) {
    console.log('client says: ', d);
    socket.end("I'm a server! Goodbye.");
  });
}

function setupServer() {
  var net = require("net");
  var server = net.createServer(handleClient);
  server.listen(1234);
}

function connectWiFi() {
  var wifi = require("Wifi");

  wifi.connect("***WIFI*SSID***", {password:"***WIFI*PASSWORD***"}, function(err) {
    console.log("connected? err = ", err, " info = ", wifi.getIP());

    setupServer();
  });
}

function onInit() {
  connectWiFi();
}

save();

Setup the client

The client, after get ready with the WiFi connection, will try to connect to the server. When the connection is established it will send a “Hello I’m a client!” message and will wait for responses from the server.

function talkToServer(socket) {
  console.log('connected to the server');

  socket.on('error', console.log);

  socket.on('data', function(d) {
    console.log('server says: ', d);
  });

  socket.on('end', function() {
    console.log('disconnected from the server');
  });

  socket.write('Hello, I\'m a client!');
}

function connectToServer() {
  var net = require("net");
  net.connect({host: "192.168.1.105", port: 1234}, talkToServer);
}

function connectWiFi() {
  var wifi = require("Wifi");

  wifi.connect("***WIFI*SSID***", {password:"***WIFI*PASSWORD***"}, function(err) {
    console.log("connected? err = ", err, " info = ", wifi.getIP());

    connectToServer();
  });
}

function onInit() {
  connectWiFi();
}

save();

The output

Here the output in the server side:

>
 _____                 _
|   __|___ ___ ___ _ _|_|___ ___
|   __|_ -| . |  _| | | |   | . |
|_____|___|  _|_| |___|_|_|_|___|
          |_| http://espruino.com
 1v93 Copyright 2016 G.Williams
Espruino is Open Source. Our work is supported
only by sales of official boards and donations:
http://espruino.com/Donate
Flash map 4MB:512/512, manuf 0xe0 chip 0x4016
>Erasing Flash..................
Writing.....
Compressed 25600 bytes to 2137
Checking...
DRunning onInit()...
connected? err =  null  info =  {
  "ip": "192.168.1.105",
  "netmask": "255.255.255.0",
  "gw": "192.168.1.1",
  "mac": "5c:cf:7f:0f:7a:2b"
 }
client connected
client says:  Hello, I'm a client!
>

Here the output for the client side:
>
 _____                 _
|   __|___ ___ ___ _ _|_|___ ___
|   __|_ -| . |  _| | | |   | . |
|_____|___|  _|_| |___|_|_|_|___|
          |_| http://espruino.com
 1v93 Copyright 2016 G.Williams
Espruino is Open Source. Our work is supported
only by sales of official boards and donations:
http://espruino.com/Donate
Flash map 4MB:512/512, manuf 0xe0 chip 0x4016
>
=undefined
Erasing Flash..................
Writing.....
Compressed 25600 bytes to 2352
Checking...
DRunning onInit()...
connected? err =  null  info =  {
  "ip": "192.168.1.104",
  "netmask": "255.255.255.0",
  "gw": "192.168.1.1",
  "mac": "5c:cf:7f:f8:53:5a"
 }
connected to the server
server says:  I'm a server! Goodbye.
disconnected from the server
>