(tcp/ip, udp)cc.ee.ntu.edu.tw/.../iot-ntuee-conversion3-mqtt.pdf · 2019. 9. 29. · (tcp/ip, udp)...

Post on 07-Dec-2020

8 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

(TCP/IP, UDP)

2

OSI 7 Layers

Physical

Data Link

Network

Transport

Session

Presentation

Application

IP Suite

Application

Transport

Network

Physical (Link)

FTP

TLS SSL

ZigBee

IEEE 802.15.4

6LoWPAN IPv6 IPv4

IEEE 802.3/IEEE 802.11

(Ethernet/WiFi)

TCPUDP

HTTPMQTTMQTT-SNCoAP

SigFoxLoRa

EnOcean

BLE

Thread

Message Queue

3

(Stack) (Queue)

4

• (Double Ended Queue)• (Priority Queue)

enqueue dequeue

frontrear

push pop

top

FIFO Queue

5

FIFO Queue

└ /

└ Throttling ( ) /

Priority Queue

└ Task Queue Job Queue Work Queue ( )

└ Wait Queue ( )

Message Queue

└ IPC, RPC

6

7

/(Load Balancing)

Scale Cube

Ref: Martin Abbott & Michael Fisher, The Art of Scalability.

Monolithic System

Y/

X

X

8

Monolithic System

X

└ Worker Process Cluster/Threads

└ Reverse Proxy

└Sticky

└/ ( )

9

10

11

API Proxy

API Orchestration

Message Broker

12

API Proxy

Front End Apps

13

Front End Apps

API Orchestration Layer (OL)

feature_1() feature_2() feature_3() feature_N()API Orchestrator

14

Peer-to-Peer Message Broker

/Broker

RabbitMQ ( AMQP, MQTT, STOMP)

15

Front End Apps

Message Broker

-

16

Producer ConsumerMQ ACK

send

Database Temp Store

PERSISTENT MSG ( )NON_PERSISTENT MSG ( )

PERSISTENT MSG

( )NON_PERSISTENT MSG

push/pull

Dead Queue

- ( )

17

Publisher SubscriberMQ

Database Temp Store

Dead Queue

Subscriber

Subscriber

18

└ MQTT, XMPP, STOMP, AMQP, OpenWire

Broker

└ Mosquitto, Openfire, Stampy, Apache Qpid, ActiveMQ

MQ

└ Apache ActiveMQ MQTT, XMPP, STOMP, AMQP, OpenWire, WS

└ RabbitMQ AMQP, MQTT, STOMP

└ Apache Kafka ActiveMQ

└ Ø MQ

└ Redis Broker

MQ Server

19

PERSISTENT MSG

NON_PERSISTENT MSG (temp store)

1. KahaDB

2. LevelDB (google)

3. 透過 JDBC (Derby, MySQL, Oracle, Postgresql, SQLServer, Sybase 等)

-

20

Broker

MQ Server

21

Ref: MQTT Essentials, http://www.hivemq.com/mqtt-essentials/

MQTT IBM/Eurotech

22

└ Andy Standford-Clack Arlen Nipper 1999

└ IBM Eurotech

└ 2013 IBM v3.1 (2010.8) OASIS

└ 2014 MQTT v3.1.1 OASIS

└ 2013 2016

MQTT

└ Message Queuing Telemetry Transport

└ 1999 MQTT

└ MQTT v3.1 2010 IBM MQTT v3.1.1 2014 OASIS

└ v5.0

/

23

└ TCP/IP

└ Header 2 bytes

Publish/Subscribe Pattern

└ /

└ Quality of Service (QoS) (LWT)Keep-Alive

Temp. Sensor

PUB: "26.5oC"Broker

Mobile PC ConstrainedDevices

PUB to topic temp: "26.5oC"

Client

Clients

Publish/Subscribe Pattern Client/Server

24

(Publisher)

└ Client

└ ( )

(Subscriber)

└ Clients

└ ( )

Broker

└ ( )

└ Publisher/Subscriber

PUB

Broker

PUB to topic

Client

Clients

Publisher

Subscriber(s)

Broker Clients TCP/IP Stack

25

Connection

└ Client Broker

└ Client Client

Return Code Return Code Response

0 Connection Accepted

1 Connection refused, unacceptable protocol version

2 Connection refused, identifier rejected

3 Connection refused, server unavailable

4 Connection refused, bad username or password

5 Connection refused, not authorized

{clientId: "client-exmaple",cleanSession: true,keepAlive: 60,[username]: "john",[password]: "mysecrete",[lastWillTopic]: "/john/will",[lastWillQos]: 2,[lastWillMessage]: "unexpected exit",[lastWillRetain]: false

}

{sessionPresent: true,returnCode: 0

}

CONNECT

CONNACK

Client Broker

Topic + Payload

26

Topic-based Filtering

└ topic

└ Broker topic clients

└ payload data-agnostic

{packetId: 1234,topicName: "foo/1",qos: 1,retainFlag: false,payload: "temperature:26.8",dupFlag: false

}

PUBLISH

Client Broker

PUBLISH

Clients

Broker Topic(s)

27

SUBSCRIBE

└ subscription topic-QoS

└ Topic wildcards ( topic pattern )

└ topic QoS

SUBACK

└ Broker SUBACK

{packetId: 1234,qos1: 1,topic1: "topic/1",qos2: 0,topic2: "topic/2"...

}

Return Code Return Code Response

0 Success – Maximum QoS 0

1 Success – Maximum QoS 1

2 Success – Maximum QoS 2

128 Failure

PUBLISH SUBSCRIBE

SUBACKPUBLISH

{packetId: 1234,returnCode1: 2,returnCode2: 0,...

}

Broker Topics

28

└ broker

└ topics topics

└ QoS topic

{packetId: 1234,topic1: "topic/1",topic2: "topic/2",...

}

{packetId: 1234

}

PUBLISH UBSUBSCRIBE

UNSUBACKPUBLISH

Topic

29

└ ( ) ( )

Topic

└ UTF-8

└ Topic multi-levels ( / )

└ topic 1

└ myhome/temperature MyHome/Temperature

└ “/” topic

myhome / groundfloor / livingroom / temp

Topic level separator

Topic level Topic level

Pattern

30

exact Topic

pattern Topic(s)

└ wildcard

└ SUB PUB

Single Level

└ Single Level: +

Multi Level└ Multi Level: # ( topic )

myhome / groundfloor / + / temp

Single-level wildcard

myhome / groundfloor / livingroom / temp

myhome / groundfloor / kitchen / temp

myhome / groundfloor / kitchen / brightness

myhome / firstfloor / kitchen / temp

myhome / groundfloor / kitchen / fridge / temp

myhome / groundfloor / #

Multi-level wildcard

myhome / groundfloor / livingroom / temp

myhome / groundfloor / kitchen / temp

myhome / groundfloor / kitchen / brightness

myhome / firstfloor / kitchen / temp

/

31

└ / /mytop //mytop

└ UTF-8

└ ASCII

└ topic uid/clientId

└ #

└ topic

└ topic topic home/room topic

32

└ QoS 0 At most once (0)

└ QoS 1 At least once (1)

└ QoS 2 Exactly once (2)

└ Client Broker (Client PUB QoS)

└ Broker Client(s)

QoS

PUB

Broker

PUB to topic with

Client

Clients

Publisher

Subscriber(s)

33

QoS 0 – At most once

└ fire and forget

└ TCP

PUBLISH

Client Broker

QoS 0

34

QoS 1 – At least once

└ PUBACKPUBLISH

└ PUB duplicate (DUP) flagDUP flag PUBACK

PUBLISH

Client Broker

QoS 1

PUBACK

{packetId: 1234

}

35

QoS 2 – Exactly once

└ Client Broker

{packetId: 1234

}

PUBLISH QoS 2

PUBCOMP

PUBREC

PUBREL

packetIdPUBREC

PUBREC packetId

36

QoS 0

QoS 1

└ duplicates

└ QoS 1

└ QoS 2 overhead ( )

QoS 2

└ duplicate

└ overhead QoS2 flow

37

└PINGREQ Broker

└ Broker

Broker

└ Broker 1.5

PINGREQ

PINGRESP

Client Broker

38

BLE + MQTT

40

BLE Wireless Module

└ BLE PIR Central

Raspberry Pi

└ BLE Central

└ MQTT Broker

PC

└ MQTT Topic PIR

41

└ API Protocol

└ PC Node-RED

└ Node

└ Node

└ Node

Raspberry Pi 3

42

1. RPi terminal

2. node-red

3. node-red

4. sudo npm install -g --unsafe-perm node-red

Node-RED

43

1. RPi terminal

2. node-red

3. PC

4. RPi IP:1880

44

node

└ inject node

└ random node

└ switch node

└ function node

└ debug node

Manage palette

45

NPM

46

1. Node-RED ~/.node-red

2. Random node

└ npm install node-red-node-random

3. Node-Red

inject node random node

47

1. inject node

2. Repeat interval

3. random node

4. 1 100

5. node

inject node

random node

switch node

48

1. switch node

2. >= number 60

3.

4. < number 60

5. node

switch node

function node

49

1. function node

2. Function Pass

3. function node

4. Function Fail

5. node

function node1

function node2

Deploy

50

1. function node debug node

2. Deploy

3. debug

Node-Red Publish PIR status

51

MQTT Broker

52

RPi mosquitto

└ apt-get install mosquitto

mosquitto

└ service mosquitto status

PIR Publish MQTT Broker

53

TCP PIRPIR Publish MQTT Broker

node

└ tcp in node

└ mqtt out node

└ debug node

TCP node

54

tcp in node

MQTT node

55

mqtt out node

BLE Central

56

57

1 Git

└ git clone https://github.com/PeterEB/ntu-iot-ble-example.git

2 Github ntu-iot-ble-exampleZIP

TCP PIR

58

var net = require('net');var BleShepherd = require('ble-shepherd');

var client = new net.Socket();

client.connect(1337, '127.0.0.1', function() {console.log('Connected');

});

// ...

central.on('ind', function(msg) {var periph = msg.periph;

switch (msg.type) {// ...

case 'attNotify':if (msg.data.value.dInState) {

console.log('PIR device sensed someone');client.write('true');

} else {console.log('Someone left');client.write('false');

}break;

}});

app.js

BLE PIR

59

1. Node-RED Deploy

2. terminal

└ sudo node app.js

Subscribe MQTT Topic

60

MQTT Lens PIR Topic

61

MQTT Lens

└ Google Chrome

└ MQTT Broker Subscribe Publish MQTT topic

PC Chrome MQTT Lens

MQTT Lens

62

RPi MQTT Broker PIR topic

63

top related