@OP
1. The pictorial view of a typical (NodeMCU 1.0(ESP-12E Module)
Figure-1: Pictorial view of NodeMCU Board
2. The UART Port (RX, TX) is connected with Serial Monitor/Arduino IDE via an on-board UART/USB converter chip (CP2102).
3. I am not sure if there is any other hardware UART Port in the NodeMCU. Therefore, let us create a Software UART Port (SUART) for serial data communication. You can create a SURT Port by including the following codes in your sketch. All commands/methods of UART Port are equally applicable for SUART Port except that the void serialEvent() procedure is reserved for hardware UART Port.
#include<SoftwareSerial.h>
SoftwareSerial mySUART(4, 5); //(SRX, STX) = (GPIO4, GPIO5) = (D2, D1)
mySUART.begin(115200);
4. If you are using Arduino IDE Environment for the NodeMCU, then you can enjoy all the resources provided by the SoftwareSerial.h Library of the Arduino IDE for exchanging serial data (from 1-character to 64-character in one .print() command).
5. This is how the SUART Port/Interface works under the supervision of SoftwareSerial.h Library.
(1) In serial communication, data exchange takes place one character at a time in ASCII frame. An ASCII frame is composed of 1-START Bit, 8-bit ASCII Code for the character, 1-parity Bit (optional), and 1-STOP Bit.
(2) When a character arrives at the Receiver Section of the NodeMCU, the MCU is immediately interrupted; the MCU goes to the ISR (Interrupt Service Routine), reads the data byte from the receiver and saves into an unseen 64-byte wide FIFO buffer. This process happens in the background.
(3) The number of characters currently present in the FIFO buffer can be known by executing this instruction: byte x = Serial.available();.
(4) The character that has entered first in the buffer can be read using this instruction: byte x = Serial.read();,
(5) Data transmission also happens on interrupt basis one character at a time. The user program puts the character(s) into the transmit buffer from which the character enters into transmitter through interrupt process.
6. An Example
Figure-2: SUART connection between NodeMCU and UNO
Sender Codes (tested)
#include<SoftwareSerial.h>
SoftwareSerial mySUART(4, 5); //D2, D1
void setup()
{
Serial.begin(115200);
mySUART.begin(115200);
}
void loop()
{
if(Serial.available()>0)
{
byte x = Serial.read();
mySUART.write(x);
}
if(mySUART.available()>0)
{
Serial.write((char)mySUART.read());
}
}
Receiver Codes (tested)
#include<SoftwareSerial.h>
SoftwareSerial mySUART(4, 5); //D2, D1
void setup()
{
Serial.begin(115200);
mySUART.begin(115200);
}
void loop()
{
if(Serial.available()>0)
{
byte x = Serial.read();
mySUART.write(x);
}
if(mySUART.available()>0)
{
Serial.write((char)mySUART.read());
}
}