WiFi Communication Between Two ESP8266 Based MCU Through the Home Router

48,255

83

68

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.

Share

Recommendations

  • First Time Author

    First Time Author
  • Make it Glow Contest 2018

    Make it Glow Contest 2018
  • Toys Contest

    Toys Contest

68 Discussions

0
None
RakeshR71

11 days ago

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

1 reply
0
None
Tiraalex

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

6 replies
0
None
Gyalu1Tiraalex

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

0
None
TiraalexGyalu1

Reply 2 months ago

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 :)

0
None
Gyalu1AnaghaD3

Answer 5 months ago

See Step2.
Then you need a server and some (max. five) clients.

0
None
RobertoM200Gyalu1

Reply 3 months ago

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?

0
None
SuDhAnSuS1

Question 4 months ago on Step 2

I want a bare minimum communication, If DHT11 server detects temperature more than 28 degree , then client esp triggers a relay. Please help

1 more answer
0
None
Gyalu1SuDhAnSuS1

Answer 4 months ago

Hi,

do you have any idea how to solve this project?

0
None
V.P.R192

4 months ago

Amazing Example!!! Thanks !

0
None
mnalxz

5 months ago

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

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

1 reply
0
None
mnalxzmnalxz

Reply 4 months ago

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

0
None
caberly

Question 5 months ago on Step 1

Regarding the basic sketch (Bare Minimum sketch):

Background: I uploaded both sketches and followed the directions exactly only making the requisite changes identified above. The sketches are both built for and uploaded on PORT 2. I plug the power into the client after the sketch is uploaded from PORT 3 on my computer and upload the Server Sketch. I'm using ESP8266 - 12E nodeMCU modules.

Problem: I'm only receiving one side of the communication in the serial monitor and it depends on which sketch is uploaded last as to which one I'm receiving the communication from. The two communicate in the first section, but the second communication doesn't go through.

Example from Serial Monitor:

.

From client: Hello server! Are you sleeping?

.

From client: Hello server! Are you sleeping?

That just loops over and over. The part where the server responds doesn't show, likely because the client (the script where the information is printed to the serial monitor in the original sketch) is plugged up to a different comm port (PORT 3) than the sketch was built for, but I can't pull up both Serial Monitors simultaneously. So, the Server is hooked up to PORT 2 and the Client side is hooked up to PORT 3.

The only reason I know that the communication is going through on the Server side back to the Client side is I rewrote some of the code in order to blink the onboard LEDs in a given pattern to tell if the function was working, but after the first HIGH to the Server and the first HIGH to the Client are received it skips through or over the next comm (i.e. it doesn't display the Server's response (even if I pass the response through client.println back to the Server since it is the one communicating back to the computer).

Are these supposed to be hooked up to the same PORT somehow and would there be a reason that the two don't communicate after the first successful passes of information?

0
None
PhonS

Question 5 months ago on Step 2

Hi gyalu1,

Thank you very much for the shared information.

I am having a question that how to work these MCU between local to Internet (MCU_1 ==> MCU_2 ==> Website)