Slide 1

Slide 1 text

Leran LoRa with Python … and Raspberry Pi 台灣樹莓派 Jun 11, 2017/PyCon Taiwan 2017

Slide 2

Slide 2 text

姓名標示 — 非商業性 — 相同方式分享 CC (Creative Commons) 姓名標示 — 你必須給予 適當表彰、提供指向本授權 條款的連結,以及 指出(本作品的原始版本)是否已 被變更。你可以任何合理方式為前述表彰,但不得以 任何方式暗示授權人為你或你的使用方式背書。 非商業性 — 你不得將本素材進行商業目的之使 用。 相同方式分享 — 若你重混、轉換本素材,或依本 素材建立新素材,你必須依本素材的授權條款來 散布你的貢獻物。

Slide 3

Slide 3 text

3 ● Raspberry Pi 官方經銷商 ● 專注 Raspberry Pi 應用與推廣 , 舉辦社群活動 關於我們

Slide 4

Slide 4 text

4 ● COSCUP,MakerConf,PyCon,HKOSCon 講者 ● 投影片 ● https://speakerdeck.com/piepie_tw ● 程式碼 ● https://github.com/piepie-tw 分享 x 教學

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

6 http://en.wikipedia.org/wiki/Internet_of_Things

Slide 7

Slide 7 text

物聯網技術重點之一在”無線傳輸”

Slide 8

Slide 8 text

速度與距離的考量 https://www.slideshare.net/PeterREgli/lpwan 數公里以上 數百公尺到一公里 數十公尺 >10Mbps 100Kbps ~ 10Mbps <10Kbps

Slide 9

Slide 9 text

這些場景需要哪種無線技術? https://www.lora-alliance.org/portals/0/documents/whitepapers/LoRa-Alliance-Whitepaper_NBIoT_vs_LoRa.pdf 智慧電錶 農場監控 寵物追蹤 智慧城市

Slide 10

Slide 10 text

不同無線傳輸技術的特點 https://www.slideshare.net/PeterREgli/lpwan

Slide 11

Slide 11 text

● LPWAN(Low Power Wide Area Network) 最近很夯的低功耗廣域網路 http://www.techbang.com/posts/49656

Slide 12

Slide 12 text

從 OSI 模型看 LPWAN https://www.slideshare.net/infiswift/lpwan-for-iot-62591541

Slide 13

Slide 13 text

● 法國公司 Cycleo 設計 ,Semtech 在 2012 收購 ● LoRa 是實體層 (PHY) 的調變技術 ● 採用 CSS(Chirp Spread Spectrum) 調變技術 ● 常用頻段 :433/470~510/868/915MHz ● 低功耗 , 低資料率 , 長距離 , 低價格 ● 低功耗 :RX<10mA, Sleep<200nA ● 長距離 :500m 到 15Km ● 靈敏度 : 低於 -137 dBm ● 資料率 :0.3kbps 到 50kbps LoRa(Long Range)

Slide 14

Slide 14 text

● 調變:發射端將低頻訊號處理成高頻訊號以後送出 ● 解調變:接收端將高頻訊號還原成低頻訊號 調變 / 解調變 http://goo.gl/wfXgjh 低頻訊號 高頻載波 傳送訊號

Slide 15

Slide 15 text

Chirp Spread Spectrum https://electronics.stackexchange.com/questions/278192 原始訊號 解調變後 FFT 將時域轉到頻域 原始訊號做 FFT 的結果

Slide 16

Slide 16 text

● 信用卡大小般的電腦 Raspberry Pi http://www.flickr.com/photos/fotero/7697063016/

Slide 17

Slide 17 text

硬體規格 http://goo.gl/pXRxJd

Slide 18

Slide 18 text

18 ● C ● C# ● Ruby ● Perl ● Python ● Scratch ● Java Pi4J Library ● Shell script 如何控制 Raspberry Pi 的 GPIO ? http://makezine.com/2013/04/14/47-raspberry-pi-projects-to-inspire-your-next-build/

Slide 19

Slide 19 text

● 特色 ● 傳輸距離可達 15km ● 長達 10 年的低功耗 ( 使用鈕扣電池 ) ● 支援 433MHz/868MHz 等頻段 ● 支援 LoRaWAN class A 協定 ● 整合 FSK,GFSK,LoRa 等調變技術 ● UART 傳輸介面 + 類 AT 命令列控制 ● 最大輸出功率可達 +14dBm ● 靈敏度可達 -143dBm ● 每次最大傳輸 255bytes Microchip RN2483 LoRa Module

Slide 20

Slide 20 text

系統架構 http://www.mouser.tw/new/microchip/microchip-rn2483-module/ RN2483 Raspberry Pi

Slide 21

Slide 21 text

● sys 設定系統與 GPIO ● radio 設定射頻 ● mac 設定 LoRaWAN 三種指令 主要設定指令

Slide 22

Slide 22 text

● radio set freq [868100000] ● radio set bw [500] ● radio set pwr [8] ● radio set sf [sf11] ● radio set wdt [0] ● mac pause ● radio rx 0 ● radio tx [1234] 相關參數 頻率 頻寬 發射功率 Watchdog Timer 設定 Spreading Factor 停用 LoRaWAN 功能 傳送 1234 啟用連續接收模式 http://ww1.microchip.com/downloads/en/DeviceDoc/40001784B.pdf

Slide 23

Slide 23 text

Raspberry Pi + RN2483 天線 RN2483 Raspberry Pi

Slide 24

Slide 24 text

P2P Network Diagram 要有相同的頻率 (Freq), 展頻因子 (SF) 和頻寬 (BW) 才能收 + 發

Slide 25

Slide 25 text

lora = serial.Serial("/dev/ttyUSB0", 57600) ● lora.write(b'radio set bw 500\r\n') lora.readline() lora.write(b'radio set sf sf11\r\n') lora.readline() lora.write(b'radio set freq 868100000\r\n') lora.readline() while True: lora.write(b'mac pause\r\n') lora.readline() t = int(time.time()) byte_cmd = bytes('radio tx ' + str(t) + '\r\n') lora.write(byte_cmd) lora.readline() lora.readline() Python 發送端程式

Slide 26

Slide 26 text

lora = serial.Serial("/dev/ttyUSB0", 57600, timeout=1) ● ● lora.write(b'radio cw off\r\n') ● lora.readline() ● lora.write(b'radio set bw 500\r\n') ● lora.readline() ● lora.write(b'radio set sf sf11\r\n') ● lora.readline() ● lora.write(b'radio set freq 868100000\r\n') ● lora.readline() ● lora.write(b'mac pause\r\n') ● lora.readline() ● ● while True: ● lora.write(b'radio rx 0\r\n') ● ● if lora.readline().strip() == "ok" or "radio_tx_ok" : ● print( str(lora.readline().strip()) ) Python 接收端程式

Slide 27

Slide 27 text

● 從捷運古亭站到捷運南勢角站 (~5KM) 實測結果 興南路南勢派出所 和平西路 & 牯嶺街口 SNR=-11

Slide 28

Slide 28 text

如何優化?

Slide 29

Slide 29 text

● 通訊領域常用 dB,dBm 表示相對強度或功率 ● 對數相加減 = 常數相乘除 ● dB 10 × log10(ratio) ≡ , 純量無單位 ● 因此 10 倍增加標示為 +10dB, 而 1/100 標示為 -20dB ● 如果是 2 倍增加 , 標示為 +3dB( 應為 +3.01dB) ● dbm 10 × log10(Power)1mW ≡ ● 因此 0.02 Watt 是 20mW, 也是 13dBm ● 可以從 10dBm(10mW)+3dBm(2mW) 計算得來 先備知識

Slide 30

Slide 30 text

Bandwidth, Spreading Factor and Data Rate https://electronics.stackexchange.com/questions/309424 固定 BW 而改變 SF 會讓 Data Rate 和 Sensitivity 改變 BW SF Data Rate Sensitivity

Slide 31

Slide 31 text

Link Budget 計算 https://www.slideshare.net/PeterREgli/lpwan 最大傳輸距離

Slide 32

Slide 32 text

如何估計 Link Budget ? http://www.semtech.com/wireless-rf/rf-transceivers/sx1272/

Slide 33

Slide 33 text

● 量測隨頻率變化的輸入信號強度 ● 硬體 ● SDR(Software Defined Radio) ● HackRF ● 軟體 ● GNU Radio ● FreqShow(Python) 頻譜分析儀 (Spectrum Analyzer) http://jensd.be/755/network/lorawan-simply-explained

Slide 34

Slide 34 text

用 RTL-SDR 分析頻譜

Slide 35

Slide 35 text

https://learn.adafruit.com/freq-show-raspberry-pi-rtl-sdr-scanner 設定參數 Waterfall 模式 Instant 模式

Slide 36

Slide 36 text

用 Pi 做 LoRa Nano Gateway

Slide 37

Slide 37 text

● LoRa ● LoRaWAN ● LoRa Node ● Gateway ● Network Server 名詞解釋

Slide 38

Slide 38 text

● 定義網路系統架構與通訊協定 ● 終端點採 LoRa 做長距離通訊 ( 星狀拓樸 ) ● 和終端點的是通訊雙向 ● AES 加密 LoRaWAN http://www.atim.com/en/technologies-2/lorawan/

Slide 39

Slide 39 text

● 定址 :DevEUI, AppEUI ● 使用 ALOHA, 沒有 CSMA 機制 , 三種 class LoRa Node https://www.thethingsnetwork.org/wiki/LoRaWAN/Home

Slide 40

Slide 40 text

● 傳輸協定轉換 (ex:LoRa + WiFi) ● 接收所有頻道的所有資訊 ● 封包直接轉發到後端 (Network Server) Gateway https://en.wikipedia.org/wiki/Gateway_(telecommunications)

Slide 41

Slide 41 text

● 冗餘封包濾除 (CRC) ● 安全性查驗 (Authentication/Authorization) ● 最佳 ACK 路徑 (ACK Routing) ● 適應性資料率 (Adaptive Data Rate, ADR) Network Server http://jensd.be/755/network/lorawan-simply-explained

Slide 42

Slide 42 text

● 使用硬體 ● Pi 3 x2 – https://www.piepie.com.tw/10684/55/ ● Pi 3 x1 ● Arduino x1 ● Microchip RN2483 Module x4 – https://www.piepie.com.tw/14724/1420/ Pi 3 Arduino RN2483

Slide 43

Slide 43 text

LoRa 封包格式 https://www.semtech.com/images/datasheet/LoraDesignGuide_STD.pdf 非必要 傳送資料 1234

Slide 44

Slide 44 text

● 限制 : ● 只能傳送 0-F 資料 (Hex) ● 每個符號以 ASCII 方式傳送 (1bytes) ● Data 資料 : ● 包含 id 與 content, 以 JSON 格式封裝 Payload 設計 Data CRLF Length SOH 1bytes 3bytes 2bytes char*bytes 0x01 0x0D 0x0A

Slide 45

Slide 45 text

● 接收端 ACK 機制與傳送時間 ● 當接收端收到封包後 , 回傳 ACK(0x06) 與 id ● 回傳時間與封包長度有關 ● 發送端重送機制類似 class A ● 傳送端發送完畢後 , 將等待一段時間等待 ACK ● 如果沒有收到 ACK, 將用 ALOHA 方式重送 ● 最大重送次數 <3 ACK 與重送機制設計

Slide 46

Slide 46 text

架構 Sensor Node Nano Gateway Gateway-02 Gateway-01 Node-01 Node-02

Slide 47

Slide 47 text

Node-01 廣播 Sensor Node Nano Gateway Gateway-02 Gateway-01 Node-01 Node-02

Slide 48

Slide 48 text

Gateway-01 最早收到後回 ACK Sensor Node Nano Gateway Gateway-02 Gateway-01 Node-01 Node-02 ACK

Slide 49

Slide 49 text

Node-02 廣播 Sensor Node Nano Gateway Gateway-02 Gateway-01 Node-01 Node-02

Slide 50

Slide 50 text

Gateway-02 最早收到後回 ACK Sensor Node Nano Gateway Gateway-02 Gateway-01 Node-01 Node-02 ACK

Slide 51

Slide 51 text

LoRa Gateway 限制 https://github.com/maartenweyn/lpwansimulation/

Slide 52

Slide 52 text

DEMO

Slide 53

Slide 53 text

53 Raspberry Pi Rocks the World Thanks