Communication between two ESP32
In this tutorial, we are going to learn:
- Communication between two ESP32 - Overview
- Communication between two ESP32 via Ethernet/WiFi
- Example application: A button/switch connected to ESP32 #1 controls an LED connected to ESP32 #2 via Ethernet/WiFi
- How to connect two ESP32 via Internet
|2||×||ESP-WROOM-32 Dev Module|
|2||×||Micro USB Cable|
Communication between two ESP32 - Overview
Let's consider a specific case: ESP32 #1 communicates with ESP32 #2. There are many methods to enable communication between two ESP32. We can choose one of them depending on the communication range. The below table shows some methods and their communication range.
|UART (TTL)||very short|
※ NOTE THAT:
- If two ESP32 are connected to the Internet, the communication range is unlimited
- If two ESP32 are not connected to the Internet, but they are connected in the same LAN network, they can still communicate with each other. The communication range is limited within the LAN network.
Among the above methods, this tutorial uses Ethernet/WiFi to enable communication between two ESP32 because it allows two ESP32 to communicate over the unlimited distance.
Communication between two ESP32 via Ethernet/WiFi
Two ESP32 can communicate with each other via Ethernet/WiFi if:
- Two ESP32 in the same LAN network, the Internet connectivity is NOT required
- Two ESP32 in the different LAN networks, the Internet connectivity is required
No matter two ESP32 connects with each other within a local LAN network or via the Internet, there are two types of communication:
- Two ESP32 communicate directly to each other
- Two ESP32 communicate through a centralized server (e.g MQTT server).
In the case of communicating directly, in most cases, one ESP32 plays the role of TCP client, the other plays the role of TCP server.
In the case of communicating through a centralized server, in most cases, both ESP32 play the role of TCP client.
Depending on the application, We need to choose an application protocol for communicating between two ESP32. Below are some of the application protocols we can use:
- Self-defined protocol over raw TCP (directly)
- Modbus TCP (directly)
- HTTP (directly)
- Telnet (directly)
- SSH (directly)
- MQTT (via a centralized server)
※ NOTE THAT:
It does NOT matter if:
- Both of ESP32 uses Ethernet
- Both of ESP32 uses WiFi
- One ESP32 uses Ethernet, the other use WiFi
UDP protocol is beyond the scope of this tutorial.
Let's realize the following application: A button/switch connected to ESP32 #1 controls an LED connected to ESP32 #2 via Ethernet/WiFi.
As mentioned above, there are some application protocols we can use. In this example, to make it simple, we will define a protocol by ourself (a self-defined protocol)
Let's define a simple protocol:
- A TCP connection is created between ESP32 #1 and ESP32 #2
- ESP32 #1:
- Act as TCP client, actively makes TCP connection request to ESP32 #2
- When the switch is switched to ON, ESP32 #1 sends a byte (command) with value 1 to ESP32 #2.
- When the switch is switched to OFF, ESP32 #1 sends a byte (command) with value 0 to ESP32 #2.
- ESP32 #2:
- Act as TCP server, listens to TCP connection request from ESP32 #1
- If the received byte is 1, Turn ON LED
- If the received byte is 0, Turn OFF LED
※ NOTE THAT:
ESP32 Code for ESP32 #1
ESP32 Code for ESP32 #2
- If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE
- Wire a button/switch to ESP32 #1
- Wire an LED to ESP32 #2
- Open Arduino IDE (called Arduino IDE #1)
- Install ezButton library on Arduino IDE
- Connect ESP32 #1 to PC via USB cable and select COM port of ESP32 #1 on Arduino IDE #1
- Open another Arduino IDE window (called Arduino IDE #2) by clicking on Arduino IDE icon on your PC (important!(**))
- Connect ESP32 #2 to PC via USB cable and select COM port of ESP32 #2 on Arduino IDE #2
- Copy ESP32 #1 code, paste to Arduino IDE #1 and save it (named ESP321)
- Copy ESP32 #2 code, paste to Arduino IDE #2 and save it (named ESP322)
- Upload ESP32 #2 code to ESP32 #2 first
- Open Serial Monitor on Arduino IDE #2, get TCP Server IP address
- Update TCP Server IP address in ESP32 #1 code
- Upload ESP32 #1 code to ESP32 #1
- Open Serial Monitor on Arduino IDE #1
- Press and hold the button on ESP32 #1 → see LED's state on ESP32 #2 (ON)
- Release button on ESP32 #1 → see LED's state on ESP32 #2 (OFF)
- Press, hold, and release the button several times.
- See output on both Serial Monitors
- Serial Monitor of ESP32 #1
- Serial Monitor of ESP32 #2
※ NOTE THAT:
- (**): If you open Arduino IDE #2 window via "File" → "New" or "Open" from Arduino IDE #2 window, you will NOT be able to open two Serial Monitors for two ESP32 in the same PC at the same time.
- There is an alternative to this self-defined protocol. It is the Modbus TCP. The Modbus protocol is standardized, it has many advantages over the self-defined protocol. See more in ESP32 - Modbus tutorial
We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.
How to connect two ESP32 via Internet
There are two kinds of IP address: private IP address and public IP address. The IP address you are using in the home network is usually a private IP address.
You can easily identify the private IP address. The private IP address has three pattern: 10.x.x.x, 172.16.x.x, 192.168.x.x
It does NOT matter to use the private IP address in the following case:
- If two ESP32 are in the same LAN network, regardless of communicating directly or via a centralized server, regardless of your LAN network connects to the Internet or not.
- If two ESP32 are in the different LAN networks and communicating with each other via a centralized server
In the case of two ESP32 are in the different LAN networks and communicating with each other directly. The ESP32 TCP client can use the private IP address. However, the ESP32 TCP server MUST use either:
- A public IP address
- A private IP address with "Port Forwarding" on Router/AP
The process of doing "Port Forwarding" is different from each router/AP. It is out of the scope of this tutorial.
※ NOTE THAT:
Note that this tutorial is incomplete. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.