3D Printed ESP32 On-Air Switch/Light Modules for Home Assistant

by cptJack22 in Circuits > Microcontrollers

6416 Views, 78 Favorites, 0 Comments

3D Printed ESP32 On-Air Switch/Light Modules for Home Assistant

768D6F12-DFA3-4060-A8C0-59B00FA71381.jpeg
0FA24DA5-A620-4C71-B42C-6CE7747FDADE.jpeg

Using Home Assistant with MQTT and Node-RED, I developed an on-air indicator switch module set that have led indicators for two offices. This project can be modified to fit many home office (or even office office) setups where these services are available.


Home Assistant is connected to our Echo's that we use for an intercom system between the offices. Setting a switch module to "On-Air" puts the Echo into "Do Not Disturb" mode, turns on the local LED indicator and any remote indicators listening to the MQTT broadcast on our home network including an LED on the second switch module.


We have two bedrooms that are used as offices since my wife (L) and I (J) are permanently working from home. The on-air switches let each other know when we are in meetings. They are basically smart home "do not disturb" signs.


Future plans include utilizing the MQTT topics to easily add indicator lights on or near the office doors and possibly other parts of the house. After the initial setup, all that is needed is another ESP board, light and Node-RED to listen for the MQTT topic to control the light.

Supplies

5 mm LEDs (any color you like--go nuts! I chose red.)

Two-position switch or button fancy as you like

220 ohm resistors x2

ESP32 WiFi board

3D printed box

M3 Hex Head Screws x2

M3 Hex Head Nuts x2

M3 Hex key

USB Micro B or whichever matches the ESP board's USB port.

3.3v-5v power source of choice (the ESP board is 3.3v, but has a protection circuit that will allow 5v without damage.. I'm told. Allegedly. Verify for your ESP board that this is true. Instructable author not liable for any damage, fires, explosions, stock market drops, or eclipses of any sort resulting from reading this instructable.

Install/Setup Home Assistant

1200px-Home_Assistant_Logo.svg.png

There are a lot of resources available to help you set up Home Assistant to your specific needs. As this is not a very large book on the topic, I'll leave you with some of the resources I've found:

The Basic Work Flow

Once completed, this is what is really going on:

  • I, in Office J, flip the switch ON.
  • ESP32 inside the little box publishes an MQTT message (literally "on") to topic /officej/onair/switch.
  • The Node-RED flow is subscribed to that topic and reads the message.
  • Node-RED is expecting "on" or "off".
  • Whichever message is received is published to the topic /officej/onair/led.
  • Node-RED also tells my Echo, "Astro", to go into Do Not Disturb mode (for "on") or takes it out of DND mode (for "off").
  • ESP32 is subscribed to /officej/onair/led and turns on or off one of the LEDs depending on the message.
  • The corresponding Office L switch module is also subscribed to /officej/onair/led and does the same with one of its LEDs.

That's basically it! Flip all officej's to officel's (and l's to j's) and you have the other switch module. The wife's office Echo is named Rosie :)


It could be simplified to one MQTT topic, but I chose to use more because I can. It's my project and I'll do what I wanna. Also, I want to scale this project up to do more with it in our smart home in the future. They call it future-proofing, but anyone who has future-proofed a project knows better.

Install Required Integrations

FCJI6EZKY1JZV9S.png

More reference links...

Sorry, there can be a wide range of setup options and I'm striving for the life goal of attaining that prestigious title, "Laziest Instructable Writer", so I'm just going to give you some resources to help you out and wish you luck.


Honestly, I found the resources to be plentiful and useful. My hair loss is not from tearing out my hair setting up these integrations or even Home Assistant. It is from genetics. Thanks Dad.


ESPHome

Getting started.

ESPHome uses YAML to write the code being flashed to your ESP board for you. The ESPHome project has a lot of built-in functionality for an impressively wide range of sensors and other components. I threw some additional components to get more information to my dashboard than "the switch is on".


The YAML below is for one switch module easily copied and changed for the second switch module.


Note the use of the secrets file to keep the YAML file fairly safe to share. There's a secrets file specifically for ESPHome, but Home Assistant also has one. An additional benefit to the secrets files is that you can use them like variables in programming. If the wifi password is changed, I only need to update one file instead of the two YAML files using them in this project alone. I will also need to update the ESP32s, with the changes, but that can be done over the air (OTA).


Changing my naming conventions will not affect the output. If you are in a situation where one office happens to be named "Office J" and the other "Office L", you may not want to change anything. Lucky you!


The MQTT (more on that below) topics can be changed as well, but they must also be changed in Node-RED.


Initially, you will need to flash the ESP directly through one of several options. After that, you should be able to flash updates over the air.

Create a new ESPHome device and update the script to the YAML below.

YAML code:

esphome:
  name: officej-onair-node
  platform: ESP32
  board: esp32dev

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: "[auto-generated by ESPHome]"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_pw

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "officej-onair"
    password: !secret ap_pw

captive_portal:

sensor:
  - platform: wifi_signal
    name: "WiFi Signal Office J On-Air Node"
    update_interval: 30s
    
mqtt:
  id: officej_onair_node
  broker: !secret mqtt_broker
  username: !secret mqtt_un
  password: !secret mqtt_pw
  birth_message:
    topic: officej/onair/status
    payload: online
  will_message:
    topic: officej/onair/status
    payload: offline
  log_topic: officej/onair/log
  on_message:
    - topic: officej/onair/led
      qos: 0
      then:
        - if:
            condition:
              lambda: 'return x == "on";'
            then:
              - light.turn_on: light_officej_onair_node_led01
            else:
              - light.turn_off: light_officej_onair_node_led01
    - topic: officel/onair/led
      qos: 0
      then:
        - if:
            condition:
              lambda: 'return x == "on";'
            then:
              - light.turn_on: light_officej_onair_node_led02
            else:
              - light.turn_off: light_officej_onair_node_led02

text_sensor:
  - platform: wifi_info
    ip_address:
      name: office J on-air node IP Address


# switch & LEDs
light:
  - platform: binary
    name: "officej-onair-node-led01"
    id: light_officej_onair_node_led01
    output: led01
    internal: true
  - platform: binary
    name: "officej-onair-node-led02"
    id: light_officej_onair_node_led02
    output: led02
    internal: true
    
binary_sensor:
  - platform: gpio
    pin: 
      number: GPIO18
      inverted: true
      mode: INPUT_PULLUP
    name: "Office J On-Air switch"
    id: "switch_officej_onair"
    on_press:
      - mqtt.publish:
          topic: officej/onair/switch
          payload: "on"
      - logger.log: "Office J On-Air"
    on_release:
      - mqtt.publish:
          topic: officej/onair/switch
          payload: "off"
      - logger.log: "Office J Off-Air"
    internal: true
    filters:
      - delayed_on_off: 10ms

output:
  - platform: gpio
    pin: GPIO19
    id: led01
  - platform: gpio
    pin: GPIO21
    id: led02


This YAML will create several entities that Home Assistant can track and add to cards on the dashboard. I've added wifi signal, led status, switch status, and the module's IP address. There are more options. You need only search for them.


MQTT broker

MQ Telemetry Transport. I assume MQ is Message Queue

MQTT is an OASIS standard for IoT connectivity. It is a publish/subscribe, extremely simple and lightweight messaging protocol, designed for constrained devices and low-bandwidth, high-latency or unreliable networks. 

-MQTT.org

This project marks my first time working with MQTT and, so far, I am a fan. You can publish messages to a topic that anything on your network can subscribe to. Once that message is received by a subscriber, the subscriber can do whatever it likes with the date.


MQTT is what makes this project scalable. I can add additional subscribers to my network to react to a module going on-air.


MQTT Mosquito is recommended in many places for Home Assistant, so I chose that.

https://www.home-assistant.io/docs/mqtt/broker/


Node-RED

Getting started

Combining with MQTT Mosquitto

Most of work is handled by Node-RED. It's a visual programming language with a surprisingly shallow learning curve. My program (flow) in Node-RED uses three types of nodes several times. Five if you count testing and debugging. The Node-RED code is visual.. so I provided it in image form. See image.

For installation and use, I watched a lot of YouTube's The Hook Up channel (I promise, that's not me. I'm not promoting my own channel) as well as many others.

Add Card to Dashboard

lovelace_card.png

In Home Assistant, you can edit the dashboards to your liking. Each element on a dashboard is a card. I combined several default cards to create a super simple visual check on the on-air statuses:

type: horizontal-stack
cards:
  - type: entities
    entities:
      - entity: binary_sensor.office_j_on_air_switch_state
        name: On Air
      - entity: sensor.office_j_on_air_node_ip_address
        name: IP
      - entity: switch.officej_astro_do_not_disturb_switch
        name: Astro DND
    title: Office J
  - type: entities
    entities:
      - entity: binary_sensor.office_l_on_air_switch_state
        name: On Air
      - entity: sensor.office_l_on_air_node_ip_address
        name: IP
      - entity: switch.officel_rosie_do_not_disturb_switch
        name: Rosie DND
    title: Office L

Note: This won't work for you if you've changed entity names.

Additional note: you probably want to change the entity names. When you do, and I know you will, you'll want to update the above YAML to reflect the new entity names.

...

...entity names!


The UI for creating and editing cards is more fun and you can use the dropdowns to select available values once they exist in the system. Values for what? That's right, entity names.

Solder Components

Schematic_bb.png

The specific pins aren't as important as making sure the pins used match the pins programmed in ESPHome. Solder this circuit keeping in mind that it will need to fit withing the container. There's a decent amount of room in the container to fit everything, so it shouldn't cause any trouble.

Print and Assemble Container

The only part of this project that is genuinely 100% mine! Be kind in the comments about my 3D modeling.

Features

  • Lid is designed to need (2) screws (and nuts) on only one side and still not pop off when flipping the switch! That was a correction made after version one.
  • Cord relief built into the main box.
  • Cord lid is meant to try and pinch the end of the USB power cord into place so it won't move around so much. Gotta protect that delicate USB port on the ESP board.
  • There are two long slits in the bottom of the box. These are for the GPIO headers from the ESP board. It arrived with them already soldered in and I didn't want to remove them. The slits also provide some placement guidance and a way both access the pins for funsies and help remove the board from the box when someone ahem hot glues the board down in version one. Version two (this one) does not have any issues with the board shifting around, thanks to the cord relief section in the back and more accurate tolerances.

Assembly

There are inset spots for the M3 nuts. The easiest way to get your nuts in there is to use the "screw pulling technique".

The cord lid pushes into place.

The screw pulling technique:

  • Leave the lid aside for now.
  • Put an M3 screw in the internal screw hole.
  • Hole nut in place at the end of the screw with finger (your choice).
  • Use M3 hex key to turn screw and hopefully "catch" nut.
  • Once nut is "caught" by the screw, just prevent it from turning.
  • Continue to screw the screw with the hex key. This will pull the nut up and eventually into its place.
  • If you can't turn the hex key any more, it's probably in place.
  • Unscrew the screw. The nut should remain without needing to be held.
  • Ta-da!

After installing the nuts, you can close the lid and screw down the screws. There's a lip on the lid, make sure that is completely inside the box when closing. The lid's lip is the secret ingredient that prevents the lid from popping off during switch use.

Plug USB power cord into any powered USB port and flip the switch. You should see that your switch module shows up on the dashboard and has an IP address.