raspberry pi as a wireless router
DESCRIPTION
TRANSCRIPT
● Raspberry Pi 好好玩 - 應用篇
● Raspberry Pi 好好玩 - GPIO 篇
相關議程
● 信用卡大小般的電腦
Raspberry Pi 是什麼 ?
http://www.flickr.com/photos/fotero/7697063016/
● SoC Broadcom BCM2835● CPU: ARM11 family, 700 MHz● GPU: Broadcom VideoCore IV, OpenGL ES 2.0,
1080p30 H.264/MPEG-4 AVC decoder● Memory: 512 Mb SDRAM
● Video outputs: Composite, HDMI● Audio outputs: 3.5 mm jack, HDMI● Onboard storage: SD, MMC, SDIO card slot● Onboard 10/100 Ethernet RJ45
Raspberry Pi 硬體規格 (Model B)
週邊 + OS
http://www.raspberrypi-tutorials.co.uk/starting-up-your-raspberry-pi-device/set-raspberry-pi/
Raspberry Pi 怎麼玩 ?
Raspberry Pi 還可以怎麼玩 ?
看了很多 還是不知道怎麼開始
Raspberry Pi 的軟硬整合之道
用 Raspberry Pi 體驗嵌入式系統開發
- 自己動手做無線路由器
● 為特定應用而設計的專用計算機系統 ● 通常執行的是帶有特定要求的預先定義的任務
- wikipedia
● 常見作法 : 特製硬體 + 嵌入式 Linux+Driver+ 客製化應用程式
嵌入式系統
● 硬體設計 & 驗證
● 軟體開發 & 系統整合
● 測試
嵌入式系統開發流程
● 無線 (wireless) + 路由器 (router)
無線路由器
使用情境
http://www.techiestate.com/how-to-set-up-a-wireless-router/
使用情境
http://www.techiestate.com/how-to-set-up-a-wireless-router/
實做目標
● 取得對外網路● 有線 / 無線?
● 如何發射訊號?● 網路的分配?● 使用者介面?● … 未知的問題
情境 = 問題
使用情境
http://www.techiestate.com/how-to-set-up-a-wireless-router/
1. 取得對外網路
2. 有線 / 無線介接
3. 發射無線訊號
5. 使用者介面
4. 網路的分配
使用情境
http://www.techiestate.com/how-to-set-up-a-wireless-router/
1. 取得對外網路
2. 有線 / 無線介接
3. 發射無線訊號
4. 網路的分配
5. 使用者介面
+ 很多未知的問題
一個我們熟悉的裝置
背後是許多知識 & 功能的集合
● 建構嵌入式系統開發環境● 設定無線 / 有線封包轉送
● 設定 DHCP/Wireless Access Point● 移植小型的網頁伺服器● 撰寫 CGI 程式
● 以 Python 控制硬體
學習目標
體驗 1 - 建構嵌入式系統開發環境
● x86, ARM, MIPS, PowerPC● 效能 ? 省電 ? 價格 ?
● 一個 ARM 架構
● 有 ARMv1, ARMv2..., ARMv8● ARM v6: Raspberry Pi Broadcom BCM2835
指令集架構
真八核 PS2 Power Macintoshes80386
● 同樣的程式碼在不同的平台 ( 架構 ) 上執行
● 問題?● addressing space● little endian/big endian● alignment● …
● 編譯器 ( 們 ) 會幫我們解決一切?
Porting( 移植 )
● 一堆工具 + library● Compiler● Linker● Assembler● Debugger● Libraries● ...
Toolchain
http://www.aboutdebian.com/compile.htm
● 在完善的開發平台下開發其他平台的軟體
● Ex: 在 x86 平台下開發 ARM 的軟體
● gcc foo.c -o foo● arm-linux-gnueabi-gcc foo.c -o foo
Cross Compile 交叉式編譯
● ./configure --build --host --target● --build: the architecture of the build machine● --host: the architecture that you want the file to run on● --target: the machine that GCC will produce code for
● 在 build 下編好 , 在 host 執行 target 版本
Ex: ./configure
--build=i686-pc-linux-gnu --host=arm-linux--target=mips-linux
利用 autotools
● 下載 Raspberry Pi 的 toolchain● https://github.com/raspberrypi/tools
● 三個選擇
● arm-bcm2708-linux-gnueabi● arm-bcm2708hardfp-linux-gnueabi ● gcc-linaro-arm-linux-gnueabihf-raspbian( 推薦 )
● 加到環境變數
● echo 'export PATH=\$PATH:/path/to/toolchain' >> ~/.bashrc
建構 cross-compiling 環境
體驗 2 - 開發嵌入式系統所需功能
● 假設● 有一個東西可以介接有線和無線
● 有一個東西可以發射無線網路的訊號
● 有一個東西可以分配網路
● 做一個控制的使用者介面
想像所需要的功能
● 假設● 有一個東西可以介接有線和無線
– iptables & ip forwarding● 有一個東西可以發射無線網路的訊號
● 有一個東西可以分配網路
● 做一個控制的使用者介面
想像所需要的功能
iptables
● netfilter( 封包過濾機制 )● 根據封包的表頭採取動作
● iptables( 用戶空間程式 )● 封包過濾軟體
Linux 封包過濾概念
封包過濾規則動作與分析流程
http://linux.vbird.org/linux_server/0250simple_firewall.php
iptables
http://linux.vbird.org/linux_server/0250simple_firewall.php
● NAT = Network Address Translation● 來源與目的之間的 IP 和 port 轉換● POSTROUTING
– SNAT/MASQUERADE
NAT Table
● iptables -A INPUT -i wlan0 -j ACCEPT● iptables -t nat -A POSTROUTING -s
192.168.77.0/24 -o eth0 -j MASQUERADE● sysctl net.ipv4.ip_forward=1
封包轉送規則
● 假設● 有一個東西可以介接有線和無線
– iptables & ip forwarding● 有一個東西可以發射無線網路的訊號
– AP mode● 有一個東西可以分配網路
● 做一個控制的使用者介面
想像所需要的功能
● Station (STA) infrastructure mode● AccessPoint (AP) infrastructure mode● Monitor (MON) mode● Ad-Hoc (IBSS) mode● Wireless Distribution System (WDS) mode● Mesh
無線網卡工作模式
http://wireless.kernel.org/en/users/Documentation/modes
● AP(Master) mode● Ralink rt2400 / rt2500● Broadcom 43xx ● Realtek RTL8180● Texas Instruments ACX100/ACX111● Intel PRO/Wireless (ipwXXXX) series● ZyDAS ZD1211● ...
選擇可以做 AP 的無線網卡
https://help.ubuntu.com/community/WifiDocs/MasterMode
● User space daemon for access point● Support IEEE 802.1X/WPA/WPA2/EAP
● 硬體的支援 + 軟體的實做
hostapd
http://www.netzmafia.de/skripten/unix/linux-daemon-howto.html
● 下載 hostapd(RTL8188CUS)● http://bit.ly/t0HWH1
● 設定編譯參數 configure● CC=arm-linux-gnueabihf-gcc ./configure --host=arm-
linux --prefix=/path/to/install● 編譯
● make && make install
Cross Compile hostapd
● 假設● 有一個東西可以介接有線和無線
– iptables & ip forwarding● 有一個東西可以發射無線網路的訊號
– AP mode● 有一個東西可以分配網路
– dhcp daemon● 做一個控制的使用者介面
想像所需要的功能
● 分派 IP, 給予相關資訊
● 管理租約
● isc-dhcp-server
DHCP Daemon
http://linux.vbird.org/linux_server/0340dhcp.php
● 下載 dhcpd● wget
ftp://ftp.isc.org/isc/dhcp/4.1-ESV-R8/dhcp-4.1-ESV-R8.tar.gz
● 設定編譯參數 configure● CC=arm-linux-gnueabihf-gcc ./configure --host=arm-linux
--prefix=/path/to/install
● 編譯
● make && make install
Cross Compile dhcpd
● 假設● 有一個東西可以介接有線和無線
– iptables & ip forwarding● 有一個東西可以發射無線網路的訊號
– AP mode● 有一個東西可以分配網路
– dhcp daemon● 做一個控制的使用者介面
想像所需要的功能
體驗 3 – 在適合的環境下開發 UI
App 在嵌入式系統是如何顯示的 ?
Framebuffer
Hardware
Application
GUI-ToolKit
Native App vs. Browser Based App
/dev/fb driver
SoC
Native App
/dev/fb driver
SoC
1. 根據 toolkit 寫 ap2. 執行速度快3. 可以和系統直接以 IPC溝通
Browser Based App
/dev/fb driver
SoC
1. 可支援 HTML5 和 CSS32. 開發速度快3. 需要以 plugin方式擴充
看看別人怎麼做
主要功能表
Browser based = web server + 程式
● 一個軟體● 回應從 80/8080 port進來的 HTTP 要求
● 可透過 CGI或module方式擴充
● 如 Apache, Nginx, Boa
網頁伺服器
http://www.resultantsys.com/index.php/general/what-is-a-web-application-server/
● 一個小型的網頁伺服器● 優點
● 小 , 25 支 C共 6899 行● 缺點
● 沒有 access control(authentication)● 無法 chroot● 不支援 SSL
● GPLv2 License
Boa Web Server (~2005)
● 下載 boa● wget http://www.boa.org/boa-0.94.13.tar.gz
● 設定編譯參數 configure● CC=arm-linux-gnueabihf-gcc ./configure --host=arm-
linux
● 編譯
● make && make install
Cross Compile Boa
有了 web server再找一個配合的動態語言
● Raspberry Pi內建
● Script語言● 強大的數學功能● 穩定的套件管理● 可結合多種web server
● 以 CGI方式執行 , 存取硬體資源
Python
● CGI = Common Gateway Interface● 一個讓程式可以在 web server被執行的標準
● C, Perl, Python, PHP...● 輸出 /輸入轉向
● Python CGI
CGI
體驗 4 - 存取硬體資源
● 控制硬體 (GPIO)● 讀寫 shell command● 用 AJAX 做畫面
以 Python 做控制
● A generic pin on an IC
General Purpose Input Output(GPIO)
http://raspberrypihobbyist.blogspot.tw/2012/09/so-many-inputs-so-few-gpio-pins.html
Raspberry Pi 的 GPIO
http://elinux.org/RPi_Low-level_peripherals
SPI / I2C / UART / PWM
線路圖
● Import module ● Define pin numbering● Setup up a channel● Input/Output● Cleanup
Python Code Flow
green = 26 , red = 24 # Define pin numbering
try:
import RPi.GPIO as GPIO # Import module
except RuntimeError:
print ("import RPi.GPIO error!!")
GPIO.setmode(GPIO.BOARD) # Setup up a channel
cmd = "sudo sysctl net.ipv4.ip_forward=1"
result = commands.getstatusoutput(cmd)
GPIO.output(green, GPIO.HIGH) # Input/Output
GPIO.output(red, GPIO.LOW) # # Input/Output
Python Code Example
DEMO
● 建構嵌入式系統開發環境● 開發嵌入式系統所需功能
● 設定 iptables 和封包轉送 ( 介接有線和無線 )● 交叉編譯 hostapd( 發射無線網路的訊號 )● 交叉編譯 isc-dhcp-server( 分配網路 )
● 在適合的環境下開發 UI● 交叉編譯 Boa( 使用者介面 )● 交叉編譯 Python( 動態語言 )
● 存取硬體資源
● 接線 , 用 Python存取硬體 (GPIO)
總結
Raspberry Pi Rocks the World
Thanks