Outside of home COVID-19. Inside at home COVID-16. Whole world is fighting against COVID-19. But for COVID-16, I am all alone. I am tired of saying "please stay away from the screen". So thought of this concept project to avoid brain infection at home.
By the way, I call two of my kids (age 1 and 6) as COVID-16 as they are equally out of control.
Project Concept
Circuit built using microcontroller (ESP32) and ultrasonic distance censor (HC-SR04) will detect the distance of the nearest obstacle. If the measured distance is less than the preconfigured threshold (say 1 meter), microcontroller will call smart TV API (e.g. in my case Roku TV API to toggle Play/Pause http://$ROKU_DEV_TARGET:8060/keypress/play
) to perform the desired activity.

Note: For my Roku TV, API is there to trigger different remote button functions.
Tools,Technologies and Components used in this article
- Thonny, Python IDE for beginners
- MicroPython Firmware on the ESP32
- Roku TV External Control Protocol (ECP)
- MicroPython Libraries:
- Hardware Components:
- ESP-WROOM-32 Dev Kit
- HC-SR04 Ultrasonic Sensor
- Oled display 128x64 - Optional
- Breadboard
- 3.3v & 5v Breadboard Power Supply
- Breadboard Jumper Wires
- 1kΩ & 470Ω Resistors
- Less Smart TV (Roku)
Build the Circuit
Connect all the components as shown in the diagram below.
Schematic Diagram:

Circuit Design:

Code
- Connect ESP32 development board to your computer.
- Open Thonny Python IDE.
- Select Run --> Select interpreter...
- Choose MicroPython (ESP32) as device and corresponding port.
- Upload following python libraries
- Upload the below "main.py" (vanilla version). Feel free to update as per your requirement. For my Roku TV Rest endpoint to simulate "Play/Pause" remote button is
http://192.168.0.11:8060/keypress/play
Roku keypress commands:'home', 'rev', 'fwd', 'play', 'select', 'left', 'right', 'down', 'up', 'back', 'replay', 'info', 'backspace', 'enter', 'volumeDown', 'volumeUp', 'volumeMute', 'inputTuner', 'inputHDMI1', 'inputHDMI2', 'inputHDMI3', 'inputHDMI4', 'inputAV1', 'channelUp', 'channelDown'
import wifimgr
from hcsr04 import HCSR04
from machine import Pin,I2C
import ssd1306,time
import urequests
def init():
i2c = I2C(scl=Pin(19), sda=Pin(18))
oled = ssd1306.SSD1306_I2C(128, 64, i2c, 0x3c)
sensor = HCSR04(trigger_pin=32, echo_pin=35, echo_timeout_us=1000000)
return oled, sensor
def log(msg, x, y):
oled.text(msg, x, y)
oled.show()
def clearDisplay():
oled.fill(0)
def toggleTvPlayPause():
try:
response = urequests.post("http://192.168.0.11:8060/keypress/play")
print("API Response %s" % response.status_code)
return response.status_code == 200
except:
print("Error !!!")
log("Error !!!", 0, 40)
return False
# Get OLED display & Distance sensor
oled, sensor = init()
# Connect to WIfi
log("Connecting...", 0, 0)
wlan = wifimgr.get_connection()
if wlan is None:
log("No wifi !!!", 0, 20)
print("Unable to connect to Wifi")
else:
log("Connected :-)", 0, 20)
wifimgr.deactivate_ap()
print("Deactivated AP mode.")
time.sleep_ms(1000)
prevDistance = -1
isPaused = False
while True:
currDistance = int(sensor.distance_cm())
time.sleep_ms(250)
if currDistance != prevDistance:
print("Distance: %s cm, isPaused: %s" %(currDistance, isPaused))
clearDisplay()
log("Dist: %s cm" % currDistance,0, 0)
prevDistance = currDistance
if currDistance < 100:
if not isPaused and toggleTvPlayPause():
isPaused = True
else:
if isPaused and toggleTvPlayPause():
isPaused = False
print("TV -> %s" % ("Pause" if isPaused else "Play"))
log("TV -> %s" % ("Pause" if isPaused else "Play"), 0, 40)
- Don't update "boot.py" unless want to do anything specific interesting.
Note: I have used web based connection manager (WiFi Manager) to setup Wifi Connectivity in ESP32. This will save wifi usernamee & password in "profile.dat" (for me NETWORK_PROFILES='profile.dat'
in wifimgr.py).
Demo
Due to home isolation, screen time got increased manifolds. I was really worried. But initial version seems working for them.
Circuit In Action

Once they will figure out the trick (if they manually flip the play/pause using remote, it may work in their favour as Roku API http://192.168.0.11:8060/keypress/play
only toggles the button), I may need to think about next version.
Video Demo:
Possible Extension
Following four may be good addition to enhance this project.
- Buzzer to make noise along with "Pause" as an additional cue.
- ESP32-CAM for face detection / recognition / emotion / gesture based action.
- PIR motion sensor to pause or switch off TV, if nobody is watching. (Kid's Eye and Energy Safe Smart and Green TV - Part 2)
- Post analytics in cloud and visualize kid's behaviour.
Download SrcCodes
All codes used in this post are available on Github: srccodes/smart-tv-distance-sensor.
Comments