the internet of things: messaging protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018....

46
The Internet of Things: Messaging Protocols Prof. Luciano Bononi [email protected] MASTER DEGREE IN COMPUTER SCIENCE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING, UNIVERSITY OF BOLOGNA, ITALY Prof. Marco Di Felice [email protected] Course website: h8p://www.cs.unibo.it/projects/iot/

Upload: others

Post on 17-Sep-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

TheInternetofThings:MessagingProtocols

[email protected]

MASTERDEGREEINCOMPUTERSCIENCEDEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

[email protected]

Coursewebsite:h8p://www.cs.unibo.it/projects/iot/

Page 2: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

2

IoTProtocolStack

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

Page 3: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

3

IoTProtocolStack

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

IEEE802.3 IEEE802.11 IEEE802.15 IEEE802.16 OTHERS

IPv4andIPv6+6LoWPAN

TCP UDP

MQTT CoAP AMQP HTTP OTHERS

PHY/MACPROTOCOLS

NETWORKPROTOCOLS

TRANSPORTPROTOCOLS

MESSAGINGPROTOCOLS

Page 4: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

4

IoTMessagingProtocols

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² Session/ApplicaWonLayerProtocols1.  ProvidingtheabstracXonof“message”(elementaryunitofdata

communicaXonamongIoTend-points).2.  ProvidingprimiWvesfordatacommunicaWon/messageexchange

totheupper-layerIoTapplicaXons.3.  ImplemenXngspecificnetworkingparadigms(e.g.publish-subscribe

orrequest-response).4.  ProvidingaddiXonalreliabilityorsecuritymechanisms.5.  SomeXmesadaptaXonofpre-exisWng(notnaXvelyM2M)soluWons

Page 5: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

5

IoTMessagingProtocols

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² Session/ApplicaWonLayerProtocols…WHICHprotocols?

Ø  HTTPØ  MQTTØ  CoAPØ  AMQPØ  XMPP(notcoveredhere)Ø  DDS(notcoveredhere)Ø  …

WewilltalkmoreaboutHTTPforM2McommunicaXonslaterinthiscourse(whendiscussingabouttheWebofThings).Inanycase,itcannotbe

consideredanIoT-naXveprotocol…

Page 6: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

6

TheMQTTProtocol

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² MessageQueuingTelemetryTransportProtocol(MQTT)²  LightweightmessagingprotocoldesignedforM2M(machineto

machine)telemetryinresource-constrainedenvironments.²  ProposediniXallybyAndyStanford-Clark(IBM)andArlen

Nipperin1999forconnecXngOilPipelinetelemetrysystemsoversatellite.

²  ReleasedRoyaltyfreein2010andasOASISstandardin2014² MQTT(currentspecificaXon3.1/3.1.1)² MQTTforSensorNetworks(MQTT-SN)

Page 7: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

7

TheMQTTProtocol

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² TheMQTTprotocolimplementsapublish-subscribemessagingmechanism,involvingthreemainactors:²  Publishersàproducedataandsendthemtoabroker.²  Subscribersàsubscribetoatopicofinterest,andreceive

noXficaXonswhenanewmessageforthetopicisavailable.²  Brokeràfilterdatabasedontopicanddistributethemto

subscribers.

ONE-TO-MANYCOMMUNICATION

BROKER

PUBLISHER

SUBSCRIBER1

SUBSCRIBER2

PUBLISH

SUBSCRIBE

SUBSCRIBE

{CLIENTs

Page 8: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

8

TheMQTTProtocol

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

BROKER

PUBLISHER

SUBSCRIBER1

SUBSCRIBER2

PUBLISH

SUBSCRIBE

²  Atopicdefinesthemessagecontext(e.g.temperaturedata).²  NodirectcommunicaWonbetweenclients(thedatamessagesare

alwaysforwardedviathebroker).²  Rolesarepurelylogical:thesamedevicecanserveasPublisher

(onatopic),andSubscriber(onadifferenttopic).

ARCHITECTURALREMARKS

SUBSCRIBE

Page 9: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

9

TheMQTTProtocol

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² MQTTControlPacketFormat

FIXED VARIABLE PAYLOAD

FixedHeader(only2bytes!) OpXonal,variablesize

OpXonal,binaryencoded,variablesizeupto256MB

²  TheFIXEDheadercontainsthepackettype,thesizeofthepayload,andthelevelofQualityofService(seenextslides).

14differentcommands:

CONNECT,CONNACK,PUBLISH,PUBACK,PUBREC,PUBREL,PUBCOMP,SUBSCRIBE,SUBACK,UNSUBSCRIBE,

UNSUBSCRIBEACK,PINGREQ,PINGRESP,DISCONNECT

Page 10: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

10

TheMQTTProtocol

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² MQTTControlPacketFormat

FIXED VARIABLE PAYLOAD

²  TheVariableheadercontainstheaddiXonalparametersbasedonthecommandtype.Forinstance,theheaderofthePUBLISH/SUBSCRIBEmessagecontainstheTOPICfield.

²  TheTOPICisastringfield,withoutaspecificformat,justnamingconvenWons(see[1]).

topic=temperature

topic=temperature/kitchen/ topic=temperature/kitchen/sensor1

PLAINNAMING

HIERARCHIALNAMING(TOPICNAME+TOPICFILTER)

Page 11: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

11

TheMQTTProtocol

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² MQTTisbuiltontopoftheTCPprotocolØ  In-orderdelivery,connecXon-oriented,ACKeretranmissions.Ø  ..butalsolongerTSPheadersizeandhighercomplexity.Ø  MQTT-SNàusesUDP,supportstopicIDs(insteadofnames).

²  MQTTkeepstheTCPconnecXonbetweenaclientandabrokeropenaslongaspossible,bymeansofPINGREQmessages.

CONNECT

CONNACK

PINGREQPINGRESP

PUBACK

PUBLISH…. ….

OPENTHECONNECTION

KEEPTHECONNECTIONOPEN

PUBLISHDATAPUBLISHER BROKER

Page 12: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

12

TheMQTTProtocol

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² DespiteusingTCP,MQTTmessagescansWllbelost…1.  TCPguaranteesdeliveryonasinglelink(agentàbroker,orviceversa),

whataboutpublisheràsubscriberdelivery?2.  Whataboutifthereceiveristemporarilydownwhileasenderis

a8empXngtosendamessage?

² MQTTclientscanrequestthreelevelofQualityofService(QoS)tothebroker:² QoSLevel0(fire&forget)àdefaultQoSlevel,clientsdonotstoremessagesanddonotreceiveACKsfrombroker,samedeliveryguaranteesthanTCP.² QoSLevel1(deliveratleastonce)àseenextslide(10)² QosLevel2(deliverexactlyonce)àseenextslide(11)

Page 13: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

13

TheMQTTProtocol

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² MQTTQoSLevel1(Deliveratleastone)

MQTTTutorial:h8p://www.steves-internet-guide.com/understanding-mq8-qos-levels-part-1/

1.  Theclientsendsamessageandwaitsforanacknowledgement(PUBACK)fromthereceiver.

2.  IfthePUBACKisreceived,theclientdeletesthemessagefromtheoutboundqueue.

3.  Otherwise,itresendsthemessageatregularintervalwiththeDUPflagsetto1,WllaPUBACKisreceived.

ThereceivermightreceivethesamedatamulWpleWmes!

Page 14: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

14

TheMQTTProtocol

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² MQTTQoSLevel2(Deliverexactlyonce)

MQTTTutorial:h8p://www.steves-internet-guide.com/understanding-mq8-qos-levels-part-1/

1.ThesendersendsamessageandwaitsforanACK(PUBREC)2.ThereceiversendsaPUBRECmessage3.Ifthesenderdoesn’treceiveanACK(PUBREC)itwillresendthemessagewiththeDUPflagset.4.WhenthesenderreceivesanACKmessagePUBRECitthensendsamessagereleasemessage(PUBREL).5.Ifthesenderdoesn’treceivethePUBRELitwillresendthePUBRECmessage5.WhenthereceiverreceivesPUBREL,itcannowprocessthedata.6.Thereceiverthensendapublishcomplete(PUBCOMP).7.Ifthesenderdoesn’treceivethePUBCOMPmessageitwillresendthePUBRELmessage.8.WhenthesenderreceivesthePUBCOMPtheprocessiscompleteanditcandeletethemessagefromtheoutboundqueue(finally!).

Page 15: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

15

TheMQTTProtocol

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² MQTTQoSLevel:evaluaWonresults

StefanMijovic,ErionShehu,ChiaraBurau,“ComparingApplicaXonLayerProtocolsfortheInternetofThingsviaExperimentaXon”,Proc.ofiEEERTSI2016

PROTO

COLEFFICIEN

CY(*

)

(*)raXobetweenapplicaXonbytesandoverheadbytes

ROUNDTR

IPTIM

E(RTT)

Page 16: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

16

TheMQTTProtocol

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² MQTTQoSlevelscanbecoupledwithaddiWonalsehngsatthebrokerside,inordertoensuredeliveryofmessagesalsoinpresenceofclientdisconnecXons.1.   RETAINEDmessage:Thebrokerstoresthelastmessagefora

specifictopic.Eachclientthatsubscribestothattopicwillreceivethemessageimmediatelyawersubscribing.Foreachtopiconlyoneretainedmessagewillbestoredbythebroker.

(AIM)Anewlyconnectedsubscriberswillreceivethelatestupdateimmediatelyandshouldn’thavetowaitWllnextPUBLISHacWon.

Page 17: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

17

TheMQTTProtocol

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² MQTTQoSlevelscanbecoupledwithaddiWonalsehngsatthebrokerside,inordertoensuredeliveryofmessagesalsoinpresenceofclientdisconnecXons.2.   PERSISTENTsession:Thebrokerstoresalltherelevant

informaXonaboutclients,like:allsubscripXons,orallQoS1-2datanotconfirmedsincetheclientwasoffline.

(AIM)AclientshouldexplicitlygetmessagesfortheXmeitisoffline…clearly,increasingtheresourceuWlizaWon(CPU/HD)atthebrokerside!

Page 18: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

18

TheMQTTProtocol

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² MQTTprovidessome(basic)securitymechanismsfordataconfidenWalityandclientauthenWcaWon,whichmainlyrelyonexternalinfrastructuresoronlowerlayersoluXons.CLIENTAUTENTICATION

ClientauthenXcaXoncanbeperformedinthreeways:

² ClientIDs:everyMQTTclientneedsaunivoqueidenXfier.² UsernameandPassword:MQTTdoesnotprovideencrypXonmechanisms,needoftransportlayer(TLS)ornetwork(IPsec)soluXons.² CerWficates:provided/managedbythird-partyauthoriXes.

(TOPICACL)BasedonClientID,thebrokercanrestrictaccesstospecifictopics.

MQTTTutorial:h8p://www.steves-internet-guide.com/mq8-security-mechanisms/

Page 19: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

19

TheMQTTProtocol

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² MQTTprovidessome(basic)securitymechanismsfordataconfidenWalityandclientauthenWcaWon,whichmainlyrelyonexternalinfrastructuresoronlowerlayersoluXons.DATACONFIDENTIALIY

DataconfidenXalitycanbeimplementedintwoways(COMPLEMENTARY):

² TSP-levelEncrypWon:notapartofMQTT,usesTLS/SSLprotocolandencryptsTCPdatasegmentsàrefersonlytotheclientàbrokerlink.

² APP-levelEncrypWon:notapartofMQTT,payloadencrypXonmustbeprovidedbytheapplicaXonàcanbeusefulforend-to-endsecurity(publisheràsubscriber),butdoesnotprotectthepasswordneededforthebrokeraccess.

MQTTTutorial:h8p://www.steves-internet-guide.com/mq8-security-mechanisms/

Page 20: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

20

TheMQTTProtocol:DEMO

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

PUBLISHER

(FishinoUNOboard)

BROKER

(MOSQUITTO)(WI-FIlink) (WI-FIlink)

SUBSCRIBER

(Laptop)

MOSQUITTOàOpensourceMQTT(1.3/1.3.1)brokerimplementaXonMulX-playorm,VersionsavailableforLinux/Ubuntu,MacOSX,WindowsDownloadat:h8ps://mosqui8o.org

apt-get install mosquitto mosquitto-client

Page 21: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

21

TheMQTTProtocol:DEMO

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

MOSQUITTOBroker:h8ps://mosqui8o.org

² MQTTclientuXliXes:mosquitto_pubemosquitto_sub

user@hostTest:$mosquitto_pub–t“Temperature/Kitchen”–m“34.5”

MESSAGEPUBLISHING

MESSAGESUBSCRIBING

user@hostTest:$mosquitto_sub–t“Temperature/Kitchen”34.5

user@hostTest:$mosquitto_sub–t“#”34.5

user@hostTest:$mosquitto_sub–t“Temperature/+”34.5

Topicname Topiccontent

Page 22: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

22

TheMQTTProtocol:DEMO

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

MOSQUITTOBroker:h8ps://mosqui8o.org

² MQTTclientuXliXes:mosquitto_pubemosquitto_sub

user@hostTest:$mosquitto_pub–t“Temperature/Kitchen”–m“34.5”

MESSAGEPUBLISHING

MESSAGESUBSCRIBING

user@hostTest:$mosquitto_sub–t“Temperature/Kitchen”34.5

user@hostTest:$mosquitto_sub–t“#”34.5

user@hostTest:$mosquitto_sub–t“Temperature/+”34.5

Topicname Topiccontent

WILDCARDS(inTOPICname)²  +singlelayerofhierarchy²  #allremaininglevelsof

hierarchy(onlythefinalpart)ü  Temperature/1/2/valueü  Temperature/+/+/valueü  Temperature/#

Page 23: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

23

TheMQTTProtocol:DEMO

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

[1]MOSQUITTOBrokerConfiguraWon:h8ps://mosqui8o.org/man/mosqui8o-conf-5.html

² Mosqui8oconfiguraXonfile:/etc/mosquitto/mosquitto.conf

#Enable/disablepersistence,i.e.messagesavingsonbrokersidepersistencetruepersistence_location/var/lib/mosquitto/

#Enable/disableauthenticationallow_anonymousfalsepassword_files/etc/mosquitto/mosquitto_pwd

#Logbrokeractivitieslog_destfile/var/log/mosquitto/mosquitto.log

#Presenceofduplicates(onlyforQoS0and1)allow_duplicate_messagesfalse

EXAMPLE,SEE[1]forthecompletefileformat

Page 24: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

24

TheMQTTProtocol:DEMO

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

[1]IoTDISI-UNIBOCoursewebsite:h8p://www.cs.unibo.it/projects/iot/

² MQTTatPublisherside(FishinoUNO,butshouldworkalsoonanyArduino*devices):

booleanpublishData(charclientID,char*topic,char*payload){booleanconnected=clientMQTT.connected();if(!connected)connected=clientMQTT.connect(clientID);if(connected){

boolresult=clientMQTT.publish(topic,payload);clientMQTT.loop();returnresult;}elseSerial.println(F("MQTTBrokernotavailable"));return(false);}

Completecodeavailableat[1]

Page 25: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

25

TheMQTTProtocol:DEMO

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² MQTTatPublisherside(FishinoUNO,butshouldworkalsoonanyArduino*devices):

#include<PubSubClient.h>PubSubClientclientMQTT;voidsetup(){

…clientMQTT.setClient(client);

clientMQTT.setServer(”192.168.1.200",1883);}voidloop(){

…publishData(“MyClientID”,”MyTopic”,”MyMessage”);

}

Completecodeavailableat[1]

[1]IoTDISI-UNIBOCoursewebsite:h8p://www.cs.unibo.it/projects/iot/

MOSQUITTOBrokerIPAddress

MOSQUITTOBrokerIPPort

Page 26: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

26

IoTMessagingProtocols

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² Session/ApplicaWonLayerProtocols…WHICHprotocols?

Ø  HTTPØ  MQTTØ  CoAPØ  AMQPØ  XMPP(notcoveredhere)Ø  DDS(notcoveredhere)Ø  …

Page 27: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

27

TheCOAPProtocol

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² ConstrainedApplicaWonProtocol(CoAP)² Messagingprotocolforusewithconstrainednodesand

constrained(e.g.,low-power,lossy)networks.²  DifferentlyfromMQTT,CoAPimplementsarequest-response

interacWonmodel(similartotheHTTPprotocol).²  RESTfularchitectureforCostrainedEnvironments(CoRE).²  EachresourceisaddressedbyanURI(UniformResourceIdenXfier).

SERVER CLIENT

REQUEST(GETTEMPERATURE)

REPLY(VALUE:20.5)

COAPSpecificaWons:h8ps://tools.iey.org/html/rfc7252#secXon-4.8

Page 28: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

28

TheCOAPProtocol

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² CoAPoperaXonscanbeLOGICALLYsplitintwosub-layers:²  Requests/responsesàclient-serverRESTfulinteracXons² MessagesàparadigmimplementaXon+reliabilitymechanisms

APPLICATION

UDPPROTOCOL

REQUESTs/RESPONSEs

MESSAGES

CoAP

FourtypesofCoAPmessages:CONFIRMABLE(CON),NON-CONFIRMABLE(NON),ACK,RESET

²  AcXons/methodsrequestedonaresource:GET,POST,PUT,DELETE²  PossibleresponsestoanacXonrequested:SUCCESS,CLIENTERROR,SERVERERROR

Page 29: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

29

TheCOAPProtocol

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² ExamplesofCoAPmessageexchanges.

SERVER

CLIENT

CON[0x7a10]GET/temperature

ACK[0x7a10]

…Timepasses…

CON[0x23bb]Token0x7a10Value“25.5”

ACK[0x23bb]

SERVER

CLIENT

NON[0x7a11]GET/temperature

Token0x74

NON[0x23bc]Token0x74Value“25.5”

REQUEST/RESPONSEUSINGCONFIRMABLE

MESSAGESREQUEST/RESPONSE

USINGNONCONFIRMABLE

MESSAGES

Page 30: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

30

TheCOAPProtocol

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² EachresourceisaddressedbyanURI(UniformResourceIdenXfier).

coap://dante.cs.unibo.it/temperature/serverRoom

² BasedontheUDPprotocol(butopXonalmechanismscanbeusedforenhancedreliability,i.e.Confirmablemessages+Retransmissions)

² AsynchronousRequest/Responseparadigm² Different(Shorter)PacketHeader(seenextslide)² ServiceDiscoveryandProxymechanisms

DIFFERENCESCOMPAREDTOTHEHTTPPROTOCOL

Page 31: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

31

TheCOAPProtocol

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² CoAPMessageHeader(fixed-size4-byteheader)Messagetype:CON,NON,ACK,RST IndicatesRequestmethodorResponsecode

MessageID:matchesCON

andACKmessages

ValueusedtocorrelateaRequestand

withthecorrespondingResponse.

Page 32: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

32

TheCOAPProtocol

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² CoAPimplementssomelightweightreliabilitymechanisms:Ø  DuplicatedetecWonforbothConfirmable(CON)andNon-

Confirmable(NON)messagesØ  Simplestop-and-waitretransmissionreliabilitywith

exponenWalback-offforConfirmablemessages

²  The sender retransmits the Confirmable message at exponenXally increasing intervals,unXlitreceivesanACK(orRSTmessage)orrunsoutofa8empts.

RandomValueà[ACK_TIMEOUT:ACK_TIMEOUT*ACK_RANDOM_FACTOR]Ø  ACK_TIMEOUTisdoubledateachretransmission,XllMAX_NUMBER_ATTEMPTSØ  ACK_RANDOM_FACTORisanode-specificvalue,usedtoavoiddistributedsynchronizaXons

Page 33: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

33

TheCOAPProtocol

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² TheOBSERVEmechanismallowsimplemenXngadatasubscripWonmechanism(similartoMQTT,butwithoutthebroker).1.  Theclientrequestsaresource(GET)withtheObserveOpXonfield. 2.  Theserveraddtheclienttothelistofobserversoftheresource3.  Ateachchangeofthetargetresource,theservernoXfiesallitsobservers

SERVERCLIENT

NON[0x7a10]Observe:0GET/temperatureToken:1234aa

NON[0x8b12]Value:34.4Token:1234aa

NON[0x8b12]Value:35.5Token:1234aa

NON[0x8b12]Value:26.5Token:1234aa

COAPSpecificaWons(DRAFT):h8ps://tools.iey.org/html/draw-iey-core-observe-08#page-6

Page 34: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

34

TheCOAPProtocol

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² AserverisusedbyaclientknowingaURIthatreferencesaresourceinthenamespaceoftheserver.²  AlternaXvely,clientscanusemulWcastCoAPrequests(onthedefaultport

5683)the"AllCoAPNodes"mulXcastaddresstofindCoAPservers²  MulXcastrequestsareNOTConfirmable(i.e.noACKmessagesaresent).²  IfaserverdoesdecidetorespondtoamulXcastrequest,itshouldback-off

(i.e.waitarandomperiodbeforesendingthereply)

GET“./well-known/core

CLIENT

SERVER1

SERVER2

Page 35: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

35

TheCOAPProtocol

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² CoAPonlysupportsalimitedsubsetofHTTPfuncXonality,² However,cross-protocolproxymechanismscanguaranteeseamlessHTTP-CoAPinteracWons(besideprovidingdatacaching).

HTTPGET/temperature(CoAP)CONGET/temperature

(CoAP)ACK

(CoAP)CONValue:23.4

(CoAP)ACK

HTTP200OK“Value:23.4”

CACHEDATA

CLIENT SERVER

PROXY

Page 36: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

36

TheCOAPProtocol

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² CoAPreliesonlower-layerprotocolsforsecuringtheclient-servercommunicaWon.²  MessageencrypXonprovidedatTSPLayer(DTLS–DatagramTransport

LayerSecurity)oratthenetworkLayer(IPSec).

²  AsCoAPrealizesasubsetofthefeaturesinHTTP/1.1,thesecurityconsideraXonsofHTTParealsoperXnenttoCoAP.InaddiXon,CoAPpresentssomeuniquevulnerabiliWes(see[1]fordetails):

1.  Proxiesarebytheirverynatureman-in-the-middle.2.  RiskofmessageamplificaXonandDDoSaqacks.3.   IPspoofingduetothelackofahandshakeinUDP.

[1]COAPSpecificaWons,SecurityconsideraWons:h8ps://tools.iey.org/html/rfc7252#page-80

Page 37: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

37

TheCoAPProtocol:DEMO

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

SERVER

(FishinoUNOboard)

CLIENT

(COPPER)(WI-FIlink)

https://addons.mozilla.org/en-US/firefox/addon/copper-270430/

FirefoxPluginsupporXngCoAPURIscheme,andenablingCoAPRequests-ResponsesinteracXonsviabrowser

Page 38: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

38

IoTMessagingProtocols

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² Session/ApplicaWonLayerProtocols…WHICHprotocols?

Ø  HTTPØ  MQTTØ  CoAPØ  AMQPØ  XMPP(notcoveredhere)Ø  DDS(notcoveredhere)Ø  …

Page 39: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

39

TheAMQPProtocol

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² AdvancedMessageQueuingProtocol(AMQP)²  Open-standardprotocolformessage-orientedapplicaWons.²  Itsupportssysteminteroperabilityindistributedenvironments.²  BasedonTCPprotocolwithaddiXonalreliabilitymechanisms

(at-most-once,at-least-onceoronce-delivery).²  Itsupportsbothpoint-to-pointcommunicaWonandpublish-

subscribecommunicaWonparadigms(likeMQTT).²  Programmableprotocol:severalenXXesandrouXngschemes

areprimarilydefinedbyapplicaXons.²  SeveralfuncXonaliXes:see[1]foracompleteprotocolillustraXon.

[1]AMQPProtocolOASISSpecificaWons:h8p://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-overview-v1.0-os.html

Page 40: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

40

TheAMQPProtocol

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² TheAMQParchitectureinvolvesthreemainactors:publishers,subscribers,andbrokers.

[1]AMQPProtocolTutorial:h8ps://www.rabbitmq.com/tutorials/amqp-concepts.html

PUBLISHER SUBSCRIBER

PUBLISHERSUBSCRIBER

EXCHANGE

QUEUE

QUEUE

AMQPBROKER

PUBLISH

ROUTE CONSUME

Page 41: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

41

TheAMQPProtocol

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² TheAMQParchitectureinvolvesthreemainactors:publishers,subscribers,andbrokers.

[1]AMQPProtocolTutorial:h8ps://www.rabbitmq.com/tutorials/amqp-concepts.html

CLIENT

CLIENT

BROKER INTERNET BROKER

CLIENT CLIENT

NETWORK1 NETWORK2

²  TheAMQPArchitecturenaXvelysupportssystemintegraWonand message-orientedcommunicaWonovertheInternet.

Firewall

Firewall

Page 42: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

42

TheAMQPProtocol

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² TheAMQParchitectureinvolvesthreemainactors:publishers,subscribers,andbrokers.

[1]AMQPProtocolTutorial:h8ps://www.rabbitmq.com/tutorials/amqp-concepts.html

² Queues:applicaXon-specificmessagebuffers² Exchanges:owencomparedtopostofficesormailboxes,takeamessageand

routeitintozeroormorequeues² Bindings:RulesfollowedbytheexchangefortherouXngprocess

AMQPEnWWes(withinthebroker):

²  DirectExchange:deliversmessagestoqueuesbasedonthemessagerouXngkey²  FanoutExchange:deliversmessagestoallofthequeuesthatareboundtoit²  TopicExchange:deliversmessagestooneormanyqueuesbasedontopicmatching²  Headersexchange:deliversmessagesbasedonmulXplea8ributesexpressedasheaders

Page 43: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

43

TheAMQPProtocol

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

² TheAMQPprotocoldefinestwotypesofmessages:²  Baremessages,thataresuppliedbythesender.²  Annotatedmessages,thatareseenatthereceiver.

E.Ahmed,I.Yaqoob,A.Gani,M.ImraniandM.Guizani,Internetofthingsbasedsmartenvironments:stateoftheart,taxonomy,andopenresearchchallenges,IEEEWirelessCommunica]ons,2016

Theheaderconveysthedeliveryparametersincluding:durability,priority,Xmetolive,firstacquirer,deliverycount

Page 44: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

44

ProtocolComparison

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

MESSAGEOVERHEADvsMESSAGESIZE RESOURCEUSEDvsPOWERCONSUMPTION

N.Naik,ChoiceofEffecXveMessagingProtocolsforIoTSystems:MQTT,CoAP,AMQPandHTTP,Proc.ofIEEEISSE,2017

Page 45: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

45

ProtocolComparison

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

BANDWIDTHvsLATENCY STANDADISATIONvsIoTUSAGE

N.Naik,ChoiceofEffecXveMessagingProtocolsforIoTSystems:MQTT,CoAP,AMQPandHTTP,Proc.ofIEEEISSE,2017

Page 46: The Internet of Things: Messaging Protocolscs.unibo.it/projects/iot/sessionprotocols.pdf · 2018. 4. 17. · 3 iot protocol stack the internet of things: messaging protocols l. bononi,

46

ProtocolComparison

THEINTERNETOFTHINGS:MESSAGINGPROTOCOLS L.BONONI,M.DiFELICE,DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERING,UNIVERSITYOFBOLOGNA,ITALY

PROVISIONINGvsSECURITY QoSvsINTEROPERABILITY

N.Naik,ChoiceofEffecXveMessagingProtocolsforIoTSystems:MQTT,CoAP,AMQPandHTTP,Proc.ofIEEEISSE,2017