WiFi Communication Between Two ESP8266 Based MCU T

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

instructables

WiFi Communication Between Two ESP8266 Based MCU Through the Home
Router

by Gyalu1

Hello Makers! I started to mine the internet for ready samples but I
didn't find an easy solution for MCU-MCU WiFi
Some months ago I started to experiment ESP8266 communication. Finally I started from the basics.
based NodeMCU and Wemos boards. At first I used According to the description of the Arduino WiFi
them (in my previous POI hunter project) as a smarter library and the WiFi library for ESP8266 I made what
Arduino, because they have higher clock frequency, I wanted.
more memory and built in "SD card" (SPIFFS).
In the next pages I will share the result with you in
In my Arduino projects sometimes I had to send two versions. At first two bare MCU will demonstrate
messages between two MCUs. I used wired the bare minimum solution of the TCP
communications (serial, i2C) and wireless (bluetooth, communication. After that I will show an easy
RF transmitter / receiver and nRF24). So there was application using an additional DHT22 sensor and
not question I will face this topic using ESP modules some OLEDs.
also.

WiFi Communication Between Two ESP8266 Based MCU Through the Home Router: Page 1
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.

https://youtu.be/x1IA7ZmO2wM

Download
https://www.instructables.com/ORIG/FE0/OP4Z/J0SEHHP4/FE0OP4ZJ0SEHHP4.ino

Download
https://www.instructables.com/ORIG/FGN/4NCJ/J0SEHHP7/FGN4NCJJ0SEHHP7.ino

WiFi Communication Between Two ESP8266 Based MCU Through the Home Router: Page 2
Step 2: DHT Server With Clients

Here is an application which uses the TCP program resets it after serving a client. If the timer
communication between ESP8266 MCUs. reaches 30000 (30 seconds), the server disconnects
from the WiFi network and reconnects again.
A Wemos server listens, reads the DHT22 and shows
the temperature and its own IP address on a small According to the attached schematics you can build
OLED screen. Two NodeMCU clients trigger the the server and client hardware. Uploading the
server and receives the temperature value as the programs you will have a remote thermometer.
answer. During the communication the server's OLED Modifying the programs you can build your own
switches to inverse. project in which the ESP8266 based MCUs can
communicate with each other.
There is only a small modification in the server's
program. After some hour work it sometimes stopped Of course every device connected to your local
serving the clients. It readed the DHT22 and network will be able to ask the DHT server. You can
refreshed the OLED, but clients didn't received any see a screenshot about my mobile also.
answer. To solve this problem I started a timer. The

https://youtu.be/90cZ3YdGKto

https://youtu.be/FzoUVwEsBUo

WiFi Communication Between Two ESP8266 Based MCU Through the Home Router: Page 3
Download
https://www.instructables.com/ORIG/FDK/WZI5/J0SEHXDK/FDKWZI5J0SEHXDK.ino

Download
https://www.instructables.com/ORIG/FDM/HKEQ/J0SEHXDL/FDMHKEQJ0SEHXDL.ino

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
thanks a lot for this!!

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..
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
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).
Thank You Gyalu1 , this project helped me a lot!!!

You are welcome Rakesh!

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.
WiFi Communication Between Two ESP8266 Based MCU Through the Home Router: Page 4
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.
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
Hi Alex, check this instructable: https://www.instructables.com/id/Fun-With-OLED-Dis...
See chapter: "There is a compile error about display size" how to modify Adafruit_SSD1306.h file.
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

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.
I will try again maybe i change in wrong place
I change first from
if (xc >= 20
|| xc <= 0) {dx *= -1;}
if (yc >= 50 || yc <= 22) {dy *=
-1;}
to
if (xc >= 124 || xc <= 0) {dx *= -1;}

if (yc >= 64 || yc <= 22) {dy *=


-1;}
and 0x3c from D
If you could help me with this I will be good
Elore is coszonom :)
Finally I did it :)
Thanks for the tutorial and help it is a nice project and it is working.

WiFi Communication Between Two ESP8266 Based MCU Through the Home Router: Page 5
And make for the LCD and ESP a board like a shield

NIce article - I was looking for a possible solution/idea to check the status of House's
doors/windows - My idea was a sensor for each windows (clients / 10 in total) and with 1/2 servers
with 10 leds on if a windows/doors is open / Am I on the right road?
Yes, you are on the right way.

Amazing Example!!! Thanks !

Hello Gyalu1,
this is a post on my pevious question (from mnalxz) because I was not able to (at least it seems
that way) to reply to your answer.
Thanks for your relpy.
The Wemos D1 R2 is an Arduino Uno sized version, but on this board my Server is running and
seems to be working (because I can sent messages and receive replies from my PC and Phones
WiFi connection).
The mini PRO's (the Clients) are the ones giving me the problems.
Also what can be seen in debug texts of the Serial connections of both boards.
I Added some debug lines in the client application (see below : but they tell me the server address
is 0.0.0.0 and should be 10.0.0.191 (what is defined in the top of the application (in serverIP)
void loop () {
client.connect(serverIP, 80); // Connection to the server
digitalWrite(ledPin, LOW); // to show the communication only (inverted logic)
client.println("Hello server! Are you sleeping?\r"); // sends the message to the server
Serial.print("To server: "); Serial.println(client.remoteIP());
Serial.print("Sent Data: "); Serial.println("Hello server! Are you sleeping?\r"); // The send message
to the server
String answer = client.readStringUntil('\r'); // receives the answer from the server
if (answer.length() > 0) {
Serial.print("From server: "); Serial.println(client.remoteIP());
Serial.print("Received Data: "); Serial.println(answer);
} else {
Serial.println("NO data received from Server!\r");
}
client.flush();
digitalWrite(ledPin, HIGH);
delay(2000); // client will (Re-)trigger the communication after two seconds
}
Status: 3
IP: 10.0.0.194

WiFi Communication Between Two ESP8266 Based MCU Through the Home Router: Page 6
Subnet: 255.255.255.0
Gateway: 10.0.0.1
SSID: mna_AP
Signal: -31
Networks: 26
To server: 0.0.0.0
Sent Data: Hello server! Are you sleeping?
NO data received from Server!
To server: 0.0.0.0
Sent Data: Hello server! Are you sleeping?
NO data received from Server!
etc....
Hello Gyalu1,
I finally solved the problem..
The problem was not in the code itself, but in the configuration of my router.
There for WiFi a setting was set named AP-isolation and was set to enabled !!!
Meaning multiple devices connected BY WIFI TO THE SAME NETWORK are unable to
communicate with each other ...
So finaly after setting this option to disabled, the devices do communicate with each other.
PS.. My phone was able to communicate with the devices because it was operating at the 5 GHz
band while the device operate at 2.4 GHz. (In my router both bands have separated settings for
AP-Isolation)
Thanks...
Hi, I'm using an ESP8166-01 to control an Arduino Mega. I was able to turn on and off an LED with
a simple web page, when the computer and the esp8266 are both connected to the same wireless
router. However, when I use the webpage on a computer that is not connected to my home router,
nothing happens. Can you advise what to do? I think there must be an issue with port forwarding or
something. Thanks!
esp8266 ? esp8266 .

Hi I want to transfer data by connecting Nodemakers. One accees point, connect to it on the other
and I want to see this data on the computer screen Can I do this with the codes you share ? help
mee please
Hi PeldaD,
I am sorry I don't understand well your question. If you would like to make an accesspoint-station
communication then my other instructable will be better for you:
https://www.instructables.com/id/Accesspoint-Station-Communication-Between-Two-
ESP8/#CGDQ2RAJ6IFA8P1
Hi
I want to communicate data between multiple ESPs. Can I do it with these codes? How can I
change these codes according to my program? can you speak Turkish ?
Use English communication is very good, I also hope that we use is Chinese. Your problem
solved?

WiFi Communication Between Two ESP8266 Based MCU Through the Home Router: Page 7
Yes. This code sends data from the client to the server and the server answers it.
Both program contains comments, which explain the work of the code. Reading this comments you
will find the program lines which sends a message and the other which receives.
No, I am sorry, I don't speak Turkish.
hello sir pls help me
I am loaded above code into my two MCUs
but i have doubt in server programe that is "
Open the settings of your home WiFi router and choose a free IP address which is out of the
DHCP range
"
IPAddress gateway(192,168,1,1); // gateway of your network
my client is working but server dnt..
Hi vinutp1989,
there may be a conflict between the DHCP settings of your router and and the fix IP request of the
server.
There are two ways to solve this conflict.
1, Set an IP range in your router in which DHCP can give IPs for the DHCP clients. The IP of your
server (which is defined in this line: IPAddress ip(192, 168, 0, 80);), has to be outside of this range.
2, Let DHCP to give IP to your server. To do this you have to comment this line: WiFi.config(ip,
gateway, subnet);. This way let the server program to print the current IP address to the serial
monitor by running this line: Serial.println(WiFi.localIP()); in the setup part of the server program.
After that in the client program you have to replace this IP: IPAddress server(192,168,0,80); with
the current IP of your server.
Hello Gyalu,
my application is bit similar to your project.
My project is to communicate with 2 esp8266 (connected with arduino and relay each) thrugh the
app via Home wifi router.
Can you help in this?
Hi Shayan,
I am not an expert, I am hobbyist as you, but can we try. Do you have a concrete question /
problem?
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.
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.
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.
WiFi Communication Between Two ESP8266 Based MCU Through the Home Router: Page 8
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.
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.
Hi Gyalu1 ,
Is there a possibility to send integers instead of strings between two WIFI modules?
My be, but I never tried.
I converted the values to string because I always wanted to send more than one numbers in one
message.
Hi,
see: https://www.instructables.com/id/Accesspoint-Station-Communication-Between-Two-ESP8/
Hi Gyalu1,
That was very helpful. Thank you!
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
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/
Hello,
i've made an ardublock for visual programming the ESP8266
it's an all in one :
arduino IDE + board + lib + ardublock ESP
here are the screenshot :
https://github.com/karlTH/arduino-esp-ardublock#screenshot-of-ardublock-esp
here are the release :
https://github.com/karlTH/arduino-esp-ardublock/releases
it work on windows
unzipp the release,
launch arduino.
in the IDE : Tools -> ardublock
Have Fun
thanks for the tuto, it work very well

WiFi Communication Between Two ESP8266 Based MCU Through the Home Router: Page 9
check this on Github:
Soft Access Point Class
Station Class
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.
Thanks for the link,
I will try.

i will say if it's work or not.


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.
hi
I am doing the loading, I enter everything but it is not loaded in the serial monitor '' .... ''
Hi Pelda,
As you can see the speed of the serial communication is set to 115200 bps in the sketch
(Serial.begin(115200);).
The default speed in the Arduino IDE is 9600 bps. Set the communication speed to 115200 in the
bottom right corner of the terminal window and you may see the sent data.
But only client data transfer from server to IP address is seen. Why is not the other seen?

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

You might also like