Baking Pi’s 3: A Networked Rafigh

In the “Baking Pi’s” series of posts, I describe a number of projects with the Raspberry Pi that I have been doing in the last few weeks. In this post, I will describe how to make a networked Rafigh using a Raspberry Pi. I will first describe how to connect the Raspberry Pi to the physical world and how to control it over the Internet. Next, I will describe how to connect this setup to a small water pump and living mushroom colony to create a networked version of Rafigh. I will be mostly using Raspberry Pi 1 (and later also try out Raspberry Zero). The same approach can easily be used with Raspberry Pi B+ as well. I will also use a Mac computer to connect to the Pi via wifi.

Setting up and Testing the Circuit

Starting from a Pi with the latest Raspbian OS, I connect it to a simple circuit that controls an LED light. For information on how to initially set up your Pi, please see my previous post. To check GPIO lists and locations on different Raspberry Pi boards, see here and here.

So using an example described in the excellent Make book, Getting Started with Raspberry Pi by Matt Richardson and Shawn Wallace, I use a breadboard to connect a single LED using female to male jumper wires to the Pi. I connect, Pi’s Ground to the Ground bus of the breadboard and the negative leg of the LED, and the Pi’s pin 24 to the positive leg of the LED. Next, I use the following Python code to blink the LED:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(24, GPIO.OUT)

While True:
   GPIO.output(24, GPIO.HIGH)
   time.sleep(1)
   GPIO.output(24, GPIO.LOW)
   time.sleep(1)

I save these in a file called blink.py and run it using the following command:

sudo python blink.py

You might notice that there are some warning messages when you run this code. Specifically, when you interrupt the program with a Ctrl+C you get a warning and also when running the script you get a warning about the channel being already in use. You can get ride of these warnings by putting the loop in an exception clause, like so:

while True:
   try:
      GPIO.output(24, GPIO.HIGH)
      time.sleep(1)
      GPIO.output(24, GPIO.LOW)
      time.sleep(1)
   except KeyboardInterrupt:
      GPIO.cleanup()
      exit()
Screen Shot 2016-02-10 at 7.02.59 PM

Using the Pi to turn on and off a light

Controlling the Light over the Internet

Once you can blink the LED, it’s time to control it over the Internet! Again, I’m using ideas from the excellent book, Getting Started with Raspberry Pi by Matt Richardson and Shawn Wallace. To access the Pi over the Internet, I turn it into a server and have it load a dynamic page which you can use to control the LED.

I’ll start by installing Flask which is a Python web framework and can be used to turn the Pi into a dynamic web server. The following commands should install Flask:

sudo apt-get install python-pip
sudo pip install flask

With Flask installed, I can create a dynamic web server that can interact with the GPIO pins. First, I create a directory called rafigh to put all the code in:

sudo mkdir rafigh 
cd rafigh

Here, I create a subfolder called templates that will hold the html files that can be accessed via the server. Now, we need to write the code to run the server. It will be in a file called rafigh.py:

import RPi.GPIO as GPIO
from flask import Flask, render_template, request
app = Flask(__name__)

GPIO.setmode(GPIO.BCM)

#I'm only using 1 pin (pin 25), if you use more, you can add more pins here. 
pins = {
     24 : {'name' : 'lamp', 'state' : GPIO.LOW}
     }

for pin in pins: 
   GPIO.setup(pin, GPIO.OUT)
   GPIO.output(pin, GPIO.LOW)

@app.route("/")
def main():
   for pin in pins:
      pins[pin]['state'] = GPIO.input(pin)
   templateData = {
      'pins' : pins
      }
   return render_template('rafigh.html', **templateData)

@app.route("/<changePin>/<action>")
def action(changePin, action):
   changePin = int(changePin)
   deviceName = pins[changePin]['name']
   if action == "on":
      GPIO.output(changePin, GPIO.HIGH)
      message = "Turned " + deviceName + " on."
   if action == "off":
      GPIO.output(changePin, GPIO.LOW)
      message = "Turned " + deviceName + " off."
   if action == "toggle":
      GPIO.output(changePin, not GPIO.input(changePin))
      message = "Toggled " + deviceName + "."
   
   for pin in pins: 
      pins[pin]['state'] = GPIO.input(pin)

   templateData = {
      'message' : message,
      'pins' : pins
   }

   return render_template('rafigh.html', **templateData)

if __name__ == "__main__":
   app.run(host='0.0.0.0', port=80, debug=True)

This code will run the server and provide dynamic data to the html page. Now, it’s time create the html page, rafigh.html, that will sit in the template folder:

<!DOCTYPE html>
<head>
  <title>Current Status</title>
</head>

<body>

<h1> Device Listing and Status </h1>

{% for pin in pins %}
<p>The {{ pins[pin].name }}
{% if pins[pin].state == true %}
   is currently on (<a href="/{{pin}}/off">turn off</a>)
{% else %}
   is currently off (<a href="/{{pin}}/on">turn on</a>)
{% endif %}
</p>
{% endfor %}

{% if message %}
<h2>{{ message }}</h2>
{% endif %}


</body>
</html>

Now, if you point your browser at the Pi’s address (either you Pi’s ip address, or if you are using Bonjour, rafigh.local), you will see a webpage that allows you to turn on and off the light.

Finally, we will to connect to the networked light from outside of the local network, which allows us to control the Pi from wherever we are, as long as we are connected to the Internet! There are several ways to do this including setting up port forwarding on your router or using a web-based forwarding service liked Weaved.  This service requires you to first create a free account on the Weaved website and then to download and install it on your Pi, using the following commands:

sudo apt-get install weavedconnectd
sudo weavedinstaller

Once setup, you can initiate SSH and HTTP services on Weaved and access your Pi by logging into your account on Weaved.

Finally, you want to let the Pi know to run the script at startup. To do this, follow these instructions.

Migration to Raspberry Pi Zero

Before putting everything together, I decided to make a Raspberry Pi Zero (sometimes referred to as #PiZero) version of the project. The Pi Zero is an ultra light, low-cost (~$5!) version of the Raspberry Pi and was released in late 2015. If you don’t want to use Raspberry Pi Zero, you can skip this section.

Screen Shot 2016-02-10 at 7.03.06 PM

Clockwise from bottom right: Raspberry Pi zero, a 2×20 Male header strip, a 3D printed case, a USB OTG Cable and a Wifi dongle

To prepare the Pi Zero, I first soldered a 2×20 Male header strip to the Pi and then 3D printed an enclosure for it so that I don’t accidentally break it! With all these in place, I  cloned the SD card that I had set up in the previous steps, using the  dd command. I placed the prepared SD card in the card reader. Then, I found the SD card disk number (disk 2) by using diskutil list. Finally, I copied the the contents of the disk into an image file on the Mac machine:

sudo dd if=/dev/disk1 of=~/Desktop/2016-2-9-rafigh.dmg 

This took a while and I checked the command’s progress using Ctrl+t. Once the image was created, I copied it to a fresh SD card. Again, I placed the new card in the reader, found out what its disk number was using diskutil list (in my case it was in disk3). Next, I unmounted it:

diskutil unmountDisk /dev/disk3

Finally, I copied the image to the SD card:

sudo dd if=~/Desktop/2016-2-9-rafigh.dmg of=/dev/disk3

Again, this might take a while and you can check the progress using Ctrl+t. Once this is done, I plugged the new SD card into the Pi Zero, attached a USB OTG Cable to one of the usb ports (this allowed me to connect a wifi dongle to the Pi). Finally, I connected the Pi Zero’s GPIO pins to the circuit (Pi Zero has the same GPIO pin layout at Raspberry Pi B+ and A+) and powered up the Pi.

Screen Shot 2016-02-10 at 7.03.16 PM

Assembled Pi Zero

Once the Pi Zero is booted up, I could SSH into it, start the server and control the light as before. Voila!

Screen Shot 2016-02-11 at 1.53.12 PM.png

Off/On

If you are using Bonjour service, it might be a good idea to change the host name by following the steps here.

A Networked Light: Putting Things Together

Now, we have all the pieces together and can replace the LED light with a Powerswitch Tail 2 that can control any small AC-powered device, like a lamp or water pump.

To do this, I connect (directly or via a breadboard) the “+in” pin of the power switch to Pin 24 of the Pi and the “-in” pin to the Ground. Next, I connect (directly or via a breadboard) the power switch to a wall plug and a lamp and test by booting up the Pi and navigating to the Pi’s local address (e.g., rafigh.local or rafighzero.local) on my browser. This allowed me to turn on and off the current from my computer.

With the weaved service setup, I was able to connect to the network remotely (over the Internet rather than the local wifi network) and control the light. Next, I will describe how to connect this setup to a small water pump and a living mushroom colony to create a networked Rafigh.

A Networked Rafigh

With all the pieces assembled above, we can now create a networked Rafigh that can be controlled remotely. Rafigh is a digital living media system that can be used to motivate children to  conduct desirable activities such as using learning and therapeutic digital applications. For more information on Rafigh, see this previous post.

20150130_133533

Rafigh

To create the networked Rafigh, we need to replace the lamp with a small water pump. I have been using the Tom Aquarium Aqua Lister Pump, which I would recommend for its low power pressure (which you need, in order not to flood the mushrooms!). Next, I connect a small water container to the input valve of the water pump and connect the output valve to the mushroom colony (I’ve been using Fungaea’s The ShroomBox) using a water tube. All the electronics and the mushroom colony are placed in the custom made Rafigh enclosure.

With the system powered up, I can access the Pi remotely via my weaved account. From here, I can either activate the water pump using the dynamic website created above.

Before ending this post, I will also briefly describe how to schedule watering times using the existing setup and Linux’s crontab. First, we need to create a simple script to turn on and off the pump from the command line. I create a file called turnPumpOn.sh:

#!/bin/bash
echo Exporting pin 24.
echo 24 > /sys/class/gpio/export
echo Setting direction to out.
echo out > /sys/class/gpio/gpio24/export
echo Turning on the pump.
echo 1 > /sys/class/gpio24/value

I also create another file called turnPumpOff.sh:

#!/bin/bash
echo Turning off the pump.
echo 0 > /sys/class/gpio24/value
echo Unexporting pin 24
echo 24 > /sys/class/gpio/unexport

I put both of these files in the same rafigh directory that I created for the project before. This helps makes things organized. I make both files executable using chmod:

chmod +x turnPumpOn.sh
chmod +x turnPumpOff.sh

Now, I can turn on and off the pump using the command:

./turnPumpOn.sh
./turnPumpOff.sh

With these two scripts in place, we can schedule specific times for the pump to be turned on and off by editing the crontab file, using the following command:

crontab -e

For example, to turn the pump on for 2 minutes everyday at 7pm, we can add the following two lines to the bottom of the file:

0 19 * * * /home/pi/rafigh/turnPumpOn.sh

2 19 * * * /home/pi/rafigh/turnPumpOff.sh

For details on how to schedule tasks in the crontab file, follow this link.

Advertisements

Baking Pi’s 2: Touch HAT

In the “Baking Pi’s” series of posts, I describe my adventures with the Raspberry Pi embedded computer. Each segment describes a specific setup. This series is fairly technical and is meant to be used to create digital prototypes.

Screen Shot 2016-02-10 at 7.03.48 PM

Raspberry Pi with Touch Hat and Fruit!

In this post, I will describe my experience using Raspberry Pi with a touch sensor to create an interactive sound box. This post assumes that you have a Raspberry Pi B+ setup and connected (either to a monitor or running in headless mode). See my previous post or the official Raspberry Pi website on how to setup your Pi.

Using Pi to create an interactive sound box

A really fun activity is playing music using fruit and other conductive things (even humans)! You can do this by connecting the fruit to a computer using a touch sensor that in effect treats them as a key on the keyboard, making it possible to activate sounds when they are touched.  Some examples of how to do this easily is using a Makey Makey board or a Bare Conductive touch board. In the past, we connected an MPR121 Capacitive touch sensor to a Raspberry Pi to create TalkBox, a DIY communication board for non-verbal users. One of the challenges of putting together TalkBoxes was to connect all the wires from the MPR121 breakout board to the Raspberry Pi. Recently, capacitive touch HATs designed specifically for Raspberry Pi have become available that make it much easier to create an interactive sound box with the Raspberry Pi.

Screen Shot 2016-02-10 at 7.04.05 PM

Preparing the Touch HAT

For this project, you need a Raspberry Pi (already setup with a Linux distribution system and connected to the Internet, see above) and Adafruit’s capacitative touch HAT. You have to do a bit of soldering of a 2×20 solder header (included in the kit) to the touch HAT which makes it fit perfectly on top of the Raspberry Pi B+ (for older Raspberry Pi’s you need to solder a different taller solder header, see here).

Screen Shot 2016-02-10 at 7.03.55 PM

Pi wearing its new HAT!

One you have soldered the header and placed the HAT on the Pi, you need to install software following instructions here. The one thing that is missing from the tutorial is that you also have to enable the I2C protocol so that the Raspberry Pi can talk to the HAT. To do this use sudo raspi-config and choose Advanced Options. Here, you can enable I2C support and set it to load by default. Once you reboot, the libraries should be loaded.

Following is an example of code that runs a simple version of TalkBox (with a small set of constant sounds):

# Copyright (c) 2016 Foad Hamidi
# Author: Foad Hamidi based on code originally written by Tony Dicola from Adafruit Industries
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import sys
import time
import pygame.mixer

import Adafruit_MPR121.MPR121 as MPR121


print 'TalkBox Test'

# Create MPR121 instance.
cap = MPR121.MPR121()

# Initialize communication with MPR121 using default I2C bus of device, and 
# default I2C address (0x5A).  On BeagleBone Black will default to I2C bus 0.
if not cap.begin():
    print 'Error initializing MPR121.  Check your wiring!'
    sys.exit(1)

#Initialize the sound system, and create empty lists of channels and sounds
pygame.mixer.init(48000, -16, 1, 1024)
soundChannelList = [None] * 12
soundList = [None] * 12

#Populate your list of sounds and channels, there are only 7 channels, so you
#will have to reuse some channels. Channels are used so that sounds can be played
#simultaneously.
#I'm using some semi-random voice samples from a previous project. 
sound1 = pygame.mixer.Sound("/home/pi/Music/Feeling/happy.wav")
soundChannel1 = pygame.mixer.Channel(1)
soundList[0] = sound1
soundChannelList[0] = soundChannel1
sound2 = pygame.mixer.Sound("/home/pi/Music/Feeling/excited.wav")
soundChannel2 = pygame.mixer.Channel(2)
soundList[1] = sound2
soundChannelList[1] = soundChannel2
sound3 = pygame.mixer.Sound("/home/pi/Music/Feeling/feeling.wav")
soundChannel3 = pygame.mixer.Channel(3)
soundList[2] = sound3
soundChannelList[2] = soundChannel3
sound4 = pygame.mixer.Sound("/home/pi/Music/Feeling/proud.wav")
soundChannel4 = pygame.mixer.Channel(4)
soundList[3] = sound4
soundChannelList[3] = soundChannel4
sound5 = pygame.mixer.Sound("/home/pi/Music/Feeling/sad.wav")
soundChannel5 = pygame.mixer.Channel(5)
soundList[4] = sound5
soundChannelList[4] = soundChannel5
sound6 = pygame.mixer.Sound("/home/pi/Music/Feeling/sick.wav")
soundChannel6 = pygame.mixer.Channel(6)
soundList[5] = sound6
soundChannelList[5] = soundChannel6
sound7 = pygame.mixer.Sound("/home/pi/Music/Feeling/tired.wav")
soundChannel7 = pygame.mixer.Channel(7)
soundList[6] = sound7
soundChannelList[6] = soundChannel7
sound8 = pygame.mixer.Sound("/home/pi/Music/Feeling/good_morning_f.wav")
soundChannel8 = pygame.mixer.Channel(1)
soundList[7] = sound8
soundChannelList[7] = soundChannel8
sound9 = pygame.mixer.Sound("/home/pi/Music/Feeling/goodbye_f.wav")
soundChannel9 = pygame.mixer.Channel(2)
soundList[8] = sound9
soundChannelList[8] = soundChannel9
sound10 = pygame.mixer.Sound("/home/pi/Music/Feeling/need_break.wav")
soundChannel10 = pygame.mixer.Channel(3)
soundList[9] = sound10
soundChannelList[9] = soundChannel10
sound11 = pygame.mixer.Sound("/home/pi/Music/Feeling/thank_you_f.wav")
soundChannel11 = pygame.mixer.Channel(4)
soundList[10] = sound11
soundChannelList[10] = soundChannel11
sound12 = pygame.mixer.Sound("/home/pi/Music/Feeling/sunny.wav")
soundChannel12 = pygame.mixer.Channel(5)
soundList[11] = sound12
soundChannelList[11] = soundChannel12
print "Soundboard Ready."

# Main loop to print a message and play a sound every time a pin is touched.
print 'Press Ctrl-C to quit.'
last_touched = cap.touched()
while True:
    current_touched = cap.touched()
    # Check each pin's last and current state to see if it was pressed or released.
    for i in range(12):
        # Each pin is represented by a bit in the touched value.  A value of 1
        # means the pin is being touched, and 0 means it is not being touched.
        pin_bit = 1 << i
        # First check if transitioned from not touched to touched.
        if current_touched & pin_bit and not last_touched & pin_bit:
            print '{0} touched!'.format(i)
            soundChannel = soundChannelList[i-1] 
            sound = soundList[i-1]
            soundChannel.play(sound)
            print'Sound played'
        # Next check if transitioned from touched to not touched.
        if not current_touched & pin_bit and last_touched & pin_bit:
            print '{0} released!'.format(i)
    # Update last state and wait a short period before repeating.
    last_touched = current_touched
    time.sleep(0.1)

    # Alternatively, if you only care about checking one or a few pins you can 
    # call the is_touched method with a pin number to directly check that pin.
    # This will be a little slower than the above code for checking a lot of pins.
    #if cap.is_touched(0):
    #    print 'Pin 0 is being touched!'
    

Finally, adjust the volume of the speakers using the following command at the terminal prompt:

amixer  sset PCM,0 90%

which will adjust the volume by the indicated percentage of its total capacity (e.g., 90% in the above example).

Here’s a video of the setup used to make two pieces of fruit have a conversation:

Note: If you are getting weird static sounds out of the speaker connected to your analog audio output, it’s probably because of the power source. Try different ones and hopefully you will find one that doesn’t’ affect the audio performance too much. Another possibility is that you have not plugged in your speaker properly. Check it again before giving up on your speakers!

Finally, if you want this code to be loaded at startup, follow these instructions.

 

 

Baking Pi’s 1: Set Up

In the past few weeks, I’ve been playing around with the amazing Raspberry Pi embedded computer platform. In the “Baking Pi’s” series of posts, I will describe how to setup and use a Raspberry Pi for different applications.

Raspberry Pi is a powerful and affordable credit-card sized computer. With some basic programming and networking skills and a lot of patience you can use it to setup customized physical computing projects. In this first post, I will describe how to setup a Raspberry Pi and in future posts, I will describe how to use it for fun and useful projects with it, including a networked Rafigh, a simple interactive sound box and other things. I will work with a variety of Pi’s including Raspberry Pi 1, Pi Zero and Raspberry Pi B+. The set ups are very similar and I will mention when there are differences. I will also use a Mac computer (although having access to a Linux machine will be useful).

Note: These posts are fairly technical, and if they are not your cup of tea, rest assured that I plan to get back to my travel and life posts as soon as I have fun adventures to report 🙂

20160210_170732.jpg

Raspberry Pi family: (bottom to top) Raspberry Pi 1, Raspberry Pi B+ and Raspberry Pi Zero

Setting up the Raspberry Pi Software

First, we need to setup the Raspberry Pi to work in headless mode, meaning that we can connect to it via a different computer and don’t need to connect it to a monitor, keyboard and mouse. There are many advantages to setting up your Pi in this mode: you won’t need extra peripherals and you can embed the Pi in physical computing projects. In the case of the networked Rafigh, this step is essential because you will need to be able to leave the Raspberry Pi next to growing mushrooms and debug and control it remotely.

Before I start with the software, a note on how to power the Pi. I’ve been using three ways of powering the Pi with varying degrees of success. Surprisingly, many of the problems that arise with using Raspberry Pi’s are related to the use of power supplies that are not adequate or consistent depending on what other peripherals (Wifi dongle, lights, speakers) you connect to the Pi. The usual way is to use a good quality 5V, 2A wall plug (like this one). The trick is that some wall plugs claim that they can provide up to 2A current but in reality they are inconsistent, so investing in a better quality adapter is worth it. The Pi does work with adapters that provide less than 2A current though and depending on your project needs, a portable battery might be enough. The other two ways, I have powered the Pi are 1) using a consumer grade Duracell portable power supply and a combination of a Lithium Ion Polymer Battery and a PowerBoost 500 Charger. To me this last combination is very promising for wireless projects, but I am still experimenting with it and not sure how stable it is.

Once you have decided on how to power the Pi, you need to prepare your SD card. You can usually get away with a 4GB MicroSD card but I recommend using an 8GB one, as you might be tempted to use more space in the future. Setting up the SD card can be pretty straight forward if you are comfortable with using the command line (terminal) in Mac. First, you need to download a Linux distribution. I recommend getting the latest version of Raspbian Jessie from here. Also, if you are comfortable with using Torrents, I recommend getting the image file from the Torrent because it is faster and also doesn’t fail in the middle of the download.

Once you have the zipped image file. You need to unzip it into an .image file. After this open up the terminal in Mac. If you have an SD card reader/writer on your Mac, plug your empty SD card in there. Alternatively, you can use an external SD card reader/writer. My on-board SD card reader/writer has become a bit faulty and sometimes doesn’t let me write to an SD card. A trick I’ve used is to set the lock switch on the SD card to the middle position and sometimes this allows the write to happen. This method doesn’t work well if you are trying to write to a MicroSD Card. So I usually prefer using an external card reader.

Next, we want to copy the image to the SD card. Note that this process will erase all data on the card, so be careful not to put the wrong card in the reader! In Mac, open a Terminal window and (with the SD card in the reader) type:

diskutil list

This will list all the devices on your mac and their partitions. You want to dismount the partitions on the SD card. Be careful about this step, you don’t want to mix up your disks! First, you should identify your disk. Usually, it is easy to do that by looking at the size of the disk. Another method is to use the diskutil list commend before you insert the SD card and note what disks are there and then insert the SD card and note which new disk was added. In any case, once you know that your SD card is, for example, disk3, then you should unmount all of its partitions (you can identify these by the letters number combinations that follow the disk name). For example, to unmount partition 1 (i.e., s1) on disk3 use the following command.

sudo diskutil unmount /dev/disk3s1

Once all the partitions are dismounted, you are ready to start the image copying process. You can use the following command:

sudo dd bs=1m if=~/Desktop/2015-11-21-raspbian-jessie.img of=/dev/disk3

Where “2015-11-21-raspbian-jessie.img” is the name of your image file that is stored on the desktop. If you have problems with the “bs=1m” parameter, use “bs=1M” or “bs=4M”. Also, note that the name of the destination drive refers to the whole drive, not to a single partition (e.g., “disk3” not “disk3s1”).

This operation will take a while. In Mac, you can use Ctrl+t to see the status of the write operation.

Update: Since writing this article, I came across a very handy free little program called ApplePi-Baker that I can highly recommend to prepare your SD card. A neat feature of this program is that it makes backing up and restoring copies of your program easy. Also, when I had trouble with formatting my SD cards in the past (including encountering the notorious “bad superblock” problem!), this program allowed me to correctly reformat the SD card and saved me from throwing it away thinking it was broken beyond repair!

Connecting to the Raspberry Pi

Once you have the SD card ready, you are on your way to a working Raspberry Pi! Now, we need to setup the Raspberry Pi to work in “headless” mode. Again, there are multiple ways to do this and I will briefly cover three options.

The first option is to plug-in your Pi directly into your router. If your router is setup to accept DHCP (i.e., assign IP addresses dynamically), then you should see the Pi on your local network within a couple of minutes. You can use the terminal command on Mac to find out the Pi’s ip address:

arp -a

A second method to connect to the Pi is via ethernet cable.  The good thing about this method is that once you set it up, you don’t need an internet connection to access the Pi. On the negative side, this method might significantly slow down your wifi connection while you are connected to the Pi. For this method to work, you have to setup your Mac first. First, you need to set ethernet to DHCP. Go to Network Settings and click on Ethernet. In the Configure IPv4 select the “DHCP” option.

Next, go to System Preferences and under Internet and Wireless, select Sharing. Here, enable Internet Sharing through Ethernet. Now, if you plug in the Pi via ethernet, you should see it after a while on the network using:

arp -a

Finally, the third way to connect to the Pi is via wifi. You can connect the Pi to the internet wirelessly via a compatible wifi dongle. This is an excellent (although sometimes faulty) option and with some tweaking should work well. In order to do this, you will have to edit the interfaces file on your Pi. In order to do that you will have to access the Pi file system either through one of the methods above or via a Linux machine. You can not use Mac without third-party software to manipulate files on the internal Pi file system. (You can access files on visible partition of the Pi in Mac using file sharing but that doesn’t give you access to the files we need for this step.) So assuming you can ssh into your pi, you will need to change the /etc/network/interfaces file:

sudo nano /etc/network/interfaces

You need to change the file contents to the following:

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

auto lo
iface lo inet loopback

iface eth0 inet dhcp

auto wlan0
allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-ssid "your-network-name"
wpa-psk "your-network-password"

Once you have done this save the file, shutdown the Pi, plug  in the wifi dongle and power up. You should see the Pi on your network in a few minutes.

Note: In case you want to connect to a wifi hot spot that doesn’t have a password and requires you to use a login page to connect you can set a static ip address for your Pi and connect by putting the name of the network in the interfaces file. So your file would look something like:

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

auto lo
iface lo inet loopback

iface eth0 inet dhcp

auto wlan0
allow-hotplug wlan0
iface wlan0 inet static
address (your ip address here) 
netmask 255.224.0.0
network 10.224.0.0
broadcast 10.255.255.255
gateway 10.224.0.1
wireless-essid xfinitywifi

allow-hotplug wlan1
iface wlan1 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

After this, Raspberry Pi will connect to the wifi network and you will need to use a browser to navigate to the login page and register your pi. If you want to setup your pi without a monitor and keyboard, a possible way (I wasn’t successful with!) is register the MAC address of your pi using anther device (e.g., your laptop) by spoofing its MAC address. See here  for more information on that method.

Once you know the ip address, you can ssh into the pi using

ssh pi@192.168.2.18

where you should replace “192.168.2.18” with your ip address. You will be prompted for a password which is “raspberry” by default.
Once you can SSH into the Raspberry Pi, the fun begins! A good first step when you are here is to do some basic maintenance in the Pi. The first thing, I usually do is to expand the file system and set the date and time. You can do both of these by accessing the configuration dialogue using:

sudo raspi-config

Next, you want to update the Pi using the following commands:

sudo apt-get update
sudo apt-get upgrade

When updating it’s good to keep an eye out on the disk size using:

df -h

To clean up after installing packages use

sudo apt-get clean

Finally, use the following commands to logout of ssh, reboot the Pi and to shutdown after you are done:

logout
sudo reboot
sudo shutdown -h now

It is important to shut down the Pi properly. Otherwise, the SD card might get damaged.

If you want to give your Pi a specific name (rather than an ip address) to use to log in to, you can use the Bonjour service described here. If you have multiple pi’s make sure you name them differently by following the instructions here.

If you would like to access the graphical interface of the Pi from your computer, you usually have to setup a VNC server. But if you are using a Mac, there’s an easy shortcut to access the graphical interface: using the X11 app! Here’s how to start it from the terminal:

ssh -x pi@<your PI's ip address>

Once logged in you can start an Xsession with:

/etc/11/Xsession

To end the session press Ctrl + x from the original terminal window.

Note: When connecting to the Pi, if you get an error with the message: “Warning: Remote Host Identification Has Changed” you can use the following command to clean your stored keys and get rid of the message:

ssh-keygen -R "you server hostname or ip"