The Raspberry Pi is undeniably reinventing the way we compute. With a plethora of advanced technology crammed on a board little larger in height than a two pence coin, it’s an incredible piece of technology. It’s a case of Moore’s Law in action.
To make interacting with the Raspberry Pi a great deal easier, today I’m going to be showing you how to integrate your Pi with the Pushbullet API. In turn, this will allow you to receive push notifications from your Raspberry Pi, regardless of where you are.
Firstly, locate the Pushbullet app in the App Store or Google Play. Download it.
Launch the app and sign up/sign in.
Boot up your Raspberry Pi and type the following command to launch the desktop environment
Wait for a minute or so for it to load, and then launch the Firefox web browser from the Applications menu in the top-left of the GUI. Type pushbullet.com in the address bar and hit return on your keyboard. Log in using the same credentials that you used to sign up through the mobile app.
Once you’ve logged in, Pushbullet will try to download its software to your Raspberry Pi. We don’t need this – where’s the fun in that?! We’re going to take a more programmatic approach. So go ahead and dismiss the download by clicking ‘close‘ in the top-right corner of the modal.
From the left-hand sidebar, click ‘Settings’.
From there, choose Account and scroll down for your Access Token. This is essentially a key to authenticate you (log you in) to the Pushbullet API. As such, you should not share this key with anyone.
Back in the Terminal, let’s install
curl so we can communicate with the Pushbullet API:
sudo apt-get install curl
Let’s give your access code a try, shall we? Minimise the Firefox web browser window and launch LXTerminal from the Raspbian desktop. Type the following:
curl -u [type your access code here] https://api.pushbullet.com/v2/pushes/ -d type=note -d title="Raspberry Pi" body="Hello World!"
Hit return on your keyboard and wait a couple of seconds.
Voila! You should’ve received a notification to your phone.
That’s all well and good – but it’s somewhat impractical to type that command each time we want to trigger a notification. For increased autonomy, let’s consolidate it into a bash script. To create a new bash script, type the following command into the Terminal:
sudo nano /usr/bin/notify.sh
and enter the following code in the text editor which appears:
|That first line (
Anyway, go ahead and press CTRL+X on your keyboard and type the letter ‘y’ when prompted to confirm that any content that may have been in that file previously will be overwritten. Hit enter (or return) on your keyboard after having done this. You should be back in the terminal.
The script may not be executable in its current state, so we’ll just elevate the permissions of the file. A simple chmod should do the trick:
sudo chmod +x /usr/bin/notify.sh
Cool! Let’s check it works, then we’ll hook it up with some Python. This is where the tutorial gets *really* interesting!
bash /usr/bin/notify.sh "Hello world" "This is my first notification from the Pushbullet API"
You should now have received a notification on your phone.
To execute this script in your Python projects, simply use the built-in
os.system() function. This function accepts an argument in which you can dictate a command to be executed in the terminal.
For example, you could have this in foo.py:
os.system('/usr/bin/notify.sh "Hello world" "This is the text of my notification"')
A common error to be made here is to mix up single quotes (‘) with double quotes (“). Make sure to encompass the entire command in single quotes, and the arguments for the bash script in double quotes, or vice versa. But you must be consistent or you’ll get an error!
In the code above, the first argument (referred to as $0 in the script) is the script’s name and location. The second ($1) is the title of the notification. The third and final one ($2) is the body of the notification (the main bit).
Well, that’s all folks! I really hope my tutorial was informative for you.