Getting started with Raspberry Pi: Difference between revisions
No edit summary |
|||
(23 intermediate revisions by 3 users not shown) | |||
Line 5: | Line 5: | ||
Before you continue make sure you're comfortable with basic command-line usage, if that's not the case take a look at | Before you continue make sure you're comfortable with basic command-line usage, if that's not the case take a look at | ||
[https://www.codecademy.com/en/courses/learn-the-command-line/ CodeAcademy track for learning command line]. | [https://www.codecademy.com/en/courses/learn-the-command-line/ CodeAcademy track for learning command line]. | ||
'''Feel free to improve this wiki page by clicking edit above.''' | |||
Line 13: | Line 15: | ||
Download and uncompress Raspbian: | Download and uncompress Raspbian: | ||
wget | wget http://upload.itcollege.ee/raspbian-jessie-2016-05-27.zip | ||
unzip | unzip raspbian-jessie-2016-05-27.zip | ||
For Linux there is no need to fetch extra tools to write the image to a SD card, simply use <code>dd</code>: | For Linux there is no need to fetch extra tools to write the image to a SD card, simply use <code>dd</code>: | ||
Line 29: | Line 31: | ||
Use [http://eclipsesource.com/blogs/wp-content/uploads/2012/10/3dr-radio-to-RasPi_marked.jpg-e1350422181130.png USB-UART bridge to power up Raspberry Pi] and gain access to the command-line. | Use [http://eclipsesource.com/blogs/wp-content/uploads/2012/10/3dr-radio-to-RasPi_marked.jpg-e1350422181130.png USB-UART bridge to power up Raspberry Pi] and gain access to the command-line. | ||
[[File:Raspberry-pi-uart bb.png|600px]] | |||
[[File:RaspberryPi_serial_wiring.jpeg|800px]] | |||
=Accessing command-line= | =Accessing command-line= | ||
Line 56: | Line 43: | ||
picocom /dev/ttyUSB0 -b 115200 | picocom /dev/ttyUSB0 -b 115200 | ||
minicom -b 115200 -o -D /dev/ttyUSB0 | 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 [http://www.prolific.com.tw/US/ShowProduct.aspx?p_id=225&pcid=41 driver for your USB-UART bridge] first. | For Windows you need to install [http://www.prolific.com.tw/US/ShowProduct.aspx?p_id=225&pcid=41 driver for your USB-UART bridge] first. | ||
For Windows 8 use [http://minhdanh2002.blogspot.com.ee/2013/12/using-prolific-pl2303-usb-to-ttl.html this workaround]. | |||
Then you can use built-in HyperTerminal, [http://www.putty.org/ PuTTY] or [http://www.9bis.net/kitty/ KiTTY] for connecting to the virtual serial port. | Then you can use built-in HyperTerminal, [http://www.putty.org/ PuTTY] or [http://www.9bis.net/kitty/ KiTTY] for connecting to the virtual serial port. | ||
The default user is <code>pi</code> with the password <code>raspberry</code>. Make sure you change the default password. For more information about user management see [https://www.raspberrypi.org/documentation/linux/usage/users.md Raspbian documentation]. | The default user is <code>pi</code> with the password <code>raspberry</code>. Make sure you change the default password. For more information about user management see [https://www.raspberrypi.org/documentation/linux/usage/users.md Raspbian documentation]. | ||
'''If you're facing issues with USB-UART bridge, simply proceed to network section below.''' | |||
=Setting up network= | =Setting up network= | ||
Line 71: | Line 67: | ||
remember to issue <code>service networking restart</code> after you've tweaked the configuration. | remember to issue <code>service networking restart</code> after you've tweaked the configuration. | ||
Use <code>nmap</code> 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= | =Remote shell= | ||
Line 101: | Line 102: | ||
Once you are comfortable with Raspberry Pi command line and SSH you can do a lot of cool stuff: | Once you are comfortable with Raspberry Pi command line and SSH you can do a lot of cool stuff: | ||
* Set up [https://doc.owncloud.org/server/8.1/admin_manual/installation/index.html ownCloud] as your very own Dropbox replacement. | |||
* Set up [https://www.raspberrypi.org/forums/viewtopic.php?f=38&t=11124 PulseAudio network sink] and use Raspberry Pi as wireless speakers. | * Set up [https://www.raspberrypi.org/forums/viewtopic.php?f=38&t=11124 PulseAudio network sink] and use Raspberry Pi as wireless speakers. | ||
* Set up [http://www.instructables.com/id/Raspberry-Pi-as-low-cost-HD-surveillance-camera/step7/Installing-the-motion-detection-software/ motion] and use Raspberry Pi with USB webcam as a IP camera. | * Set up [http://www.instructables.com/id/Raspberry-Pi-as-low-cost-HD-surveillance-camera/step7/Installing-the-motion-detection-software/ motion] and use Raspberry Pi with USB webcam as a IP camera. | ||
Line 107: | Line 109: | ||
* [https://github.com/fenlogic/vga666 Add VGA port to your Raspberry Pi]. | * [https://github.com/fenlogic/vga666 Add VGA port to your Raspberry Pi]. | ||
* Turn your Raspberry Pi into [http://www.icrobotics.co.uk/wiki/index.php/Turning_the_Raspberry_Pi_Into_an_FM_Transmitter FM transmitter]. | * Turn your Raspberry Pi into [http://www.icrobotics.co.uk/wiki/index.php/Turning_the_Raspberry_Pi_Into_an_FM_Transmitter FM transmitter]. | ||
=Blinking a LED= | |||
In the following examples the LED is connected to [https://en.wikipedia.org/wiki/General-purpose_input/output GPIO] pin 10: | |||
[[File:rpi led.png|400px]] | |||
==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 10 | |||
echo 10 > /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/gpio10/direction | |||
And now you can blink your led on and off. | |||
# Turn the led on | |||
echo 1 > /sys/class/gpio/gpio10/value | |||
# Turn the led off | |||
echo 0 > /sys/class/gpio/gpio10/value | |||
# blink the led forever with 1 second interval | |||
while true; do echo 1 > /sys/class/gpio/gpio10/value; sleep 1; echo 0 > /sys/class/gpio/gpio10/value; sleep 1; done | |||
==Blinking LED from Python script== | |||
Save this script as a <code>blink.py</code> in the Raspberry Pi home directory. | |||
You can use command-line text editor <code>nano</code> to do that, or open up the file remotely with <code>sftp://</code> or WinSCP. | |||
nano blink.py | |||
In <code>nano</code> 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 = 10 # 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 10 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() |
Latest revision as of 17:24, 20 September 2016
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 http://upload.itcollege.ee/raspbian-jessie-2016-05-27.zip unzip raspbian-jessie-2016-05-27.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:
- Set up ownCloud as your very own Dropbox replacement.
- Set up PulseAudio network sink and use Raspberry Pi as wireless speakers.
- Set up motion and use Raspberry Pi with USB webcam as a IP camera.
- Set up hostapd, dnsmasq and packet forwarding turning your Raspberry Pi into a wireless hotspot. Easier approach here.
- Set up nginx and create static web page using Jekyll.
- Add VGA port to your Raspberry Pi.
- Turn your Raspberry Pi into FM transmitter.
Blinking a LED
In the following examples the LED is connected to GPIO pin 10:
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 10
echo 10 > /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/gpio10/direction
And now you can blink your led on and off.
# Turn the led on echo 1 > /sys/class/gpio/gpio10/value # Turn the led off echo 0 > /sys/class/gpio/gpio10/value
# blink the led forever with 1 second interval while true; do echo 1 > /sys/class/gpio/gpio10/value; sleep 1; echo 0 > /sys/class/gpio/gpio10/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 = 10 # 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 10 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()