WiFi Communication Between Two ESP8266 Based MCU Through the Home Router

75,722

90

79

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.

Microcontroller Contest 2017

Participated in the
Microcontroller Contest 2017

Share

Recommendations

  • Beauty Tips Contest

    Beauty Tips Contest
  • Sew Tough Challenge

    Sew Tough Challenge
  • Planter Challenge

    Planter Challenge

79 Discussions

0
None
neurocyber

6 weeks ago

Would like to use 2 ESP8266 exactly the way you did, but would like a switch on one that would trigger a relay on the other. Tried to modify your great code for dht but having trouble. A code snippet maybe ? thanks

1 reply
0
None
raudet250643

6 weeks ago on Step 1

HI
It's exactly what I want. but I just want to pick a relay from client board to the server board.

0
None
ALS2121

Question 2 months ago on Introduction

Hi Gyalu 1,
thanks for you information.
I have tried it.
It works well, but unfortunately not so long. I'm trying it since 2 days.
After about 300 cycles the system freezes.
I have changed the delay value n the loop to 20 i and so I see the result more fast.
But also with longer delay or with manually transfer, started by a button: after about 200 .. 300 cycles, the system stops.
Any idea?
Thank you.

2 answers
0
None
Gyalu1ALS2121

Answer 2 months ago

Hi ALS2121, I added a Tip to the instructable you commented.

0
None
Gyalu1ALS2121

Reply 2 months ago

Hi ALS2121,
I received similar comment a few months ago but have not experienced it with the bare minimum hw and sw I shared.
Did you modified anything?
I make some trials and come back if I have any solution.
Gyalu

0
None
Gyalu1

Tip 2 months ago

Hello Makers,
I have not found a way to add new step to my instructable, so I am adding a Tip.

Here you can see the extended version of my bare minimum example.
I added a counter into each program (server and client also) which is increased after each successful communication. Each device sends its current counter value to the other.
I added two oleds. Each shows the counter value received from the other device. If client reboots after a reset or a failure, the server's oled will show numbers from c1, c2 and so, but the client's oled will show the next server counter value. (I deminstrate it at the end of the video.)
Finally I decreased the delay timer in the client program to zero. After a finished communication client triggers the server without any delay.
As you can see the two counter runs paralelly and shows much higher values as 200-300.
So I think the bare minimum example can run long time without any failure.
I tried to modify the conditions of this smooth running.
I moved the devices far from the router into weak wifi signal strength area. If the MCUs were able to connect to the router, then they talked without failure.

At the end of the 2nd step I am writing the server freeze. I think if the server has enough time to finish the currect communication before it receives the next trigger, then it is able to serve the client long time. If more clients trigger the server in same time then the server slows down, which can cause freezing, but it can be a problem if you use more clients.

IMG_20190514_070930.jpg
0
None
shashanks113

5 months ago

HOW TO UPLOAD THE DATA RECEIVED FORM CLIENT SIDE TO SERVER ON HTML BASED WEB PAGE.?????CAN SOME ONE HELP PLEASE WITH REFERENCE OF THIS CODE..

0
None
ShivamP88

Question 6 months ago

Hi Gyalu1, First of all, thanks a lot for this nice article. I am working on something like this but with ESP-01 modules. I made 1 as server and 4 modules as clients but the problem is the clients are not receiving the command sent from the server at the same. They are having some delay which is varying from client to client. the delay i am getting is upto 1 min. And i want to decrease this(upto 15 sec). Please help me, this project is really important for me.

2 answers
0
None
Gyalu1ShivamP88

Reply 6 months ago

Hi Shivam,
I do not understand your question well. Would you like to send message to the four clients in the same time?
If it is right I can not support you. I am not a expert in this topic.
My server sends message to the current client only which is triggering the server. Using my sketch you can send same message to the clients, but in different time.
Gyalu

0
None
ShivamP88Gyalu1

Reply 6 months ago

I want to send a command from server to 4 clients but they are not recieving it at the same time, they are always having delay (which is always varying).

0
None
RakeshR71

7 months ago

Thank You Gyalu1 , this project helped me a lot!!!

1 reply
0
None
Tiraalex

9 months ago

Hi Gyalu1 this is a nice project (szep munka :) ) and helpful.

I think I have the same problem like in the other post, I have upload the DHT server / Client Sketch on ESP8266 mini, but the server (with a DHT11 sensor ) wont communicate with the client.

On the Oled LCD from the client is appearing the "C" , but on the serial monitor only some symbols

On the Server ESP nothing is visible on the Oled or the serial monitor only some symbols appear.

I will try to play with the router again to see if I can solve this.

5 replies
0
None
Gyalu1Tiraalex

Reply 9 months ago

I am far from home in the next some weeks. I will be back later.

I never used DHT11. Have you modified my sketch? I used DHT22.

0
None
TiraalexGyalu1

Reply 9 months ago

Thanks for the reply,

I have modify it base on my old web version sketch but it is possible that I miss something :)

I will receive some new DHT22 sensors and I will try again

0
None
TiraalexGyalu1

Reply 9 months ago

Hi I almost found all the problems but now the server with the DHT 22 is working and I change only the oled.begin(SSD1306_SWITCHCAPVCC,
0x3D); to Cthat is why my LCD did not work and I was thinking is not working at all, and the IP is steel not showing on the Oled like in your video.

An I modify that the temperature to be fix on the LCD

But the Client is not receiving the temperature it is showing only " C" , when I unplug the Server from the PC I have notice on the Client that the onboard LED is flashing so I think there is connection between the 2 ESP only is not showing on he Oled

Here are some pictures

20180927_183806[1].jpg20180927_183812[1].jpg20180927_213859[1].jpg20180927_213903[1].jpg20180927_213905[1].jpg20180927_210914[1].jpg
0
None
Gyalu1Tiraalex

Reply 9 months ago

At first: I think you have to set the resolution of your oled to 128x64. (128 columns oszlopok, and 64 horizontal rows sorok). This way all info will be shown in the screen.
Next: If you check the station's program, you will see, the onboard led is switched on before the station triggers the communication with the server. So the meaning of the flashing led on the station: I am triing to speak to the server. The led will switched off after a succeafull communication or after a timeout, when station, stops triing to reach the server.
The meaning of the flashing led on the server: Somebody is connected to me.