Einführung in Raspberry Pi und GPIO

16
Python auf dem Raspberry Pi Thomas Koch @tomy_koch PythonCamp, Köln, Mai 2013

description

Vorstellung des Raspberry Pi und der Nutzung der GPIO-Schnittstelle

Transcript of Einführung in Raspberry Pi und GPIO

Page 1: Einführung in Raspberry Pi und GPIO

Python auf dem Raspberry Pi

Thomas Koch @tomy_koch

PythonCamp, Köln, Mai 2013

Page 2: Einführung in Raspberry Pi und GPIO

Agenda

Specs:

Was ist der Raspberry Pi ?

Anwendungen:

Was kann der Raspberry Pi ?

GPIO mit Python

Programmierung & Beispiele

Weitere Infos

Links, Literatur

05/2013 Thomas Koch

Page 3: Einführung in Raspberry Pi und GPIO

Einführung: Der Raspberry Pi

Grundlagen

SOC Minicomputer: "System on a chip"

512 MB Hauptspeicher und 700 MHZ ARM CPU

Ethernet-Schnittstelle und zwei USB-Anschlüsse (Modell B)

"BYOP"-Prinzip: "bring your own peripherals“

Lieferung ohne Maus, Tastatur, Netzstecker etc.

Nackte Platine (ohne Gehäuse), ohne SD-Card/OS

Einschränkungen

keine Uhr

kein BIOS (d.h. Raspi kann NUR von SD-Card booten)

05/2013 Thomas Koch

Page 4: Einführung in Raspberry Pi und GPIO

Hardware: Der Raspberry Pi

05/2013 Thomas Koch

Das Board

GPIO (General Purpose Input/Output)

CSI (Camera Serial Interface)

DVI-Out

OS

Raspbian (Debian)

Arch Linux

RISC OS

Page 5: Einführung in Raspberry Pi und GPIO

Anwendungsmöglichkeiten

Network Device

Print- und Webserver, VPN-Server, DHCP-Server etc.

Cloud für eigenes Netzwerk

Raspberry Pi als Mediacenter

XBMC basierende Distributionen wie OpenELEC, Raspbmc

Oder auch als AirPlay Empfänger

Remote Control

Steuereinheit für eigene Roboter oder Luftfahrzeuge

Home Automation

Alarm Anlage

Kiosk-Systeme

eingebauter Web-Browser "midori" bietet Vollbildmodus

$ midori -e Fullscreen -a <URL>

http://lifehacker.com/5978871/ten-more-awesome-projects-for-your-raspberry-pi

Page 9: Einführung in Raspberry Pi und GPIO

Mini Tutorial

05/2013 Thomas Koch

Erste Schritte

Raspian auf SD-Karte „installieren“ und einstecken

Einstecken (kein An/Aus-Schalter!)

Login: user: pi / pwd: raspberry (Tipp: passwd ,-)

Desktop-GUI: startx

Debian basics

$ apt-get update

$ apt-get upgrade

$ apt-get install <pkg>

$ apt-get autoclean

Page 10: Einführung in Raspberry Pi und GPIO

Mini Tutorial II

05/2013 Thomas Koch

Konfiguration:

$ raspi-config

‘enable ssh’

‘change_pass ‘ change system password (for the user 'pi')

http://learn.adafruit.com/adafruits-raspberry-pi-lesson-2-first-time-configuration/overview

nützliche Debian Pakete

für GPIO: "WiringPi"

https://projects.drogon.net/raspberry-pi/wiringpi/

$ sudo apt-get install git-core

$ git clone git://git.drogon.net/wiringPi

Page 11: Einführung in Raspberry Pi und GPIO

GPIO Basics

05/2013 Thomas Koch

GPIO per WiringPI steuern

Ports per WiringPI ansteuern

command-line utility gpio

can be used to program and setup the GPIO pins

pi@raspi ~ $ gpio readall

Python-Bindings

Bindings für GPIO-Projekt WiringPi auf github:

https://github.com/WiringPi/WiringPi-Python

+----------+------+--------+------+-------+

| wiringPi | GPIO | Name | Mode | Value |

+----------+------+--------+------+-------+

| 0 | 17 | GPIO 0 | IN | Low |

| 1 | 18 | GPIO 1 | IN | Low |

| 2 | 27 | GPIO 2 | OUT | Low |

| 3 | 22 | GPIO 3 | IN | Low |

| 4 | 23 | GPIO 4 | IN | Low |

| 5 | 24 | GPIO 5 | IN | Low |

| 6 | 25 | GPIO 6 | IN | Low |

| 7 | 4 | GPIO 7 | IN | Low |

| 8 | 2 | SDA | ALT0 | High |

| 9 | 3 | SCL | ALT0 | High |

| 10 | 8 | CE0 | IN | Low |

| 11 | 7 | CE1 | IN | Low |

| 12 | 10 | MOSI | IN | Low |

| 13 | 9 | MISO | IN | Low |

| 14 | 11 | SCLK | IN | Low |

| 15 | 14 | TxD | ALT0 | High |

| 16 | 15 | RxD | ALT0 | High |

| 17 | 28 | GPIO 8 | IN | Low |

| 18 | 29 | GPIO 9 | IN | Low |

| 19 | 30 | GPIO10 | IN | Low |

| 20 | 31 | GPIO11 | IN | Low |

+----------+------+--------+------+-------+

$ gpio mode 0 out

$ gpio write 0 1

$ gpio write 0 0

Page 12: Einführung in Raspberry Pi und GPIO

Python auf dem Raspberry PI

05/2013 Thomas Koch

Python included

Raspi kommt mit Python 2.7 und 3.2

und pygame http://www.pygame.org/

GPIO mit Python

simple GPIO Python Wrapper: RPi.GPIO

A module to control Raspberry Pi GPIO channels

includes a C extension

any script using RPi.GPIO must run with root privileges!

https://pypi.python.org/pypi/RPi.GPIO

Page 13: Einführung in Raspberry Pi und GPIO

Code-Beispiele

Demonstration

05/2013 Thomas Koch

Page 14: Einführung in Raspberry Pi und GPIO

Beispiel: WiringPi

05/2013 Thomas Koch

Serielle Schnittstellte GPIO bietet pins für 'transmit' signal (TxD) and 'receive' signal (RxD)

Achtung: Raspberry Pi verwendet 0 – 3.3V level (RS-232: +/-12V)

serialOpen: Requires device/baud and returns an ID

https://projects.drogon.net/raspberry-pi/wiringpi/

http://elinux.org/RPi_Serial_Connection

serial = wiringpi.serialOpen('/dev/ttyAMA0',9600) wiringpi.serialPuts(serial,"hello") wiringpi.serialClose(serial) // Pass in ID

Page 15: Einführung in Raspberry Pi und GPIO

Beispiel: RPi.GPIO

05/2013 Thomas Koch

LED einschalten

http://elinux.org/RPi_Low-level_peripherals#Python

https://projects.drogon.net/raspberry-pi/gpio-examples/tux-crossing/gpio-examples-1-a-single-led/

import RPi.GPIO as GPIO

# header pin numbering BCM GPIO 00..nn

GPIO.setmode(GPIO.BCM)

# Set up the GPIO channels:

# one input and one output

GPIO.setup(17, GPIO.IN)

GPIO.setup(18, GPIO.OUT)

input_value = GPIO.input(17)

print „pin#17 is“, input_value

GPIO.output(18, GPIO.HIGH)