顯示具有 raspberry pi 標籤的文章。 顯示所有文章
顯示具有 raspberry pi 標籤的文章。 顯示所有文章

2014/10/03

Connect PN532 NFC to Raspberry Pi

1. Enable SPI on Raspberry Pi


PN532 NFC提供3種介面, SPI, I2C, 與UART
先選SPI做測試,

在RPi上面, SPI與I2C預設是關的, 我們要先打開它,

$ sudo nano /etc/modprobe.d/raspi-blacklist.conf


會看到裡面有一些黑名單

blacklist spi-bcm2708
blacklist i2c-bcm2708
blacklist snd-soc-pcm512x
blacklist snd-soc-wm8804


我們要用到spi, 所以把它mark掉

#blacklist spi-bcm2708

存檔離開, 然後重開機讓設定生效


重開機之後, 來看看SPI driver有沒有起來

$ ls /dev/spidev*
/dev/spidev0.0  /dev/spidev0.1

會看到有兩個 SPI driver, 前面的數字是第幾個SPI peripheral,
兩個數字都是1表示 RPi 只有一個SPI port
第二個數字是 Chip Selectt 使用pin CS0 & CS1


2. Introduction to SPI


Serial Peripheral Interface (SPI), 跟 I2C很像
是同步序列資料協定(synchronous serial data link)

常用的接角定義:
  • SCLK—Serial Clock(自master輸出), 有時也叫SCK
  • MOSI/SIMO—Master Output, Slave Input(自master輸出)
  • MISO/SOMI—Master Input, Slave Output(自slave輸出)
  • SS—Slave Select(active low;自master輸出), 在RPi上叫SPI_CE0_N, SPI_CE1_N, 在PN532上叫SSEL

在使用上, 1個master可以對應多個slave
可能的接法如下
如果device扮演的是Master, 那麼它的MOSI就等於是它的Output, MISO就是它的Input
如果device扮演的是Slave, 那麼它的MOSI就等於是它的Input, MISO就是它的Output

3. Connect PN532 to RPi via SPi


底下是 RPi 的接腳配置


其中跟 SPI 相關的有:
SPI_MOSI
SPI_MISO
SPI_SCLK
SPI_CE0_N
SPI_CE1_N

我們這樣接:

  • PN532 的 3.3V 接到 RPi 的3V3 (可接1或17的位置)
  • PN532 的 GND 接到 RPi 的接地 (可接 6, 9, 14, 20, 25, 30, 34, 39, 我是接39)
  • PN532 的 SCK 接到 RPi 的 SPI_SCLK (23的位置)
  • PN532 的 MISO 接到 RPi 的 SPI_MISO (21的位置)
  • PN532 的 MOSI 接到 RPi 的 SPI_MOSI (19的位置)
  • PN532 的 SSEL 接到 RPi 的SPI_CE0_N


4. Install libnfc in RPi


接好之後, 有很多種方式可以與PN532溝通, 用libnfc是其中一種方便的方式

首先先更安裝有dependency的library
sudo apt-get update
$ sudo apt-get install libusb-dev libpcsclite-dev

然後抓libnfc source code, 可以上它們的網站來找
https://code.google.com/p/libnfc/

其中stable version在寫這篇文章的時候在這裡
https://bintray.com/nfc-tools/sources/libnfc

抓好之後, 將它解壓縮

$ tar -xf libnfc-1.7.1.tar.bz2

然後compile & install
$ cd libnfc-1.7.1
$ ./configure --prefix=/usr --sysconfdir=/etc
$ make
$ sudo make install

修改它的config
$ cd /etc
$ sudo mkdir nfc
$ sudo nano /etc/nfc/libnfc.conf

把底下copy paste
# Allow device auto-detection (default: true)
# Note: if this auto-detection is disabled, user has to set manually a device
# configuration using file or environment variable
allow_autoscan = true

# Allow intrusive auto-detection (default: false)
# Warning: intrusive auto-detection can seriously disturb other devices
# This option is not recommended, user should prefer to add manually his device.
allow_intrusive_scan = false

# Set log level (default: error)
# Valid log levels are (in order of verbosity): 0 (none), 1 (error), 2 (info), 3 (debug)
# Note: if you compiled with --enable-debug option, the default log level is "debug"
log_level = 1

# Manually set default device (no default)
# To set a default device, you must set both name and connstring for your device
# Note: if autoscan is enabled, default device will be the first device available in device list.
device.name = "Itead_PN532_SPI"
device.connstring = "pn532_spi:/dev/spidev0.0:500000"

設定完成, 來試試看有沒有問題, 拿一張悠遊卡放在PN532旁邊
$ nfc-list
$ nfc-list
nfc-list uses libnfc 1.7.1
NFC device: pn532_spi:/dev/spidev0.0 opened
1 ISO14443A passive target(s) found:
ISO/IEC 14443A (106 kbps) target:
    ATQA (SENS_RES): 00  04
       UID (NFCID1): 2b  6a  20  87
      SAK (SEL_RES): 08

會動之後, 就可以玩其它功能了





2014/10/01

Setting raspberry pi uart

1. How to connect


首先需要一條 USB-TTL 線
(試過RS232轉TTL, 似乎不成功)

底下是 Raspberry Pi (RPi) B+的 40-pin 配置圖


其中
USB-TTL 接地線(GND) 接到 RPi GND (JB6)
USB-TTL RX 接到 RPi TX (JB8)
USB-TTL TX 接到 RPi RX (JB10)

2. Serail setting


用putty或其它serial terminal (Ex. tera term)

類型選 serial
Serail Line 填 USB-TTL 的 COM port
Speed 填 115200
Data Bits 填 8
Stop Bits 填 1
Parity 填 None
Flow Control 填 None


3. screen size


登入之後會發現, 它的 row = 24, column = 80, 使用起來不方便
用 vim 輸入幾個tab就超出螢幕了
可以用 stty 來修改

這個可以看當前的設定
$ stty size

修改行數為40

$ stty rows 40

修改每行字元數為128

$ stty columns 128




2014/09/29

Setting raspberry pi network

1. Use wpa_cli command connect to ssid


打這個 command 進入 wpa command line interface, (提示符號會變成 ">")

$ wpa_cli

掃瞄目前的wifi訊號, 並且列出結果
> scan
OK
> scan_result
bssid / frequency / signal level / flags / ssid
e4:71:85:04:98:f4       2442    100     [WPA-PSK-TKIP+CCMP][WPA2-PSK-TKIP+CCMP][ESS]    Almond-0326_nomap_5F-7
<3>CTRL-EVENT-SCAN-RESULTS

加入網路ssid, psk是密碼
> add_network
0
> set_network 0 ssid "MYSSID"
> set_network 0 psk "passphrase"
> enable_network 0
<2>CTRL-EVENT-CONNECTED - Connection to 00:00:00:00:00:00 completed (reauth) [id=0 id_str=]

如果是open network
> set_network 0 key_mgmt NONE

把config存起來
> save_config
OK

這些動作會存在
/etc/wpa_supplicant/wpa_supplicant.conf

其它常用的command

status (看看目前的狀態)
help (查看參數與指令)
terminate (關閉wpa_supplicant)
interface (查看有那些無線網卡介面)
list_networks (查看wpa_supplicant.conf檔裡的設定)
select_network (選擇不同的AP,id為AP代號0,1,2,3...)
enable_network 
disable_network 
remove_network (刪除AP資訊)
reconfigure (重新讀取wpa_supplicant.conf設定內容)
save_config (儲存寫入到wpa_supplicant.conf中,否責變更無效)
disconnect (斷線)
reconnect (重新連線)
scan (掃描附近的AP)
scan_results (印出附近AP的相關資訊)


2. search raspberry pi in subnet


用nmap就可以掃, 在windows底下要安裝

假如電腦的ip是 192.168.1.100/24
那麼就下這個command
nmap -sn 192.168.1.0/24

掃出來的結果裡, 可能有
Starting Nmap 6.40 ( http://nmap.org ) at 2014-03-10 12:46 GMT
Nmap scan report for hpprinter (192.168.1.2)
Host is up (0.00044s latency).
Nmap scan report for Gordons-MBP (192.168.1.4)
Host is up (0.0010s latency).
Nmap scan report for ubuntu (192.168.1.5)
Host is up (0.0010s latency).
Nmap scan report for raspberrypi (192.168.1.8)
Host is up (0.0030s latency).
Nmap done: 256 IP addresses (4 hosts up) scanned in 2.41 seconds

可以看到裡面有raspberrypi