raspberry pisosorry.s3.amazonaws.com/raspberrypi/doc/slide/20150516-introduction... ·...

72
Raspberry Pi 入門 May 16, 2015 /CLBC

Upload: others

Post on 29-Oct-2019

5 views

Category:

Documents


0 download

TRANSCRIPT

Raspberry Pi 入門

May 16, 2015 /CLBC

前情提要

2012 COSCUP 報到

http://www.plurk.com/p/h39baa

2013 COSCUP 報到

https://www.flickr.com/photos/coscup/9634483039/

2014 COSCUP 報到

http://www.ithome.com.tw/news/89509

● 2012 年以人工開 10 個櫃台報到 (1119 人 ) ● 2013 年以平板感應 NFC 的方式 ( 約 1500 人 )● 2014 年在報到入口加裝感應門 ( 約 1560 人 )

● 1 分鐘可以有 41 人報到● 平均一人報到時間為 1.46 秒● 有 197 人通過閘門但卡片感應失敗

● 2013/2014 的 NFC 由雪爾萊克 (Sharelike) 提供

近年來 COSCUP 報到方式

http://www.ithome.com.tw/news/89509

● 問題:● 本屆 PyCon 預估會有 800 人參加● 報到區需要 8 人以上

● 問題:● 本屆 PyCon 預估會有 800 人參加● 報到區需要 8 人以上

● 可能的解決方法:● 每個人上傳照片 , 會場以人臉識別報到● 寫手機 App, 讓所有人安裝 , 報到用手機感應● 寄發 NFC 識別證 , 報到時用識別證感應

● 問題:● 本屆 PyCon 預估會有 800 人參加● 報到區需要 8 人以上

● 可能的解決方法:● 每個人上傳照片 , 會場以人臉識別報到● 寫手機 App, 讓所有人安裝 , 報到用手機感應● 寄發 NFC 識別證 , 報到時用識別證感應

● 考慮因素:● 實做時間 , 價格 , 學習性

NFC 近場通訊

11

http://blogs.ifsworld.com/2013/06/come-on-apple-nfc-is-good-for-business/

NFC 線圈

12http://us.moo.com/nfc/http://blogs.ifsworld.com/2013/06/come-on-apple-nfc-is-good-for-business/

NFC 線圈

可做成貼紙

13http://2d-code.co.uk/nfc-business-card/http://us.moo.com/nfc/http://blogs.ifsworld.com/2013/06/come-on-apple-nfc-is-good-for-business/

NFC 線圈

可做成貼紙

NFC 名片和手機互動的應用

FAQ 第一名:

NFC 和 RFID 有什麼不同 ?

● 無線射頻辨識 (Radio Frequency Identification)

RFID

http://www.intechopen.com/books/designing-and-deploying-rfid-applications/rfid-middleware-design-and-architecture

● 被動式 RFID● 晚上你坐在陽台 , 開啟燈光 , 鄰居走過來時因為

被燈光照到 , 所以你看到他

● 主動式 RFID● 晚上你坐在陽台 , 開啟燈光 , 鄰居看到光以後也

開了他的燈 , 所以你就看到他

RFID 情境

RFID 使用頻段

https://rapidnfc.com/blog/72/the_difference_between_nfc_and_rfid_explained

● 剛剛的鄰居經過你家 , 你邀請他走進陽台 , 然後開始聊天 , 交換了彼此之間訊息

NFC 情境

● 剛剛的鄰居經過你家 , 你邀請他走進陽台 , 然後開始聊天 , 交換了彼此之間訊息

近 + 交換訊息

NFC 情境

NFC = RFID + Exchange Data

NFC Protocol Stack

http://shop.oreilly.com/product/0636920021193.do

RFID vs. NFC

https://rapidnfc.com/blog/72/the_difference_between_nfc_and_rfid_explained

FAQ 第二名:

那我們到底是用 NFC 還是 RFID ?

● 識別證為被動式 RFID,● Reader 是採用 NFC

我們的 Solution

FAQ 第三名:

為什麼不是 RFID 識別證 +RFID reader ?

● 使用 Adafruit PiTFT 觸控螢幕

● 觸控螢幕 = 顯示 + 觸控 (SPI)● 常用的 RC522 RFID reader 使用 SPI 做資料交換

因為螢幕

https://www.adafruit.com/products/1601

因此我們用 NFC(UART) 做 RFID Reader

常用的 NFC Reader + RFID Tag:PN532 + MiFare Classic Cards

https://www.adafruit.com/products/364

NFC Data Exchange Format (NDEF)

Beginning NFC - Near Field Communication with Arduino, Android, and PhoneGap

● 硬體

● NFC Reader : PN532● RFID Tag : MiFare 感應白卡

● 軟體

● libnfc, pynfc

● 邏輯

● 事先儲存感應白卡的 UID● 當 reader 讀到 tag 的 UID 時做比對

實做 NFC 感應所需要的準備

報到功能發想

● 有帶識別證● 刷卡報到

● 沒帶識別證● 當場補卡 + 報到

● 領 t-shirt● 需要有識別證

基本功能

資料庫架構 : SQLite3 一張 Table

● CREATE TABLE regist (

reg_no TEXT PRIMARY KEY,

uid TEXT,

ticket_type TEXT,

qrcode TEXT,

fullname TEXT,

nickname TEXT,

email TEXT,

gender TEXT,

nationality TEXT,

food TEXT,

tshirt TEXT,

mobile TEXT,

zip TEXT,

address TEXT,

attend_type TEXT,

regist_wtime TEXT DEFAULT 0,

tshirt_wtime TEXT DEFAULT 0,

is_sync TEXT DEFAULT 0);

建置開發環境

36

環境設定: Serial + WiFi

1. 樹莓派 Serial 連線

2. 樹莓派 WiFi 連線

用 Serial 來設定 WiFi

3. 筆電、手機、樹莓派 在同一個 LAN

37

● 以 USB 轉 TTL 傳輸線和 Pi 相連

● 接線方式● 黑色: Pin 6 (GND)● 白色: Pin 8 (Tx)● 綠色: Pin 10 (Rx)● 紅色:不接

Serial 連線方式

38

Raspberry Pi B 的 GPIO (P1)

黑色線接 6 號 ( 第三根 )

白色線接 8 號 ( 第四根 )

綠色線接 10 號 ( 第五根 )

http://elinux.org/RPi_Low-level_peripherals

39

Raspberry Pi B+ 的 GPIO (P1)

黑色線接 6 號 ( 第三根 )

白色線接 8 號 ( 第四根 )

綠色線接 10 號 ( 第五根 )

http://www.raspberrypi-spy.co.uk/2014/07/raspberry-pi-b-gpio-header-details-and-pinout/

40

● 安裝驅動程式 , http://goo.gl/QC5Q3O

● 從裝置管理員找到 COM 的埠號 ( 本例為 COM9)● 下載 putty, http://goo.gl/zdD9G9

● 執行 putty● Session● Serial line 填 COM9● Speed 填入 115200● Open !

● 沒畫面 , 重新插拔電源

Serial Port in Windows

41

● $ ls /dev/ttyUSB*● 開啟 putty

● 選擇 Session● 在 Serial line 填入 /dev/ttyUSB0 ( 本例為 ttyUSB0)● Speed 填入 115200● Open !

Serial Port in Linux

42

● 安裝驅動程式 , http://goo.gl/htlt3F● 重開機生效● $ ls /dev/cu*

● 如果有 /dev/cu.usbserial● $ screen /dev/cu.usbserial 115200

● 如果沒畫面 , 重新插拔電源

Serial Port in Mac

43

● 預設帳號 /密碼: pi / raspberry● 如果沒有畫面 , 將電源重新插拔

● 如果出現亂碼 , 確定 baud rate 為 115200

連線成功

44

● 登入畫面

● pi 是登入的使用者

● @表示”在”● raspberrypi 是主機名稱

● ~表示在家目錄 (home directory)● $表示該使用者所使用的 shell( 一種文字工具介面 )● 例如 $ nano myfile.txt

● 表示用 nano編輯器開 myfile.txt檔案

符號說明

45

$ sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

# /etc/wpa_supplicant/wpa_supplicant.conf

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev

update_config=1

network={

ssid="room142"

key_mgmt=NONE

auth_alg=OPEN

wep_key0=0223635251

}

在文字模式下設定無線網路

46

$ sudo ifdown wlan0

$ sudo ifup wlan0

$ sudo kill -9 $(ps -ef | grep wpa | awk '{print $2}')

$ sudo wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf

$ sudo dhclient wlan0

指令列設定 WiFi - 連線

47

$ ifconfig wlan0

IP = 192.168.43.102

連線成功 查詢 IP

48

● 預設帳號 /密碼: pi / raspberry

有了 IP, 用 SSH 連線

安裝 PiTFT 觸控螢幕

● $ sudo apt-get update

● $ curl -SLs https://apt.adafruit.com/add | sudo bash

● $ sudo apt-get install -y adafruit-pitft-helper

● $ sudo vim /boot/config.txt

[pi1]

device_tree=bcm2708-rpi-b-plus.dtb

[pi2]

device_tree=bcm2709-rpi-2-b.dtb

[all]

dtparam=spi=on

dtparam=i2c1=on

dtparam=i2c_arm=on

dtoverlay=pitft28r,rotate=90,speed=32000000,fps=20

安裝 PiTFT 驅動程式 (1)

https://learn.adafruit.com/adafruit-pitft-28-inch-resistive-touchscreen-display-raspberry-pi?view=all

● $ sudo mv /usr/share/X11/xorg.conf.d/99-fbturbo.conf ~

● $ sudo vim /etc/modules

stmpe-ts

● $ sudo mkdir /etc/X11/xorg.conf.d

● $ sudo vim /etc/X11/xorg.conf.d/99-calibration.conf

Section "InputClass"

Identifier "calibration"

MatchProduct "stmpe-ts"

Option "Calibration" "3800 200 200 3800"

Option "SwapAxes" "1"

EndSection

安裝 PiTFT 驅動程式 (2)

https://learn.adafruit.com/adafruit-pitft-28-inch-resistive-touchscreen-display-raspberry-pi?view=all

● $ sudo vim /usr/share/X11/xorg.conf.d/99-pitft.conf

Section "Device"

Identifier "Adafruit PiTFT"

Driver "fbdev"● Option "fbdev" "/dev/fb1"● EndSection

安裝 PiTFT 驅動程式 (3)

https://learn.adafruit.com/adafruit-pitft-28-inch-resistive-touchscreen-display-raspberry-pi?view=all

安裝 NFC Reader

● $ sudo apt-get install libusb-dev● $ sudo apt-get install libpcsclite-dev

安裝必要套件 @ Pi

PI PN532● PIN6 GND ● PIN4 5.0V ● PIN8 TX (TX to TX)● PIN10 RX (RX to RX)

接線

● $ cd ~● $ wget http://libnfc.googlecode.com/files/libnfc-

1.7.0-rc7.tar.gz● $ tar zxvf libnfc-1.7.0-rc7.tar.gz● $ cd libnfc-1.7.0-rc7● $ ./configure --with-drivers=pn532_uart --with-

drivers=all ● $ make && sudo make install

下載與編譯 libnfc @ Pi

● $ sudo mkdir -p /usr/local/etc/nfc/devices.d● $ sudo cp

contrib/libnfc/pn532_uart_on_rpi.conf.sample /usr/local/etc/nfc/devices.d/pn532_uart_on_rpi.conf

● $ sudo vim /usr/local/etc/nfc/libnfc.conf

name = "PN532 board via UART"

connstring = pn532_uart:/dev/ttyAMA0

allow_intrusive_scan = true

複製設定檔 @ Pi

啟用 UART = 停用 Serial

啟用 UART = 停用 Serial

啟用 UART = 停用 Serial

● $ cd ~/libnfc-1.7.0-rc7/utils

● $ LIBNFC_LOG_LEVEL=3 ./nfc-list

● $ cd ~/libnfc-1.7.0-rc7/examples

● $ ./nfc-poll

/home/pi/libnfc-1.7.0-rc7/examples/.libs/lt-nfc-poll uses libnfc 1.7.0-rc7

NFC reader: pn532_uart:/dev/ttyAMA0 opened

NFC device will poll during 30000 ms (20 pollings of 300 ms for 5 modulations)

ISO/IEC 14443A (106 kbps) target:

ATQA (SENS_RES): 00 04

UID (NFCID1): fd 81 90 b5

SAK (SEL_RES): 08

測試 @ Pi

● $ sudo apt-get install sqlite3● $ sudo apt-get install -y python-pip● $ sudo pip install requests

安裝必要軟體

● Single(ATOM) statement● 合併 SQL語法● BEGIN TRANSACTION; SQL; COMMIT;

● 批次處理改為單次處理● execute改為 executescript

SQLite3 Tunning

● 情境● Remote server 有更新程式 / 設定檔需要部署

● 實做● Local host 開機後每分鐘檢查是否需要更新?● 當有網路以後會下載 remote server更新 script● 在下次重開機前將不會再更新

部署策略

● 每個使用者都有各別 DISPLAY 環境變數

● 切換成當前的使用者身份執行

● 實做● mysh = '/home/pi/sh/mysh.sh'● sudoPassword = 'raspberry'● os.system('echo %s|sudo -u pi %s' % (sudoPassword,

mysh.sh))

背景喚醒GUI 程式

http://simonmcc.blogspot.tw/2013/09/raspberry-pi-boot-straight-into-gui.html

RC522 接線圖

http://fuenteabierta.teubi.co/2013/07/utilizando-el-lector-nfc-rc522-en-la.html

● | Name | Pin # | Pin name |

|----------|--------- |---------------- |

| SDA | 24 | GPIO8 |

| SCK | 23 | GPIO11 |

| MOSI | 19 | GPIO10 |

| MISO | 21 | GPIO9 |

| IRQ | None | None |

| GND | Any | Any Ground |

| RST | 22 | GPIO25 |

| 3.3V | 1 | 3V3 |

腳位說明

● $ sudo apt-get install python-dev python-pip

安裝所需套件

● $ git clone https://github.com/lthiery/SPI-Py● $ cd SPI-Py ● $ sudo python setup.py install

下載函式庫

● $ git clone https://github.com/mxgxw/MFRC522-python● $ cd MFRC522-python● $ sudo python Read.py

Card detected

Card read UID: 45,112,232,124,201

Size: 8

AUTH ERROR!!

AUTH ERROR(status2reg & 0x08) != 0

AUTH ERROR

下載 Reader 程式 & 測試

● 將結果丟到主畫面

$ export DISPLAY=":0"

ssh登入後工作方式

Raspberry Pi Rocks the World

Thanks