翰尼斯有限公司、CAVE教育團隊 葉律佐 yeh062005@gamil.com 2013.06.14 超級小電腦 Raspberry Pi 翰尼斯有限公司、CAVE教育團隊 葉律佐 yeh062005@gamil.com 2013.06.14
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 變多媒體娛樂中心
關於樹莓(Raspberry) 的故事 「樹莓」是一棵常綠喬木,非常喜歡陽光,不耐蔭,怕積水的環境,所以必須是排水性良好的土壤,很能耐旱。 她第一次被發現是在1792年由一位蘇格蘭植物學家所收集,並命名為東方草莓樹。 「樹莓」是一株相當知名的蜜源樹,因為她在春末時會開出有蠟質白或粉紅的小白花,有濃濃的蜂蜜味道,是蜜蜂的最愛。 「樹莓」的掛果相當長久須一年左右才能成熟,掛果量相當多,是相當著名的觀果樹,果肉是甜的,可食。
樹莓集草本、木本植物優勢於一體,形似草莓卻勝於草莓。 紅樹莓是當今發現的天然鞣化酸含量最高的食物。 鞣化酸是一種能有效預防和治療癌症的物質,對結腸、宮頸、乳腺和胰臟癌等有特殊療效。 因此被世界公認為第三代水果,在國際國內市場上前景極為廣闊,是中國目前調整農業産業結構,發展高效種植業的好專案。
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.
以Python為主要程式語言的電腦主機板 Raspberry Pi 以Python為主要程式語言的電腦主機板
樹莓派的圖示
硬體規格 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
軟體 Raspberry Pi使用Linux核心的作業系統 例如Raspbian就是一套修改自Debian且針對Raspberry Pi硬體做最佳化的系統,也是官方所推薦的作業系統
作業系統 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 WebOS RISC OS FreeBSD NetBSD
Session 1: Raspberry Pi Setup Video: Raspberry Pi Starts up…. http://youtu.be/e7x-yjZwY_E
系統安裝
所需周邊配備
在SD記憶卡上安裝映像檔 Windows所需軟體 下載點 Win32DiskImager(燒錄映像檔的軟體) 2013-02-09-wheezy-raspbian.zip(作業系統映像檔) 下載點 http://www.raspberrypi.org/downloads
燒錄程序 1. 選取映像檔 2. 寫入SD
測試音效http://www. raspberrypi-spy. co 測試音效http://www.raspberrypi-spy.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
Test2: $ sudo wget http://www.freespecialeffects.co.uk/soundfx/sirens/police_s.wav $ sudo aplay police_s.wav
安裝中文環境 http://yehnan.blogspot.tw/2012/08/raspberry-pi.html
安裝中文字型,文泉驛微米黑、文泉驛正黑、文泉驛點陣宋體。 $ sudo apt-get install ttf-wqy-microhei ttf-wqy-zenhei xfonts-wqy
安裝中文輸入法 $ sudo apt-get install scim scim-tables-zh scim-chewing 其中scim-chewing是注音的輸入法,若是大陸用的拼音輸入法,請安裝scim-pinyin 重開機後,以Ctrl-Space即可切換輸入法,輸入中文。
能跑Android http://yehnan.blogspot.tw/2012/08/raspberry-piandroid-40.html
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
桌面與遠端連線
遠端登入Pi 葉律佐 2013.04.06
登入LXTerminal和x11 登入LXTerminal(pi的文字介面)─〉使用putty軟體的ssh 登入X11(pi的視窗介面) ─〉使用Windows的遠端 連線
如何知道Pi的ip(floating ip) pi@raspberrypi ~ $ ifconfig
pi@raspberrypi ~ $ sudo raspi-config 若您想利用 SSH 來登入 Pi ,首先您必須使用 raspi-config , 將 Pi 上的 SSH server 選項設定為開啟。 pi@raspberrypi ~ $ sudo raspi-config 之後,重開機pi@raspberrypi ~ $ sudo reboot
設定Pi的ip 在windows使用putty軟體
在Pi的文字介面輸入帳號(pi)和密碼(raspberry)
Remote Desktop With Raspberry Pi http://www. jeremymorgan pi@raspberrypi ~ $ sudo apt-get install xrdp
pi@raspberrypi ~ $ sudo apt-get update 安裝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 www-data: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> <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 模組。
"bin-path" => "/usr/bin/php-cgi", "socket" => "/tmp/php.socket" 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
FTP Server Install
sudo apt-get install vsftpd sudo nano /etc/vsftpd.conf uncomment the following options – local_enable=YES, write_enable=YES sudo service vsftpd restart
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.
如何知道Pi的ip(floating ip) pi@raspberrypi ~ $ ifconfig
Using a Static IP Address http://www. raspberrypi-tutorials. co 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 新手入門
透過Wifi來控制Raspberry Pi的LED之亮滅 Project S4Pi 透過Wifi來控制Raspberry Pi的LED之亮滅
控制流程圖
接線圖
Scratch controlling the GPIO on a RaspberryPi $ sudo □ wget □ https://dl.dropbox.com/s/gery97qildl7ozd/install_scratch_gpio.sh □ -O □ /boot/install_scratch_gpio.sh $ sudo □ /boot/install_scratch_gpio.sh 從X11開啟ScratchGPIO->檔案/開啟舊檔/blink11,按下綠色旗子,就可以看到LED在閃滅。
Session 4: Python Introduction
啟動IDEL 3
在Python Shell鍵入兩行指令(1) w=3 print (w)
在Python Shell鍵入兩行指令(2) >>>w=20 >>>for i in range(w):print(">"+" "*(w-i)+"."*i*2+" "*(w-i)+"<") 按兩次ENTER,看看結果‧
Session 5: GPIO http://elinux.org/RPi_Low-level_peripherals
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
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
GPIO
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 Bits: 8 Parity: None Stop Bits: 1 Flow Control: None http://elinux.org/RPi_Serial_Connection
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.
Raspberry Pi - Installing a RS232 Serial Port http://www.savagehomeautomation.com/projects/raspberry-pi-installing-a-rs232-serial-port.html
Raspberry Pi - RS232 Serial Interface Options (Revisit) SOFTWARE USE OF THE SERIAL PORT
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
Next $ sudo nano /boot/cmdline.txt 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
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
Now towards the bottom of the file, look for a configuration line that includes the "ttyAMA0" port address
Place a pound sign ("#") in front of the line to comment it out 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
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.
Project:Create an internet controlled robot using Livebots http://www.instructables.com/id/Create-an-internet-controlled-robot-using-Livebots/step1/Have-Fun/
Serial Terminal (Communications) Software http://64.13.91.137:86/Comp/RPi/Serial.html
GPIO Programming Python’s GPIO Scratch’s GPIO OUTPUT: LED blinking INPUT: Switch On and Off Motor controlling Scratch’s GPIO ScratchGPIO
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
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+)
RPIO的範例網站 http://pythonhosted.org/RPIO/
接線圖
Installation $ sudo apt-get install python-setuptools $ sudo easy_install -U RPIO
LXTerminal中,使用nano編輯程式test.py
下指令 $ sudo python test.py,接到pin 11的LED以每0.1秒閃爍
思考RPIO.cleanup()指令 釋放GPIO的channel
How to use GPIOs on raspberry pi (Simple I/O, PWM and UART) https://sites.google.com/site/semilleroadt/raspberry-pi-tutorials/gpio
Session 6 Raspberry Pi + Arduino
Raspberry Pi + Arduino: Getting Started http://niltoid.com/blog/raspberry-pi-arduino/
$ sudo apt-get install arduino 方法:http://niltoid.com/blog/raspberry-pi-arduino/ Install Arduino To get started, install the Arduino IDE on the Raspberry Pi: $ sudo apt-get update $ sudo apt-get install arduino
Video
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_syntax.htm http://cymplecy.wordpress.com/2012/08/26/scratch-controlling-the-gpio-on-a-raspberrypi/ http://elinux.org/RPi_Low-level_peripherals