For our son (2years) we converted a nice Tonka Mighty Truck into a lamp. Because he like to have some light in his room as he goes to sleep, the idea was born to use the truck as a night lamp … So I bought a dimmable powerplug (TKB Home) and made a flow so the truck goes on and off with the main light and makes the truck into dim mode at 19:00 and turned off after 45min.
Recently my son woke up regularly and started crying. When we turned on the night light, everything was fine again. I therefore wanted to connect a sound sensor, but I noticed that these are not / badly available. That is why I started my own little DIY project, i bought:
Raspberry Pi Zero W: € 15
MicroSD card: € 3
Sound sensor: € 1
Pulse push button € 2
I flashed the MicroSD with Raspbian OS, and did a headless install. A good tutorial how to do this can be found here. The Pi runs two Python scripts that automatically start when booting the Pi.
A very detailed manual on how to connect to a Pi over SSH you can find here.
The sound sensor and push button are connected to the GPIO pins of the Pi and call a Homey “webhook” via a Lynx Browser (command line browser). To install the Lynx browser you make an SSH connection and enter:
sudo apt-get install lynx
After this step I created two python files, to do this over SSH you enter:
sudo nano YOURFILENAME1.py
for the sound sensor you can use the following source for example:
#!/usr/bin/python import RPi.GPIO as GPIO import time import subprocess from time import sleep #GPIO SETUP channel = 17 GPIO.setmode(GPIO.BCM) GPIO.setup(channel, GPIO.IN) def callback(channel): if GPIO.input(channel): print "Sound Detected!" subprocess.Popen("lynx -accept_all_cookies -dump https://YOUROWNCLOUDID.connect.athom.com/api/manager/logic/webhook/TRIGGER?tag=on", shell = True) sleep(3) else: print "Sound Detected!" subprocess.Popen("lynx -accept_all_cookies -dump https://YOUROWNHCLOUDID.connect.athom.com/api/manager/logic/webhook/TRIGGER?tag=on", shell = True) sleep(3) GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300) # let us know when the pin goes HIGH or LOW GPIO.add_event_callback(channel, callback) # assign function to GPIO PIN, Run function on change # infinite loop while True: time.sleep(0.3)
for the pushbutton you repeat he step to create a Python file, you can use the following source for the pushbutton:
import subprocess from gpiozero import Button from time import sleep button = Button(2) while True: if button.is_pressed: print("Button pressed") subprocess.Popen("lynx -accept_all_cookies -dump https://YOUROWNCLOUDID.connect.athom.com/api/manager/logic/webhook/TRIGGER?tag=button", shell = True) sleep(5) else: sleep(0.5)
As you can see in the sourcecode you need your own Cloud ID, you can find your own ID here.
After you did those steps you need to let the python scripts to AutoStart at boot. How to do this can be found here.
Now it’s time to put everything together. I created a star-shaped piece of MDF so it matches my sons bed. I covered it in a piece of vinyl for decorative purpose, I noticed no difference in the output of the sound sensor.
For more info about GPIO you can look here. When everything wired up we can start making Flow’s. Since I am a absolute newbie to Homey, I guess my flows can be much more efficient, but it works for now…
To prevent the lamp from turning on with every sound (bumps, yawning, etc.) I made a flow that causes it to add +1 to a variable, this variable is reset to 1 every minute. The lamp turns on as the variable equals 3, this way at least three sounds must be detected within an minute to activate the light.
This is one of the flows I use for the buttons: