Introduction: WiFi Communication Between Two ESP8266 Based MCU Through the Home Router

Hello Makers!

Some months ago I started to experiment ESP8266 based NodeMCU and Wemos boards. At first I used them (in my previous POI hunter project) as a smarter Arduino, because they have higher clock frequency, more memory and built in "SD card" (SPIFFS).

In my Arduino projects sometimes I had to send messages between two MCUs. I used wired communications (serial, i2C) and wireless (bluetooth, RF transmitter / receiver and nRF24). So there was not question I will face this topic using ESP modules also.

I started to mine the internet for ready samples but I didn't find an easy solution for MCU-MCU WiFi communication. Finally I started from the basics. According to the description of the Arduino WiFi library and the WiFi library for ESP8266 I made what I wanted.

In the next pages I will share the result with you in two versions. At first two bare MCU will demonstrate the bare minimum solution of the TCP communication. After that I will show an easy application using an additional DHT22 sensor and some OLEDs.

Step 1: Bare Minimum, TCP Communication Between Two ESP8266 MCUs

Yo can see any HW you need in the picture. Only two MCUs (and a PC).

Both MCUs connect to your home WiFi router. (Don't forget to put your SSID and password into the code before upload the code.) One of the MCUs will be a server and the other will be the client. The client triggers the communication asking the server and the server will send an answer. Modifying the messages you will be able to send anything between the two modules.

Your router will give IP addresses to your MCUs during the connection. Launching the server you will see its IP address in the lower row of the OLED. You have to put this IP into the client's program, because the client will connect to the server using this address. Running the devices some days later you server will receive different IP address, and you will need to modify the client program again. To avoid this annoying thing give fix IP address to your server.

Things you have to do to launch the server:

  • Connect one MCU to a PC with USB cable.
  • Open the "wifi_server_01.ino".
  • Replace the asterisks "****" with the SSID and the password of your home WiFi router.
  • Open the settings of your home WiFi router and choose a free IP address which is out of the DHCP range.
  • Set the IP of your server (row15).
  • Gateway will be the IP of your WiFi router.
  • Upload the program.
  • Run the serial monitor of the Arduino IDE.

Things you have to do to launch the client(s):

  • Run an additional Arduino IDE.
  • Open the "wifi_client_01.ino".
  • Replace the asterisks "****" with the SSID and the password of your home WiFi router.
  • Put the IP address of your server into the 15th row.
  • Upload the program.
  • Run the serial monitor for the new Arduino IDE also.

The client connects to the server soon and you have to see the received messages in both terminal windows. During the communication the built in LEDs will flash for a short time.

Step 2: DHT Server With Clients

Here is an application which uses the TCP communication between ESP8266 MCUs.

A Wemos server listens, reads the DHT22 and shows the temperature and its own IP address on a small OLED screen. Two NodeMCU clients trigger the server and receives the temperature value as the answer. During the communication the server's OLED switches to inverse.

There is only a small modification in the server's program. After some hour work it sometimes stopped serving the clients. It readed the DHT22 and refreshed the OLED, but clients didn't received any answer. To solve this problem I started a timer. The program resets it after serving a client. If the timer reaches 30000 (30 seconds), the server disconnects from the WiFi network and reconnects again.

According to the attached schematics you can build the server and client hardware. Uploading the programs you will have a remote thermometer. Modifying the programs you can build your own project in which the ESP8266 based MCUs can communicate with each other.

Of course every device connected to your local network will be able to ask the DHT server. You can see a screenshot about my mobile also.

Comments

author
karlt1 (author)2017-06-17

Hello,

Is it possible to echange data between 2 esp ( one in access point and one in station mode ).

I tryed to change you're code but i don't arrrived to make it work.

I arrived to make it work with the 2 esp on my box

Thanks you

author
Gyalu1 (author)karlt12017-08-07

Hi karit1,

I made my version of accesspoint-station communication.

You can find it here: https://www.instructables.com/id/Accesspoint-Station-Communication-Between-Two-ESP8/

author
karlt1 (author)Gyalu12017-08-07

thanks for the tuto, it work very well

author
Gyalu1 (author)karlt12017-06-18
author
karlt1 (author)Gyalu12017-06-18

Hello,

i don't arrived to make work. station to station the code work, but when i pass to access point to station it doesn't work.

here is the code i make.

acces point receive data : Nom from station

from my laptop i send data to this access point :

https://pastebin.com/65JxpNRR

station who send data to access point. It connect to the access point.

from station to station tis code work well :

https://pastebin.com/bt05Hn7B

i cant' understand the fact that it work well from station to station. When i pass acess point to station, the station connect to Access point and i arrived to send data from the laptoop ....

if you have any idea ??

did it work on you're hardware.

author
karlt1 (author)Gyalu12017-06-18

Thanks for the link,
I will try.

i will say if it's work or not.

author
Gyalu1 (author)karlt12017-06-18

Hello Karit1,

Yes, theoretically it is possibile, but I haven't tried it yet. (It is on my list. :-)

If you will be successful, please share your project wit us.

author
adithyaramgopal (author)2017-07-24

How to set the IP for the server module. You mean its local IP. And is there a possibility of establishing a communication between two ESP modules without internet connection.

author
Gyalu1 (author)adithyaramgopal2017-08-05

Hi,

see: https://www.instructables.com/id/Accesspoint-Station-Communication-Between-Two-ESP8/

author
adithyaramgopal (author)Gyalu12017-08-06

Hi Gyalu1,
That was very helpful. Thank you!

author
Gyalu1 (author)adithyaramgopal2017-07-25

Hi,

1, you can find the answer in the attached program:

WiFiServer server(80); // launches the server

IPAddress ip(192, 168, 0, 80); // fix IP of the server

IPAddress gateway(192,168,0,1); // WiFi router's IP

IPAddress subnet(255,255,255,0);

So the server's IP address will be: 192.168.0.80

2, Yes, two ESPs can communicate with each other without internet connection. One has to an access point, the other has to be a client. See the links below.

author
adithyaramgopal (author)Gyalu12017-07-25

I am a beginner so kindly elaborate me on obtaining IP address of the server. How do I ascertain IP address of the module as 192.168.0.80. Is that a local IP.

My other doubt : Will there be an ssid for the wifi module? Can I use this wifi module like a NRF to transmit and receive data?

Thanks in advance.

author
Gyalu1 (author)adithyaramgopal2017-07-30

Hi,

here is a sample ino, which prints out the local IP of your module to the serial monitor. https://www.arduino.cc/en/Reference/WiFiConfig

Try to understand the program, make small modifications according the instructions and check the results.

author
D_Fence (author)2017-08-03

THX for that ! It helped me a lot and it works perfect.

But how can I manage, that the Server sends more (e.g. three values) than one value and the client recieves more (e.g. three values) than one ?

THX for help & suggestions

Toni

author
Gyalu1 (author)D_Fence2017-08-05

Hi D_Fence,

I suggest to make one string from the three values, separeted by a ";" e.g. Thats way you have to send only one string through the WiFi. On the reciever side you can separate the data checking the position of the ";" mark.

See: https://www.arduino.cc/en/Reference/StringObject

About This Instructable

4,655views

53favorites

License:

More by Gyalu1:Accesspoint - Station Communication Between Two ESP8266 MCUsWiFi Communication Between Two ESP8266 Based MCU Through the Home RouterPOI hunter
Add instructable to: