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!”.
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 here. Let’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)
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:
- Click here to download ESPlorer
- Unzip that folder
- Go to the main folder
- Run ESPlorer.jar. It’s a JAVA program, so you need JAVA installed on your computer
- Open the ESPlorer
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)
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:
- Connect your FTDI programmer to your computer
- Set bad raute as 9600
- Select your FTDI programmer port (COM3, for example)
- Press Open/Close
- Select NodeMCU+MicroPtyhon tab
- 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.
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)
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).
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.
Hope you will also create tutorials on using it just as a WiFi module working with the Arduino Uno which most of us are already familiar.
Thanks
Hi Wongss,
Thank you for your feedback. I actually prefer to use the ESP as a standalone device, because it’s capable of doing that.
There’s no need to use another microcontroller for most projects.
And you can always attach an Arduino to your ESP and make them communicate via serial.
Could you please upload a tutorial on how to do that?
Serial conn with arduino
I’m working on it. It should be on my blog next week.
Thanks!
Well Done Rui!
Worked first time —- only thanks to your amazing tutor skills!
Looking forward to the next topic.
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
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
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
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
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?
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?
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.
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?
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..
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
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
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?
Hi Anderson,
I have something a bit different, but it might help: https://randomnerdtutorials.com/sending-data-from-an-arduino-to-the-esp8266-via-serial/
Thanks by your reply, but my porblem is how to connect two ESP. One to another by wifi.
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
In this example both your ESP are connected to each other.
There isn’t a router involved in the process
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
Could you please read the troubleshooting guide? The reset method might solve your issue: https://randomnerdtutorials.com/esp8266-troubleshooting-guide/
Problem solved! Thank you
Awesome!
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 ?
Remove the old script with file.format() and restart your ESP8266 node.restart().
Here’s a troubleshooting guide: https://randomnerdtutorials.com/esp8266-troubleshooting-guide/
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 ?
Remove the
Hi, the tutorial was crisp and clear.
how many client esp8266 can we connect to one esp8266 server ??
Thanks in advance
You can connect more than one sir
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!
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.?
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
I don’t have any tutorials on that exact subject.
Thanks for asking,
Rui
Ajith,
Did you come across anything eventually? i am trying something similar using arduino but i cant seem to figure it out
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.
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
Hi I don’t have any tutorials on that exact subject
Thanks for asking,
Rui
Tq for reply… let me know if you know about digital data transfer between two modules
Hello,
Do you know if it is possible to connect two esp8266 as station ? I mean, not one in station mode and one in AP mode, but both as station mode.
One needs to be set as access point and the other as station, otherwise there’s not way they can establish a wireless communication.
your examples are very educational and accurate. very good
Thanks Marco!
Nice session. I wonder if you could help me with the displaying the code transmitted by esp8266 on an LCD using arduinointerfaceon both ends.
Yes, you could add a display, but I don’t have any tutorial on that exact subject.
Thanks for asking,
Rui
how connect 3 x esp8266
You must set 2 ESPs as clients connected to the main server
Hi Rui
I want to send data from server to client
Please tell me code for this
The code is on this web page… Simply copy the code to your code editor.
Thanks
Rui
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.
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.
The ESP can only be configured as a client or as a server once a time.
Thanks,
Rui
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 !
Awesome work !!!!!
I have one question.
If I want to send some data to client, then what should I do ???????
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.
This project was programmed with Lua code… Regards,
Rui
Looking forward for you next posts
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.
Hi Vic.
The best way to communicate between multiple ESP8266 boards is using MQTT: https://randomnerdtutorials.com/what-is-mqtt-and-how-it-works/
https://randomnerdtutorials.com/esp8266-and-node-red-with-mqtt/
Regards,
Sara
that sounds overly complicated. think i will try ESPNOW. thanks.