Skip to content

Commit

Permalink
Allow setting rx and tx pin
Browse files Browse the repository at this point in the history
  • Loading branch information
mapero committed Oct 20, 2023
1 parent 98b5d96 commit 26abbbd
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 23 deletions.
26 changes: 9 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ This is a esphome-based adaption of the HCPBridge. Credits for the initial devel

### Example esphome configuration

```
```YAML
esphome:
name: hcpbridge
libraries:
- emelianov/modbus-esp8266
- emelianov/modbus-esp8266 # Required for communication with the modbus
platformio_options:
board_build.f_cpu: 240000000L
board_build.flash_mode: qio
Expand All @@ -26,14 +26,15 @@ esphome:

external_components:
source: github://mapero/esphome-hcpbridge
refresh: 0s # Ensure you always get the latest version

esp32:
board: adafruit_feather_esp32s3
framework:
type: arduino

hcpbridge:
is_connected:
is_connected: # Sensor to display the connection status to the motor
name: "HCPBridge Connected"

cover:
Expand All @@ -44,21 +45,12 @@ switch:
- platform: hcpbridge
name: Garage Light

# API to communicate with home assistant
api:
encryption:
key: !secret api_key

web_server:
port: 80
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable logging
logger:
level: DEBUG
baud_rate: 9600
# Example configuration entry
# Enable OTA updates
ota:
safe_mode: true
```
Expand Down Expand Up @@ -96,7 +88,7 @@ You can find more information on the project here: [Hörmann garage door via MQT
- [ ] Use esphome modbus component instead of own code
- [ ] Map additional functions to esphome
- [ ] Use callbacks instead of pollingComponent
- [ ] Expert options for the HCPBridge component (GPIOs ...)
- [x] Expert options for the HCPBridge component (GPIOs ...)

# Contribute

Expand Down
14 changes: 13 additions & 1 deletion components/hcpbridge/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import binary_sensor
from esphome import pins
from esphome.const import (
CONF_ID,
DEVICE_CLASS_CONNECTIVITY,
CONF_RX_PIN,
CONF_TX_PIN,
)

AUTO_LOAD = ["binary_sensor"]

CONF_IS_CONNECTED = "is_connected"


hcpbridge_ns = cg.esphome_ns.namespace("hcpbridge")
HCPBridge = hcpbridge_ns.class_("HCPBridge", cg.Component)

Expand All @@ -22,6 +26,8 @@
cv.Required(CONF_IS_CONNECTED): binary_sensor.binary_sensor_schema(
device_class=DEVICE_CLASS_CONNECTIVITY,
),
cv.Optional(CONF_RX_PIN): pins.gpio_input_pin_schema,
cv.Optional(CONF_TX_PIN): pins.gpio_output_pin_schema,
})
)

Expand All @@ -31,4 +37,10 @@ async def to_code(config):

if CONF_IS_CONNECTED in config:
sens = await binary_sensor.new_binary_sensor(config[CONF_IS_CONNECTED])
cg.add(var.set_is_connected(sens))
cg.add(var.set_is_connected(sens))
if CONF_RX_PIN in config:
rx_pin = await cg.gpio_pin_expression(config[CONF_RX_PIN])
cg.add(var.set_rx_pin(rx_pin))
if CONF_TX_PIN in config:
tx_pin = await cg.gpio_pin_expression(config[CONF_TX_PIN])
cg.add(var.set_tx_pin(tx_pin))
6 changes: 5 additions & 1 deletion components/hcpbridge/hcpbridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ namespace esphome
void HCPBridge::setup()
{
this->is_connected_->publish_state(false);

int8_t rx = this->rx_pin_ == nullptr ? 18 : this->rx_pin_->get_pin();
int8_t tx = this->tx_pin_ == nullptr ? 17 : this->tx_pin_->get_pin();

this->engine = &HoermannGarageEngine::getInstance();
this->engine->setup();
this->engine->setup(rx, tx);
}

void HCPBridge::update() {
Expand Down
7 changes: 7 additions & 0 deletions components/hcpbridge/hcpbridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "esphome/core/component.h"
#include "esphome/components/binary_sensor/binary_sensor.h"
#include "esphome/core/defines.h"
#include "esphome/core/hal.h"

#include "hoermann.h"

Expand All @@ -17,11 +19,16 @@ namespace esphome
void update() override;

void set_is_connected(binary_sensor::BinarySensor *is_connected) { this->is_connected_ = is_connected; }
void set_tx_pin(InternalGPIOPin *tx_pin) { this->tx_pin_ = tx_pin; }
void set_rx_pin(InternalGPIOPin *rx_pin) { this->rx_pin_ = rx_pin; }


HoermannGarageEngine* engine;

protected:
binary_sensor::BinarySensor *is_connected_;
InternalGPIOPin *tx_pin_;
InternalGPIOPin *rx_pin_;
};
}
}
4 changes: 2 additions & 2 deletions components/hcpbridge/hoermann.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ HoermannGarageEngine &HoermannGarageEngine::getInstance()
return instance;
}

void HoermannGarageEngine::setup()
void HoermannGarageEngine::setup(int8_t rx, int8_t tx)
{
RS485.begin(57600, SERIAL_8E1, PIN_RXD, PIN_TXD);
RS485.begin(57600, SERIAL_8E1, rx, tx);
mb.begin(&RS485);
mb.slave(SLAVE_ID);

Expand Down
4 changes: 2 additions & 2 deletions components/hcpbridge/hoermann.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#define PIN_RXD 18
#else
#define PIN_TXD 17 // UART 2 TXT - G17
#define PIN_RXD 16 // UART 2 RXD - G16
#define PIN_RXD 16 // UART 2 RXD - G16
#endif

// workaround as my Supramatic did not Report the Status 0x0A when it's en vent Position
Expand Down Expand Up @@ -108,7 +108,7 @@ class HoermannGarageEngine

static HoermannGarageEngine& getInstance();

void setup();
void setup(int8_t rx, int8_t tx);
void handleModbus();
Modbus::ResultCode onRequest(Modbus::FunctionCode fc, const Modbus::RequestData data);
void setCommandValuesToRead();
Expand Down
5 changes: 5 additions & 0 deletions example_hcpbridge.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ esphome:

external_components:
source: github://mapero/esphome-hcpbridge
refresh: 0s

esp32:
board: adafruit_feather_esp32s3
Expand All @@ -21,6 +22,8 @@ esp32:
hcpbridge:
is_connected:
name: "HCPBridge Connected"
rx_pin: 18 # optional, default=18
tx_pin: 17 # optional, default=17

cover:
- platform: hcpbridge
Expand All @@ -31,6 +34,8 @@ switch:
name: Garage Light

api:
encryption:
key: !secret api_key

web_server:
port: 80
Expand Down
1 change: 1 addition & 0 deletions secrets.yaml.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
wifi_ssid: "<your wifi ssid>"
wifi_password: "<your wifi password>"
api_key: "<your key>"

0 comments on commit 26abbbd

Please sign in to comment.