Introduction
UART (Universal Asynchronous Receiver/Transmitter) is a serial communication protocol in which data is transferred serially bit by bit at a time. Asynchronous serial communication is widely used for byte-oriented transmission. In Asynchronous serial communication, a byte of data is transferred at a time.
UART serial communication protocol uses a defined frame structure for their data bytes. Frame structure in Asynchronous communication consists:
START bit: It is a bit with which indicates that serial communication has started and it is always low.
Data bits packet: Data bits can be packets of 5 to 9 bits. Normally we use an 8-bit data packet, which is always sent after the START bit.
STOP bit: This usually is one or two bits in length. It is sent after the data bits packet to indicate the end of the frame. The stop bit is always logic high.
Usually, an asynchronous serial communication frame consists of a START bit (1 bit) followed by a data byte (8 bits) and then a STOP bit (1 bit), which forms a 10-bit frame as shown in the figure above. The frame can also consist of 2 STOP bits instead of a single bit, and there can also be a PARITY bit after the STOP bit.
NodeMCU based ESP8266 has two UART interfaces, UART0 and UART1. The ESP8266 data transfer speed via UART interfaces can reach 40 times of 115200 i.e. 4.5 Mbps. By default, the UART0 baud rate is 115200 for the oscillator of 40MHz. It can be changed to a user-defined value according to the need for the application.
NodeMCU UART Pins
TXD (Data Transmit Pin)
This pin is used to transmit data serially.
RXD (Data Receive Pin)
This pin is used to receive data serially.
Note: SD_DATA_1/RXD1 pin is internally used for SPI quad communication with flash memory. Hence, we can only use the TXD1 pin from UART1.
NodeMCU LUA based functions for UART
Let’s see the LUA based functions that are available to use with UART interface in NodeMCU.
uart.setup()
This function configures the communication parameters of the UART.
Note: Bytes sent through UART will be lost if this function re-configures the UART while the reception is in progress.
Syntax: uart.setup(id, baud, databits, parity, stopbits[, echo])
Parameters:
id: UART id (0 or 1).
baud: baud rate which can be one of the following : 300, 600, 1200, 2400, 4800, 9600, 19200, 31250, 38400, 57600, 74880, 115200, 230400, 256000, 460800, 921600, 1843200, 3686400.
databits: no. of data bits per frame. It can take one of the following values :5, 6, 7, 8
parity: set parity option if required from uart.PARITY_NONE, uart.PARITY_ODD, or uart.PARITY_EVEN
stopbits: set no. of stop bit per frame. It can be one of the following: uart.STOPBITS_1, uart.STOPBITS_2
echo: if 0, disable echo, otherwise enable echo (default if omitted)
Returns: configured baud rate.
Example:
-- configure for 9600, 8N1, with echo
uart.setup(0, 9600, 8, uart.PARITY_NONE, uart.STOPBITS_1, 1)
uart.on()
This function sets the callback function to handle UART events. Currently only the "data" event is supported.
Note: Due to limitations of the ESP8266, only UART 0 is capable of receiving data.
Syntax: uart.on(method, [number/end_char], [function], [run_input])
Parameters:
method: "data", data is to be received on the UART
number/end_char: number is used to set how many data bytes to receive. end_char is any character that will be used as the end of data.
function: This is a callback function, which triggers whenever data is received completely according to the way defined in number/end_char. It has data as an input argument to process with.
e.g. function(data) print(“Received data:”, data) end
run_input: 0 or 1. If 0, input from UART will not go into the Lua interpreter. It can accept binary data. If 1, input from UART will go into Lua interpreter and run.
To unregister the callback, provide only the "data" parameter. i.e. uart.on(“data”)
Returns: null
uart.alt()
This function is used to change the UART pin assignment.
Syntax: uart.alt(on)
Parameters:
On: 0 for standard pins i.e. GPIO3 & GPIO1 and 1 to use alternate pins i.e. GPIO13 & GPIO15.
Returns: null
uart.getconfig()
This function is used to return the current configuration parameters of the UART.
Syntax: uart.getconfig(id)
Parameters:
id: UART id (0 or 1).
Returns: It return four values as follows:
baud: one of the following :300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 74880, 115200, 230400, 256000, 460800, 921600, 1843200, 3686400
databits: one of the following : 5, 6, 7, 8
parity: one of the following :uart.PARITY_NONE, uart.PARITY_ODD, or uart.PARITY_EVEN
stopbits: one of the following :uart.STOPBITS_1, uart.STOPBITS_1_5, or uart.STOPBITS_2
uart.write()
This function writes string or byte to the UART.
Syntax: uart.write(id, data1 [, data2, ...])
Parameters:
id: UART id (0 or 1).
data1, data2, ...: string or byte to sent via UART
Returns: null
Example:
uart.write(0, "Hello, world\n")
Example
Let’s write a Lua Script for UART serial communication between two NodeMCUs.
Here transmitter NodeMCU will send a string from UART1 transmit pin i.e. TXD1. Receiver NodeMCU will receive it and print on the serial monitor window.
Lua script for NodeMCU Transmitter
uart.setup(1, 115200, 8, uart.PARITY_NONE, uart.STOPBITS_1, 1)-- setup UART1 i.e. pin GPIO2
while true do --send string per second continuously
uart.write(1, "Hello friend\n")
tmr.delay(1000000)
end
Lua script for NodeMCU Receiver
—callback on receiver data
uart.on("data","\n",function(data) print("receive from uart:", data) end, 0)
Output Window
Below is ESPlorer serial monitor output window at the receiver end. Receiver NodeMCU will print received data on the ESPlorer output window as shown in the below figure.
Components Used:-
Comments