Valentine's day is coming up! It's time to start thinking about what you are going to get for that special somebody. But everything just feels so played out! Flowers, chocolate, jewelry, it's all just so gauche you say.
No, this Valentine's day you want to give them something special. Something that comes from the heart. Something personal you've built with your own blood, sweat and tears! And something cheap, because you just spent all your money on GameStop stock. Well you're in luck, because with just a raspberry pi and a few spare parts I'm going to teach you how to build your SO their very own attention button. This magical button will, at their simple press, summon you for whatever type of attention your special someone needs. Sound great, right?
Let's get started!
Parts needed
- Raspberry Pi - https://www.raspberrypi.org/products/raspberry-pi-4-model-b/
- Raspberry Pi Power Supply (if one didn't come with your kit) - https://www.raspberrypi.org/products/type-c-power-supply/
- Breadboard - https://www.amazon.com/DEYUE-breadboard-Set-Prototype-Board/dp/B07LFD4LT6
- Breadboard cables - https://www.amazon.com/EDGELEC-Breadboard-Optional-Assorted-Multicolored/dp/B07GD2BWPY/132-5436773-5950636?psc=1
- Breadboard buttons - https://www.amazon.com/Tactile-Momentary-Assortment-Kit-200-Switches/dp/B0723BG637
Assembly
Set up your raspberry pi! See https://projects.raspberrypi.org/en/projects/raspberry-pi-getting-started for instructions. You will likely need to be able to hook up your Pi to a keyboard, mouse and monitor to get this to work. Once you've booted to the Raspbian desktop you're good to go.
Hooking up Pi
Now, you'll need to hook up your Raspberry Pi to your push button. This is pretty simple to do! Get out your breadboard, push button, and two male-to-female breadboard cables.
On the raspberry pi, connect your cables to ground (pin 6) and GPIO 2 (pin 3). See https://pinout.xyz/# for details.
Note: You can actually use any GPIO button you want, I just went with GPIO 2
Hooking up breadboard
Now, take those cables and connect them to your breadboard. They should be placed vertically, one spot apart on the column, and then the button should be placed on the same rows as the two cables.
See this diagram I borrowed from here. Ignore the placement of the gpio pin, just pay attention to the breadboard.
And this image:
And that's it! You now have your hardware wired up!
Code
You can either write this code directly on the Raspberry pi using the text editor and terminal, or you can SSH into your Pi and use a terminal on a different computer. To do that, first find the IP of your pi using hostname -I
and then on another computer ssh pi@IP_ADDRESS
and enter your password. I highly recommend using something like VS Code SSH to develop on Pi.
Registering button press
The code we will write will be in Python. Let's start with just getting the button to register. We will be using the gpiozero library to do this:
import time
from gpiozero import Button
button = Button(2)
while True:
if button.is_pressed:
print('button pressed!')
time.sleep(0.2)
Wow, that was easy! If you've used a different GPIO pin then I did (remember to check https://pinout.xyz/# !) just use that number in the place of where I wrote "2". Save your file as attn_button.py
and run your code with python attn_button.py
(or python3 attn_button.py
if needed).
Now press the button on your Pi and watch the text appear in your terminal! You've got your button wired up!
Sending a notification:
A button is no good if it doesn't do anything, right? So now we need the button to ping your phone whenever the user your special smookums smashes that button. I decided to leverage the twitter API for this as it made it easy to send notifications to my phone, but there are limitless ways to go about this. Try experimenting with something like https://ifttt.com/ or even developing your own Android or iOS app if you want to get creative!
The way I did it was by creating a new twitter account which then will DM my main twitter account (see how I snuck that in there) whenever the button is pressed.
Setting up the twitter API
Head to https://developer.twitter.com and set up the API on whatever account will be sending the DM. You then will need to create a new project from the dashboard:
This will lead you through a quick process.
Once you finish, you will need to copy your API key and API secret key to your python file:
twitter_api_key = 'KEY'
twitter_api_key_secret = 'SECRET_KEY'
Then, head to the "app settings" of the app that was created and click the "keys":
Click "generate" next to access token and secret and copy those over to your python script as well:
twitter_access_token = 'ACCESS_TOKEN'
twitter_access_token_secret = 'ACCESS_TOKEN_SECRET'
Almost there, I promise! Now, install the python twitter api wrapper called tweepy by running pip install tweepy
or pip3 install tweepy
and add import tweepy
to the top of your file.
Finally, get the ID of the twitter account you will be sending the message to using https://tweeterid.com/. Obviously, you will need to be logged into this account on a mobile device you carry around and have notifications for direct messages turned on.
Now, here is your final code:
import time
from gpiozero import Button
import tweepy
twitter_api_key = 'API'
twitter_api_key_secret = 'API_SECRET'
twitter_access_token = 'ACCESS_TOKEN'
twitter_access_token_secret = 'ACCESS_TOKEN_SECRET'
button = Button(2)
while True:
if button.is_pressed:
print('button pressed!')
auth = tweepy.OAuthHandler(twitter_api_key, twitter_api_key_secret)
auth.set_access_token(twitter_access_token, twitter_access_token_secret)
api = tweepy.API(auth)
api.send_direct_message('TWITTER_ACCT_ID', 'Yo! Your SO needs attention! Get to it!')
time.sleep(1)
time.sleep(0.2)
Phew! That was a lot, but now we are ready! Take a read through this code and you'll find it's pretty self explanatory. tweepy.OAuthHandler
and auth.set_access_token
get access to twitter, and send_direct_message
sends a message. The time
functions are just there to prevent the code from sending a whole bunch of DMs at once. Easy, right!
Go ahead and run the script and give it a test, with the script running, you should be able to press the button and very quickly get a ding on your phone indicating a twitter DM has just arrived. That's it!
Keeping the script running.
Now, one last thing, you've gotta make sure the script stays running all the time. There's an easy way to do this. You are going to use linux's screen command.
In an SSH'd terminal to your Pi: screen -S attn-button
then run your file with python attn_button.py
. That's it! You can close the terminal and your python script will still be running.
Congrats, you're done!
You now have a working attention button. Nice work! Your special someone is going to love it.
What's next?
Why not try adding multiple buttons that can send multiple messages? Maybe try 3d printing a more professional looking button then the sketchy looking breadboard? There are all kinds of ways to extend this project.
Thanks for reading!
Top comments (0)