iot rpi day4 - burapha universitynutthanon/... · rpi to internet connect to internet testing to...
TRANSCRIPT
IoT Day 4 : MQTT Infrastructure Design and Cloud Services
IoT
Quality of services
MQTT
Quality of services
Quality of Services ( Qos ) เป็นข้อตกลงระหว่างผู้ส่งและผู้รับ เพื่อยืนยันความแน่นอนในการส่งข้อมูลในที่นี้จะถูกแบ่งออกเป็น 3 ระดับได้แก่ 0, 1, 2
Quality of services
MQTT QoS Level
▪ 0 – at most once
▪ 1 – at least one
▪ 2 – Exactly one
เมื่อพูดถึง QoS ต้องแบ่งออกเป็นสองพาร์ทได้แก่ 1. Publish client to Broker2.Broker Publish to Client
Quality of services
เมื่อพูดถึง QoS ต้องแบ่งออกเป็นสองพาร์ทได้แก่ 1. Publish client to Broker จะใช้ QoS level ที่ถูกเซตโดย client ที่ส่งมาในข้อมูล 2.Broker Publish to Client จะใช้ QoS level ที่เซตตอน Subscribe หมายความว่าสมมติ Client publish ด้วย QoS 2 ฝั่ง Subscribe สามารถรับด้วย QoS ที่ต่ำกว่าได้
Quality of services
MQTT QoS Level
▪ 0 – at most once delivery
▪ 1 – assured delivery but may be duplicated
▪ 2 – once and once only delivery
Quality of services
Quality of services
Quality of services
Quality of services
การเลือกใช้ระดับของ QoS ใน Application จริง QoS0 สำหรับงานประเภทที่ข้อมูลสามารถสูญหายได้บ้างเช่น เซนเซอร์โหนด QoS1สำหรับ application ที่ต้องการความแน่นอนแต่ฝ่ายรับ ข้อมูลต้องสามารถจัดการกับข้อมูลที่อาจจะมาซ้ำได้QoS2สำหรับค่าที่ต้องการความแน่นอนและครั้งเดียว
Quality of services on Raspberry PIpho MQTT client library
publish(topic, payload=None, qos=0, retain=False)
- topic ชื่อหัวข้อที่ต้องการ publish
- payloadข้อมูล
- qosQoS Level
- retainแจ้งให้ทาง Server เก็บรักษา payload ที่ส่งขึ้นไปล่าสุด เมื่อมีคน subscribe ทีหลังจะได้รับ ข้อมูลที่เก็บเอาไว้นี้ด้วย
Quality of services on Raspberry PIpho MQTT client library
subscribe(topic, qos=0)
- topic ชื่อหัวข้อที่ต้องการ subscribe
- qosQoS Level
Example Debugging MQTT client
def on_log(mqttc, obj, level, string): print(string) mqttc.on_log = on_log
More MQTT Paho Python API
https://eclipse.org/paho/clients/python/docs/
Broker on the Clouds
MQTT
RPI to Internet
Connect to Internet
Broker on the cloud
LAN
RPI to Internet
Connect to Internet
Testing to iot.eclipse.org** note DW.CLOUD ระวังชื่อในตัวแปร
clientID ต้องไม่ซ้ำกัน
LAN
RPI to Internet
Connect to Internet
Testing to Deaware private cloudและเปิด Browser ไปที่ Cloud IP
LAN
Testing to Deaware private cloudและเปิด Browser ไปที่ Cloud IP
Infrastructure Practice and Design
MQTT
Create Sample IoT System
- High feature with Linux operating system - Database system - Security network - High processing power - High level peripheral exe. Camera, USB
Linux Node
- Low cost - Power management - ไมโครคอนโทรลเลอร์จะเสถียรกว่า
Embedded Linux ในกรณีที่ทำงานไม่ซับซ้อน - นำไปใช้งานเช่นสั่ง IO อ่านค่าเซนเซอร์แล้วส่ง
เข้า gateway เป็นต้น - ในงานที่ไม่ต้องมี gateway ก็สามารถเชื่อมต่อ
Internet ได้โดยตรง
WiFI Node
Basic use of MQTT
การรวม Broker ไว้ที่ระบบ Cloud ที่เดียวอาจจะทำให้ระบบช้าเนื่องจาก Internet และการส่งข้อมูลอาจจะต้องอ้อมไปข้างนอกก่อนจึงค่อยกลับมา
ถ้าเราจะลองวางระบบให้สามารถทำ Bridge Broker ได้ระหว่าง Local กับ Cloud ?
MQTT
RPI to Internet
Connect to RPI
Network infrastructure
LAN
MQTT Broker bridge
PUB/SUB to RPI
Network infrastructure
LAN
Cloud Server
How to configuration MQTT Broker bridge
sudo nano /etc/mosquitto/mosquitto.conf
How to configuration MQTT Broker bridge
connection bridge_test address xxx.xxx.xxx.xxx:1883 topic local/
Single direction bridgeFrom local to server
** หมายเหตุ address ให้ใส่ address ของ cloud และ topic สามารถเลือกที่ต้องการได้ตามใจชอบ
sudo /etc/init.d/mosquitto stop sudo /etc/init.d/mosquitto start
How to configuration MQTT Broker bridge
connection bridge_test address xxx.xxx.xxx.xxx:1883 topic local/
Single direction bridgeFrom local to server
ทดลอง subscribe ไปที่ cloud server ด้วยหัวข้อ local/ แล้วทดสอบ publish ไปที่ IP ของ Raspberry PI
How to configuration MQTT Broker bridge
connection bridge_test address xxx.xxx.xxx.xxx:1883 topic # both 2 local/ local/
Dual Direction Bridge
** หมายเหตุ address ให้ใส่ address ของ cloud และ topic สามารถเลือกที่ต้องการได้ตามใจชอบ
sudo /etc/init.d/mosquitto stop sudo /etc/init.d/mosquitto start
How to configuration MQTT Broker bridge
connection bridge_test address xxx.xxx.xxx.xxx:1883 topic # both 2 local/ local/
Dual Direction Bridge
ทดสอบ subscribe ไปที่ Raspberry PI และทดสอบ Publish ไปบน CLOUD
How to configuration MQTT Broker bridge
connection bridge_test address xxx.xxx.xxx.xxx:1883 topic # both 2 local/ local/ topic # both 2 test/ test/
Dual Direction Bridge add more topic
ทดสอบ subscribe ไปที่ Raspberry PI และทดสอบ Publish ไปบน CLOUD
How to configuration MQTT Broker bridge
ทดลองใช้ DW.CLOUD ยิงไปที่ หัวข้อที่ทำการ bridge แล้วลองใช้ terminal ของ pisubscribe ไปที่ CLOUD ดูว่าสามารถ รับข้อมูลได้ถูกต้องหรือไม่
used case
MQTT
Facebook messenger
Security over Bridge ?
MQTT
Protocol - username, password- payload encryption Transport- TLS- SSLBroker- Sub/Pub permission
MQTT
MQTT
จากการทดลองที่ผ่านมาจะเห็นว่า DW.CLOUD ยังไม่ได้ซัพพอร์ต การเข้ารหัสในการส่งข้อมูลจึงอาจจะทำให้เป็นปัญหาในการใช้งานจริงได้ รอบนี้ Raspberry PI และการทำ Bridge จึงเป็นพระเอก** ฮาร์ดแวร์ของ DW.CLOUD ซัพพอร์ตการทำ SSL แต่ในการเดินระบบ MQTTก็ยังมีหลากหลายอุปกรณ์ที่ไม่สามารถทำการเข้ารหัสได้หัวข้อนี้จึงสำคัญ
MQTT Broker bridge
PUB/SUB to RPI
MQTT infrastructure
LAN
SSL Tunnel
No security
MQTT Broker bridge
PUB/SUB to RPI
Example SSL configuration bridge ( in mosquitto.conf )
LAN
SSL Tunnel
No security
username SomeUser password SomePassword
bridge_cafile /etc/keys/myCA.crt bridge_certfile /etc/keys/myServer.crt bridge_keyfile /etc/keys/myServer.key
Mobile/PC App communication
MQTT
Mobile/PC App communication
MQTT MQTT Pub/Sub
MQTT Pub/Sub
Your infrastructure
Mobile/PC App communication
MQTT
MQTT +
Cross platform Application
MQTT +
Basic Qt and Modification session
MQTT Notification Plugin
MQTTwarn
•amqp•apns•asterisk•carbon•dbus•dnsupdate•emoncms•file•freeswitch•gss•http•instapush•irccat•linuxnotify•log•mqtt•mqttpub•mysql•mysql_dynamic• nma •nntp
•nsca•osxnotify•osxsay•pastebinpub•pipe•prowl•pushalot•pushbullet•pushover•redispub•slack•sqlite•smtp•syslog•twilio•twitter•xbmc•xmpp•xively•zabbix
mqttwarn support a lot of number services
mqttwarn installation on raspberry pi
git clone https://github.com/jpmens/mqttwarn.git
mqttwarn installation on raspberry pi and how to use
nano mqttwarn.ini
cd mqttwarn
เข้าไปใน folder เพื่อเตรียม configuration
เปิดไฟล์ configuration
ใส่คอนฟิก สั่งให้ mqttwarn log ข้อมูลที่ได้จาก mqtt เก็บลงในไฟล์ /tmp/mqtt.logโดยจะเก็บในหัวข้อ test/+ ของ MQTT
[defaults] hostname = 'localhost' port = 1883
; name the service providers you will be using. launch = file, log
[config:file] append_newline = True targets = { 'mylog' : ['/tmp/mqtt.log'] }
[config:log] targets = { 'info' : [ 'info' ] }
[test/+] targets = file:mylog, log:info
mqttwarn installation on raspberry pi and how to use
สั่ง mqttwarn ให้ทำงานโดยใช้
python mqttwarn.py
mqttwarn installation on raspberry pi and how to use
ให้ทดสอบ publish ข้อมูลมาที่ topic test/mqtt แล้วตรวจสอบไฟล์ที่ /tmp/mqtt.log ดูว่ามีข้อมูลที่ ถูกส่งมาเก็บเอาไว้หรือไม่
mqttwarn installation on raspberry pi and how to use
mqttwarn installation on raspberry pi and how to use
MQTTwarmer
PushNofication
สมัคร account ที่เว็บ
https://pushover.net
แล้ว Login เข้าระบบ
ดาวน์โหลด application pushover ใน Store ของโทรศัพท์
Register application เพื่อสร้าง key ใหม่
[defaults] hostname = 'localhost' port = 1883
; name the service providers you will be using. launch = file, log, pushover
[config:file] append_newline = True targets = { 'mylog' : ['/tmp/mqtt.log'] }
[config:log] targets = { 'info' : [ 'info' ] }
[config:pushover] targets = { 'deaware' : ['XXXXXUSERKEYXXXXXXX', 'XXXXAPPLICATIONKEYXXXXXX'], }
[test/+] targets = file:mylog, log:info, pushover:deaware
เพิ่ม PushOver configuration ทดลองรันโปรแกรมแล้วส่ง MQTT อีกครั้ง
SQLite with Mqttwarn
[config:sqlite] targets = { #path #tablename 'demotable' : [ '/tmp/m.db', 'mqttwarn' ] }
Testing configuration Sqlite for mqttwarn
ข้อจำกัดได้แค่ column เดียวโดยเก็บชื่อว่า payload type TEXT ลงในฐานข้อมูลเท่านั้นถ้าต้องการนำไปใช้จริงอาจจะแนะนำให้พัฒนา MQTT client มา subscribe ข้อมูล แล้วเก็บลง Database ดีกว่า
ทดลองอ่าน help แล้ว configuration ด้วยตัวเอง ตัวอย่างเป็นของ SQLite
MQTT for Sensor network
MQTT SN
MQTT SN
MQTT-SN จะถูกออกแบบให้ใกล้เคียงกับ MQTT มากที่สุดแต่เน้นไปกับระบบ wireless sensor ที่ต้องใช้ battery และประหยัดพลังงานเป็นต้นดังนั้นจึงจะมีข้อจำกัดเช่น ชื่อ topic ไม่สามารถตั้งยาวได้ มีการลิมิตแบนวิดท์ที่ใช้ในการส่งข้อมูลเป็นต้น
MQTT -SN Architecture
MQTT -SN Architecture
Tranparent GW and Aggregating GW
All connecting to server Only one connection
MQTT -SN Architecture
Supporting sleeping client
MQTT -SN Architecture
MQTT -SN Architecture Demo
nRF24L01+IEEE 802.15.4 support
MQTT -SN Architecture Demo
nRF24L01+IEEE 802.15.4 support
ไม่ได้ซัพพอร์ต MQTT-SN แบบเต็มแต่พอประยุกต์ใช้งานได้
MQTT -SN Architecture Demo
sample design system with MQTT
MQTT
MQTT
MQTT
MQTTnot only the embedded devices
MQTT and Cloud Broker
MQTT
CLOUD Server For MQTTMQTT
Or build your own cloudMQTT
Real Time Web
83
WebSocket ?
84
Deaware MQTT CLOUD support web socket
85
Deaware MQTT CLOUD support web socket
86
WebServer ( Port 80, 8080 )Websocket ( Port … )
เรียกหน้าเว็บผ่าน port มาตรฐานเรียกข้อมูลผ่าน websocket
MQTT (mosquitto compile )
ปกติ mosquitto สามารถ forward ข้อมูลไปที่ web socket พอร์ตได้ทันทีแต่ต้องคอมไพล์ซอร์สโค้ดให้ซัพพอร์ต
MQTT to Websocket
87
WebServer ( Port 80, 8080 )Websocket ( Port … )
เรียกหน้าเว็บผ่าน port มาตรฐานเรียกข้อมูลผ่าน websocket
MQTT
หรือจะเลือกใช้ MQTT Proxy เช่น HiveMQ
MQTT to Websocket
88
WebServer ( Port 80, 8080 )Websocket ( Port … )
เรียกหน้าเว็บผ่าน port มาตรฐานเรียกข้อมูลผ่าน websocket
MQTT
node-red ก็เป็นอีกหนึ่งตัวเลือกที่น่าสนใจเพราะ ว่าใช้ง่าย
ทดลองส่งค่าจาก MQTT ไปที่เว็บไซต์
89
ทดลองส่งค่าจาก MQTT ไปที่เว็บไซต์
90
xxx.xxx.xxx.xxx/webwidget
ทดลองส่งค่าจาก MQTT ไปที่เว็บไซต์
91
กำหนดค่า mainwidget ที่ต้องการทดสอบ
ทดลองส่งค่าจาก MQTT ไปที่เว็บไซต์
92
pub ไปที่ test01/gauge ** ตาม mainwidget ที่คุณเลือกไว้
ทดลองส่งค่าจาก MQTT ไปที่เว็บไซต์
93
pub ไปที่ test01/gauge ** ตาม mainwidget ที่คุณเลือกไว้
ทดลองส่งค่าจาก MQTT ไปที่เว็บไซต์
94
subscribe ไปที่ test01/switch
ทดลองส่งค่าจาก MQTT ไปที่เว็บไซต์
95
ให้ทดลองเขียนโปรแกรมให้ DW.CLOUD หรือ RPI ในการเชื่อมต่อ
Final Application
sub and pub
Local System
sensor/an0 data collect from sensor/an0
subscribe for LED control Qt app ( Mobile/Desktop )
IoThttps://www.facebook.com/groups/iotthai/
Disscussion Group for IOT Thailand