Understanding IoT Data Protocol - PyCon ID 2018

tegarimansyah 92 views 26 slides Nov 30, 2018
Slide 1
Slide 1 of 26
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26

About This Presentation

IoT device is different from our computer or server. As well as the way they communicate. Here we will discuss the common way IoT devices are connected with other devices


Slide Content

Understanding IoT Data Protocol
Tegar Imansyah - 4 November 2018

About
●Backend Engineer for Ngabarin IoT Platform
●Surabaya.py Organizer
●Mechatronics Engineering from PENS
●Interested in: Microcontroller, Raspberry Pi, Linux, Web, Data
Get in touch:
http://github.com/tegarimansyah
http://t.me/tegarimansyah
http://linked.in/tegarimansyah

Agenda
●Understanding IoT
●HTTP Request
●WebSocket
●MQTT
●Conclusion

Are you familiar with...
Web framework Microcontroller
WebSocket
Communication Protocol

What is IoT
IoT = Internet of Things, a fancy buzzword when we talk
about Industry 4.0
The Internet of things is the network of physical devices.
Combination from electronics, software, sensors,
actuators, and connectivity which enables these things to
connect, collect and exchange data. - Wikipedia, with
adjustments.

Electronics Software
Sensors Actuators Connectivity
Light Sensor
Humidity Sensor
Water Pump
Microcontroller Server

Sensor
Actuator
Microcontroller
Gateway
(Microcontroller)
Sensor
Actuator
Microcontroller
Sensor
Actuator
Microcontroller
Server
Gateway
(Microcontroller)
Gateway
(Microcontroller)
Connectivity

Why IoT Device Different With Computer
Google Cloud F1-micro (Smallest)
●0.2 vCPU Intel Xeon E5
●Memory: 0.6 GB = 588 MiB

ESP32 (Popular Powerful Microcontroller)
●CPU : Dual Core 240 Mhz
●Memory: 520 KiB
Arduino Uno (ATmega328) - (Most Popular dev board)
●CPU : 20 Mhz
●Memory: 2048 B = 2 KiB

What to discuss
●HTTP
●Websocket
●MQTT

HTTP
HTTP is a request–response protocol in the
client–server computing model.
Request is usually using REST -
GET, POST, PUT, DELETE
Response with status code:
200 - OK
201 - Created
400 - Bad Request
etc….

Test HTTP
Server Side
Client Side

Light Sensor
Humidity Sensor
Water Pump
Microcontroller Server
Problem: How to send data to Microcontroller in Real
Time?

WebSocket
Websockets allows persistent
connections with simultaneous
bi-directional communication.
They come with all the benefits of
HTTP since websockets initially
start off as an HTTP handshake,
before getting elevated to
continue the rest of the
communication in websockets.

Test WebSocket
Callback Function

MQTT
MQTT is a publish-subscribe-based messaging protocol.
It works on top of the TCP/IP protocol.
It is designed for connections with remote locations where a "small code footprint"
is required or the network bandwidth is limited.
The publish-subscribe messaging pattern requires a message broker.

MQTT

16
Brokertable-lamp
room-lamp
subscribe
table-lamp
subscribe
room-lamp
subscribe table-lamp
subscribe room-lamp
subscribe table-lamp
subscribe room-lamp

MQTT
17
Brokertable-lamp
room-lamp
publish table-lamp (255,0,0)
receive table-lamp (255,0,0)
receive table-lamp (255,0,0)

Who is Broker?
Adafruit.io
Google
Cloud IoT Core

Test MQTT
Publisher Subscriber

What to learn about MQTT
●Topic
○Wildcart = home/+/lamp
○Wildcart = home/bedroom/#
●User & Access Control List (ACL)
○%u/home/bedroom => for user == tegar, only able to access tegar/home/bedroom
●Quality of Service (QoS)
○0
○1
○2
●Broker Bridging

MQTT over WebSocket (?)
=> HTTP, HTTPS, FTP, FILE
MQTT <> HTTP, HTTPS
WebSocket == HTTP (HTTPS via wss)

MQTT over WebSocket
TCP/IP
Physical Layer
:
HTTP WebSocket MQTT
Browser

HTTP vs MQTT performance tests
Credit to: https://flespi.com/blog/http-vs-mqtt-performance-tests
1K Messages Bytes TransmittedMosquitto MQTT
Client
MQTT over SSL
1 Publish per msg
283.743 5.911
HTTPS
1 POST per msg
15.373.263 115.669
HTTPS
1 POST with 1000
msg
20.515 0.307
Conclusion: MQTTS is 20 times faster and requires 50 times less traffic on the task of
posting consistent time-valuable data.

Conclusion
●Be wise to choose your protocol, there is no One Size Fits All
●HTTP best for one way transmission (client-to-server)
Websocket best for full-duplex persistent connection in browser
MQTT best for persistent connection in IoT
●Many other message broker you can try: AMQP, Kafka, etc.
They best on their own purpose.

Ready to Play with IoT?
●No Undo
●Unseen
●Broken = Start
from Beginning
●Very Fun!

Thanks!
Any questions?
You can find me at @tegarimansyah
Get slide and code:
https://github.com/tegarimansyah/presentation/tree/PyCon-ID-2018