How to use a headless Raspberry Pi with GPIO to call webhooks

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

for the sound sensor you can use the following source for example:

import RPi.GPIO as GPIO
import time
import subprocess
from time import sleep

channel = 17
GPIO.setup(channel, GPIO.IN)

def callback(channel):
        if GPIO.input(channel):
                print "Sound Detected!"
                subprocess.Popen("lynx -accept_all_cookies -dump", shell = True)

                print "Sound Detected!"
                subprocess.Popen("lynx -accept_all_cookies -dump", shell = True)

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:

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", shell = True)

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:


Great work! Thanks for sharing!

Great idea!
Some things you could improve is to use MQTT locally (that way if internet it will still work).
You could also have the counting in the Python script in your Raspberry Pi to save you some flow in Homey.

But again, nice example in usable home automation! :+1:

Nice project, but the raspberry pi is kinda overkill. Take a look at Homeyduino and a Esp8266 (like a cheap wemos d1 mini) if you want to do more projects like these. Way cheaper and energy efficient than a Raspberry pi. It’s also a direct connection to Homey, no cloud necessary.

Or ESPeasy is also usable for this.

1 Like

Thanks for your input!