Effective Communication between ESP32 and ESP8266 using Serial Monitor
ESP32-ESP8266 Serial Communication

Effective Communication between ESP32 and ESP8266 using Serial Monitor

Abstract: Learn how to effectively send and receive messages between ESP32 and ESP8266 using the Serial Monitor.

by

Effective Communication between ESP32 and ESP8266 using Serial Monitor

The ESP32 and ESP8266 are popular microcontrollers in the IoT world. They can communicate with each other using various methods, including Serial Communication. In this article, we will discuss how to establish effective communication between ESP32 and ESP8266 using the Serial Monitor.

Prerequisites

To follow along with this article, you will need the following:

  • An ESP32 and ESP8266 development board
  • A USB cable to power and program the boards
  • The Arduino IDE installed on your computer
  • The ESP32 and ESP8266 boards add-ons for the Arduino IDE

Setting up the Hardware

To set up the hardware, follow these steps:

  1. Connect the ESP32 and ESP8266 to your computer using the USB cables.
  2. Connect the TX pin of the ESP32 to the RX pin of the ESP8266 and vice versa.
  3. Connect the GND pins of both boards together.

Serial Communication Basics

Serial communication is a method of transmitting data one bit at a time over a communication channel. It is a simple and reliable way of communication between microcontrollers, computers, and other devices.

In the context of ESP32 and ESP8266, serial communication is established using the UART (Universal Asynchronous Receiver/Transmitter) interface. The UART interface allows the microcontrollers to send and receive data asynchronously, meaning that they do not need a common clock signal to synchronize their data transmission.

Establishing Serial Communication

To establish serial communication between ESP32 and ESP8266, we need to initialize the UART interface on both boards. We can do this by using the following code:

// ESP32 Code #include <HardwareSerial.h> HardwareSerial Serial2(1); void setup() { Serial.begin(115200); Serial2.begin(115200, SERIAL_8N1, RXD2, TXD2); } void loop() { // Send and receive data } // ESP8266 Code #include <SoftwareSerial.h> SoftwareSerial Serial2(RX, TX); void setup() { Serial.begin(115200); Serial2.begin(115200); } void loop() { // Send and receive data }

In the ESP32 code, we are using the HardwareSerial library to initialize the UART interface on UART1 (Serial2). In the ESP8266 code, we are using the SoftwareSerial library to initialize the UART interface on D2 and D3 pins (RX and TX).

Sending and Receiving Data

To send and receive data between ESP32 and ESP8266, we can use the following code:

// ESP32 Code #include <HardwareSerial.h> HardwareSerial Serial2(1); void setup() { Serial.begin(115200); Serial2.begin(115200, SERIAL_8N1, RXD2, TXD2); } void loop() { if (Serial.available() > 0) { String data = Serial.readString(); Serial2.print("ESP32: " + data); } if (Serial2.available() > 0) { String data = Serial2.readString(); Serial.print("ESP8266: " + data); } } // ESP8266 Code #include <SoftwareSerial.h> SoftwareSerial Serial2(RX, TX); void setup() { Serial.begin(115200); Serial2.begin(115200); } void loop() { if (Serial.available() > 0) { String data = Serial.readString(); Serial2.print("ESP8266: " + data); } if (Serial2.available() > 0) { String data = Serial2.readString(); Serial.print("ESP32: " + data); } }

In the above code, we are checking if there is any data available on the serial port. If there is, we are reading the data and sending it to the other board using the Serial2 interface. We are also printing the received data to the Serial Monitor for debugging purposes.

In this article, we have discussed how to establish effective communication between ESP32 and ESP8266 using the Serial Monitor. We have covered the following topics:

  • Prerequisites
  • Setting up the hardware
  • Serial Communication Basics
  • Establishing Serial Communication
  • Sending and Receiving Data

References

Discover the steps to establish communication between ESP32 and ESP8266 using the Serial Monitor in Arduino IDE.

Latest news