Close

Relay control software

A project log for Calling for hot water

Remote controlled buttons for activating hot water recirculation.

wjcarpenterWJCarpenter 09/10/2023 at 03:410 Comments

It's child's play to use ESPHome to configure the D1 Mini to operate the companion relay.

switch:
  - platform: gpio
    id: i_relay
    name: '${node_name} relay'
    pin: '${RELAY}'
    restore_mode: ALWAYS_OFF
    icon: mdi:hot-tub
    on_turn_on:
      - delay: 500ms
      - switch.turn_off: i_relay

This switch can be directly controlled by Home Assistant. When something turns the switch on, it waits 500 milliseconds and then turns itself off. I had the switch turn the relay off automatically to isolate that information to the D1 Mini. There's no need for any outside entity to know the implementation detail of how we're simulating a human button press.

The relay board has a red LED that lights while the relay is activated, but that only lasts for a fraction of a second. The D1 Mini on-board blue LED wasn't being used for anything, so I exposed it so that Home Assistant can turn it on and off. The idea is that pushing the button is momentary, but the recirculation pump then runs for some number of minutes. Home Assistant will turn on the on-board LED for the duration of the pump activity. Both LEDs will be somewhat visible through the enclosure without needing their own openings.

My previous water watcher project included a BME280 climate sensor as part of my sprinkling of climate sensors around my house. I configured 2 pins of the D1 Mini as I2C so I can do the same thing here. The sensor will dangle out the side of the enclosure on a few dupont jumper wires. (I might later change that to a BME688 sensor, which can also detect various types of gases, but that's still I2C.)

In addition to the remote controls that I'll describe in a later project log, I also wanted to have a physical button to activate things. That's mainly for testing purposes while I'm fiddling with things. This is not the same thing as the simple sort of button that can be used directly with the Navien unit. Instead, it will follow the entire path of handoffs, just like the remote buttons. Even though it originates on the D1 Mini with the relay, it doesn't directly activate the relay. It's detected as a GPIO binary sensor by both ESPHome and Home Assistant.

Although this is a work in progress, here's what the ESPHome configuration for the D1 Mini looks like today. Notice that a few values come via the ESPHome "!secret" mechanism.

# https://hackaday.io/project/192719-calling-for-hot-water

substitutions:
  node_name: hotbuttonrelay
  RELAY:  'D1'
  SCL:    'D5'
  SDA:    'D2'
  LED:    'D4'
  BUTTON: 'D7'
  log_level: 'DEBUG'

esphome:
  name: ${node_name}
  platform: ESP8266
  board: d1_mini

wifi:
  ssid:      !secret wifi_ssid
  id:        !secret wifi_ssid
  password:  !secret wifi_password
  power_save_mode: high
  fast_connect: on
  manual_ip:
    static_ip: !secret hotbuttonrelay_ip
    gateway:   !secret wifi_gateway
    subnet:    !secret wifi_subnet
    dns1:      !secret wifi_dns1
    dns2:      !secret wifi_dns2

logger:
   level: ${log_level}
api:
  encryption:
    key: !secret hotbuttonrelay_apikey
  reboot_timeout: 60min
ota:
  password: !secret ota_password

switch:
  - platform: restart
    name: "${node_name} Reboot"

  - platform: gpio
    id: i_relay
    name: '${node_name} relay'
    pin: '${RELAY}'
    restore_mode: ALWAYS_OFF
    icon: mdi:hot-tub
    on_turn_on:
      - delay: 500ms
      - switch.turn_off: i_relay

  - platform: gpio
    id: i_led
    name: '${node_name} LED'
    pin: '${LED}'
    inverted: true
    restore_mode: ALWAYS_OFF
    icon: mdi:led-on

# Local test button. This goes through the relay logic, so it's not an analog press of the water heater button.
binary_sensor:
  - platform: gpio
    name: "${node_name} Button"
    id: i_button
    pin:
      number: '${BUTTON}'
      inverted: true
      mode:
        input: true
        pullup: true
    filters:
      - delayed_on: 10ms
      - delayed_off: 10ms

i2c:
  sda: ${SDA}
  scl: ${SCL}
  scan: false
  id: i_i2c

sensor:
  - platform: bme280
    id: i_bme280
    address: 0x76
    update_interval: 60s
    temperature:
      name: "${node_name} Temp"
      device_class: "temperature"
      oversampling: 16x
    pressure:
      name: "${node_name} AirPr"
      device_class: "pressure"
      oversampling: 16x
    humidity:
      name: "${node_name} RHumidity"
      device_class: "humidity"
      oversampling: 16x


# From the Arduino board definitions file
# #define PIN_WIRE_SDA (4)
# #define PIN_WIRE_SCL (5)

# static const uint8_t SDA = PIN_WIRE_SDA;
# static const uint8_t SCL = PIN_WIRE_SCL;

# #define LED_BUILTIN 2

# static const uint8_t D0   = 16;
# static const uint8_t D1   = 5;
# static const uint8_t D2   = 4;
# static const uint8_t D3   = 0;
# static const uint8_t D4   = 2;
# static const uint8_t D5   = 14;
# static const uint8_t D6   = 12;
# static const uint8_t D7   = 13;
# static const uint8_t D8   = 15;
# static const uint8_t RX   = 3;
# static const uint8_t TX   = 1;

The D1 Mini was first produced a long time ago, and the pin labels reflect the Arduino style. That's very handy if you are using the Arduino IDE, but I am not. ESPHome can use the Arduino designations for a lot of things, but it confuses my tiny brain. It's harder than I expected to find the Espressif GPIO numbering of the pins. I finally found it by working my way through the Arduino IDE board definition files. 

Discussions