+ All Categories
Home > Documents > CoAP and MQTT

CoAP and MQTT

Date post: 05-Jan-2017
Category:
Upload: vothuy
View: 272 times
Download: 7 times
Share this document with a friend
42
CoAP and MQTT Antonio Liñán Colina, Zolertia
Transcript
Page 1: CoAP and MQTT

CoAP and

MQTT

Antonio Liñán Colina,

Zolertia

Page 2: CoAP and MQTT

http://electronicdesign.com/iot/mqtt-and-coap-underlying-protocols-iot

Page 3: CoAP and MQTT

03-coap

Page 4: CoAP and MQTT

https://tools.ietf.org/html/rfc7252http://coap.technology/

• UDP-reliable (confirmable), SMS supported

• CoRE Link-format (GET /.well known/core)

• Client/Server

• IANA Registered (error codes, content format)

• Resource Discovery and asynchronous subscription

• Four-bytes compact header

• Multicast and one-to-one supported

• HTTP verbs GET, PUT, POST, DELETE

• HTTP-like header (Options)

• URI (Uniform Resource Identifier)

Page 5: CoAP and MQTT

http://www.slideshare.net/zdshelby/coap-tutorial

Page 6: CoAP and MQTT

CoAP URI

coap://[aaaa::c30c:0:0:1234]:5683/actuators/leds?color=b

Host Port Path Query

Page 7: CoAP and MQTT

http://www.slideshare.net/paolopat/mqtt-iot-protocols-comparison

Page 8: CoAP and MQTT

apps/er-coapapps/rest-engine

Page 9: CoAP and MQTT

apps/er-coapapps/rest-engine

Page 10: CoAP and MQTT

apps/er-coapapps/rest-engine

Page 11: CoAP and MQTT

examples/zolertia/tutorial/03-coap/resources/res-adxl345.c

Resource declaration

Resource implementation

URI Query

Function to invoke whenever

there’s a GET request

The CoAP Accept option can be

used to indicate which Content-

Format is acceptable to the client.

Page 12: CoAP and MQTT

examples/zolertia/tutorial/03-coap/resources/er-example-server.c

Resource activation

Importing the Resource

Page 13: CoAP and MQTT

examples/zolertia/tutorial/03-coap/resources/Makefile

Search resources to include

in the “resources” directory

REST engine and CoAP libraries

Page 14: CoAP and MQTT

https://addons.mozilla.org/en-US/firefox/addon/copper-270430/http://people.inf.ethz.ch/mkovatsc/copper.php

Page 15: CoAP and MQTT

examples/zolertia/tutorial/02-ipv6/03-client-and-server

er-example-server

CoAP server in Contiki OS

Border Router

IPv6/6LoWPAN

Copper CoAP user-agent

On Firefox browse the CoAP Server,

discover and learn its resources

Page 16: CoAP and MQTT

apps/er-coapapps/rest-engine

TIP: enable the DEBUG to 1 to print more information about the processes

taking place inside the CoAP and REST libraries (all .c files inside!)

Page 17: CoAP and MQTT

Pong! – check the CoAP server is online

examples/zolertia/tutorial/03-coap/resources/er-example-server.c

Page 18: CoAP and MQTT

Discover – learn the Resources the CoAP server has

examples/zolertia/tutorial/03-coap/resources/er-example-server.c

Page 19: CoAP and MQTT

Hover over the resources to learn its attributes

examples/zolertia/tutorial/03-coap/resources/er-example-server.c

Page 20: CoAP and MQTT

POST/PUT – change the LEDs state (on or off)

examples/zolertia/tutorial/03-coap/resources/er-example-server.c

Page 21: CoAP and MQTT

GET – read the ADXL345 with JSON format (application/json)

examples/zolertia/tutorial/03-coap/resources/er-example-server.c

Page 22: CoAP and MQTT

OBSERVE – get notifications about an event (press the user button)

examples/zolertia/tutorial/03-coap/resources/er-example-server.c

Page 23: CoAP and MQTT

04-mqtt

Page 24: CoAP and MQTT

http://mqtt.org

• On top of TCP/IP

• Publish/Subscribe messaging pattern

• Message Broker distributes topics to clients

• Topics are UTF-8 string-based with hierarchical structure

• No direct connection between clients

• Quality of Service

• Retain-Flag: new subscribed clients will received last value

• Last Will: notify other clients when disconnected ungracefully

• KeepAlive: ping request messages to the bróker

• Clients have to know beforehand the structure of the data published to a

topic

• MQTT is data-agnostic

Page 25: CoAP and MQTT

CONNECTWaits for a connection to be established with the server

DISCONNECTWaits for the MQTT client to finish any pending task and closes the TCP session

SUBSCRIBERequest the server to subscribe the client to one or more topics

UNSUBSCRIBERequest the server to subscribe the client to one or more topics

PUBLISHUpdates a topic with data

Page 26: CoAP and MQTT

http://www.hivemq.com/blog/how-to-get-started-with-mqtt

Page 27: CoAP and MQTT

http://www.slideshare.net/paolopat/mqtt-iot-protocols-comparison

Page 28: CoAP and MQTT

http://www.hivemq.com/blog/mqtt-essentials-part-5-mqtt-topics-best-practices

Topics starting with $ are special

These are reserved for the broker

statistics

Page 29: CoAP and MQTT

apps/mqtt

Required to be included in the process using MQTT

Page 30: CoAP and MQTT

apps/mqtt

To start the MQTT client this function should be called first

The max_segment_size is the TCP chunk of data to be sent (default is 32 bytes)

The client_id is a string identifying the client

Page 31: CoAP and MQTT

apps/mqtt

The keep_alive value is used by a timer waiting a PINGRES from the broken,

if expired and no response is obtained, it triggers a disconnection

Page 32: CoAP and MQTT

apps/mqtt

Message ID (mid) is zero for QoS=0

Page 33: CoAP and MQTT

apps/mqtt

Page 34: CoAP and MQTT

apps/mqtt

Page 35: CoAP and MQTT

mqtt-demo

The MQTT client publishing to the

topic “zolertia/evt/status” and

subscribed to “zolertia/cmd/leds”

Border Router

IPv6/6LoWPAN

mqtt_client.py

Paho MQTT client

Subscribed to the “zolertia/evt/status”

topic, used to check the example

examples/zolertia/tutorial/04-mqtt

MQTT bróker

test.mosquitto.org

Page 36: CoAP and MQTT

examples/zolertia/tutorial/04-mqtt

Page 37: CoAP and MQTT

examples/zolertia/tutorial/04-mqtt

static voidmqtt_event(struct mqtt_connection *m, mqtt_event_t event, void *data){switch(event) {case MQTT_EVENT_CONNECTED: {

printf("APP - Application has a MQTT connection\n");state = STATE_CONNECTED;break;}case MQTT_EVENT_DISCONNECTED: {

printf("APP - MQTT Disconnect. Reason %u\n", *((mqtt_event_t *)data));state = STATE_DISCONNECTED;process_poll(&mqtt_demo_process);break;}case MQTT_EVENT_PUBLISH: {

pub_handler(msg_ptr->topic, strlen(msg_ptr->topic), msg_ptr->payload_chunk,msg_ptr->payload_length);

break;}

case MQTT_EVENT_SUBACK: {printf("APP - Application is subscribed to topic successfully\n");break;}case MQTT_EVENT_UNSUBACK: {

printf("APP - Application is unsubscribed to topic successfully\n");break;}case MQTT_EVENT_PUBACK: {

printf("APP - Publishing complete.\n");break;}}

Page 38: CoAP and MQTT

examples/zolertia/tutorial/04-mqtt

STATE_INIT: mqtt_register(…)

STATE_REGISTERED: mqtt_connect(…)

STATE_CONNECTING: ctimer checking(…)

STATE_CONNECTED: mqtt_subscribe(…)

STATE_PUBLISHING: etimer_set(publish interval)

DISCONNECTED: mqtt_disconnect(…)

Page 39: CoAP and MQTT

examples/zolertia/tutorial/04-mqtt

Set default configuration values

Create topic/subscription/id strings (STATE_INIT)

Polls the state machine as described before

Page 40: CoAP and MQTT

apps/mqtt/mqtt.c

TIP: enable the DEBUG to 1 to print more information about the processes

taking place inside the MQTT library

Page 41: CoAP and MQTT

examples/zolertia/tutorial/04-mqtt

MQTT demo client running on the Z1 mote

Paho MQTT client in Python subscribed

Mosquitto publishing to turn a LED on

Page 42: CoAP and MQTT

Antonio Liñán Colina

Twitter: @4Li6NaN

LinkedIn: Antonio Liñan Colina

github.com/alignan

hackster.io/alinan

[email protected]

[email protected]


Recommended