投影片_樹莓派研習會_20130614

Download Report

Transcript 投影片_樹莓派研習會_20130614

超級小電腦
Raspberry Pi
翰尼斯有限公司、CAVE教育團隊
葉律佐
[email protected]
2013.06.14
1
Agenda
•
•
•
•
•
•
Preface: Tell me a story: Raspberry Pi
Session 1: Raspberry Pi Setup
Session 2: Linux 基本指令集
Session 3: Scratch in Raspberry Pi
Session 4: Python in Raspberry Pi
Session 5: 和其它Devices做鄰居:Raspberry
Pi的GPIO
• Session 6: Raspberry Pi + Arduino
• Session 7: 把 Pi 變多媒體娛樂中心
2
3
關於樹莓(Raspberry)
的故事
• 「樹莓」是一棵常綠喬木,非常喜歡陽光,不
耐蔭,怕積水的環境,所以必須是排水性良好
的土壤,很能耐旱。
• 她第一次被發現是在1792年由一位蘇格蘭植物
學家所收集,並命名為東方草莓樹。
• 「樹莓」是一株相當知名的蜜源樹,因為她在
春末時會開出有蠟質白或粉紅的小白花,有濃
濃的蜂蜜味道,是蜜蜂的最愛。
• 「樹莓」的掛果相當長久須一年左右才能成熟,
掛果量相當多,是相當著名的觀果樹,果肉是
甜的,可食。
4
• 樹莓集草本、木本植物優勢於一體,形似
草莓卻勝於草莓。
• 紅樹莓是當今發現的天然鞣化酸含量最高
的食物。
• 鞣化酸是一種能有效預防和治療癌症的物
質,對結腸、宮頸、乳腺和胰臟癌等有特
殊療效。
• 因此被世界公認為第三代水果,在國際國
內市場上前景極為廣闊,是中國目前調整
農業産業結構,發展高效種植業的好專案。
5
The name 'Raspberry Pi'
• In the early days of Home Micros, there were
a number of "Fruit" named computers.
• Apart from Apple, Apricot(杏) and
Tangerine(橘子) spring to mind(映入腦海), Pi
is said to be derived from the programming
language Python, which was an early runner
in suggestions for a suitable "official"
language for the Raspberry.
6
Raspberry Pi
• 以Python為主要程式語言的電腦主機
板
7
8
樹莓派的圖示
9
10
硬體規格
• SoC Broadcom
BCM2835 (CPU, GPU,
DSP, and SDRAM)
• CPU: 700 MHz
ARM1176JZF-S core
(ARM11 family)
• GPU: Broadcom
VideoCore IV, OpenGL
ES 2.0, 1080p30
h.264/MPEG-4 AVC
high-profile decoder
• Memory (SDRAM): 512
Megabytes
• Video outputs:
Composite RCA, HDMI
• Audio outputs: 3.5 mm
jack, HDMI
• Onboard storage: SD,
MMC, SDIO card slot
• 10/100 Ethernet RJ45
onboard network
• Storage via SD/ MMC/
SDIO card slot
11
軟體
• Raspberry Pi使用Linux核心的作業系統
• 例如Raspbian就是一套修改自Debian且針
對Raspberry Pi硬體做最佳化的系統,也是
官方所推薦的作業系統
12
作業系統
• Android 4.0(Ice
Cream Sandwich)
• Arch Linux ARM
• Debian Squeeze
• Firefox OS
• Gentoo Linux
• Google Chrome OS
• Raspberry Pi Fedora
Remix
•
•
•
•
•
•
•
•
Raspbian
Slackware ARM
QtonPi
Slackware ARM
WebOS
RISC OS
FreeBSD
NetBSD
13
Session 1: Raspberry Pi
Setup
Video: Raspberry Pi Starts up….
http://youtu.be/e7x-yjZwY_E
14
系統安裝
15
所需周邊配備
16
在SD記憶卡上安裝映像檔
• Windows所需軟體
– Win32DiskImager(燒錄映像檔的軟體)
– 2013-02-09-wheezy-raspbian.zip(作業系統映
像檔)
• 下載點
– http://www.raspberrypi.org/downloads
17
燒錄程序
1. 選取映像檔
2. 寫入SD
18
測試音效http://www.raspberrypispy.co.uk/2012/06/raspberry-pi-speakers-analog-sound-test/
• 音效輸出預設為自動
模式,你可以底下指
令指定之。
$ sudo amixer cset
numid=3 n
n可以是0、1、2。
0:自動
1:耳機插座
2:HDMI
• Test1:
– rpi裡有些範例程式,請以底下
指令編譯:
$ cd /opt/vc/src/hello_pi/
$ ./rebuild.sh
$ cd hello_audio
然後,就能執行並發出"嗚嗚"聲。
./hello_audio.bin
19
• Test2:
$ sudo wget
http://www.freespecialeffects.
co.uk/soundfx/sirens/police_
s.wav
$ sudo aplay police_s.wav
20
安裝中文環境
http://yehnan.blogspot.tw/2012/08/
raspberry-pi.html
21
• 安裝中文字型,文泉驛微米黑、文泉驛正
黑、文泉驛點陣宋體。
$ sudo apt-get install ttf-wqy-microhei ttfwqy-zenhei xfonts-wqy
22
安裝中文輸入法
$ sudo apt-get install scim scim-tables-zh
scim-chewing
• 其中scim-chewing是注音的輸入法,若是
大陸用的拼音輸入法,請安裝scim-pinyin
• 重開機後,以Ctrl-Space即可切換輸入法,
輸入中文。
23
能跑Android
http://yehnan.blogspot.tw/2012/08/
raspberry-piandroid-40.html
24
Session 2: Linux 基本指令集
• To update the repository database on your
Rpi
$ sudo apt-get update
• To update all current packages with the
latest version
$ sudo apt-get upgrade
• 清除畫面
$ clear
25
桌面與遠端連線
26
遠端登入Pi
葉律佐
2013.04.06
27
登入LXTerminal和x11
• 登入LXTerminal(pi的文字介面)─〉使用
putty軟體的ssh
• 登入X11(pi的視窗介面) ─〉使用Windows
的遠端 連線
28
如何知道Pi的ip(floating ip)
• pi@raspberrypi ~ $ ifconfig
29
遠端登入Pi的文字介面
• 若您想利用 SSH 來登入 Pi ,首先您必須
使用 raspi-config , 將 Pi 上的 SSH
server 選項設定為開啟。
pi@raspberrypi ~ $ sudo raspi-config
之後,重開機
pi@raspberrypi ~ $
sudo reboot
30
設定Pi的ip
• 在windows使用putty軟體
31
在Pi的文字介面輸入帳號(pi)和密碼
(raspberry)
32
Remote Desktop With Raspberry Pi
http://www.jeremymorgan.com/tutorials/rasp
berry-pi/how-to-remote-desktop-raspberrypi/
pi@raspberrypi ~ $ sudo apt-get install xrdp
33
34
安裝Google Chrome
pi@raspberrypi ~ $ sudo apt-get install
chromium-browser
• If you receive any errors running this
command, try running
pi@raspberrypi ~ $ sudo apt-get update
將 Pi 當作網路伺服器
HTTP 伺服器:安裝與執行
Lighttpd
• 指令
pi@raspberrypi ~ $ sudo apt-get install lighttpd
• 安裝結束後,Lighttpd 便會啟動,此時您可
由桌上型電腦的瀏覽器輸入 Pi 的 IP位址。
網路伺服器的歡迎頁面
以下指令會將 Pi 的使用者加入 www-data 群
組內,並設定/var/www 資料夾的編輯權限:
pi@raspberrypi ~ $ sudo adduser pi www-data
pi@raspberrypi ~ $ sudo chown -R wwwdata:www-data /var/www
pi@raspberrypi ~ $ sudo chmod -R 775
/var/www
• 等到下次登入之後,這名 Pi 使用者就能建
立新的網頁了。
• 您可使用任何文字編輯器,例如先前介紹
過的 nano 來編輯網頁。
• 以下指令會建立一個名為index.html 的檔案,
這將會是您網站的起始頁面:
pi@raspberrypi ~ $ nano /var/www/index.html
接著在文字編輯器中輸入下列文字:
Networking/index.html
<!DOCTYPE html>
<html>
<head>
<title>Hello, world!</title>
</head>
<body>
<h1>Hello, world!</h1>
</body>
</html>
• 輸入完畢後,按下 Ctrl+X 來離開 nano。
• 按下 Y 確認儲存檔案,再按下 Enter確認檔案名稱。
此時在瀏覽器中輸入該網頁位址,
所見如下:
建立動態內容: PHP
pi@raspberrypi ~ $ sudo apt-get update
pi@raspberrypi ~ $ sudo apt-get install php5-cgi
pi@raspberrypi ~ $ sudo lighty-enable-mod fastcgi
pi@raspberrypi ~ $ sudo /etc/init.d/lighttpd force-reload
• 上述指令將安裝 PHP 解譯器與啟動 Lighttpd 伺服器中的
FastCGI 模組。
• FastCGI可大幅提高動態網頁內容的處理速度,因此最
好將它啟動。
• 為了完成安裝程序,您需要修改 Lighttpd 的組態檔案內
容。
pi@raspberrypi ~ $ sudo nano /etc/lighttpd/lighttpd.conf
• 請在檔案最後加入下方幾行指令,用來啟動 PHP 及
FastCGI。
fastcgi.server = (".php" => ((
"bin-path" => "/usr/bin/php-cgi",
"socket" => "/tmp/php.socket"
)))
• 儲存組態檔案變更完成之後,請重新啟動網路伺服器。
pi@raspberrypi ~ $ sudo service lighttpd restart
• 為了測試每項功能是否正確地運行,我們
建立了一個名為/var/www/index.php的檔案
來測試,它包含了以下內容:
• pi@raspberrypi ~ $ nano
/var/www/index.php
Networking/index.php
<?php
phpinfo();
?>
• 在您的網頁瀏覽器中開啟這個檔案,您將
會見到如圖 18[用 Pi 傳送動態頁面資訊]所
示的畫面。
PHP檔案從windows編輯後,上
傳到Raspberry Pi
• 上傳到/var/www/底下的php檔案,必須將
權限重設定,否則會產生403 ERR。設定
指令如下:chmod 644 variables2.php
47
FTP Server Install
48
• sudo apt-get install vsftpd
• sudo nano /etc/vsftpd.conf
• uncomment the following options –
local_enable=YES, write_enable=YES
• sudo service vsftpd restart
49
• After doing a test upload using either
Filezille or the ftp client of your choice, you
can check the vsftpd log file to see what it
thinks happened.
• Use tail /var/log/vsftpd.log to view the
log file.
50
如何知道Pi的ip(floating ip)
• pi@raspberrypi ~ $ ifconfig
Using a Static IP Address
http://www.raspberrypitutorials.co.uk/raspberry-pi-static-ip-address/
• Enter the following command into the terminal window.
$ sudo nano /etc/network/interfaces
• This will allow you to edit the file using nano. Change
the line that reads
iface eth0 inet dhcp
• to
iface eth0 inet static
• Below this line enter the following.
address 192.168.100.1
netmask 255.255.255.0
network 192.168.100.0
broadcast 192.168.100.255
gateway 192.168.100.254
利用 WiFi Config 來設定 WiFi 組
態
• 請參考
– [6.5 在 Pi 中加入 WiFi 功能]
Session 3: Scratch
Introduction
新手入門
54
55
Project S4Pi
透過Wifi來控制Raspberry Pi的
LED之亮滅
56
控制流程圖
57
接線圖
58
Scratch controlling the GPIO
on a RaspberryPi
□ wget □
https://dl.dropbox.com/s/gery97qil
dl7ozd/install_scratch_gpio.sh □ O □ /boot/install_scratch_gpio.sh
$ sudo
$ sudo □ /boot/install_scratch_gpio.sh
• 從X11開啟ScratchGPIO->檔案/開啟舊檔/blink11,
按下綠色旗子,就可以看到LED在閃滅。
59
Session 4: Python Introduction
60
啟動IDEL 3
61
在Python Shell鍵入兩行指令(1)
w=3
print (w)
62
在Python Shell鍵入兩行指令(2)
>>>w=20
>>>for i in range(w):print(">"+" "*(wi)+"."*i*2+" "*(w-i)+"<")
• 按兩次ENTER,看看結果‧
63
64
Session 5: GPIO
http://elinux.org/RPi_Lowlevel_peripherals
65
Introduction
• R-Pi offers lower-level interfaces intended to
connect more directly with chips and subsystem
modules
• Signals on the 2x13 header pins include SPI,
I2C, serial UART, 3V3 and 5V power
• Not "plug and play" and require care to avoid
miswiring
• The pins use a 3.3V logic level and are not
tolerant of 5V levels, such as you might find on a
5V powered Arduino
66
• Not yet software-enabled are the flex
cable connectors with CSI (camera serial
interface) and DSI (display serial interface),
and a serial link inside the HDMI
connector called CEC
• They provide 8 GPIO pins plus access to
I²C, SPI, UART), as well as +3.3 V, +5 V
and GND supply lines
67
GPIO
68
RPi Serial Connection
•
The following parameters are needed to connect to the Pi console, and
apply on both Linux and Windows.
– Speed (baud rate): 115200
http://elinux.org/RPi_Serial_Connection
– Bits: 8
– Parity: None
– Stop Bits: 1
– Flow Control: None
69
Connection to a microcontroller
or other peripheral
• The TxD and RxD signals can also be connected directly
to similar signals on a microcontroller board like the
Arduino provided the signals are all at 3V3 levels.
• It's still a good idea to put 2K2 series resistors in the
lines to prevent damage when two outputs are
connected together, which could also happen if a GPIO
input pin is accidentally programmed as output.
• If your microcontroller uses 5V logic levels, level
conversion is usually necessary - see 'Connecting to a
PC' for details.
70
Raspberry Pi - Installing a
RS232 Serial Port
http://www.savagehomeautomatio
n.com/projects/raspberry-piinstalling-a-rs232-serial-port.html
71
Raspberry Pi - RS232 Serial
Interface Options (Revisit)
SOFTWARE USE OF THE
SERIAL PORT
72
make of backup of the two files
that we intend to modify
• sudo cp /boot/cmdline.txt
/boot/cmdline.bak
• sudo cp /etc/inittab /etc/inittab.bak
73
Next
• we need to remove the "console=ttyAMA0,115200" and
"kgdboc=ttyAMA0,115200" configuration parameters from the
"/boot/cmdline.txt" configuration file.
• To edit the file use this command:
$ sudo nano /boot/cmdline.txt
• The file probably contains this default configuration line:
– dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200
console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
• After removing the two configuration parameters, it will look similar
to this:
– dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4
elevator=deadline rootwait
74
Last
• edit the "/etc/inittab" file and comment out
the use of the "ttyAMA0" serial port.
• To edit the file use this command:
$ sudo nano /etc/inittab
75
Now towards the bottom of the file, look for a
configuration line that includes the "ttyAMA0" port
address
76
Place a pound sign ("#") in front of the line to comment it
out. With a pound sign ("#") at the beginning of the line,
Linux will ignore this configuration line.
• Save the "/etc/inittab" file and then issue
this command to reboot the Raspberry Pi:
$ sudo reboot
77
• Now you are ready to use the serial port
with a software application and the
operating system won't interfere with the
port.
• Use the device address "ttyAMA0" in your
application to access this serial port.
78
Project:Create an internet
controlled robot using
Livebots
http://www.instructables.com/id/Cr
eate-an-internet-controlled-robotusing-Livebots/step1/Have-Fun/
79
Serial Terminal
(Communications) Software
http://64.13.91.137:86/Comp/RPi/
Serial.html
80
GPIO Programming
• Python’s GPIO
– OUTPUT: LED blinking
– INPUT: Switch On and Off
– Motor controlling
• Scratch’s GPIO
– ScratchGPIO
81
Advanced GPIO for the
Raspberry Pi
https://pypi.python.org/pypi/RPIO
Extends RPi.GPIO with PWM,
GPIO interrups, TCP socket
interrupts, command line tools and
more
82
RPIO is an advanced GPIO module
for the Raspberry Pi
• PWM via DMA (up to 1µs resolution)
• GPIO input and output (drop-in replacement
for RPi.GPIO)
• GPIO interrupts (callbacks when events occur on
input gpios)
• TCP socket interrupts (callbacks when tcp socket
clients send data)
• Command-line tools rpio and rpio-curses
• Well documented, fast source code with minimal
CPU usage
• Open source (LGPLv3+)
83
RPIO的範例網站
http://pythonhosted.org/RPIO/
84
接線圖
85
Installation
• $ sudo apt-get install python-setuptools
• $ sudo easy_install -U RPIO
86
LXTerminal中,使用nano編輯程式
test.py
87
88
下指令 $ sudo python test.py,接到
pin 11的LED以每0.1秒閃爍
89
思考RPIO.cleanup()指令
釋放GPIO的channel
90
How to use GPIOs on
raspberry pi (Simple I/O, PWM
and UART)
https://sites.google.com/site/semill
eroadt/raspberry-pi-tutorials/gpio
91
Session 6
Raspberry Pi +
Arduino
92
Raspberry Pi + Arduino:
Getting Started
http://niltoid.com/blog/raspberrypi-arduino/
93
方法:http://niltoid.com/blog/raspberry-piarduino/
• Install Arduino
– To get started, install the Arduino IDE on the
Raspberry Pi:
$ sudo apt-get update
$ sudo apt-get install arduino
94
95
96
97
98
99
100
101
102
103
Video
104
Web Resources
•
•
•
•
•
http://info.scratch.mit.edu/zh-hant/Support
http://www.raspberrypi.org/
http://yehnan.blogspot.tw/2012/08/raspberry-pi_13.html
http://linux.vbird.org/linux_basic/
http://www.tutorialspoint.com/python/python_basic_synta
x.htm
• http://cymplecy.wordpress.com/2012/08/26/scratchcontrolling-the-gpio-on-a-raspberrypi/
• http://elinux.org/RPi_Low-level_peripherals
105