raspberry pi - it.kmitl.ac.thpanwit/iot002.pdf · what is raspberry pi • the raspberry pi is a...

27
โครงการจัดอบรมพัฒนาความรู ้ทักษะด้าน Internet of Things ได้รับการสนับสนุนจาก งบประมาณแผ่นดิน ปี 2560 ตามแผนบูรณาการการพัฒนาคนตลอดช่วงวัย 1 Raspberry Pi

Upload: truonglien

Post on 10-May-2018

273 views

Category:

Documents


8 download

TRANSCRIPT

Page 1: Raspberry Pi - it.kmitl.ac.thpanwit/IoT002.pdf · What is Raspberry Pi • The Raspberry Pi is a low cost, credit‐card sized computerthat plugs into a computer monitor or TV, and

โครงการจดอบรมพฒนาความรทกษะดาน Internet of Things

ไดรบการสนบสนนจาก งบประมาณแผนดน ป 2560ตามแผนบรณาการการพฒนาคนตลอดชวงวย

1

Raspberry Pi

Page 2: Raspberry Pi - it.kmitl.ac.thpanwit/IoT002.pdf · What is Raspberry Pi • The Raspberry Pi is a low cost, credit‐card sized computerthat plugs into a computer monitor or TV, and

What is Raspberry Pi

• The Raspberry Pi is a low cost, credit‐card sized computer that plugs into a computer monitor or TV, and uses a standard keyboard and mouse. It is a capable little device that enables people of all ages to explore computing, and to learn how to program in languages like Scratch and Python.

Raspberry Pi 2 Model B

The Raspberry Pi 2 Model B is the second generation Raspberry Pi. It replaced the original Raspberry Pi 1 Model B+ in February 2015. Compared to the Raspberry Pi 1 it has:

A 900MHz quad‐core ARM Cortex‐A7 CPU1GB RAMLike the (Pi 1) Model B+, it also has:4 USB ports40 GPIO pinsFull HDMI portEthernet portCombined 3.5mm audio jack and composite videoCamera interface (CSI)Display interface (DSI)Micro SD card slotVideoCore IV 3D graphics core

Because it has an ARMv7 processor, it can run the full range of ARM GNU/Linux distributions, including Snappy Ubuntu Core, as well as Microsoft Windows 10

Page 3: Raspberry Pi - it.kmitl.ac.thpanwit/IoT002.pdf · What is Raspberry Pi • The Raspberry Pi is a low cost, credit‐card sized computerthat plugs into a computer monitor or TV, and

Raspberry Pi 2 Model B

Raspberry Pi 3 Model B

The Raspberry Pi 3 is the third generation Raspberry Pi. It replaced the Raspberry Pi 2 Model B in February 2016. Compared to the Raspberry Pi 2 it has:

A 1.2GHz 64‐bit quad‐core ARMv8 CPU802.11n Wireless LANBluetooth 4.1Bluetooth Low Energy (BLE)

Like the Pi 2, it also has:1GB RAM4 USB ports40 GPIO pinsFull HDMI portEthernet portCombined 3.5mm audio jack and composite videoCamera interface (CSI)Display interface (DSI)Micro SD card slot (now push‐pull rather than push‐push)VideoCore IV 3D graphics core

Page 4: Raspberry Pi - it.kmitl.ac.thpanwit/IoT002.pdf · What is Raspberry Pi • The Raspberry Pi is a low cost, credit‐card sized computerthat plugs into a computer monitor or TV, and

Raspberry Pi 3 Model B

การทดลองท 1 ตดตงระบบปฎบตการ Raspbian OS

• ระบบปฎบตการทเปน Official Support จาก Raspberry Pi ชอวา Raspbian OS สามารถ Download ไดท

https://www.raspberrypi.org/downloads/

• ตดตงโปรแกรม Win32 Disk Imager

1. เลอก Image ไฟล2. เลอก Device ทตองการจะลง OS3. กด Write

Page 5: Raspberry Pi - it.kmitl.ac.thpanwit/IoT002.pdf · What is Raspberry Pi • The Raspberry Pi is a low cost, credit‐card sized computerthat plugs into a computer monitor or TV, and

Raspberry Pi Connect

• connect via a DVI connection, commonly available on newer monitors and digital TVs.

Raspberry Pi Connect

• Raspberry Pi Remote Connections

Page 6: Raspberry Pi - it.kmitl.ac.thpanwit/IoT002.pdf · What is Raspberry Pi • The Raspberry Pi is a low cost, credit‐card sized computerthat plugs into a computer monitor or TV, and

Setting the Raspberry Pi’s IP address

• Quick Setup / Setting up when you don’t have a monitor– Ensure the Raspberry Pi is powered off, and remove the SD‐Card.

– Insert the SD‐Card into a card reader and plug it into your laptop.

– Make a copy of cmdline.txt and rename it cmdline.normal

– Edit cmdline.txt and add the IP address at the end (be sure you don’t add any extra lines).      10.30.4.1XX    XX = เบอรฝากลอง

ขอควรระวง บน Windows ไมควรใช Notepad ในการแก เพราะมปญหาเรองอกขระพเศษและการเวนบรรทด ควรใช Editor ตวอนๆ เชน Notepad++

การทดลองท 2 เรมตนใชงาน Raspberry Pi

• นา SD Card ทตดตง OS เสรจแลว และแกไข IP Address เรยบรอยแลว ไปตดตงท Rasp Pi

• เสยบสาย LAN เขาท Rasp Pi

• เสยบ Adapter เพอจายกาลงงานไฟฟา ใหกบ Rasp Pi 

• ทดลองใชคาสง ping ทเครอง PC ไปยง IP Address ทกาหนดไวตอนตน วาสามารถตดตอกบ Rasp Pi ไดหรอไม

Page 7: Raspberry Pi - it.kmitl.ac.thpanwit/IoT002.pdf · What is Raspberry Pi • The Raspberry Pi is a low cost, credit‐card sized computerthat plugs into a computer monitor or TV, and

การทดลองท 2 เรมตนใชงาน Raspberry Pi

• ทาการตดตงโปรแกรม Putty เพอทาการเชอมตอกบ Rasp Pi ผาน Secure Shell V2 (SSH)

• ระบ IP Address ของ Rasp Pi

• เลอก SSH

• กดป ม Open

การทดลองท 2 เรมตนใชงาน Raspberry Pi• จะปรากฏหนาจอ Console ของ Rasp Pi

• ใส username :  pi และ Password คอ raspberry

• เมอ Login เสรจ จะปรากฏหนาจอดงรป

Page 8: Raspberry Pi - it.kmitl.ac.thpanwit/IoT002.pdf · What is Raspberry Pi • The Raspberry Pi is a low cost, credit‐card sized computerthat plugs into a computer monitor or TV, and

Basic Linux command

การทดลองท 2 เรมตนใชงาน Raspberry Pi

• ทดลองใชคาสง basic command ของ Linux เชน – ls ในการแสดง รายชอไฟล

– cd  ในการเปลยน directory  เชน cd /  ,  cd .. 

– df –h ในการดขนาดของ file system

• ทาการ expand file system เพอขยายพนทใหเตมความจของ disk โดยใชคาสง sudo raspi‐config จากนนเลอกหวขอ Expand FileSystemจากนนเลอนไปท <Finish>  แลว Rasp Pi จะทาการ Reboot ใหม

Page 9: Raspberry Pi - it.kmitl.ac.thpanwit/IoT002.pdf · What is Raspberry Pi • The Raspberry Pi is a low cost, credit‐card sized computerthat plugs into a computer monitor or TV, and

การทดลองท 2 เรมตนใชงาน Raspberry Pi• ทาการ Config Static IP Address ของ Raspi บน file system• ใชคาสง

sudo nano /etc/dhcpcd.conf

• พมพขอมลตอไปนลงไป ตอทายบรรทดลางสด

interface eth0

static ip_address=10.30.4.1XX/24

static routers=10.30.4.1

static domain_name_servers=8.8.8.8

• ออกจากโปรแกรม nano โดยการกด Ctrl+x และ save file โดยการกด yes

การทดลองท 2 เรมตนใชงาน Raspberry Pi• จากนน ลบคา IP Address ใน cmdline.txt ออก โดยใชคาสง

sudo nano /boot/cmdline.txt

• ทาการ Reboot และ ทดลองเชอมตอ Rasp pi ดวย IP Address ใหม ทกาหนด

sudo shutdown –r now

Page 10: Raspberry Pi - it.kmitl.ac.thpanwit/IoT002.pdf · What is Raspberry Pi • The Raspberry Pi is a low cost, credit‐card sized computerthat plugs into a computer monitor or TV, and

Python เปนภาษาระดบสงภาษาหนง ทมความสามารถสงถกสรางขนในป 1989 โดย Guido van Rossum ซงถกพฒนาขนมาโดยไมยดตดกบแพลตฟอรม

เปนภาษาท Raspberry PI แนะนาใหใช สงเกตไดวามไลบรารในการเชอมตอฮารดแวรออกมาเปนจานวนมากและตวอยางโปรแกรมของ Raspberry PI กมกจะเปนภาษา Python

สรป ภาษา python แบบรวบรด‐ การแสดงคา ใชคาสง print เชน print “Hello”‐ การประกาศตวแปร ไมตองระบประเภทตวแปร เชน varint = 10 ‐ คาสงทางคณตศาสตร เชน + ‐ * /‐ คาสงในการเปรยบเทยบ เชน == , >= , <=, > , <‐ คาสงการตดสนใจ if ..else  เชน

if x > 10:

print  “x is large”else:

print  “x is small”

‐ คาสงวน loop  เชนx = 0

while x < 10print “Hello”x = x+1

Page 11: Raspberry Pi - it.kmitl.ac.thpanwit/IoT002.pdf · What is Raspberry Pi • The Raspberry Pi is a low cost, credit‐card sized computerthat plugs into a computer monitor or TV, and

ขนตอนการเขยนโปรแกรม python

• เปดไฟลเพอเขยนโปรแกรม ใชคาสง nano เชน

nano helloworld.py

• Run ไฟลโปรแกรมทเขยน ดวย python interpreter โดยใชคาสง python เชน

python helloworld.py  หรอ

sudo python helloworld.py

การทดลองท 3 GPIO Blink

• ตอขา GPIO4 เขากบ LED และ ตวตานทาน 100 โอหม ดงรป

GPIO4

GND

LED

100 Ohm

1 23 4

Page 12: Raspberry Pi - it.kmitl.ac.thpanwit/IoT002.pdf · What is Raspberry Pi • The Raspberry Pi is a low cost, credit‐card sized computerthat plugs into a computer monitor or TV, and

การทดลองท 3 GPIO Blink

• พพมคาสง nano led.py• เขยนโปรแกรม python ดงน

import RPi.GPIO as GPIO import time 

GPIO.setmode (GPIO.BCM) LIGHT = 4 

GPIO.setup(LIGHT,GPIO.OUT) 

while True: GPIO.output(LIGHT,True) time.sleep(0.5) GPIO.output(LIGHT,False) time.sleep(0.5)

• Run โปรแกรมโดยใชคาสง sudo python led.py

การทดลองท 3 GPIO Blinkอธบายโปรแกรม

import RPi.GPIO as GPIO  # เรยก Library สาหรบควบคม GPIO Pinimport time  # เรยก Library สาหรบสราง time delay 

GPIO.setmode (GPIO.BCM)  # Set the pin designation type to GPIO.BCM 

to use BCM numbering convention

( ใชการกาหนดเลข Pin ตาม chip ของ BCM)LIGHT = 4  # กาหนด GPIO4 เทากบตวแปร Light 

GPIO.setup(LIGHT,GPIO.OUT)  # กาหนด GPIO4 เปนขา Output

while True: GPIO.output(LIGHT,True)  # กาหนด GPIO4 มคา Hightime.sleep(0.5)  # delay 0.5 วนาทGPIO.output(LIGHT,False)  # กาหนด GPIO4 มคา Lowtime.sleep(0.5) # delay 0.5 วนาท

Page 13: Raspberry Pi - it.kmitl.ac.thpanwit/IoT002.pdf · What is Raspberry Pi • The Raspberry Pi is a low cost, credit‐card sized computerthat plugs into a computer monitor or TV, and

แบบฝกหดจงเขยนโปรแกรมแสดงส 8 ส โดยเวนชวงสละ 1 วนาทกาหนดสเอง ตามใจชอบ

การทดลองท 4 GPIO Input

• ตอขา GPIO17 เขากบ Switch  และ ตวตานทาน 10 กโลโอหม ดงรป

Page 14: Raspberry Pi - it.kmitl.ac.thpanwit/IoT002.pdf · What is Raspberry Pi • The Raspberry Pi is a low cost, credit‐card sized computerthat plugs into a computer monitor or TV, and

การทดลองท 4 GPIO Inputการกาหนดคาให GPIO17 เปน Input ใชคาสง

GPIO.setup(17,GPIO.IN)  # กาหนด GPIO17 เปนขา Input

และการอานคาจาก GPIO17 ใชคาสง

val = GPIO.input(17) # คาทอานได มาเกบในตวแปร val โดยจะไดคาเปน 0 หรอ 1

แบบฝกหดเมอกดป ม ให LED ตด และเมอปลอยให LED ดบ

การทดลองท 5 ตดตง MQTT Broker

ในทนจะใช MQTT Broker ของ Mosquitto

ในการตดตง MQTT Broker จะตดตง package 3 ตวดงน • mosquitto is the MQTT broker (i.e. server)

• mosquitto‐clients are the command‐line clients

• python‐mosquitto are the Python Library

MosquittoAn Open Source MQTT v3.1/v3.1.1 Broker

Page 15: Raspberry Pi - it.kmitl.ac.thpanwit/IoT002.pdf · What is Raspberry Pi • The Raspberry Pi is a low cost, credit‐card sized computerthat plugs into a computer monitor or TV, and

Repository

• โดยปกต เวลาเราตดตงโปรแกรมบน Windows คอเราจะตองโหลดโปรแกรมมาตดตงเอง จากเวบไหนกได ซงบางทอาจไดของแถมมาดวย แตการตดตงแอพพลเคชนบน Ubuntu หรอ OS ตระกล Linux จะตางกน โดย Ubuntu จะใชการตดตงซงดงไฟลแอพพลเคชนจากเวบไซตโดยตรง ซงเราเรยก เวบทเกบโปรแกรมพวกนนวา repository ซงเวลามนกพฒนาแอพพลเคชนสงขนไปบนเวบเหลานจะมการตรวจสอบกอนทจะปลอยใหผใชทวไปตดตง ซงโดยปกตแอพพลเคชนทถกสงไปบนrepository มกจะเปนโปรแกรมทสมบรณ ไมใชเวอรชน Alpha,Beta และเปนโปรแกรมทถกรวมเขาไปใน default repository

• ทมา http://www.ubuntuthailand.com/

การทดลองท 5 ตดตง MQTT Broker

1. add key repository ของ mosquitto โดยใชคาสง

wget http://repo.mosquitto.org/debian/mosquitto‐repo.gpg.key

sudo apt‐key add mosquitto‐repo.gpg.key

2. เขาโฟลเดอร source.list.d เพอเตรยมตดตง repository ใหมโดยใชคาสงcd /etc/apt/sources.list.d/

3. add repository ของ mosquitto โดยใชคาสงsudo wget http://repo.mosquitto.org/debian/mosquitto‐wheezy.list

Page 16: Raspberry Pi - it.kmitl.ac.thpanwit/IoT002.pdf · What is Raspberry Pi • The Raspberry Pi is a low cost, credit‐card sized computerthat plugs into a computer monitor or TV, and

การทดลองท 5 ตดตง MQTT Broker

4. สง update apt information โดยใชคาสงsudo apt‐get update

5. ตดตง package ทงสาม โดยใชคาสงsudo apt‐get install mosquitto mosquitto‐clients python‐mosquitto

6. ตดตง Library สาหรบ paho.mqtt เพอเขยน Python เชอมตอMQTTโดยใชคาสง (ออกมา home directory กอนตดตง)

git clone https://github.com/eclipse/paho.mqtt.python.git

cd paho.mqtt.python

sudo python setup.py install

การทดลองท 6 ทดสอบการทางานของ MQTT Broker

• เปด Terminal หนงเพอทาการ Subscribe  โดยใชคาสง

mosquitto_sub ‐h 127.0.0.1 ‐t topic

• และเปดอก Terminal หนง เพอทาการ Publish

mosquitto_pub ‐h 127.0.0.1 ‐t topic ‐m "Hello world“

• เมอทาการ Publish แลว ใหสงเกตผลลพธทหนาจอ Subscribe

Note:‐ h คอ host‐ t คอ topic‐ m คอ message

Page 17: Raspberry Pi - it.kmitl.ac.thpanwit/IoT002.pdf · What is Raspberry Pi • The Raspberry Pi is a low cost, credit‐card sized computerthat plugs into a computer monitor or TV, and

การเชอมตอ Wi‐Fi ของ Raspberry Pi

• ทาการ Scan หา SSID โดยใชคาสง sudo iwlist wlan0 scan

• ทาการ Config file system โดยใชคาสง sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

• พมพขอมลตอไปนลงไป

network={ 

ssid="The_SSID_from_earlier" psk="Your_wifi_password" 

}

• ออกจากโปรแกรม nano โดยการกด Ctrl+x และ save file โดยการกด yes• หากตองการด IP Adress ใชคาสง ifconfig

แบบฝกหด

ให Raspberry Pi ทาการ Subscribe และ publish ไปท Broker คณะ

neutron.it.kmitl.ac.th, 1883หรอ161.246.38.194, 1883

Page 18: Raspberry Pi - it.kmitl.ac.thpanwit/IoT002.pdf · What is Raspberry Pi • The Raspberry Pi is a low cost, credit‐card sized computerthat plugs into a computer monitor or TV, and

ตวอยาง MQTT Publish โดยภาษา python• พมพคาสง nano pub.py• เขยนโปรแกรม python ดงน

import  paho.mqtt.client as mqttimport  time

mqttc = mqtt.Client()mqttc.connect("127.0.0.1", 1883)

while True: mqttc.publish(“test/pub", “Hello”)    time.sleep(2)

• Run โปรแกรมโดยใชคาสง sudo python pub.py

Note:  อาจจะใช mqttc.connect(“neutron.it.kmitl.ac.th", 1883) สาหรบ publish ลง Broker คณะหรอ mqttc.connect(“161.246.38.194", 1883)

ตวอยาง MQTT Subscribe โดยภาษา python• พพมคาสง nano sub.py• เขยนโปรแกรม python ดงน

import  paho.mqtt.client as mqtt

def on_connect(client, userdata, rc):    print("Connected with result code "+str(rc))    client.subscribe(“test/sub")

def on_message(client, userdata, msg):print(msg.topic+" "+str(msg.payload))

client = mqtt.Client()client.on_connect = on_connectclient.on_message = on_message

client.connect("127.0.0.1", 1883)client.loop_forever()

• Run โปรแกรมโดยใชคาสง sudo python sub.py

Page 19: Raspberry Pi - it.kmitl.ac.thpanwit/IoT002.pdf · What is Raspberry Pi • The Raspberry Pi is a low cost, credit‐card sized computerthat plugs into a computer monitor or TV, and

แบบฝกหด

แบบฝกหดท 1.1 Raspberry Pi  Publish ไปท Broker คณะ

• อานคาจาก ป มกด

• ถากดป ม ให Publish ไป Broker คณะ วา ON 

แบบฝกหดท 1.2 Raspberry Pi ทาการ Subscribe จาก Broker คณะ

• รอรบคาจาก Broker คณะ

• ถารบไดคาวา ON ให LED ตด

• ถารบไดคาวา OFF ให LED ดบ

MQTT broker gets Websockets support

มองคประกอบคอ‐ Web Server ทาหนาทเกบไฟล html ทมโคด javascript‐ Web Browser หรอ Web application  ทาหนาท เรยกไฟล html จาก Web Server มาแสดงผล‐ MQTT Broker ทเปด Web Socket เพอให Javascript ไปดงขอมล มาแสดงผล

Page 20: Raspberry Pi - it.kmitl.ac.thpanwit/IoT002.pdf · What is Raspberry Pi • The Raspberry Pi is a low cost, credit‐card sized computerthat plugs into a computer monitor or TV, and

MQTT broker gets Websockets support

ตดตง Websocket บน Raspberry Pi

Download    wget http://www.it.kmitl.ac.th/~panwit/mosquitto‐ws.zip

ทาการ unzip ไฟล unzip mosquitto‐ws.zip

เขาไปยง folder ท unzip ไฟลcd mosquitto‐1_4‐ws

ทาการตดตง chmod +x installsudo service mosquitto stop sudo ./installsudo service mosquitto start 

การทดลองท 7 Real Time graph

Simple example of plotting live numbered data from a subscribed mqtt/websockets

topic and plotting it using highcharts. http://www.highcharts.com/

สงทตองม คอ ‐Web Server หรอ Web hosting‐ ไฟล html ทเขยนโคด Javascript

ไฟลทเตรยมไวให คอlivemqttchart.html จะ plot กราฟ เมอมการ Publish คาlivemqttchart1.html จะ plot กราฟ แบบ real time

mqttws31.js

ไฟลตวอยางทใหน คอ ทาหนาท Subscribe ตาม Topic ทกาหนด และ รบคามาแสดงผล

Page 21: Raspberry Pi - it.kmitl.ac.thpanwit/IoT002.pdf · What is Raspberry Pi • The Raspberry Pi is a low cost, credit‐card sized computerthat plugs into a computer monitor or TV, and

การทดลองท 7 Real Time graph

1. แกไขไฟล livemqttchart.html ดงน

var MQTTbroker = ‘IP ของ Raspi';   // Raspi brokervar MQTTport = 8888; // port ของ Webseocket ทเปดไวvar MQTTsubTopic = 'temp';   //แกชอ topic ไดตามใจชอบ

2.  ทาการ Upload ไฟลทง 2 ไปยง web hosting 3.  ทดสอบการแสดงผล โดยการ publish คาขอมล ไปยง topic ทกาหนด

‐ การ Publish คา จะ Publish โดยการเปดอก Terminal หนง เพอทาการ Publishmosquitto_pub ‐h 10.X.X.X ‐t temp ‐m "Hello world“

Note:Raspberry เปน Broker  ทมคา Default ของ Websocket คอพอรต 8888สามารถแกไขได โดยไปแกทไฟล mosquitto.conf โดยใชคาสง sudo nano /etc/mosquitto/mosquitto.conf

แลวหาบรรทด # listener port‐number [ip address/host name]listener 8888

การทดลองท 8 Control via Websocket

ไฟลทเตรยมไวให คอcontrol.html 

ไฟลตวอยางทใหน คอ ทาหนาท Publish ไปยง Topic ทกาหนดเมอกดป ม ON จะ publish คา on ไปยง Raspi Brokerเมอกดป ม OFF จะ publish คา off ไปยง Raspi Broker

Page 22: Raspberry Pi - it.kmitl.ac.thpanwit/IoT002.pdf · What is Raspberry Pi • The Raspberry Pi is a low cost, credit‐card sized computerthat plugs into a computer monitor or TV, and

การทดลองท 8 Control via Websocket

1. แกไขไฟล control.html ดงน

var MQTTbroker = ' IP ของ Raspi ';   // broker ของคณะvar MQTTport = 8888; // port ของ Webseocket ทเปดไวvar MQTTsubTopic = 'temp';   //แกชอ topic ไดตามใจชอบ

2.  ทาการ Upload ไฟล ไปยง web hosting 3.  ทดสอบการแสดงผล โดยการ ใช Raspi Broker ทาการ Subscribe  topic ทกาหนด

เมดกดป มตางๆ จะปรากฎคา on, off และคาตวเลข ท Broker

แบบฝกหด

Web Server

Web Browser

Local BrokerMQTT Client

( Arduino + Ethernet Shield)

ณ แบบฝกหดน ทกทานจะมโครงสรางระบบเปนดงรป จงเขยนโปรแกรมให สามารถสง ปด-เปด LED และหรไฟ LED  จาก website ไดNote แก website ทไฟล control.html ดวยนะครบ

Page 23: Raspberry Pi - it.kmitl.ac.thpanwit/IoT002.pdf · What is Raspberry Pi • The Raspberry Pi is a low cost, credit‐card sized computerthat plugs into a computer monitor or TV, and

แบบฝกหด : หลอด LED & R ปรบคาได

Local BrokerMQTT Client

( Arduino + Ethernet Shield)

วตถประสงค : ตองการให Arduino ทงสองคยกนผาน Internet - ใหจบค ทาดวยกน1. ปรบตวตานทาน ทฝง Team A แลวใหไป หรไฟ ทฝง Team B 2. ใหฝง Team B หนงใช sensor แสง วดแสง แลวสงไปยง Team A โดยท

‐เมอแสงนอย ใหหลอดไฟ LED ตด ‐เมอแสงมาก ใหหลอดไฟ LED ดบ

MQTT Client( Arduino + Ethernet Shield)

Team A Team B

Bridge Broker

Page 24: Raspberry Pi - it.kmitl.ac.thpanwit/IoT002.pdf · What is Raspberry Pi • The Raspberry Pi is a low cost, credit‐card sized computerthat plugs into a computer monitor or TV, and

Configuring Bridges

• connection name

This variable marks the start of a new bridge connection. It is also used to give the bridge a name which is used as the client id on the remote broker.

• address address[:port] [address[:port]], addresses address[:port] [address[:port]]– Specify the address and optionally the port of the bridge to connect to. This must be given for 

each bridge connection. If the port is not specified, the default of 1883 is used. Multiple host addresses can be specified on the address config.

Configuring Bridges

• topic pattern [[[ out | in | both ] qos‐level] local‐prefix remote‐prefix]– Define a topic pattern to be shared between the two brokers. Any topics matching the pattern 

(which may include wildcards) are shared. The second parameter defines the direction that the messages will be shared in, so it is possible to import messages from a remote broker using in, export messages to a remote broker using out or share messages in both directions. If this parameter is not defined, the default of out is used. The QoS level defines the publish/subscribe QoS level used for this topic and defaults to 0.

– The local‐prefix and remote‐prefix options allow topics to be remapped when publishing to and receiving from remote brokers. This allows a topic tree from the local broker to be inserted into the topic tree of the remote broker at an appropriate place.

– For incoming topics, the bridge will prepend the pattern with the remote prefix and subscribe to the resulting topic on the remote broker. When a matching incoming message is received, the remote prefix will be removed from the topic and then the local prefix added.

– For outgoing topics, the bridge will prepend the pattern with the local prefix and subscribe to the resulting topic on the local broker. When an outgoing message is processed, the local prefix will be removed from the topic then the remote prefix added.

Page 25: Raspberry Pi - it.kmitl.ac.thpanwit/IoT002.pdf · What is Raspberry Pi • The Raspberry Pi is a low cost, credit‐card sized computerthat plugs into a computer monitor or TV, and

การทดลองท 9 Bridge Broker Configurationเปนการทดลองเชอม Rasp pi เขากบ Broker ของคณะ แบบ Single Direction• เปดไฟล mosquitto.conf โดยใชคาสง

sudo nano /etc/mosquitto/mosquitto.conf

– แกไขไฟลดงน connection bridge_test address 161.246.38.194:1883topic local

• ทาการ Restart โดยใชคาสงsudo /etc/init.d/mosquitto stopsudo /etc/init.d/mosquitto start

• ทดสอบโดยการ – Subscribe ท Broker คณะ ดวย Topic  local– Publish  ไปท Raspberry Pi Broker  ดวย Topic  local

Note:ชอ Connection ตองไมซากนAddress คอ address ของปลายทางทไปเชอมตอ

การทดลองท 9 Bridge Broker Configurationเปนการทดลองเชอม Rasp pi เขากบ Broker ของคณะ แบบ Dual Direction• เปดไฟล mosquitto.conf โดยใชคาสง

sudo nano /etc/mosquitto/mosquitto.conf

– แกไขไฟลดงน connection bridge_test address 161.246.38.194:1883topic # both 2 local/topic/  remote/topic/

• ทาการ Restart โดยใชคาสงsudo /etc/init.d/mosquitto stopsudo /etc/init.d/mosquitto start

• ทดสอบโดยการ – Subscribe ท Broker คณะ ดวย Topic  remote/topic– Publish  ท Raspberry Pi   ดวย Topic  local/topic

– Subscribe ท Raspberry Pi ดวย Topic  local/topic– Publish  ท Broker คณะ ดวย Topic  remote/topic

local remote

map an entire topic tree

Page 26: Raspberry Pi - it.kmitl.ac.thpanwit/IoT002.pdf · What is Raspberry Pi • The Raspberry Pi is a low cost, credit‐card sized computerthat plugs into a computer monitor or TV, and

การทดลองท 9 Bridge Broker Configurationเปนการทดลองเชอม Rasp pi เขากบ Broker ของคณะ แบบ Dual Direction• เปดไฟล mosquitto.conf โดยใชคาสง

sudo nano /etc/mosquitto/mosquitto.conf

– แกไขไฟลดงน connection bridge_test address 161.246.38.29:1883topic # both 2 local/topic/  remote/topic/

• ทาการ Restart โดยใชคาสงsudo /etc/init.d/mosquitto stopsudo /etc/init.d/mosquitto start

• ทดสอบโดยการ – Subscribe ท Broker คณะ ดวย Topic  remote/topic/test

– Publish  ท Raspberry Pi   ดวย Topic  local/topic/test  ดวยคาสงmosquitto_pub ‐h 127.0.0.1 ‐t local/topic/test  ‐m "hello"

local remote

map an entire topic tree

แบบฝกหด

Web Server

Web Browser

Public BrokerLocal Broker และMQTT Client

ณ แบบฝกหดน ทกทานจะมโครงสรางระบบเปนดงรป Arduino

Pub/subPub/sub

Bridge

Internet Home

Page 27: Raspberry Pi - it.kmitl.ac.thpanwit/IoT002.pdf · What is Raspberry Pi • The Raspberry Pi is a low cost, credit‐card sized computerthat plugs into a computer monitor or TV, and

แบบฝกหด

ณ แบบฝกหดน ทกทานจะมโครงสรางระบบเปนดงรป

- จงเขยนโปรแกรมให Arduino คยกบ Raspberry Pi โดยท กดป มท Raspberry Pi แลวให LED ท Arduino ตด เมอปลอยป มกด ท Raspberry Pi ให LED ท Arduino ดบ

- จงเขยนโปรแกรมให Arduino ทาการ Publish คา Analog ทอานได ไปแสดงผลออกกราฟ real time graph ท Website   (ทดสอบการ Bridge)

‐จงเขยนโปรแกรมให Website มาควบคมการปด เปด หรไฟ ท Arduino ได (ทดสอบการ Bridge)