Building a Sleep Training Light with ESP8266, ESPHome, & Home Assistant

My kids, specifically the eldest, loves to wake up before 7. My wife and I, do not like to wake up before 7. My kids can’t read the time yet or understand why every month the sun seems gets up at a different time.

Our solution to help stop this was a sleep training clock: a visual way to differentiate sleeping time with awake time. Online they run about $40 and, in my opinion, are just over the top with features and buttons. A simple scheduled LED was all we were after. So I built one with an esp8266, ESPHome, & HomeAssistant:

Requirements:

Wiring it Together

I’ve just moved to using an iPad as my primary personal computer so I no longer have Fritzing, so my crude drawing in Notes.app will have to do:

Common anaode RGB wiring to an ESP8266, pins D5, D6, D7.

Setting up ESPHome Module

In Home Assistant follow the ESPHome Add Module wizard filling in info about your ESPHome and WIFI networking settings. Editing the file once complete, mine looks like:

esphome:
  name: sleep_training_light
  platform: ESP8266
  board: esp12e

wifi:
  ssid: !secret wifi_name
  password: !secret wifi_pass
  manual_ip:
    static_ip: !secret sleep_training_light_ip
    gateway: !secret ip_gateway
    subnet: !secret ip_subnet

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "sleep-training-light"
    password: !secret fallback_pass

# LED
# @see https://community.home-assistant.io/t/inverse-rgb-led-values/170069/3
# common Anode
light:
  - platform: rgb
    name: "Sleep Training Light"
    red: output_red
    green: output_green
    blue: output_blue  
    
output:
  - platform: esp8266_pwm
    id: output_red
    #D6
    pin: GPIO12
    inverted: true
  - platform: esp8266_pwm
    id: output_green
    #D7
    pin: GPIO13
    inverted: true
  - platform: esp8266_pwm
    id: output_blue
    #D5
    pin: GPIO14
    inverted: true

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: !secret api_pass

ota:
  password: !secret ota_pass

Add to HA

Home Assistants notification area should alert you to the new ESPHome device. You can add this entity.

Automation

At time of writing, I found the Confugration > Automattions area of Home Assistant a bit buggy, I ended up mostly setting up the automation via the File Editor. Here’s why my /config/automations.yaml looks like:

- id: sleep_training_light_night
  alias: Sleep Training Light -> Night
  description: ''
  trigger:
  - platform: time
    at: '20:00:00'
  action:
  - data:
      brightness: 80
      entity_id:
      - light.sleep_training_light
      rgb_color:
      - 162
      - 116
      - 255
    service: light.turn_on
    entity_id: light.sleep_training_light
  mode: single
- id: sleep_training_light_wakeup
  alias: Sleep Training Light -> Wake Up
  description: ''
  trigger:
  - at: 07:00:00
    platform: time
  action:
  - data:
      brightness: 0
      entity_id:
      - light.sleep_training_light
      rgb_color:
      - 0
      - 0
      - 0
    entity_id: light.sleep_training_light
    service: light.turn_on
  mode: single

This turns on the light at 8:00pm, and turns off the light at 7:00am.

Case

I designed a custom case (will update this post with a link to the thingiverse.com design shortly) for another project (IR Blaster) and reused it for this one, however I printed with clear filament and kept the RGB LED inside, giving the box a nice glow:

Create Custom IR remote w/ Home Assistant & ESPHome

I had several nagging reasons to create a single universal remote that was controllable from an app on our phones:

  • I was looking for a cool Home Assistant project, plus a project to use up some of the extra ESP8266’s I had
  • We had way too many IR remotes in our living room
  • We only ever pressed one or two button per remotes
  • Setting up for the evening using said remotes, was a juggle
  • Remotes were always stashed high and far from kids, or lost in couch cushions
  • Related to above, many of the batteries in the remotes are dangerous to small kids if somehow broken/opened
  • We always had our phones on us, but remotes were always out of reach or forgotten, always after getting comfy

Here’s what I did to build a custom universal remote that replaced all my IR remotes including my AppleTV touch remote:

Requirements

  1. HomeAssistant setup & running (mines on a rpi3b)
  2. HomeAssistant mobile App
  3. ESP8266 (or ESP32) w/ micro usb cable
  4. IR LED and IR Receiver
  5. NPN transistor (BC337)
  6. Jumper cables, or wire + soldering stuff
  7. Heat shrink
  8. Optionally for installation: long micro-usb to usb A cable, 3d printed case, Velcro to stick case to wall

Optional: Setup Apple TV to use IR

Not totally sure what interface the Apple Remote uses, but if looking to Replace Apple TV: you can map the Apple TV to listen to some arbitrary button presses from any unused remote following this doc.

Build an IR Receiver to Capture Codes

First step is to capture all the IR codes. I set up a receiver as follows:

The nice part about this layout for the IR receiver I had is I was able to just slip it into a breadboard, no wiring:

In the ESPHome section of Home Assistant:

  1. Click the big “+” add button in bottom
  2. Fill out the wizard
  3. Once created, edit the module and append to the bottom of the config:
remote_receiver:
  #D5
  pin: 
    number: GPIO14
    inverted: True
    mode: INPUT_PULLUP
  dump: raw
  idle: 25ms
Continue reading Create Custom IR remote w/ Home Assistant & ESPHome

ESP8266 NodeMCU LED Busy Server

I built a Slack status light because it looked awesome and simple, and at that time I knew nothing about DIY electronics. As I learned more, I outlined how to build a cheaper one on a Raspberry Pi. Then my friend then informed me that you can do it even cheaper with an ESP32 NodeMCU board. He was right. Here’s how I built a IoT busy light for around $5.

For this example, I opted for the older ESP8266 ESP12-E NodeMCU board, it is only $2.65 + 1.73 shipping and has everything needed. Pair that with a cheap RGB shared cathode LED $0.50-$2.00, and a few cents for resistors, for a grand total of around $5* in parts.

I won’t go into the details on the boards, but if unfamiliar it, the tl;dr is you write some C/Lua code using the Ardunio IDE, include WIFI for ESP8266 and Web Server libraries (here’s hello world example code and a great setup video) and add some handlers to toggle the GPIO pins on certain requests, then flash that code onto the board over USB. If never used before, I assure you it’s not as hard or complicated as it may sound or appear. When the board is plugged in, it fires up, the code executes, connecting to WIFI, and this tiny board becomes a server in your local network.

Here’s the wiring I did:

& here’s the code I flashed onto it (note you will need to add your WIFI credentials):

Continue reading ESP8266 NodeMCU LED Busy Server