This commit is contained in:
Eugen Scobich 2024-05-07 14:45:02 +02:00 committed by GitHub
commit 9a194213d6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 185 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 377 KiB

View File

@ -0,0 +1,183 @@
Stm32 Port Expander
=====================
.. seo::
:description: Instructions on using an STM32F103 board, like the BluePill for expanding ports of an ESPHome node
:image: stm32_logo.svg
:keywords: STM32 port expander extender ESPHome
With this sketch you can control pins of a remote STM32 board through ESPHome. The STM32 acts as a port
expander, allowing you to use more pins than a standard ESP8266/ESP32 has.
.. figure:: images/stm32f103.jpg
:align: center
:width: 75.0%
The STM32 is connected to the ESP via I²C.
Currently it is supported:
- reading digital inputs (0/1)
- reading analog inputs (0/255)
- writing digital outputs (0/1)
- writing analog outputs (0/255)
The STM32 project can be retrieved from `here <https://github.com/eugenscobich/stm32-port-expander>`__.
You will need STM32CubeIDE to change and build it.
The project if made to extand 10 ports for AC dimmable output, 15 digiat inputs, 2 analog outputs and one anolog input to configure temperature treshhold.
You need to download `stm32_port_expander.h <https://github.com/eugenscobich/stm32-port-expander/blob/main/esp_home/stm32_port_expander.h>`__
and include the stm32_port_expander.h in the ESPHome configuration.
Schematics and PCB can be found `here <https://oshwlab.com/eugenscobich/esphome-8-channel-dimmer>`__.
.. code-block:: yaml
esphome:
# ...
includes:
- stm32_port_expander.h
Setup your :ref:`I²C Bus <i2c>` and assign it an ``id``:
.. code-block:: yaml
i2c:
id: i2c_component
frequency: 400kHz
By default ESP8266 uses ``SDA`` pin ``GPIO4`` which you need to connect to STM32F103's ``PB7`` and the ``SCL``
is ``GPIO5`` which goes to Arduino's ``PB6``.
STM32F103 can operate at maximum 400kHz and the framework was optimized to work on this speed.
Then create a ``custom_component``, this will be the main component we will be referencing later when creating
individual IOs.
.. code-block:: yaml
custom_component:
- id: stm32_port_expander_1
lambda: |-
auto expander = new Stm32PortExpander(i2c_component, 0x08, 40);
return {expander};
By default the I²C address is ``0x08`` but you can change it on the STM32 Cube so you can have more devices
on the same bus.
Now it is time to add the ports.
Binary Sensor
-------------
This project is inspired by Arduino Port Expander and configuration are pretty much the same.
Pin numbers must be in sync between ESPHome config and STM32 frameware.
To setup binary sensors, create a custom platform as below, list in braces all the sensors you want,
in the example below two binary sensors are declared on pin 11 and 12, that correspond to PB15 and PA12 respectively.
Then declare the ESPHome reference of the binary sensor in the same order as declared in the lambda:
.. code-block:: yaml
- platform: custom
lambda: |-
return {stm32_port_expander_binary_sensor(stm32_port_expander_1, 11),
stm32_port_expander_binary_sensor(stm32_port_expander_1, 12)};
binary_sensors:
- id: binary_sensor_pin_11
name: Binary Sensor Pin 11
filters:
- invert:
- id: binary_sensor_pin_12
name: Binary Sensor Pin 12
filters:
- invert:
The listed ``binary_sensors`` supports all options from :ref:`Binary Sensor <config-binary_sensor>` like
automations and filters.
Sensor
------
Sensors allows for reading the analog value of an analog pin. In this example an internal temperauture sensor is used via DMA.
Then declare the ESPHome reference of the sensor in the same order as declared in the lambda:
.. code-block:: yaml
- platform: custom
lambda: |-
return {stm32_port_expander_analog_sensor(stm32_port_expander_1, 0)};
sensors:
- id: analog_sensor_pin_0
name: Dimmer module temperature
filters:
- throttle: 10s
unit_of_measurement: "°C"
device_class : temperature
state_class: measurement
The listed ``sensors`` supports all options from :ref:`Sensor <config-sensor>` like
automations and filters.
.. note::
Sensors are polled by default every loop cycle so it is recommended to use the ``throttle`` filter
to not flood the network.
Binary Output
-------------
To setup outputs, create a custom platform as below, list in braces all the outputs you want,
in the example below two outputs are declared on pin ``1`` and ``28``.
.. code-block:: yaml
- platform: custom
type: float
lambda: |-
return {stm32_port_expander_binary_output(stm32_port_expander_1, 29),
stm32_port_expander_binary_output(stm32_port_expander_1, 30)};
outputs:
- id: binary_output_pin_29
inverted: true
- id: binary_output_pin_30
Float Output
------------
To setup outputs, create a custom platform as below, list in braces all the outputs you want,
in the example below two outputs are declared on pin ``1`` and ``28``. ``28`` is not an real
output pin and it is used to set the temperature treshold when epander must go in error state and reset all outputs.
.. code-block:: yaml
- platform: custom
type: float
lambda: |-
return {stm32_port_expander_float_output(stm32_port_expander_1, 1),
stm32_port_expander_float_output(stm32_port_expander_1, 28)};
outputs:
- id: float_output_pin_1
- id: float_output_pin_28
min_power: 0.25
max_power: 0.60
light:
- id: dimmerized_light_1
platform: monochromatic
output: float_output_pin_1
name: Dimmerized Light Pin 1
gamma_correct: 0
- id: dimmerized_light_28
platform: monochromatic
output: float_output_pin_28
name: Dimmer module temperature limit
gamma_correct: 0
See Also
--------
- :doc:`/cookbook/arduino_port_extender`
- :ghedit:`Edit`

1
images/stm32_logo.svg Normal file
View File

@ -0,0 +1 @@
<svg viewBox="0 0 79.37 79.37" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" id="Layer_1" overflow="hidden"><defs></defs><path d="M39.58 79.37C61.4394 79.37 79.16 61.6494 79.16 39.79 79.16 17.9306 61.4394 0.210001 39.58 0.210001 17.7206 0.210001 1.83105e-06 17.9306 1.83105e-06 39.79 1.83105e-06 61.6494 17.7206 79.37 39.58 79.37Z" fill="#FFFFFF"/><path d="M20.58 14.27C25.73 10.27 40.81 16.86 44.47 20.27 44.19 19.15 44.23 17.17 45.18 16.27 46.43 16.58 47.47 18.11 47.89 19.32 49.64 15.9 62.15-4.35 67.49 3.54 71.18 8.98 67.88 19.35 63.7 23.08 70.26 26.4 75 31.52 69.27 38.3 62.51 46.3 55.65 39.04 50.8 33.68 51.86 36.44 52.8 40.61 52.29 42.91 51.01 41.36 49.53 36.91 49.05 34.06 47.51 40.49 46.57 47.61 38.63 47.78 27.16 48.04 27.75 37.38 32.25 31.23 26.51 31.7 20.4 22.23 19.8 16.63 19.6643 15.7636 19.9548 14.8849 20.58 14.27" fill="#3CB4E6"/><path d="M14.87 61.68 14.87 62C14.87 63.14 15.2 63.87 16.48 63.87 17.2524 63.9052 17.9071 63.3075 17.9422 62.5351 17.9447 62.4801 17.944 62.4249 17.94 62.37 17.94 61.27 17.38 60.87 15.77 60.31 13.6 59.56 12.6 58.66 12.6 56.64 12.6 54.29 14.04 53.08 16.35 53.08 18.66 53.08 20.1 54.02 20.1 56.34L20.1 56.68 17.76 56.68C17.76 55.54 17.37 54.88 16.43 54.88 15.7484 54.7702 15.1069 55.2337 14.9971 55.9153 14.975 56.0527 14.976 56.1929 15 56.33 15 57.07 15.22 57.53 16.26 57.97L18.14 58.79C19.97 59.58 20.45 60.51 20.45 61.99 20.45 64.55 18.89 65.7 16.24 65.7 13.59 65.7 12.39 64.41 12.39 62.18L12.39 61.68Z" fill="#3CB4E6"/><path d="M28.79 53.36 28.79 55.36 26.15 55.36 26.15 65.47 23.71 65.47 23.71 55.36 21.06 55.36 21.06 53.36Z" fill="#3CB4E6"/><path d="M29.9 53.36 33.52 53.36 35.25 61.91 35.25 61.91 37 53.36 40.61 53.36 40.61 65.47 38.4 65.47 38.4 55.8 38.4 55.8 36.25 65.47 34.35 65.47 32.17 55.8 32.17 55.8 32.17 65.47 29.9 65.47Z" fill="#3CB4E6"/><path d="M44.59 58.7 45.34 58.7C46.61 58.7 47.17 57.79 47.17 56.58 47.17 55.37 46.76 54.58 45.61 54.58 44.03 54.58 43.88 56.04 43.88 56.92L42.39 56.92C42.39 54.87 43.39 53.46 45.58 53.46 47.136 53.313 48.5166 54.4552 48.6637 56.0112 48.6815 56.2005 48.6803 56.391 48.66 56.58 48.7457 57.7767 47.9624 58.8631 46.8 59.16L46.8 59.16C48.1831 59.3518 49.1599 60.6129 49 62 49 64.19 47.9 65.71 45.59 65.71 43.8266 65.8279 42.3016 64.4939 42.1837 62.7305 42.1757 62.6105 42.1745 62.4901 42.18 62.37L42.18 62.22 43.68 62.22C43.68 63.32 44.22 64.59 45.59 64.59 46.47 64.59 47.51 64.15 47.51 62.12 47.6753 61.0279 46.9239 60.0085 45.8318 59.8432 45.669 59.8186 45.5039 59.8142 45.34 59.83L44.57 59.83Z" fill="#3CB4E6"/><path d="M57.11 65.47 50.39 65.47C50.4634 63.5176 51.4246 61.7056 53 60.55L54 59.72C54.9723 59.0807 55.5671 58.0034 55.59 56.84 55.59 55.84 55.13 54.68 53.85 54.68 52 54.68 51.95 56.6 51.95 57.28L50.43 57.28C50.43 55.01 51.43 53.46 53.83 53.46 55.4821 53.3341 56.9234 54.5713 57.0493 56.2233 57.0606 56.372 57.0609 56.5213 57.05 56.67 57.05 58.5 56.29 59.46 54.95 60.62L53.26 62.09C52.6179 62.5969 52.1581 63.2989 51.95 64.09L57.11 64.09Z" fill="#3CB4E6"/></svg>

After

Width:  |  Height:  |  Size: 2.9 KiB

View File

@ -1043,6 +1043,7 @@ Cookbook
Non-Invasive Power Meter, cookbook/power_meter, power_meter.jpg
Sonoff Fishpond Pump, cookbook/sonoff-fishpond-pump, cookbook-sonoff-fishpond-pump.jpg
Arduino Port Extender, cookbook/arduino_port_extender, arduino_logo.svg
STM32 Port Extender, cookbook/stm32_port_extender, stm32_logo.svg
EHMTX a matrix status/text display, cookbook/ehmtx, ehmtx.jpg
Share data directly between ESPHome nodes, cookbook/http_request_sensor, connection.svg, dark-invert