LearnRaspi

Get notifications from a Raspberry Pi with Pushbullet

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.

Pushbullet app on the Google Play Store

Pushbullet app on the Google Play Store

Launch the app and sign up/sign in.

Boot up your Raspberry Pi and type the following command to launch the desktop environment

startx

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’.

Pushbullet sidebar screenshot

Pushbullet sidebar screenshot

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.

Pushbullet Dashboard - API Access Keys

Pushbullet Dashboard – API Access Keys

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:

Lightbulb icon That first line (#!/bin/bash) is referred to colloquially as the ‘shebang’ – it’s basically the initiation of a bash file.

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:

import os
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.

Categories: Tutorials, Uncategorized

« How to install Raspberry Pi Minecraft: Pi Edition (Tutorial)

7 Comments

  1. I have tried to do this on my Raspberry pi, but nothing happened. I didn’t receive a notification and I couldn’t even do the other option to receive a notification. Is there something I missed in the coding?

  2. Or does this only play a role when trying to reformat the drive under
    Windows?

  3. Whenever I type in the curl command It always asks for a host password, what do I do then?

  4. It works with:
    curl –header ‘Access-Token: ‘ –header ‘Content-Type: application/json’ –data-binary ‘{“body”:”Hello World”,”title”:”Raspberry Pi”,”type”:”note”}’ –request POST https://api.pushbullet.com/v2/pushes

Leave a Reply

Your email address will not be published.

*

Copyright © 2017 LearnRaspi

A little project by joelci.esUp ↑