Communication between two ESP32

This tutorial instructs you how to create connection between two ESP32 via TCP/IP over Ethernet/WiFi and exchange data

communication between two ESP32

Hardware Used In This Tutorial

2×ESP-WROOM-32 Dev Module
2×Micro USB Cable
1×(Optional) 5V Power Adapter
1×(Optional) DC Power Jack
1×(Optional) ESP32 Screw Terminal Adapter
Disclosure: some of these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.

Communication between two ESP32 - Overview

There are many ways to make ESP32 #1 communicate with ESP32 #2. Depending on the distance, we can choose one of the below ways:

Methods Distance
SPI very short
I2C very short
UART (TTL) very short
UART (RS-232/485/422) short
Bluetooth short
LoRa long
Ethernet/WiFi unlimited(*)

※ NOTE THAT:

(*):

  • If we connect both ESP32 to the Internet, the communication distance is unlimited
  • If we does not connect both ESP32 to the Internet, but we connect them to the same LAN network, the communication distance is restricted within the LAN network.

This tutorial is going to use Ethernet/WiFi for communication between two ESP32.

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. They does not need to connect to Internet
  • Two ESP32 in the different LAN networks. They need to connect to Internet

There are two ways that two ESP32 can communicate to each other.

  • Two ESP32 communicate with each other directly: one ESP32 is a TCP client, the other is a TCP server.
  • Two ESP32 communicate with each other indirectly via a centralized server (e.g. MQTT server): both ESP32 are TCP clients.

It needs an application protocol that two ESP32 uses to communicate to each other. For example:

  • Self-defined protocol over raw TCP (directly)
  • Modbus TCP (directly)
  • HTTP (directly)
  • Telnet (directly)
  • SSH (directly)
  • MQTT (indirectly via a server)

※ NOTE THAT:

  • It does NOT matter if:
    • Both of ESP32 uses WiFi
    • Both of ESP32 uses Ethernet
    • One ESP32 uses Ethernet, the other use WiFi
  • UDP protocol is beyond the scope of this tutorial.

Example Application

Let's realize the following application: A button/switch connected to ESP32 #1 controls an LED connected to ESP32 #2 via Ethernet/WiFi.

communication between two ESP32

To make it simple, This tutorial will use a self-defined protocol (Defined by us, no standard)

Self-defined Protocol

We define a simple protocol as follows:

  • Create a TCP connection between ESP32 #1 and ESP32 #2
  • ESP32 #1:
    • TCP client: actively makes TCP connection request to ESP32 #2
    • If the switch's state is changed to ON, ESP32 #1 sends a byte (command) with value 1 to ESP32 #2.
    • If the switch's state is changed to OFF, ESP32 #1 sends a byte (command) with value 0 to ESP32 #2.
  • ESP32 #2:
    • TCP server: listens to a TCP connection request from ESP32 #1. If there is a TCP request, it accepts and the connection is created.
    • If It receives a byte:
      • If the value is 1, Turn LED on
      • If the value is 0, Turn LED off

      Wiring Diagram

      https://esp32io.com/images/tutorial/two-esp32-led-switch-button.jpg

      ※ NOTE THAT:

      It needs touse a resistor for LED if it does not have a built-in resistor. We have dedicated tutorials for button and LED. You can learn more about them in:

      ESP32 Code for ESP32 #1

      /* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/communication-between-two-esp32 */ // ESP32 #1: TCP CLIENT + A BUTTON/SWITCH #include <ezButton.h> #include <SPI.h> #include <Ethernet.h> #define BUTTON_PIN 7 #define SERVER_PORT 4080 ezButton button(BUTTON_PIN); // create ezButton that attach to pin 7; byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x03}; IPAddress serverAddress(192, 168, 0, 180); EthernetClient TCPclient; void setup() { Serial.begin(9600); button.setDebounceTime(50); // set debounce time to 50 milliseconds Serial.println("ESP32 #1: TCP CLIENT + A BUTTON/SWITCH"); // Initialize Ethernet Shield: if (Ethernet.begin(mac) == 0) Serial.println("ESP32 #1: Failed to configure Ethernet using DHCP"); // connect to TCP server (ESP32 #2 ) if (TCPclient.connect(serverAddress, SERVER_PORT)) Serial.println("ESP32 #1: Connected to TCP server"); else Serial.println("ESP32 #1: Failed to connect to TCP server"); } void loop() { button.loop(); // MUST call the loop() function first if (!TCPclient.connected()) { Serial.println("ESP32 #1: Connection is disconnected"); TCPclient.stop(); // reconnect to TCP server (ESP32 #2) if (TCPclient.connect(serverAddress, SERVER_PORT)) Serial.println("ESP32 #1: Reconnected to TCP server"); else Serial.println("ESP32 #1: Failed to reconnect to TCP server"); } if (button.isPressed()) { TCPclient.write('1'); TCPclient.flush(); Serial.println("ESP32 #1: - The button is pressed, sent command: 1"); } if (button.isReleased()) { TCPclient.write('0'); TCPclient.flush(); Serial.println("ESP32 #1: - The button is released, sent command: 0"); } }

      ESP32 Code for ESP32 #2

      /* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/communication-between-two-esp32 */ // ESP32 #2: TCP SERVER + AN LED #include <SPI.h> #include <Ethernet.h> #define LED_PIN 7 #define SERVER_PORT 4080 byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02}; EthernetServer TCPserver(SERVER_PORT); void setup() { Serial.begin(9600); pinMode(LED_PIN, OUTPUT); Serial.println("ESP32 #2: TCP SERVER + AN LED"); // Initialize Ethernet Shield: if (Ethernet.begin(mac) == 0) Serial.println("ESP32 #2: Failed to configure Ethernet using DHCP"); // Print your local IP address: Serial.print("ESP32 #2: TCP Server IP address: "); Serial.println(Ethernet.localIP()); Serial.println("ESP32 #2: -> Please update the serverAddress in ESP32 #1 code"); // Listening for a TCP client (from ESP32 #1) TCPserver.begin(); } void loop() { // Wait for a TCP client from ESP32 #1: EthernetClient client = TCPserver.available(); if (client) { // Read the command from the TCP client: char command = client.read(); Serial.print("ESP32 #2: - Received command: "); Serial.println(command); if (command == '1') digitalWrite(LED_PIN, HIGH); // Turn LED on else if (command == '0') digitalWrite(LED_PIN, LOW); // Turn LED off Ethernet.maintain(); } }

      Quick Instructions

      • 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
      COM6
      Send
      ESP32 #2: TCP SERVER + AN LED TCP Server IP address: 192.168.0.2 -> Please update the serverAddress in ESP32 #1 code
      Autoscroll Show timestamp
      Clear output
      9600 baud  
      Newline  
      • 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)
      • Do the above process several times.
      • See output on both Serial Monitors
        • Serial Monitor of ESP32 #1
        COM6
        Send
        ESP32 #1: TCP CLIENT + A BUTTON/SWITCH ESP32 #1: Connected to TCP server ESP32 #1: - The button is pressed, sent command: 1 ESP32 #1: - The button is released, sent command: 0 ESP32 #1: - The button is pressed, sent command: 1 ESP32 #1: - The button is released, sent command: 0 ESP32 #1: - The button is pressed, sent command: 1 ESP32 #1: - The button is released, sent command: 0
        Autoscroll Show timestamp
        Clear output
        9600 baud  
        Newline  
        • Serial Monitor of ESP32 #2
        COM6
        Send
        ESP32 #2: TCP SERVER + AN LED ESP32 #2: TCP Server IP address: 192.168.0.2 ESP32 #2: -> Please update the serverAddress in ESP32 #1 code ESP32 #2: - Received command: 1 ESP32 #2: - Received command: 0 ESP32 #2: - Received command: 1 ESP32 #2: - Received command: 0 ESP32 #2: - Received command: 1 ESP32 #2: - Received command: 0
        Autoscroll Show timestamp
        Clear output
        9600 baud  
        Newline  

        ※ 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

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

How to connect two ESP32 via Internet

See How to connect two devices via Internet

WARNING

Please note that this tutorial is under development. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.

※ OUR MESSAGES