LearnRaspi

Author: Joel Cieslar

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.

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

In this tutorial I aspire to provide a simple, easy-to-follow tutorial on how to install Minecraft: Pi Edition on your Raspberry Pi. For this tutorial, you will need the following resources:

  • A Raspberry Pi (we used Model B 512MB.)
  • An SD card with the Raspbian operating system installed
  • A mouse and keyboard
  • A compatible power cable
  • An Ethernet cable
  • A monitor cable (HDMI or composite video)
  • And a monitor to code & play on

If you don’t have the aforementioned equipment, the Kano kit is the perfect cost-effective kit to get started with the Raspberry Pi. You can get yours at www.kano.me.

Note: in addition, you may also need a mains-powered USB hub to prevent overloading the Pi electronically with peripherals. Let’s get started…

1.) Firstly, you need to boot up your Raspberry Pi, set it up via raspi-config to your liking, and then type:

startx

…into the Terminal to boot up the desktop environment. From the desktop environment, select LXTerminal. A terminal window will open. This step is important because these instructions do not work in the normal Terminal that you see upon booting.

2.) In LXTerminal, type the following code to update & upgrade your Raspberry Pi. It’s good practice to do this regularly.

sudo apt-get update
sudo apt-get upgrade

3.) Now you’re all up-to-date, you can go ahead and install the Minecraft: Pi Edition files.

wget https://s3.amazonaws.com/assets.minecraft.net/pi/minecraft-pi-0.1.1.tar.gz

4.) Once the download has completed, you should unzip and extract the files.

tar -zxvf minecraft-pi-0.1.1.tar.gz

5.) To open, navigate to the mcpi directory:

cd mcpi

And to open the executable file:

./minecraft-pi

 

Minecraft gameplay on a Raspberry Pi

Minecraft gameplay on a Raspberry Pi

 

Have fun with your new Raspberry Pi Minecraft: Pi Edition installation!

Raspberry Pi Webcam Stream Server Tutorial (Live Streaming)

Raspberry Pi Live Stream

Raspberry Pi Live Stream

One of the more popular Raspberry Pi projects is turning your board into a webcam server capable of hosting a live stream. Most people would utilize this as a CCTV system (I plan to) however others will have unique uses. If you have some weird yet wonderful idea, be sure to let me know about it! I think this is accomplishable for about £40/£50, however I used a few bits and pieces that I had lying around and was able to put this together for practically nothing. I used a Logitech Quickcam webcam for this project.

What You’ll Need:

  • A Raspberry Pi (obviously)
  • An SD Card (about 8GB)
  • A webcam
  • An Ethernet Cable or a Wi-Fi Adapter (Wireless Adapter) and Internet Access
  • A powered USB hub
  • A keyboard
  • A mouse (if you chose a WiFi Adapter)
  • Okay, so first we’re going to need to get a fresh install of Debian “Wheezy” onto an SD Card – this is obtainable from the Raspberry Pi website for free. You can put this onto an SD Card using Win32DiskImager for Windows or there’s an alternative app for Mac users that can do this for you.

    Once you’ve done that, insert the SD Card into your Raspberry Pi. Connect either an Ethernet Cable or your Wireless Adapter to your board and the Keyboard. Oh, and don’t forget your externally-powered USB hub.

    Turn on your Raspberry Pi, set the time zone according to where you live from the Raspi-Config menu and then hit Finish.

    Log in if prompted.
    Username: pi
    Password: raspberry

    Before you do anything, you should update your password from the default to prevent intruders. To do this, you should type sudo passwd into the command line. Type a new UNIX password, and you’re ready to go!

    – IGNORE BELOW IF USING AN ETHERNET CABLE –
    If you want to set up your wireless adapter and connect to WiFi in the desktop environment, you’ll need to type in the following to the terminal:

    startx

    You can then set up your wireless connection here. Once finished, click the menu in the bottom left and then ‘Log out’ to return to the command line.
    – IGNORE ABOVE IF USING AN ETHERNET CABLE –

    Now you need to upgrade your system. At the command line, type:

    sudo apt-get update
    sudo apt-get upgrade

    And wait for that to finish. Now we need to install Motion – this is the streaming software that makes everything work with a build in web server. For that, please enter:

    sudo apt-get install motion

    And wait. After installation, please plug in your web cam via the powered USB port. Without the external power boost, your Pi may not be capable of powering the webcam on its own.
    You need to edit the Motion configuration file to make it all work, so enter the following code in the terminal:

    sudo nano /etc/motion/motion.conf

    Your configuration file will appear, and you can change a few settings here if you like. Most importantly though, we need to change the following:

    daemon OFF (to ON – this can be changed near the top of the file)
    webcam_localhost ON (to OFF – this can be changed near the bottom of the file)
    control_localhost ON (to OFF – located just below the above setting)

    Do Ctrl + X to save, type ‘y’ to save save modified buffer, and press enter to confirm the file name.

    Now you need to enable the Daemon to start by typing:

    sudo nano /etc/default/motion

    …and changing start_motion_daemon=NO to start_motion_daemon=yes

    Now we need to start the Motion server:

    sudo service motion start

    Wait for about 60 seconds and then navigate to your Raspberry Pi’s internal IP address (visible on startup of Pi) in your web browser. The latest versions of Firefox work best.

    It should look something like this: 192.168.X.X:8081

    Make sure you include port 8081 at the end, as this is where your webcam images will appear. You can configure your setup in a web-based interface from now on by connecting to port 8080 instead of port 8081.

    Port 8081 – video
    Port 8080 – web configuration interface

    Note: this is only visible on your own WiFi network. If you attempt to view it on a different Internet Connection, it will not work. You can enable Port Forwarding in your router’s GUI to enable global viewing.

    Congratulations! You’ve just made your own Raspberry Pi Webcam Server!

    Troubleshooting Guide

    Does your stream give a grey screen with an error relating to your webcam?
    Type ls /dev/video* into your terminal to find out the name of your video device. The default in the /etc/motion/motion.conf file is normally video0 so if you get a different output then it may require changing. Restart the Motion software to apply changes.

    How to connect to Raspberry Pi remotely using SSH

    Connecting a monitor, mouse and keyboard to your Raspberry Pi can be tedious. If you’re running just a couple of Terminal commands, you can execute commands on your Raspberry Pi from a Windows or Mac computer using an SSH client.

    The first thing you need to do is find out your Raspberry Pi’s internal LAN IP address on your network. Chances are you can view a list of connected devices in your router’s GUI.

    Check the back of your router – it should have instructions on how to log in to this.

    As you can see, my internal IP address for the Raspberry Pi is 192.168.31.241. This IP address is specific to your Raspberry Pi and each device connected to your WiFI network will have one similar to this. Yours will be different to mine. It will always begin with 192.168.XX.XXX. You wouldn’t be able to connect to your Pi using this IP address if you were on a different WiFi network, for instance your neighbour’s, or from an Internet Cafe.

    Now, let’s connect to the Raspberry Pi.

    Windows

    Windows does not ship with an SSH client by default, so you’ll have to download one. I always recommend PuTTY, which you can download from here.

    To be continued.

    Mac OS X

    Lucky you! Mac OS X has a built-in SSH client, so you don’t have to download any additional software. Click the magnifying glass in the top-right of your screen next to the time and Spotlight will open.

    Type ‘Terminal’ and hit enter (or return) on your keyboard.

    A screenshot of Spotlight on OSX

    The Terminal will open. Type in the following command to connect:

    ssh [USERNAME]@[IP ADDRESS]

    Obviously, substitute [USERNAME] for the Raspberry Pi user you’d like to connect to and [IP ADDRESS] for your internal IP address that we found earlier in the tutorial.

    The default user on Raspbian has username pi and password raspberry – so if it’s a fresh install, chances are this will work for you.

    Connecting to a Raspberry Pi in the Terminal on Mac OSX using SSH

    You should now be logged in to your Raspberry Pi and able to execute commands. Have fun! 🙂

    Copyright © 2017 LearnRaspi

    A little project by joelci.esUp ↑