WiFi Communication Between Two ESP8266 Based MCU Through the Home Router

158,764

130

118

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

4 People Made This Project!

Recommendations

  • For the Home Contest

    For the Home Contest
  • Game Design: Student Design Challenge

    Game Design: Student Design Challenge
  • Make It Bridge

    Make It Bridge

118 Comments

0
Akhilan
Akhilan

7 months ago

WiFiClient client = server.available();
what does it mean if the server. available is not assigned to the client vavriable.

0
Akhilan
Akhilan

Question 7 months ago

As you could see in this image, I'm not able to get any reply from the server, Please help me with it and where am i going wrong, but it seems the client it connected to the WIFI.

Screenshot (242).png
0
sknishad3488
sknishad3488

Question 1 year ago on Introduction

hi all,
i want to build wifiphone means without simcard. Is it possible? what contents in electronic(devive) fingerprint in device?

0
Zafarictrl
Zafarictrl

Question 2 years ago

Hi all .
How can many of these modules be networked together?

0
LarryF24
LarryF24

Answer 1 year ago

Why not just use a PLC , has to be easier and less issues?

0
Gyalu1
Gyalu1

Answer 2 years ago

Hi Zafarictrl,
You have to start more clients.
As far I remember the theoretically maximum is five clients for one server, but if you use more than three clients then you have to eliminate the connections in same time, because it can collapse the communication.

0
henzup
henzup

Question 1 year ago

Hello all, I need your help!!
So I have this project to increase production in company where I am intern at
They have a production line, almost 40 line which need input material , mechanic, fabrics... Etc. And I wanted to design a system where a signaling unit will be on each line and then a display unit will be stationed in location where the people who handle that task will. For example if fabric is needed by a line they press a button in line and a display in the fabric department will show the message to which they can respond. For as many as 7 different conditions
Since the working are is very big shed.
I was planning a nodemcu board be a signaling device. One nodemcu for each line. This boards will be connected to a wifi router And one central node mcu will gather Information from the router.
The central node mcu will be connected to a raspberry pi which will display the request.
Now I am not so sure on how to create this type of network where the boards can send to the central node mcu through the router . almost 40 node mcu in total as signal device for each line line.????

0
bag.stg.kft
bag.stg.kft

Question 2 years ago

I want to shorten the project and I want to ask for help.
I have a lot of ESP8266, all of them send the data to Thingspeak from BME280 (more data, like DHT11) and as a client also show the weather from Openweather.
My question is, instead of a server, how can I show all rooms together on another display (Nextion)? It doesn't work with "read Thingspeak".

0
bag.stg.kft
bag.stg.kft

Reply 2 years ago

I also have your "idea" (many small devices communicate) ...... only they should all be "collected" .... Without a server !!!! Have a server, because this is Thingspeak for me ...... But it doesn't solve my display and other problems ...... About 6 years ago I did my first project with "Hello Mesh" and IFTT. .. LED strip (WLED in Android) also switches the Google Assistant

1
bag.stg.kft
bag.stg.kft

Reply 2 years ago

The problem is that neither IDE nor VS (IO) find many errors in Sketch, even though they don't come from me, but from GitHub, whereby one could assume that it was written correctly ........ Many Laberys are no longer up-to-date or do not even exist ..... Is there anything that corrects these errors itself? Incidentally, I did server (ESP32) but to look at the phone every now and then, why ??????
My plan is a real station, all measured values, with which I might be able to control the heating ... I not only want a separate thermostat in every room, but also centrally ...... So as not to water the plants too much to avoid unnecessary heating, or switch on at least, or the air conditioning, that's what I call home automation ....... That starts with a real weather station, where not only the weather forecast for the current day is shown ... .. Google can do that better (14 days) I even changed the display icons (nice, large, no mini images, like on Android weather display, with Nextion even video is possible, so moving clouds and thunderstorm sound)

0
bag.stg.kft
bag.stg.kft

Reply 2 years ago

SPI ????????????
I've had enough trouble with Wire .......
I want a real station where all measured values are collected and is big enough ....... Thanks to Thingspeak I can currently use it on a 13 "tablet on the table, although I want to put my 10" Nextion on shelves ... .....

0
hannes.reinsch.2004
hannes.reinsch.2004

Question 2 years ago on Step 1

Hello,
is there any posibillity to print and read the float variable on the server or how can i convert the "answer" back in a float variable? I want to send the data to annother server and this only works with float and not with string.

It does not work with: <<< float temperature = atof(answer); >>>

Thank you

0
cybercyber44
cybercyber44

2 years ago

Hi, is there any possibility to put in code for example
WiFi.config(mydomainname, gateway, subnet) instead WiFi.config(ip,
gateway, subnet)

I am a IT network guy and I have DDNS to my network IP. My network IP every day changed, but my domain name no. Everything about port forwarding I have resolve. I want to have client in my work and server in my flat and check it trought internet.

Thank you

0
Gyalu1
Gyalu1

Reply 2 years ago

Hi cybercyber44,
I am sorry I am not an IT guy, I am a hobbyst. I can not support you in this topic.
Mining the specs you may find answer to your questions.
Br
Gyalu

0
brahmasp
brahmasp

Question 2 years 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 2 years ago

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

0
cybercyber44
cybercyber44

Reply 2 years ago

Hi, is there any possibility to put in code for example WiFi.config(mydomainname, gateway, subnet) instead WiFi.config(ip, gateway, subnet)

I am a IT network guy and I have DDNS to my network IP. My network IP every day changed, but my domain name no.

Thank you

0
brahmasp
brahmasp

Reply 2 years 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 2 years 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.