2018-08-22 22:05:28 +02:00
|
|
|
.. _uart:
|
|
|
|
|
|
|
|
UART Bus
|
|
|
|
========
|
|
|
|
|
2018-11-14 22:12:27 +01:00
|
|
|
.. seo::
|
|
|
|
:description: Instructions for setting up a UART serial bus on ESPs
|
2018-11-19 18:32:16 +01:00
|
|
|
:image: uart.png
|
2018-11-14 22:12:27 +01:00
|
|
|
:keywords: UART, serial bus
|
|
|
|
|
2018-08-22 22:05:28 +02:00
|
|
|
UART is a common serial protocol for a lot of devices. For example, when uploading a binary to your ESP
|
|
|
|
you have probably used UART to access the chip. UART (or for Arduino often also called Serial) usually
|
|
|
|
consists of 2 pins:
|
|
|
|
|
|
|
|
- **TX**: This line is used to send data to the device at the other end.
|
|
|
|
- **RX**: This line is used to receive data from the device at the other end.
|
|
|
|
|
|
|
|
Please note that these the naming of these two pins depends on the chosen perspective and can be ambiguous. For example,
|
|
|
|
while the ESP might send (``TX``) on pin A and receive (``RX``) data on pin B, from the other devices
|
|
|
|
perspective these two pins are switched (i.e. *it* sends on pin B and receives on pin A). So you might
|
|
|
|
need to try with the two pins switched if it doesn't work immediately.
|
|
|
|
|
2019-02-16 23:25:23 +01:00
|
|
|
Additionally, each UART bus can operate at different speeds (baud rates), so ESPHome needs to know what speed to
|
2018-08-22 22:05:28 +02:00
|
|
|
receive/send data at using the ``baud_rate`` option. The most common baud rates are 9600 and 115200.
|
|
|
|
|
|
|
|
In some cases only **TX** or **RX** exists as the device at the other end only accepts data or sends data.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
On the ESP32, this component uses the hardware UART units and is thus very accurate. On the ESP8266 however,
|
2019-02-16 23:25:23 +01:00
|
|
|
ESPHome has to use a software implementation as there are no other hardware UART units available other than the
|
2018-08-22 22:05:28 +02:00
|
|
|
ones used for logging. Therefore the UART data on the ESP8266 can have occasional data glitches especially with
|
|
|
|
higher baud rates..
|
|
|
|
|
2018-11-19 18:32:16 +01:00
|
|
|
.. code-block:: yaml
|
2018-08-22 22:05:28 +02:00
|
|
|
|
|
|
|
# Example configuration entry
|
|
|
|
uart:
|
|
|
|
tx_pin: D0
|
|
|
|
rx_pin: D1
|
|
|
|
baud_rate: 9600
|
|
|
|
|
|
|
|
Configuration variables:
|
2018-08-24 22:44:01 +02:00
|
|
|
------------------------
|
2018-08-22 22:05:28 +02:00
|
|
|
|
|
|
|
- **baud_rate** (**Required**, int): The baud rate of the UART bus.
|
|
|
|
- **tx_pin** (*Optional*, :ref:`config-pin`): The pin to send data to from the ESP's perspective.
|
|
|
|
- **rx_pin** (*Optional*, :ref:`config-pin`): The pin to receive data on from the ESP's perspective.
|
|
|
|
- **id** (*Optional*, :ref:`config-id`): Manually specify the ID for this UART hub if you need multiple UART hubs.
|
|
|
|
|
2019-02-13 11:20:34 +01:00
|
|
|
.. _uart-hardware_uarts:
|
|
|
|
|
|
|
|
Hardware UARTs
|
|
|
|
--------------
|
|
|
|
|
|
|
|
Whenever possible, esphome will use the Hardware UART unit on the processor for fast and accurate communication.
|
|
|
|
When the hardware UARTs are all occupied, esphome will fall back to a software implementation that may not
|
|
|
|
be accurate at higher baud rates.
|
|
|
|
|
|
|
|
``UART0`` is (by default) used by the :doc:`logger component </components/logger>`, using ``tx_pin: GPIO1`` and
|
|
|
|
``rx_pin: GPIO3``. If you configure a UART that overlaps with these pins, you can share the hardware with the
|
|
|
|
logger and leave others available. If you have configured the logger to use a different hardware UART, the pins
|
|
|
|
used for hardware sharing change accordingly.
|
|
|
|
|
|
|
|
The ESP32 has three UARTs. Any pair of GPIO pins can be used, as long as they support the proper output/input modes.
|
|
|
|
|
|
|
|
The ESP8266 has two UARTs; the second of which is TX-only. Only a limited set of pins can be used. ``UART0`` may
|
|
|
|
use either ``tx_pin: GPIO1`` and ``rx_pin: GPIO3``, or ``tx_pin: GPIO15`` and ``rx_pin: GPIO13``. ``UART1`` must
|
|
|
|
use ``tx_pin: GPIO2``. Any other combination of pins will result in use of a software UART.
|
|
|
|
|
2019-05-29 19:28:30 +02:00
|
|
|
.. _uart-write_action:
|
|
|
|
|
|
|
|
``uart.write`` Action
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
This :ref:`Action <config-action>` sends a defined UART signal to the given UART bus.
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
on_...:
|
|
|
|
- uart.write: 'Hello World'
|
|
|
|
|
2019-06-26 21:49:43 +02:00
|
|
|
# For escape characters, you must use double quotes!
|
|
|
|
- uart.write: 'Hello World\r\n'
|
|
|
|
|
2019-05-29 19:28:30 +02:00
|
|
|
# Raw data
|
|
|
|
- uart.write: [0x00, 0x20, 0x42]
|
|
|
|
|
|
|
|
# Templated, return type is std::vector<uint8_t>
|
|
|
|
- uart.write: !lambda
|
|
|
|
return {0x00, 0x20, 0x42};
|
|
|
|
|
2018-08-22 22:05:28 +02:00
|
|
|
See Also
|
|
|
|
--------
|
|
|
|
|
2019-02-13 11:20:34 +01:00
|
|
|
- :doc:`/components/logger`
|
2019-05-12 22:44:59 +02:00
|
|
|
- :apiref:`uart/uart.h`
|
2019-02-07 13:54:45 +01:00
|
|
|
- :ghedit:`Edit`
|