Code
HardwareSerial Serial2(2); //Needed since this is an ESP32
void setup() {
Serial.begin(115200);
Serial2.begin(115200, SERIAL_8N1, 16, 17);
}
void loop() {
if (Serial.available()) // If user typed something through the USB...
{
Serial2.write(Serial.read()); // Push it through Port 2 to Central ESP32
}
if (Serial2.available()) // If data is received through Port 2 from Central ESP32
{
Serial.write(Serial2.read()); // Show data on Serial Monitor 2
}
}
error code
Arduino: 1.8.8 (Windows 10), Board: "DOIT ESP32 DEVKIT V1, 80MHz, 115200, None"
core\core.a(HardwareSerial.cpp.o):(.bss.Serial2+0x0): multiple definition of `Serial2'
sketch\1x1_Beta_SoilWatch10_Send.ino.cpp.o:(.bss.Serial2+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board DOIT ESP32 DEVKIT V1.
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
วิธีแก้ไข
เนื่องจากมีการประกาศ Serial ซ้ำกันจึงต้องลบบรรทัดที่ซ้ำออก
#include <HardwareSerial.h>
//HardwareSerial Serial2(2); //Needed since this is an ESP32
void setup() {
Serial.begin(115200);
Serial2.begin(115200, SERIAL_8N1, 16, 17);
}
void loop() {
if (Serial.available()) // If user typed something through the USB...
{
Serial2.write(Serial.read()); // Push it through Port 2 to Central ESP32
}
if (Serial2.available()) // If data is received through Port 2 from Central ESP32
{
Serial.write(Serial2.read()); // Show data on Serial Monitor 2
}
}