Getting started with Raspberry Pi

From ICO wiki
Revision as of 09:21, 30 September 2015 by Lvosandi (talk | contribs) (→‎Powering up)
Jump to navigationJump to search

Raspberry Pi is a credit-card sized single-board computer which can be used for variety of purposes. The official operating system for Raspberry Pi is Raspbian. Rasbian and Ubuntu both are derived from Debian, providing seamless access to thousands of software package ready to go via Advanced Packaging Tool. Compared to other similar single-board computers Raspberry Pi has vibrant community and plenty of code examples that you can run out of the box. Before you continue make sure you're comfortable with basic command-line usage, if that's not the case take a look at CodeAcademy track for learning command line.

Feel free to improve this wiki page by clicking edit above.


Preparing SD card

Before you continue make sure you have backed up any important data on the SD-card

Download and uncompress Raspbian:

wget https://downloads.raspberrypi.org/raspbian/images/raspbian-2015-05-07/2015-05-05-raspbian-wheezy.zip
unzip 2015-05-05-raspbian-wheezy.zip

For Linux there is no need to fetch extra tools to write the image to a SD card, simply use dd:

 sudo dd bs=4M if=2015-05-05-raspbian-wheezy.img of=/dev/mmcblk0

Make sure you flush buffers, otherwise you'll get partial write:

 sync

If you're running on Windows, you can use Win32 Disk Imager to write the image to the SD card.

Powering up

Use USB-UART bridge to power up Raspberry Pi and gain access to the command-line.

Accessing command-line

For Linux there are variety of programs for connecting to serial port:

screen /dev/ttyUSB0 115200
picocom /dev/ttyUSB0 -b 115200
minicom -b 115200 -o -D /dev/ttyUSB0

If you get the error message [screen is terminating], you must put your user in the "dialout" group. To do this, enter the following command (only with your own computer):

sudo gpasswd -a $USER dialout

Then you will see a message that your user has been added to the group dialout. Now log out from Ubuntu and log in once again.


For Windows you need to install driver for your USB-UART bridge first. For Windows 8 use this workaround. Then you can use built-in HyperTerminal, PuTTY or KiTTY for connecting to the virtual serial port.

The default user is pi with the password raspberry. Make sure you change the default password. For more information about user management see Raspbian documentation.

If you're facing issues with USB-UART bridge, simply proceed to network section below.

Setting up network

Raspbian as Debian attempts to request IP address using DHCP on the Ethernet port. This assumes a functioning DHCP server on the network, which is the case most of the time. If you want to connect your laptop directly to the Raspberry Pi you can set up manual IP addresses. The networking configuration is stored in /etc/network/interfaces, remember to issue service networking restart after you've tweaked the configuration.

Use nmap to discover Raspberry Pi on your network:

 nmap 192.168.0.1-255 -p22

Don't worry if you mess up the network configuration, you can always recover with the USB-UART bridge method described above.

Remote shell

For more convenient access to command-line SSH is recommended. You can start by installing OpenSSH server on Raspberry Pi:

apt-get install openssh-server

Assuming Raspberry Pi sits on an DHCP enabled network you can use following on your Linux laptop to connect to remote shell:

ssh pi

Otherwise determine the IP address of your Raspberry Pi, for example with ip addr list:

ssh 192.168.x.x


Make your life easier by setting up public key authentication. Windows users might want to take a look at PuTTYgen instructions.

Note that while you are connected via USB-UART bridge you can simply copy the contents of your public key and paste it to Raspberry Pi as follows:

mkdir -p /root/.ssh
cat - >> /root/.ssh/authorized_keys
# Paste now
# Press Ctrl-D

Beyond installation

Once you are comfortable with Raspberry Pi command line and SSH you can do a lot of cool stuff:

Blinking a LED

In the following examples the LED is connected to GPIO pin 18:

File:Raspberry-pi-gpio-18-led bb.png


Blinking from the terminal

Raspbian has driver loaded for GPIO pins, so you can control them from shell programmatically.

To blink it from terminal you first have to become a root user.

sudo su

Then you have to export gpio number 18

echo 18 > /sys/class/gpio/export

After that is done you have to set the direction of the GPIO pin, in this case you have to set it to out direction.

echo out > /sys/class/gpio/gpio18/direction

And now you can blink your led on and off.

# Turn the led on
echo 1 > /sys/class/gpio/gpio18/value

# Turn the led off
echo 0 > /sys/class/gpio/gpio18/value
# blink the led forever with 1 second interval
while true; do echo 1 > /sys/class/gpio/gpio18/value; sleep 1; echo 0 > /sys/class/gpio/gpio18/value; sleep 1; done

Blinking LED from Python script

Save this script as a blink.py in the Raspberry Pi home directory. You can use command-line text editor nano to do that, or open up the file remotely with sftp:// or WinSCP.

nano blink.py

In nano Ctrl-O writes the file to the disk and Ctrl-X returns back to command-line.

The blink.py script:

# import Raspberry Pi GPIO support into Python environment
import RPi.GPIO as GPIO
# import a sleep function from time module
from time import sleep

led = 18  # GPIO number where the led is connected

# Tell the GPIO module to use GPIO numbering used by processor
GPIO.setmode(GPIO.BCM)
# Set GPIO no 18 to output mode
GPIO.setup(led, GPIO.OUT)

# Blink some leds
while True:
    GPIO.output(led, False)
    sleep(1)  # Sleep for 1 second
    GPIO.output(led, True)
    sleep(1)

You have to run this python script as a root user, easiest way to do that is to use sudo command in front of python blink.py command

sudo python blink.py

Minimal HTTP server in Python

This is a Python 2 webserver example.

# make_server is used to create this simple python webserver
from wsgiref.simple_server import make_server

# Function that is ran when a http request comes in
def simple_app(env, start_response):
    
    # set some http headers that are sent to the browser
    status = '200 OK'
    headers = [('Content-type', 'text/plain')] 
    start_response(status, headers)

    # What did the user ask for?
    if env["PATH_INFO"] == "/on":
        print("user asked for /on")
        return "got on"
    elif env["PATH_INFO"] == "/off":
        print("user asked for /off")
        return "got off"
    else:
        print("user asked for something else")
        return "Hello world!"            

# Create a small python server
httpd = make_server("", 8000, simple_app)
print "Serving on port 8000..."
print "You can open this in the browser http://192.168.1.xxx:8000 where xxx is your rpi ip aadress"
print "Or if you run this server on your own computer then http://localhost:8000" 
httpd.serve_forever()