WiFi Communication Between Two ESP8266 Based MCU Through the Home Router

120,733

113

102

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.

Microcontroller Contest 2017

Participated in the
Microcontroller Contest 2017

3 People Made This Project!

Recommendations

  • Hand Tools Only Challenge

    Hand Tools Only Challenge
  • Modify It Speed Challenge

    Modify It Speed Challenge
  • Remix Contest

    Remix Contest

102 Discussions

0
brahmasp
brahmasp

Question 7 days ago on Step 2

Thanks for the above information. Can I connect two esp modules to 2 different wifi networks and make the trigger action work?? (Like when there is an action taken on 1 end it should trigger an action on the other)

0
Gyalu1
Gyalu1

Answer 6 days ago

Hi brahmasp,
I am sorry but I do not understand the topology you desribed.
Gyalu

0
brahmasp
brahmasp

Reply 5 days ago

Sorry, about the confusion. What I was trying to ask was, there are 2 nodemcu board in 2 different home across the town I want to communicate between these two board over internet. So, what should be my approach? Like a need a very simple sketch that has only communication code. Please can you help me on this.

0
Gyalu1
Gyalu1

Reply 5 days ago

I am sorry brahmasp, I can not support you in this project. I am not an IT expert, I am only a hobbyst as you.
As far as I know the router in which you run the server mcu has to have a fix IP. It is an extra service from your ISP and you have to set port forwarding to reach the server mcu from the internet.

0
Yasin7622
Yasin7622

Question 2 months ago

Hi .... to change if an LED is in the receiver 0 or 1 .... and in the transmitter with analog value .... what change should I make?

0
liordahan0507333363
liordahan0507333363

Question 3 months ago on Step 2

Hi All,

I’m trying to create same project with TTGO(esp8266 with build in oled)

Can you please let me know what is the code for the display 🙏🏻

Thanks in advance
Lior

0
Gyalu1
Gyalu1

Answer 3 months ago

Hi liordahan,
it depends on the MCU type you bought.
Check the the sample programs on the manufactures's site.

0
Cha96
Cha96

Question 4 months ago on Step 1

I tried above two codes for communication between ESP8266 and ESP32 after a slight change. Then I included a external interrupt code to the above code. Now most of the data sent are lost. How can I solve above problem??

0
Gyalu1
Gyalu1

Answer 4 months ago

Hi Cha,
I don't know.
You should start fom my code. At first test it without any modification. If it is working then go step by step. Small modification, and test. If it is working, then do the next small modification.
If you follow this strategy, then you will know which modification collapses the communication.
But I think you have to grope somewhere near the interrupt. Why do you need it?
Gyalu

0
pedromonteiroev
pedromonteiroev

Question 6 months ago

Hi thanks a lot for the project, can we transfer the both values? (temp and Humidity)?

0
Gyalu1
Gyalu1

Answer 6 months ago

Yes, you have to merge the values into one string, and you can send them in one message.
On the other side you cat cut the long string into temp and hum parts.
You can cut according to fix lengths, or at the separator caracter you use during the merging.

0
pedromonteiroev
pedromonteiroev

Reply 6 months ago

than to split I use "substring" funtion, it works!!!! thanks a lot one more time!
Ex:
answer1 = answer.substring(0,4);
answer2 = answer.substring(5,9);

and like this I have both values in different variables

answer was for example: 19.9070.40
after we split we have 19.90 (position 0,1,2,3,4) and 70.40(position 5,6,7,8,9)

0
Gyalu1
Gyalu1

Reply 6 months ago

Yes, that's it.
A bit better solution to use a special separator character eg. ";".
String message = String(t) + ";" + String(h);
On the receiver side you have to locate the ";" character using the
int pos = answer.indexOf(';') function, and feed the
answer1 = answer.substring(0,pos) and
answer2 = answer.substring(pos+1) with the result of the indexOf() function.
This way you will be able to cut "9.90;70.40" and "19.90;70.40" in the correct position.

0
pedromonteiroev
pedromonteiroev

Reply 6 months ago

yeah, that´s much better ;) thank you very much!!!

0
pedromonteiroev
pedromonteiroev

Reply 6 months ago

Thanks, can you show-me an example of how to merge?
I come up with:
String StringThree;
StringThree = String(t) + String(h);
client.println(StringThree); // sends the temperature / Humidity to the client

1
joseben
joseben

Question 9 months ago

Hi, I want to send an array of two variables, I tried a lot rewriting the code but I can't even send even a single variable other than a string, Can you help me out?

1
reddyreddy1968
reddyreddy1968

1 year ago

It is working fine I done for my overhead tank also to blynk to monitor water level using ultrasonic sensor. Motor automatic off after reaching 1500 liters thanks a lot.2 o led for monitoring water volume.

0
patrickenoc.pe
patrickenoc.pe

Reply 1 year ago

Can you please send me your codes? Is it an automation for a water pump consisting two esp32 and two watertank?
Here's my email patrickenoc.pe@gmail.com

Thankyou verymuch