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

**URL:** <https://community.homey.app/t/how-to-use-a-headless-raspberry-pi-with-gpio-to-call-webhooks/30970>\
**Category:** Tutorials\
**Tags:** tools, devices, flow\
**Created:** [June 7, 2020, 2:11pm UTC](https://community.homey.app/t/how-to-use-a-headless-raspberry-pi-with-gpio-to-call-webhooks/30970 "2020-06-07T14:11:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![r\_braber](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/r_braber/32/16844_2.png) [@r\_braber](https://community.homey.app/u/r_braber)\
**Post date:** [June 7, 2020, 2:11pm UTC](https://community.homey.app/t/how-to-use-a-headless-raspberry-pi-with-gpio-to-call-webhooks/30970/1 "2020-06-07T14:11:50Z")

</div>

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.

 ![Small tonka](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/1/3/131ce16206e02a0ba662c0c34faff4eb80785b0b.jpeg)  
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

 ![Parts](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/2/8/280daf494e41ae2a99d9da5c2c17332853d289a6.png)

I flashed the MicroSD with Raspbian OS, and did a headless install. A good tutorial how to do this can be found [here.](https://desertbot.io/blog/headless-raspberry-pi-3-bplus-ssh-wifi-setup) 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.](https://magpi.raspberrypi.org/articles/ssh-remote-control-raspberry-pi)

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](http://developer.athom.com/tools/system).

After you did those steps you need to let the python scripts to AutoStart at boot. How to do this can be found [here.](https://www.raspberrypi-spy.co.uk/2015/02/how-to-autorun-a-python-script-on-raspberry-pi-boot/)

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.

 ![star proces](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/d/7/d7fa2cf4589ed6f9b23bd144bc0e66a13d972bc2.jpeg)  
 ![overzicht](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/2/9/29bc0ca0ea481e51c8fdcaf6fa03d6454cc89692.png)

For more info about GPIO you can look [here.](https://www.raspberrypi.org/documentation/usage/gpio/) 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:

---

<div class="post-metadata">

**Author:** ![Maurice](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/maurice/32/26294_2.png) [@Maurice](https://community.homey.app/u/Maurice)\
**Post date:** [June 9, 2020, 8:06pm UTC](https://community.homey.app/t/how-to-use-a-headless-raspberry-pi-with-gpio-to-call-webhooks/30970/2 "2020-06-09T20:06:29Z")

</div>

Great work! Thanks for sharing!

---

<div class="post-metadata">

**Author:** ![Robin\_van\_Kekem](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robin_van_kekem/32/8187_2.png) [@Robin\_van\_Kekem](https://community.homey.app/u/Robin_van_Kekem)\
**Post date:** [June 16, 2020, 11:57am UTC](https://community.homey.app/t/how-to-use-a-headless-raspberry-pi-with-gpio-to-call-webhooks/30970/3 "2020-06-16T11:57:48Z")

</div>

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! 👍

---

<div class="post-metadata">

**Author:** ![Satoer](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/satoer/32/38113_2.png) [@Satoer](https://community.homey.app/u/Satoer)\
**Post date:** [July 10, 2020, 6:13am UTC](https://community.homey.app/t/how-to-use-a-headless-raspberry-pi-with-gpio-to-call-webhooks/30970/4 "2020-07-10T06:13:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JPe4619](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jpe4619/32/9017_2.png) [@JPe4619](https://community.homey.app/u/JPe4619)\
**Post date:** [July 10, 2020, 11:10am UTC](https://community.homey.app/t/how-to-use-a-headless-raspberry-pi-with-gpio-to-call-webhooks/30970/5 "2020-07-10T11:10:01Z")

</div>

Or ESPeasy is also usable for this.

> **[ESP Easy](https://homey.app/en-us/app/nl.joolee.homey.espeasy/ESP-Easy/)**
>
> Build your own sensors

---

<div class="post-metadata">

**Author:** ![r\_braber](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/r_braber/32/16844_2.png) [@r\_braber](https://community.homey.app/u/r_braber)\
**Post date:** [July 10, 2020, 2:29pm UTC](https://community.homey.app/t/how-to-use-a-headless-raspberry-pi-with-gpio-to-call-webhooks/30970/6 "2020-07-10T14:29:43Z")

</div>

Thanks for your input!
