Recently I had an idea that I wanted to explore: what if I could build a real button and make it trigger ANYTHING using simply an URL (webhook)? In this very quick post, I will walk you through how I managed to build this project in one evening:
See it in action:
To begin with, I needed an actual physical button (this seems obvious right?). Luckily, I had already ordered on Amazon an "emergency button" a couple of weeks ago. I stumbled upon this cool button and decided to buy in case someday I needed to build something with it. It seems I was right!
Of course, you can have any other button, just make sure it has a latching switch (it's more realistic this way!) but feel free to take any other kind of switches.
I am a big fan of the Onion Omega boards. I actually do have a lot of them (you know, just in case). And I use them in each one of my IoT projects, such as my "voice-controlled standing desk" hack.
For this setup, things are going to be way easier, since we only have 2 wires to connect (solder, coming from the button's switch and going into the Omega2+ pins.
Here is the Fritzing diagram:
If you want to know more about the Onion Omega2+ board, here is a great getting started tutorial.
One thing (which is not mandatory) is to use an external Wifi antenna for the Onion Omega2+ board. This is just in case we encounter any signal loss due to the surface-mounted (SMT) antenna.
For my setup, I used a 6dBi antenna that was lying around inside my toolbox. Just make sure your antenna is compatible with the u.FL connector. If you need more information about how to connect this kind of antennas, I highly recommend reading the Onion guide.
Finally, we need to connect the micro USB cable to the Omega2+ (to power the board) and get everything tightly positioned inside the button's box:
When it comes to the software part, and as I mentioned this is an easy setup, we can really choose any technology (python, node...) that can communicate with an HTTP endpoint. S for this quick DIY project, I just wrote a quick Shell script to take care of the button behavior. I've added the comments inside the code:
What this code does is basically start a loop and listen continuously for the
$PIN state change. When we press the button, we activate the switch which connects the 2 wires altogether and closes the circuit. When this happens, the Omega 2+ board receives a high voltage (3.3v) in
$PIN. Using the
fast-gpio included package, we can read the state.
- A value of
0means the button is pressed.
- A value of
1means the button is released.
The rest of the logic is only there to deal with press and lock mode of this particular button, and this is because this button is using a latching switch.
Now, you may be wondering what can you do with this button? Well, to be honest, there are endless use case scenarios to use this button for but basically, any service that exposes an HTTP endpoint (deploy URLs, a serverless API...etc) would be a great candidate!
In case you want to start playing around and hook this button to an API, you can try out the Azure Functions which let you quickly create and deploy your application logic to a serverless environment, see how you can do it.
Let me know in the comments below what would be your use (be creative)?