Co CoAP an and d MQTT TT Antonio Lin Colina, Zolertia - - PowerPoint PPT Presentation

co coap an and d
SMART_READER_LITE
LIVE PREVIEW

Co CoAP an and d MQTT TT Antonio Lin Colina, Zolertia - - PowerPoint PPT Presentation

Co CoAP an and d MQTT TT Antonio Lin Colina, Zolertia http://electronicdesign.com/iot/mqtt-and-coap-underlying-protocols-iot 03 03-coap coap UDP- reliable (confirmable), SMS supported CoRE Link-format (GET /.well known/core


slide-1
SLIDE 1

Co CoAP an and d MQTT TT

Antonio Liñán Colina, Zolertia

slide-2
SLIDE 2

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

slide-3
SLIDE 3

03 03-coap coap

slide-4
SLIDE 4

https://tools.ietf.org/html/rfc7252 http://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)
slide-5
SLIDE 5

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

slide-6
SLIDE 6

CoAP P UR URI

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

Host Port Path Query

slide-7
SLIDE 7

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

slide-8
SLIDE 8

apps/er-coap apps/rest-engine

slide-9
SLIDE 9

apps/er-coap apps/rest-engine

slide-10
SLIDE 10

apps/er-coap apps/rest-engine

slide-11
SLIDE 11

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.

slide-12
SLIDE 12

examples/zolertia/tutorial/03-coap/resources/er-example-server.c Resource activation Importing the Resource

slide-13
SLIDE 13

examples/zolertia/tutorial/03-coap/resources/Makefile Search resources to include in the “resources” directory REST engine and CoAP libraries

slide-14
SLIDE 14

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

slide-15
SLIDE 15

examples/zolertia/tutorial/02-ipv6/03-client-and-server er er-examp ample-se server ver CoAP server in Contiki OS Border der Router er IPv6/6LoWPAN Copper per CoAP P user-ag agen ent On Firefox browse the CoAP Server, discover and learn its resources

slide-16
SLIDE 16

apps/er-coap apps/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!)

slide-17
SLIDE 17

Pong! – check the CoAP server is online

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

slide-18
SLIDE 18

Discover – learn the Resources the CoAP server has

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

slide-19
SLIDE 19

Hover over the resources to learn its attributes

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

slide-20
SLIDE 20

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

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

slide-21
SLIDE 21

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

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

slide-22
SLIDE 22

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

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

slide-23
SLIDE 23

04 04-mqtt mqtt

slide-24
SLIDE 24

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
slide-25
SLIDE 25

CONNECT

Waits for a connection to be established with the server

DISCONNECT

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

SUBSCRIBE

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

UNSUBSCRIBE

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

PUBLISH

Updates a topic with data

slide-26
SLIDE 26

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

slide-27
SLIDE 27

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

slide-28
SLIDE 28

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

slide-29
SLIDE 29

apps/mqtt Required to be included in the process using MQTT

slide-30
SLIDE 30

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

slide-31
SLIDE 31

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

slide-32
SLIDE 32

apps/mqtt Message ID (mid) is zero for QoS=0

slide-33
SLIDE 33

apps/mqtt

slide-34
SLIDE 34

apps/mqtt

slide-35
SLIDE 35

mqtt tt-de demo mo The MQTT client publishing to the topic “zolertia/evt/status” and subscribed to “zolertia/cmd/leds” Border der Router er IPv6/6LoWPAN mqtt_c tt_cli lient nt.p .py Paho MQTT client Subscribed to the “zolertia/evt/status” topic, used to check the example examples/zolertia/tutorial/04-mqtt MQTT brók óker test.mosquitto.org

slide-36
SLIDE 36

examples/zolertia/tutorial/04-mqtt

slide-37
SLIDE 37

examples/zolertia/tutorial/04-mqtt

static void mqtt_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; } }

slide-38
SLIDE 38

examples/zolertia/tutorial/04-mqtt STATE_I TE_INIT NIT: mqtt_register(…) STATE_R TE_REG EGIS ISTER TERED: ED: mqtt_connect(…) STATE_C TE_CONNEC NECTING TING: : ctimer checking(…) STATE_C TE_CONNEC NECTED TED: : mqtt_subscribe(…) STATE_PUBLISH TE_PUBLISHING ING: : etimer_set(publish interval) DISCO CONNEC NECTED TED: : mqtt_disconnect(…)

slide-39
SLIDE 39

examples/zolertia/tutorial/04-mqtt Set default configuration values Create topic/subscription/id strings (STATE_INIT) Polls the state machine as described before

slide-40
SLIDE 40

apps/mqtt/mqtt.c

TIP: enable the DEBUG to 1 to print more information about the processes taking place inside the MQTT library

slide-41
SLIDE 41

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

slide-42
SLIDE 42

An Antonio

  • nio Li

Liñán án Colina lina

Twitter: @4Li6NaN LinkedIn: Antonio Liñan Colina github.com/alignan hackster.io/alinan

alinan@zolertia.com antonio.lignan@gmail.com