2018-12-05 10:19:48 +01:00
|
|
|
Custom UART Device
|
|
|
|
==================
|
|
|
|
|
2024-05-07 07:16:56 +02:00
|
|
|
.. seo::
|
|
|
|
:description: Instructions for setting up Custom C++ UART devices with ESPHome.
|
|
|
|
:image: language-cpp.svg
|
|
|
|
:keywords: C++, Custom
|
|
|
|
|
2023-11-01 06:47:57 +01:00
|
|
|
.. warning::
|
|
|
|
|
2024-05-07 07:16:56 +02:00
|
|
|
Custom components are deprecated, not recommended for new configurations and will be removed from ESPHome in a
|
|
|
|
future release. Please look at creating a real ESPHome component and "importing" it into your configuration with
|
|
|
|
:doc:`/components/external_components`.
|
|
|
|
|
|
|
|
You can find some basic documentation on creating your own components at :ref:`contributing_to_esphome`.
|
|
|
|
|
|
|
|
.. warning::
|
2023-11-01 06:47:57 +01:00
|
|
|
|
2024-05-07 07:16:56 +02:00
|
|
|
While we try to keep the ESPHome YAML configuration options as stable as possible, the ESPHome API is less
|
|
|
|
stable. If something in the APIs needs to be changed in order for something else to work, we will do so.
|
2023-11-01 06:47:57 +01:00
|
|
|
|
2018-12-05 10:19:48 +01:00
|
|
|
Lots of devices communicate using the UART protocol. If you want to integrate
|
2019-02-16 23:25:23 +01:00
|
|
|
a device into ESPHome that uses this protocol you can pretty much use almost
|
|
|
|
all Arduino-based code because ESPHome has a nice abstraction over the UART bus.
|
2018-12-05 10:19:48 +01:00
|
|
|
|
|
|
|
See the other custom component guides for how to register components and make
|
|
|
|
them publish values.
|
|
|
|
|
|
|
|
.. code-block:: cpp
|
|
|
|
|
2019-02-16 23:25:23 +01:00
|
|
|
#include "esphome.h"
|
2018-12-05 10:19:48 +01:00
|
|
|
|
|
|
|
class MyCustomComponent : public Component, public UARTDevice {
|
|
|
|
public:
|
|
|
|
MyCustomComponent(UARTComponent *parent) : UARTDevice(parent) {}
|
|
|
|
|
|
|
|
void setup() override {
|
|
|
|
// nothing to do here
|
|
|
|
}
|
|
|
|
void loop() override {
|
|
|
|
// Use Arduino API to read data, for example
|
|
|
|
String line = readString();
|
|
|
|
int i = parseInt();
|
|
|
|
while (available()) {
|
|
|
|
char c = read();
|
|
|
|
}
|
|
|
|
// etc
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
And in YAML:
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
# Example configuration entry
|
2019-02-16 23:25:23 +01:00
|
|
|
esphome:
|
2018-12-05 10:19:48 +01:00
|
|
|
includes:
|
|
|
|
- my_custom_component.h
|
|
|
|
|
|
|
|
uart:
|
|
|
|
id: uart_bus
|
2024-05-07 07:40:12 +02:00
|
|
|
tx_pin: GPIOXX
|
|
|
|
rx_pin: GPIOXX
|
2018-12-05 10:19:48 +01:00
|
|
|
baud_rate: 9600
|
|
|
|
|
|
|
|
custom_component:
|
|
|
|
- lambda: |-
|
|
|
|
auto my_custom = new MyCustomComponent(id(uart_bus));
|
|
|
|
return {my_custom};
|
|
|
|
|
|
|
|
See Also
|
|
|
|
--------
|
|
|
|
|
2019-02-07 13:54:45 +01:00
|
|
|
- :ghedit:`Edit`
|