2016-01-10 16:04:23 +01:00
# UART Module
2016-03-05 10:47:01 +01:00
| Since | Origin / Contributor | Maintainer | Source |
| :----- | :-------------------- | :---------- | :------ |
2019-01-13 22:01:57 +01:00
| 2014-12-22 | [Zeroday ](https://github.com/funshine ) | [Zeroday ](https://github.com/funshine ) | [uart.c ](../../app/modules/uart.c )|
2016-03-05 10:47:01 +01:00
2016-01-10 16:04:23 +01:00
The [UART ](https://en.wikipedia.org/wiki/Universal_asynchronous_receiver/transmitter ) (Universal asynchronous receiver/transmitter) module allows configuration of and communication over the UART serial port.
2016-01-05 04:38:41 +01:00
2016-04-17 23:44:18 +02:00
The default setup for the uart is controlled by build-time settings. The default rate is 115,200 bps. In addition, auto-baudrate detection is enabled for the first two minutes
after platform boot. This will cause a switch to the correct baud rate once a few characters are received. Auto-baudrate detection is disabled when `uart.setup` is called.
2017-04-24 22:06:54 +02:00
!!! important
Although there are two UARTs(0 and 1) available to NodeMCU, **UART 1 is not capable of receiving data and is therefore transmit only** .
2016-01-10 21:49:42 +01:00
## uart.alt()
Change UART pin assignment.
#### Syntax
`uart.alt(on)`
#### Parameters
2016-05-16 22:25:07 +02:00
`on`
- 0 for standard pins
- 1 to use alternate pins GPIO13 and GPIO15
2016-01-10 21:49:42 +01:00
#### Returns
`nil`
2016-01-05 04:38:41 +01:00
## uart.on()
2016-01-10 16:04:23 +01:00
Sets the callback function to handle UART events.
2016-01-05 04:38:41 +01:00
Currently only the "data" event is supported.
2017-04-24 22:06:54 +02:00
!!! note
Due to limitations of the ESP8266, only UART 0 is capable of receiving data.
2016-01-10 16:04:23 +01:00
#### Syntax
2016-01-05 04:38:41 +01:00
`uart.on(method, [number/end_char], [function], [run_input])`
2016-01-10 16:04:23 +01:00
#### Parameters
- `method` "data", data has been received on the UART
- `number/end_char`
2016-12-14 07:34:00 +01:00
- if n=0, will receive every char in buffer
- if n< 255 , the callback is called when n chars are received
- if one char "c", the callback will be called when "c" is encountered, or max n=255 received
2016-01-10 16:04:23 +01:00
- `function` callback function, event "data" has a callback like this: `function(data) end`
- `run_input` 0 or 1. If 0, input from UART will not go into Lua interpreter, can accept binary data. If 1, input from UART will go into Lua interpreter, and run.
2016-01-05 04:38:41 +01:00
To unregister the callback, provide only the "data" parameter.
2016-01-10 16:04:23 +01:00
#### Returns
2016-01-05 04:38:41 +01:00
`nil`
2016-01-10 16:04:23 +01:00
#### Example
2016-01-05 04:38:41 +01:00
```lua
-- when 4 chars is received.
uart.on("data", 4,
function(data)
print("receive from uart:", data)
if data=="quit" then
uart.on("data") -- unregister callback function
end
end, 0)
-- when '\r' is received.
uart.on("data", "\r",
function(data)
print("receive from uart:", data)
if data=="quit\r" then
uart.on("data") -- unregister callback function
end
end, 0)
```
2016-01-10 16:04:23 +01:00
## uart.setup()
2017-03-01 12:41:56 +01:00
(Re-)configures the communication parameters of the UART.
!!! note
Bytes sent to the UART can get lost if this function re-configures the UART while reception is in progress.
2016-01-10 16:04:23 +01:00
#### Syntax
2017-03-01 12:41:56 +01:00
`uart.setup(id, baud, databits, parity, stopbits[, echo])`
2016-01-10 16:04:23 +01:00
#### Parameters
2017-04-24 22:06:54 +02:00
- `id` UART id (0 or 1).
2017-01-04 12:31:47 +01:00
- `baud` one of 300, 600, 1200, 2400, 4800, 9600, 19200, 31250, 38400, 57600, 74880, 115200, 230400, 256000, 460800, 921600, 1843200, 3686400
2016-01-10 16:04:23 +01:00
- `databits` one of 5, 6, 7, 8
- `parity` `uart.PARITY_NONE` , `uart.PARITY_ODD` , or `uart.PARITY_EVEN`
- `stopbits` `uart.STOPBITS_1` , `uart.STOPBITS_1_5` , or `uart.STOPBITS_2`
2017-03-01 12:41:56 +01:00
- `echo` if 0, disable echo, otherwise enable echo (default if omitted)
2016-01-10 16:04:23 +01:00
#### Returns
configured baud rate (number)
#### Example
```lua
-- configure for 9600, 8N1, with echo
uart.setup(0, 9600, 8, uart.PARITY_NONE, uart.STOPBITS_1, 1)
```
2016-12-11 20:35:04 +01:00
## uart.getconfig()
Returns the current configuration parameters of the UART.
#### Syntax
`uart.getconfig(id)`
#### Parameters
2017-04-24 22:06:54 +02:00
- `id` UART id (0 or 1).
2016-12-11 20:35:04 +01:00
#### Returns
Four values as follows:
- `baud` one of 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 74880, 115200, 230400, 256000, 460800, 921600, 1843200, 3686400
- `databits` one of 5, 6, 7, 8
- `parity` `uart.PARITY_NONE` , `uart.PARITY_ODD` , or `uart.PARITY_EVEN`
- `stopbits` `uart.STOPBITS_1` , `uart.STOPBITS_1_5` , or `uart.STOPBITS_2`
#### Example
```lua
print (uart.getconfig(0))
-- prints 9600 8 0 1 for 9600, 8N1
```
2016-01-10 16:04:23 +01:00
## uart.write()
Write string or byte to the UART.
#### Syntax
`uart.write(id, data1 [, data2, ...])`
#### Parameters
2017-04-24 22:06:54 +02:00
- `id` UART id (0 or 1).
2016-01-10 16:04:23 +01:00
- `data1` ... string or byte to send via UART
#### Returns
`nil`
#### Example
```lua
uart.write(0, "Hello, world\n")
```
2016-01-05 04:38:41 +01:00