WiFi Communication Between Two ESP8266 Based MCU Through the Home Router

78,716

92

83

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.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

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

  • Indoor Lighting Contest

    Indoor Lighting Contest
  • Make It Fly Challenge

    Make It Fly Challenge
  • Growing Beyond Earth Maker Contest

    Growing Beyond Earth Maker Contest

83 Discussions

0
None
Beginner159

Question 1 day ago

Hi, I had tried the step 1 of this project guide and it's working perfectly.
But I would like to modify the code for the purpose of send temperature reading from server and receive temperature reading from client then display at serial monitor, It is possible to do that?
I tried to modify by my own, but failed. Hope anyone can help to solve my problem, thanks in advance.
--------------------------------------------------------------------------------------------------------------------------
Code for Server as below
--------------------------------------------------------------------------------------------------------------------------
#include SPI.h
#include ESP8266WiFi.h
#include DHT.h// DHT22
#define DHTPIN 0
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
char ssid[] = "tele@unifi"; // SSID of your home WiFi
char pass[] = "75951535"; // password of your home WiFi
unsigned long DHTtimer = 0;
float h, t;
unsigned long clientTimer = 0;
WiFiServer server(80);
IPAddress ip(192, 168, 0, 80); // IP address of the server
IPAddress gateway(192,168,1,1); // gateway of your network
IPAddress subnet(255,255,255,0); // subnet mask of your network
void setup() {
Serial.begin(115200); // only for debug
WiFi.config(ip, gateway, subnet); // forces to use the fix IP
WiFi.begin(ssid, pass); // connects to the WiFi router
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
server.begin(); // starts the server
delay(500);
clientTimer = millis();
}
void loop () {
if (millis() > DHTtimer + 2000) {
h = dht.readHumidity();
t = dht.readTemperature(); // reads the DHT for temperature
if (isnan(h) || isnan(t)) {
return;
} else {
DHTtimer = millis();
}
}
WiFiClient client = server.available();
if (client) {
if (client.connected()) {
String request = client.readStringUntil('\r'); // reads the message from the client
client.flush();
client.println(String(t, 1));// sends the temperature to the client
}
client.stop(); // tarminates the connection with the client
clientTimer = millis();
}
if (millis() - clientTimer > 30000) { // stops and restarts the WiFi server after 30 sec
WiFi.disconnect(); // idle time
delay(500);
}
}

--------------------------------------------------------------------------------------------------------------------------
Code for Client as below
--------------------------------------------------------------------------------------------------------------------------
#include SPI.h
#include ESP8266WiFi.h
byte ledPin = 2;
char ssid[] = "tele@unifi"; // SSID of your home WiFi
char pass[] = "75951535"; // password of your home WiFi
String answer;
unsigned long askTimer = 0;
IPAddress server(192,168,1,47); // the fix IP address of the server
WiFiClient client;
void setup() {
Serial.begin(115200); // only for debug
WiFi.begin(ssid, pass); // connects to the WiFi router
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
pinMode(ledPin, OUTPUT);
}
void loop () {
if (millis() - askTimer > 2340) { // time between two connection to the server
client.connect(server, 80); // connects to the server
client.println("Haliho szerver!\r"); // trigger message to the server, its value is scrapped
answer = client.readStringUntil('\r'); // received the server's answer
Serial.println(answer);
client.flush();
askTimer = millis();
} // client will trigger the communication after two seconds
}

0
None
reddyreddy1968

26 days 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
None
anas.ahm5

Question 4 weeks ago

I am facing this error.
Arduino: 1.8.9 (Windows 10), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, 4M (1M SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch, 115200"
C:\Users\This-PC\Desktop\wifi_client_01\wifi_client_01.ino: In function 'void loop()':
wifi_client_01:44:14: error: 'class IPAddress' has no member named 'available'
if (server.available()) {
^
exit status 1
'class IPAddress' has no member named 'available'
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.


erroe01.JPG
1 answer
0
None
Gyalu1anas.ahm5

Reply 4 weeks ago

Hi,
I think you have to install the ESP8266WiFi library.
Gyalu

0
None
neurocyber

2 months 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

2 months 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 3 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 3 months ago

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

0
None
Gyalu1ALS2121

Reply 3 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 3 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

6 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 7 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 7 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 7 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

8 months ago

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

1 reply
0
None
Tiraalex

10 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.

1 reply
0
None
Gyalu1Tiraalex

Reply 10 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.