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:

The result should be:

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.”.

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.

The output

Here the output in the server side:

Here the output for the client side: