How to Make Two ESP8266 Talk

In this project you’re going to make two ESP8266 talk with each other.

How it works? You’re going to set one ESP as an Access Point (Server) and another ESP as a Station (Client). Then they’ll establish a wireless communication and the Client sends a message to the Server saying “Hello World!”.

esp client vs server

Before continue reading this project, please complete the following tutorials:

If you like the ESP and you want to do more projects you can read my eBook Home Automation using ESP8266 hereLet’s get started!

Parts List

Here’s the hardware that you need to complete this project:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Schematics (3.3V FTDI Programmer)

The schematics for this project are very straight forward. You only need to establish a serial communication between your FTDI programmer and your ESP8266 to upload some code. (Repeat the schematics below for the Client and Server)

ESP-Bitcoin-price_bb

Downloading ESPlorer

I recommend using the ESPlorer program created by 4refr0nt to create and save Lua files into your ESP8266.

Follow these instructions to download and install ESPlorer:

  1. Click here to download ESPlorer
  2. Unzip that folder
  3. Go to the main folder
  4. Run ESPlorer.jar. It’s a JAVA program, so you need JAVA installed on your computer
  5. Open the ESPlorer

esplorer start

Writing Your Server Script

Don’t forget that first you need to flash both your ESPs with NodeMCU firmare. Copy and paste the code below into ESPlorer.

Summary: The ESP Server acts as an Access Point and it has its own SSID=test and Password=12345678. The server is continuously listening for a connection, when it successfully establishes a connection and receives a message prints that string on the serial monitor.

-- Rui Santos
-- Complete project details at https://randomnerdtutorials.com
-- ESP8266 Server

print("ESP8266 Server")
wifi.setmode(wifi.STATIONAP);
wifi.ap.config({ssid="test",pwd="12345678"});
print("Server IP Address:",wifi.ap.getip())

sv = net.createServer(net.TCP) 
sv:listen(80, function(conn)
    conn:on("receive", function(conn, receivedData) 
        print("Received Data: " .. receivedData)         
    end) 
    conn:on("sent", function(conn) 
      collectgarbage()
    end)
end)

View raw code

Uploading Your Lua Script

When you open the ESPlorer you should see a window similar to the preceding Figure, follow these instructions to send commands to your ESP8266:

  1. Connect your FTDI programmer to your computer
  2. Set bad raute as 9600
  3. Select your FTDI programmer port (COM3, for example)
  4. Press Open/Close
  5. Select NodeMCU+MicroPtyhon tab
  6. Copy the your Lua script into ESPlorer

Then you simply click the button Save to ESP and save your file with the name “init.lua”. Everything that you need to worry about or change is highlighted in red box in the following Figure.

esplorer

Writing Your Client Script

Having your ESP flashed with NodeMCU. Copy and paste the code below into ESPlorer. Summary: The ESP Client acts as a Station and it is continuously looking for an Access Point. When the Client finds the Server establishes a communication and sends a message “Hello World!” every 5 seconds.

-- Rui Santos
-- Complete project details at https://randomnerdtutorials.com
-- ESP8266 Client

print("ESP8266 Client")
wifi.sta.disconnect()
wifi.setmode(wifi.STATION) 
wifi.sta.config("test","12345678") -- connecting to server
wifi.sta.connect() 
print("Looking for a connection")

tmr.alarm(1, 2000, 1, function()
     if(wifi.sta.getip()~=nil) then
          tmr.stop(1)
          print("Connected!")
          print("Client IP Address:",wifi.sta.getip())
          cl=net.createConnection(net.TCP, 0)
          cl:connect(80,"192.168.4.1")
          tmr.alarm(2, 5000, 1, function() 
            cl:send("Hello World!") 
          end)
      else
         print("Connecting...")
      end
end)

View raw code

Note: To upload the Client Script code follow the preceding heading called “Uploading Your Lua Script”.

Demonstration

In this project the Client is sending a message “Hello World!” to the Server via wireless. Here’s what you should see in your serial terminal windows (see Figure below).

data received esp8266

Note: On the left window, I’m using the ESPlorer Output Window . On the right window, I’m establishing a serial communication with PuTTY.org.

Do you have any questions? Leave a comment down below!

Thanks for reading. If you like this post probably you might like my next ones, so please support me by subscribing my blog and my Facebook Page.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our newsletter!

61 thoughts on “How to Make Two ESP8266 Talk”

  1. Hi Rui
    I am playing with the stand-alone Server and Client.
    If the ESP8266 Server has a power failure and is restarted, it does not seem to receive anything from the Client.
    If one however reboot the Client, the message appears again.
    How can one “forces” the Server to connect automatically after a “lost” connection?
    Thank you

    Reply
    • What you want to do is to force the client to reconnect to the Server.
      You could do something like this:

      cl:on(“disconnection”, function(cl)
      print(“Disconnected!”)
      connect_to_wifi()
      end )

      Basically when the client faces on “disconnection” it executes a function called connect_to_wifi(). You simply reconnect to the Server in that connect_to_wifi() function.
      I hope this helps,
      Rui

      Repl

      Reply
  2. I have the same question as Vlies Walker :
    How can one “force” the Server to reconnect automatically after a “lost” connection? Without having to reboot the Client.
    Thank you

    Reply
    • Hi Rolland,
      What you want to do is to force the client to reconnect to the Server.
      You could do something like this:

      cl:on(“disconnection”, function(cl)
      print(“Disconnected!”)
      connect_to_wifi()
      end )

      Basically when the client faces on “disconnection” it executes a function called connect_to_wifi(). You simply reconnect to the Server in that connect_to_wifi() function.
      I hope this helps,
      Rui

      Reply
  3. HI Rui,
    I have purchased ESP8266, and try to flash NodeMCU firmware.
    But ESP8266 flasher does not work. No MAC address were shown . When I use Esplorer, the serial port cannot open at all, though I tried different COM speeds.
    In this case, how do I make it work?

    Reply
    • Hi Sunwoo,
      Thank you for supporting my work.
      Which ESP are you using? ESP-01? Have you connected the ESP with my exact schematics? Are you using an FTDI programmer? Have you tried with different baud rates 9600 or 115200?

      Reply
      • Thanks for your prompt reply, Rui.
        I am using ESP-01, and I use FTDI programmer with 3.3V jumper setting. Also, follow your schemetis on wiring and tried with various boud rate I could choose from the list.
        I wonder whether my ESP-01, from Aliexpress, did not flashed anything from the factory.

        Reply
  4. Is there a way to seamlessly send the serial data from the input of one of them to the output of another with no additional info? So basically if one device is getting “abcd” the other device will put out exactly “abcd” with no other data. Also is there a way to automatically reconnect when the devices come back in range of one another?

    Reply
    • Yes, you can do that. I’ll be posting a tutorial similar to what you’re looking for next week.
      Where an Arduino is sending some characters to the ESP via serial and the ESP is using that info..

      Reply
  5. Hello:
    Very nice articles.
    Is it possible to put the ESP8266 in sleep/power down mode using LUA or do I have to use the native SDK?
    I would like to create a sensor with the ESP8266 and transmit data every 15 minutes, but it should sleep to save power rest of the time.

    Thanks in advance;
    Gus

    Reply
    • Yes Gus, The NodeMCU firmware supports sleep, you can use it with this function: node.dsleep()
      Here’s the official API usage web page: github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en#nodedsleep

      Reply
  6. Hi guy.

    so, I would like to connect two ESP divices to talk but, I need to send a msg by serial to an ESP, and this send this by wifii to another. How can I do this?

    Reply
  7. hey Rui ,this code will work if my client esp8266 is on one wifi network and my server esp8266 is on another wifi network. if not can u please tell me some way doing it.thanks

    Reply
  8. HI!
    First of all, I want to thank you for this tools, are genial.
    Second of all , I can’t connect to ESP through the ESPlorer, I ve selected the port and affter I open the port is trying to connect but without succes.
    Thank you!
    It is flashed with NodeMcu

    Reply
  9. init.lua:10: only one tcp server allowed

    How to stop currently running server to flash new code since I am getting above error when try to flash ?

    Reply
  10. init.lua:10: only one tcp server allowed

    How to stop currently running server to flash new code since I am getting above error when try to flash ?

    Reply
  11. Hi, the tutorial was crisp and clear.
    how many client esp8266 can we connect to one esp8266 server ??
    Thanks in advance

    Reply
  12. Hi,
    Appreciate all your esp tutorial, they helped alot!

    Currently I am testing out the communication between 2 ESP using the WEMOS d1r2 module, and I am wondering if its possible for me to get the server ESP to acknowledge the data received from the client ESP (printout from client ESP)?
    Thank you!

    Reply
  13. Dear Rui,
    Loved the way you demonstrated. Moreover i want to use esp8266 01 as remote switch and remote device. without using internet, i.e. similar to as of rf module. I just want to keep led on client on till server has an input. any Idea how to do that.?

    Reply
  14. i need a code and programming steps to make 2 esp modules communicate each other. ie, each module interconnect and able to send and receive. Can you please help me out in this concern

    Reply
    • Ajith,
      Did you come across anything eventually? i am trying something similar using arduino but i cant seem to figure it out

      Reply
      • Youn said you were having difficulty transmitting/receiving with an Arduino. The ESP8266 was suggested. I hope you have realised that the ESP8266 has the necessary hardware to transmit/receive. You can see the “square wave” antenna etched on the module. Unless you have plugged some module onto the Arduino, it dpoesn’t have that facility.

        Reply
  15. I need a code for wireless communication between two esp8266 modules like…. if a pin in 1st module is high then any required pin in 2nd module should glow…. please help me with the code… It would be better if it is a arduino sketch… please

    Reply
  16. Nice session. I wonder if you could help me with the displaying the code transmitted by esp8266 on an LCD using arduinointerfaceon both ends.

    Reply
  17. cl:connect(80,”192.168.4.1″)
    The IP addr in this line in the client code is determined by the IP addr printed in the server code, right?
    So for me to connect my client to the server, I must first run the server code and see what IP gets printed and enter that IP into my client code. Correct me if I am wrong.

    Reply
  18. Dear sir,
    Thank you for this useful tuto. I have a question concerning how to configure two ESP modules both as clients and my laptop as a server. Thank you for your time.

    Reply
  19. Hi, thanks for the useful tuto. My question is the following: I have 2 esp8266’s connected to an arduino mega each in order to be able to send instructions wirelessly from PC to the 2 arduinos, I already configured both esp8266’s as clients , how to establish the communication between them and the pc and how to send a program from this latter to the 2 arduinos wirelessly using the wifi mosules? Thanks a million !

    Reply
  20. Trying to get two esp8266 talking using ESP8266HTTPClient.h but can’t find a download. Copied all the files from git hub and reassembled them in a library directory but still Arduino can’t find it.

    Reply
  21. Great project. I would like to have a client that reads a PIR, then sends the 1 or 0 to 4 8266’s to turn on undercabinet lights. Would I need 4 servers? How would I code this? thanks.

    Reply

Leave a Reply to venkat Cancel reply

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.