Skip to content
123 changes: 71 additions & 52 deletions docs/Setup/rpi.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
# Setting Up the Raspberry Pi 2

In order to use the RPi2 with openaps development tools, the RPi2 must have an operating system installed and be set up in a very specific way. By far, the most convenient approach for setting up the RPi2 is by avoiding the use of cables, which is also known as a headless install. This is the quickest way to get up and running with your RPi2 and avoids frustration.
In order to use the RPi2 with openaps development tools, the RPi2 must have an operating system installed and be set up in a very specific way. By far, the most convenient approach for setting up the RPi2 is by avoiding the use of cables. This is the quickest way to get up and running with your RPi2 and avoids frustration.

For the install, you will need:

* Raspberry Pi 2
* 8 GB micro SD Card
* 8 GB micro SD Card (see note below)
* Low Profile USB WiFi Adapter
* 2.1 Amp USB Power Supply
* Micro USB cable

Note: If you ordered the recommended CanaKit, your SD card will already come imaged and ready to install Raspian, rather than having to first getting the image in step 1.

1.: **Getting Raspbian**
1. **Getting Raspbian**
<br>Raspbian is the recommended operating system for OpenAPS. You can download the latest version of Raspbian [here](http://downloads.raspberrypi.org/raspbian_latest). Make sure to extract the ZIP file.

2.: **Writing Raspbian to the Micro SD Card**
<br>Please read this [excellent guide](http://elinux.org/RPi_Easy_SD_Card_Setup). Please view the sections on flashing the SD card using Windows, Mac OS X, or Linux, depending on which operating system you use.
2. **Writing Raspbian to the Micro SD Card**
<br>If needed, use this [guide](http://elinux.org/RPi_Easy_SD_Card_Setup). Please view the sections on flashing the SD card using Windows, Mac OS X, or Linux, depending on which operating system you use.

3. **3a. For PC Users: Configuring WiFi Settings**
<br>Keep the SD card in the reader in your computer. In this step, the WiFi interface is going to be configured in Raspbian, so that we can SSH in to the RPi2 and access the device remotely, such as on a computer or a mobile device via an SSH client, via the WiFi connection that we configure. Go to the directory where your SD card is with all of the files for running Raspbian on your RPi2, and open this file in a text editor.

3.: **Configuring WiFi Settings**
<br>_(a note for Mac users: You cannot access EXT4 partitions without using 3rd party software. The easiest alternative it is to temporarily connect RPi to a router with and ethernet cable, SSH in (see below), and continue setting things up in /etc/network/interfaces to get the wifi running.)_
<br><br>
Keep the SD card in the reader in your computer. In this step, the WiFi interface is going to be configured in Raspbian, so that we can SSH in to the RPi2 and access the device remotely, such as on a computer or a mobile device via an SSH client, via the WiFi connection that we configure. Go to the directory where your SD card is with all of the files for running Raspbian on your RPi2, and open this file in a text editor.
<br><br>
`/path/to/sd/card/etc/network/interfaces`
<br><br>

Edit the file so it looks like this:
<br>

```
auto lo
Expand All @@ -40,59 +38,73 @@ wpa-psk <your-password>
```

Replace `<your-network-name>` and `<your-password>` with your own credentials. Save the file (without adding any additional extensions to the end of the filename).
<br><br>
Now, put the SD card into the RPi2. Plug in the compatible USB WiFi adapter into a RPi2 USB port. Get a micro USB cable and plug the micro USB end into the side of the RPi2 and plug the USB side into the USB power supply.
<br><br>
4.: **Testing SSH Access**

**Windows:** Make sure that the computer is connected to the same WiFi router that the RPi2 is using. Download PuTTY [here](http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html). Hostname is `pi@raspberrypi.lan` and default password for the user `pi` is `raspberry`. The port should be set to 22 (by default), and the connection type should be set to SSH. Click `Open` to initiate the SSH session.
Boot your Pi. (Put the SD card into the RPi2. Plug in the compatible USB WiFi adapter into a RPi2 USB port. Get a micro USB cable and plug the micro USB end into the side of the RPi2 and plug the USB side into the USB power supply.) Skip to step 4.

**3b. For Mac Users: Configuring WiFi Settings**
<br>First boot your Pi. (Put the SD card into the RPi2. Plug in the compatible USB WiFi adapter into a RPi2 USB port. Get a micro USB cable and plug the micro USB end into the side of the RPi2 and plug the USB side into the USB power supply.)

**Mac OS X:** Make sure that the computer is connected to the same WiFi router that the RPi2 is using.
You cannot do 3a on a Mac, aka access EXT4 partitions without using 3rd party software. The easiest alternative it is to a) get a console cable (use [this guide](https://learn.adafruit.com/downloads/pdf/adafruits-raspberry-pi-lesson-5-using-a-console-cable.pdf)) or b) temporarily connect RPi to a router with an ethernet cable, SSH in (see below), and continue setting things up as described below (here in 3b) to get the wifi running. The below method will help you set up two or more wifi networks. This is highly recommended so you can add your home wifi network and your phone's hotspot network to use on the go.

Open Terminal and enter this command:
Type `sudo bash` and hit enter

`ssh pi@raspberrypi.lan`
Input `wpa_passphrase "<my_SSID_hotspot>" "<my_hotspot_password>" >> /etc/wpa_supplicant/wpa_supplicant.conf` and hit enter (where `<my_SSID_hotspot>` is the name of your phone's hotspot and `<my_hotspot_password>` is the password).

Default password for the user `pi` is `raspberry`
(It should look like: `wpa_passphrase "OpenAPS hotspot" "123loveOpenAPS4ever" >> /etc/wpa_supplicant/wpa_supplicant.conf`)

**Linux:** Make sure that the computer is connected to the same WiFi router that the RPi2 is using.
Input your home wifi next: `wpa_passphrase "<my_SSID_home>" "<my_home_network_password>" >> /etc/wpa_supplicant/wpa_supplicant.conf` (and hit enter)<br><br>
4. **Testing SSH Access**
<br><br>*Windows:* Make sure that the computer is connected to the same WiFi router that the RPi2 is using. Download PuTTY [here](http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html). Hostname is `pi@raspberrypi.local` and default password for the user `pi` is `raspberry`. The port should be set to 22 (by default), and the connection type should be set to SSH. Click `Open` to initiate the SSH session.
<br>*Mac OS X:* Make sure that the computer is connected to the same WiFi router that the RPi2 is using.
<br> Open Terminal and enter this command:

Open Terminal and enter this command:
`ssh pi@raspberrypi.local`

`ssh pi@raspberrypi.lan`
Default password for the user `pi` is `raspberry`

Default password for the user `pi` is `raspberry`
*Linux:*
<br> Make sure that the computer is connected to the same WiFi router that the RPi2 is using.

**iOS:** First, you need to make your phone a hotspot and configure the WiFi connection in `step 3` to use the hotspot. Make sure that the iOS device is connected to the same WiFi network that the RPi2 is using. Download Serverauditor or Prompt 2 (use this if you have a visual impairment). Hostname is `pi@raspberrypi.lan` and the default password for the user `pi` is `raspberry`. The port should be set to 22 (by default), and the connection type should be set to SSH.
Open Terminal and enter this command:

**Android:** First, you need to make your phone a hotspot and configure the WiFi connection in `step 3` to use the hotspot. Make sure that the Android device is connected to the same WiFi network that the RPi2 is using. Download an SSH client in the Google Play store. Hostname is `pi@raspberrypi.lan` and the default password for the user `pi` is `raspberry`. The port should be set to 22 (by default), and the connection type should be set to SSH.
`ssh pi@raspberrypi.local`

**Note:** If connecting to the RPi2 fails at this point, the easiest alternative it is to temporarily connect RPi to router with ethernet cable, and SSH in, given both the computer and the RPi2 are connected to the same router.
Default password for the user `pi` is `raspberry`

5.: **raspi-config**
*iOS:* <br>You probably want to make your phone a hotspot and configure the WiFi connection in `step 3` to use the hotspot. Make sure that the iOS device is connected to the same WiFi network that the RPi2 is using. Download Serverauditor or Prompt 2 (use this if you have a visual impairment). Hostname is `pi@raspberrypi.local` and the default password for the user `pi` is `raspberry`. The port should be set to 22 (by default), and the connection type should be set to SSH.

*Android:* <br>You probably want to make your phone a hotspot and configure the WiFi connection in `step 3` to use the hotspot. Make sure that the Android device is connected to the same WiFi network that the RPi2 is using. Download an SSH client in the Google Play store. Hostname is `pi@raspberrypi.local` and the default password for the user `pi` is `raspberry`. The port should be set to 22 (by default), and the connection type should be set to SSH.

Note: If connecting to the RPi2 fails at this point, the easiest alternative it is to temporarily connect RPi to router with ethernet cable, and SSH in, given both the computer and the RPi2 are connected to the same router.<br><br>
5. **raspi-config**

Run

`sudo raspi-config`

to expand filesystem, change user password and set timezone (in internalization options)

6.: **Password-less login**
to expand filesystem, change user password and set timezone (in internationalization options)<br><br>
6. **Optional:Password-less login**

Secure your RPi2. Log out by executing
*For PC (Mac users see below)*
<br>Secure your RPi2. Log out by executing

`exit`

and copy your public SSH key into your RPi2 by entering

`ssh-copy-id pi@raspberrypi.lan`
`ssh-copy-id pi@raspberrypi.local`

Now you should be able to log in without a password. Repeat `step 4` and try to SSH into the RPi2 without a password.

**Don't have an SSH key?** Follow this guide from GitHub to obtain one. [Link](https://help.github.com/articles/generating-ssh-keys/)
*Don't have an SSH key?* Follow this guide from GitHub to obtain one. [Link](https://help.github.com/articles/generating-ssh-keys/)

7.: **SSH configuration**
**For Mac and Linux**
First `ssh-keygen` (keep hitting enter to accept all the defaults)

Next `scp ~/.ssh/id_rsa.pub pi@raspberrypi.local:~/.ssh/authorized_keys`

Finally `ssh pi@raspberrypi.local`<br><br>
7. **SSH configuration**
Since we have no password, we need to disable password login. Open the `sshd_config` file in nano text editor as follows

`sudo nano /etc/ssh/sshd_config`
Expand All @@ -113,17 +125,17 @@ PasswordAuthentication no

(note that the second line was previously commented out).

From now on you will be able to SSH in with your private SSH key only.
From now on you will be able to SSH in with your private SSH key only.<br><br>
8. **Update**

8.: **Update**
<br>Update the RPi2.
Update the RPi2.

`sudo apt-get update && sudo apt-get upgrade`
`sudo apt-get update && sudo apt-get -y upgrade`

Be patient while the packages install.
The packages will take some time to install.<br><br>
9. **Watchdog**

9.: **Watchdog**
<br>Now we are going to install watchdog, which restarts the RPi2 if it becomes unresponsive.
Now we are going to install watchdog, which restarts the RPi2 if it becomes unresponsive.

Enter in:

Expand All @@ -133,30 +145,37 @@ Then enter:

`sudo modprobe bcm2708_wdog`


Create a new file 8192cu.conf in /etc/modprobe.d/:

`sudo nano /etc/modprobe.d/8192cu.conf`

Add this line to the file (paste it in):
`options 8192cu rtw_power_mgnt=0 rtw_enusbss=0` (Look at hints on bottom of screen; Control x to exit, yes to save, enter)

Then enter this line to open up the following file:

`sudo nano /etc/modules`

At the bottom of the file add
At the bottom of the file copy and paste:

`bcm2708_wdog`
`bcm2708_wdog` (Look at hints on bottom of screen; Control x to exit, yes to save, enter)

Add watchdog to startup applications
Next, add watchdog to startup applications:

`sudo update-rc.d watchdog defaults`

Edit its config file by opening up nano text editor
Edit the config file by opening up nano text editor

`sudo nano /etc/watchdog.conf` <br />
<br />
`sudo nano /etc/watchdog.conf`

Uncomment the following: (remove the # from the following lines):
Uncomment the following: (remove the # from the following lines, scroll down as needed to find them):

```
max-load-1
watchdog-device
```

Start watchdog by entering
Finally, start watchdog by entering:

`sudo service watchdog start`
`sudo service watchdog start`