Communicating with Internet of Things

by Peter Martin / @pe7er

Overview Presentation

  • Internet of Things (IoT)
  • My 2 IoT "projects"
  • MQTT
  • Installation
  • Demo

1. Internet of Things (IoT)

“Internet of Things is a global infrastructure for the information society, enabling advanced services by interconnecting (physical and virtual) things based on existing and evolving interoperable information and communication technologies.”
International Telecommunication Union (ITU), June 2012
“The Internet of Things (IoT) is the network of physical objects—devices, vehicles, buildings and other items—embedded with electronics, software, sensors, and network connectivity that enables these objects to collect and exchange data”
Wikipedia.org on IoT
“The Internet of Things (IoT) is
a network of devices that are connected to each other using TCP/IP
and communicate autonomously.
#IoT #JaB16
@pe7er (in exactly 140 characters)

Be Careful!

Communication IoT devices

Input
Text Messages

Sensors
   - temperature
   - movement
   - sound
   - light
   - touch / push button
   - pressure
   - etc

Output
Text Messages

Signals
    - light (LED)
    - switch (Relay)
    - sound
    - screen

Self-ordering Fridge: not for me!

Arduino

Microcontroller

ESP8266

Wifi chip

Add Wifi to Arduino

ESP8266

Out-of-the-box
low-cost WiFi chip: $5
full TCP/IP stack
connect to a Wi-Fi network
Hayes-style "AT" commands

No need for microcontroller
it has its own, programmable:
- Software Development Kit (SDK)
- esp-open-sdk (GCC toolchain)
- NodeMCU + LUA scripts
- Arduino IDE + ESP8266WiFi library

ESP8266 Versions

Pins of ESP8266-01

Ready to flash the ESP8266

2. My IOT v1

Flashing the ESP8266

Tinkering with PCB, wire,
IC socket, DS18B20 Transistor

Bridge

Temperature sensor

Connect to ThingSpeak.com (RESTFull)

2. My IOT v2

The Sandwich

My co-working place

Last year, me with longer hair, working

Remko, my co-working colleague

No lunch for me

A Raspberry Pi for everything

What is the first thing
you do at work?


Co-working people: connect laptop / mobile to wifi

Raspberry Pi?


Raspberry Pi: collects MAC addresses of their devices
+ sends to my external webservice

Action photo of Raspberry Pi

Webservice?


Webservice (RESTFull): SLIM Framework

Connect with Joomla database!
(See github Pierre-André Vullioud)

Joomla?


Joomla Website: = External site
custom "com_members" Component
Matches known MAC addresses with MAC data of webservice

Easier access?


Mobile App: displays co-working colleagues + their presence

Nice, but

all 1-to-1 communication
RESTFull needs Webserver: can be heavy


And Now For Something Completely Different...

Some Message Protocols

6LoWPAN (IPv6 over Low power Wireless Personal Area Networks). DASH7 active RFID. MyriaNed wireless sensor network (WSN) platform. Z-Wave wireless communications protocol for home automation. ZigBee for personal area networks with small, low-power digital radios. Low-Power Wide-Area Network (LPWAN). Thread protocol for household devices to communicate on a network. Matrix open protocol for real-time communication. Constrained Application Protocol (CoAP) used for very simple electronics devices. AMQP (Advanced Message Queuing Protocol). STOMP (Simple (or Streaming) Text Oriented Message Protocol). MQTT (MQ Telemetry Transport).

person-to-device

device-to-device

devices-to-devices

MQTT

http://mqtt.org/

  • simple and lightweight messaging protocol
  • layer on TCP/IP
  • ISO/IEC PRF 20922
  • “MQ Telemetry Transport”
    (IBM's MQ - message queuing)
  • publish/subscribe

Publish/subscribe pattern

“publish/subscribe decouples a client,
who is sending a message (called publisher)
to MQTT server (called broker)
from another client (or more clients),
who is receiving the message (called subscriber)”

Decoupling of: Space, Time, Synchronization

3. MQTT Basics

Server: (MQTT Broker)
Clients: Publisher + Subscribers

Topics

Topic name: hierarchically structured with forward slashes

minimal 1 character
Case Sensitive

Examples of topics





myhouse/groundfloor/kitchen/temperature
myhouse/groundfloor/livingroom/brightness
myhouse/groundfloor/livingroom/temperature
myhouse/firstfloor/bathroom/temperature


Wildcards in topics


Single level: +
myhouse/groundfloor/+/temperature

myhouse/groundfloor/kitchen/temperature
myhouse/groundfloor/livingroom/brightness
myhouse/groundfloor/livingroom/temperature
myhouse/firstfloor/bathroom/temperature

Wildcards in topics


Multi level: #
myhouse/groundfloor/#

myhouse/groundfloor/kitchen/temperature
myhouse/groundfloor/livingroom/brightness
myhouse/groundfloor/livingroom/temperature
myhouse/firstfloor/bathroom/temperature

4. MQTT Communication

MQTT Client/Server:

Client

device + TCP/IP,
sends / receives messages using MQTT

Broker

server + TCP/IP,
receives all messages,
filters
and sends to subscribed clients

1. Connect

Client

sends CONNECT message to the broker

Broker

responds with a CONNACK to acknowledge the connection.

2. Subscribe to topic(s)

Client

sends SUBSCRIBE message to the broker with:

Packet Identifier: identify message in message flow (QoS > 0)
List of Subscriptions: one or more topic(s) + QoS level


Broker

responds with a SUBACK (Subscribe Acknowledge) message:

Packet Identifier: identify message in message flow(QoS > 0)
Return Code: 0,1,2 (success + QoS level), 128 = failure

3. Publish

Client

sends PUBLISH message to the broker with:

Topic Name: jandbeyond/room1
Payload (content) : Hello Joomlers!

QoS: level 0, 1 or 2

Quality of Service (QoS)

QoS 0 - No-feedback needed

3. Publish with QoS 0

At most once: no acknowledge, no store + redelivered,
fire and forget

Client

sends PUBLISH message to the broker with:

Topic Name, Payload, Packet Identifier, QoS 0


Broker

does not respond

QoS 1 - Acknowledge receipt

3. Publish with QoS 1

At least once.
Sender stores message until ACK (PUBACK) from receiver

Client

sends PUBLISH message to the broker with:

Topic Name, Payload, Packet Identifier, QoS 1


Broker

responds with a PUBACK (Publish Acknowledge) message:

Packet Identifier

3a - Just Once

3. Publish with QoS 2

exactly once (Received > Released > Completed).

Client

sends PUBLISH message to the broker with:

Topic Name, Payload, Packet Identifier, QoS 2, DUP flag


Broker

responds with a PUBREC (Publish Received Acknowledge):

Packet Identifier

3. Publish QoS 2

Client

stores PUBREC message and answers to broker with PUBREL (Publish Release) message with:

Packet Identifier


Broker

receives PUBREL, will disregard all future messages with same Packet ID, and answers to client with PUBCOMP (Publish Complete) message with:

Packet Identifier

4. Unsubscribe

Client

sends UNSUBSCRIBE message to the broker with:

Packet Identifier: identify message in message flow (QoS > 0)
List of Subscriptions: one or more topic(s) + QoS level


Broker

responds with a UNSUBACK (unsubscribe Acknowledge):

Packet Identifier: identify message in message flow(QoS > 0)
Return Code: 0,1,2 (success + QoS level), 128 = failure

3. Security

3a - Authenticate

Authentication

- Username + Password
- X509 Client Certificate (Public/Private Keys)
- OAuth 2.0


Authorization

specify access rights to certain resources.

Topic permissions on the broker side:
- allowed topics
- allowed operation (publish and/or subscribe)
- allowed QoS

Connect & Authenticate

Protect Broker with username + password

Client

sends CONNECT message to the broker with

ClientId, cleanSession true,
username, password, keepAlive seconds


Broker

responds with a CONNACK with

sessionPresent true and returnCode

3b - Secure communication

Secure Communication

Transport Layer Security (TLS)
   - Using Let’s Encrypt certificates with mosquitto
   - MQTT over SSL / TLS using Mosquitto broker


MQTT Payload Encryption

Other MQTT features

  • Persistent Session
  • Retained Messages
  • Last Will and Testament
  • MQTT Keep Alive
  • Client Take-Over
  • MQTT over WebSockets

5. Installation

MQTT Software as a Service

Some Services:
ThingSpeak.com
io.adafruit.com
CloudMQTT.com



List of publically-accessible MQTT brokers (for testing and prototyping).

MQTT Broker

Mosquitto

Install Mosquitto broker + client on Linux

$ sudo apt-get install mosquitto mosquitto-clients

Publish message to channel “test/msg”

$ mosquitto_pub -d -t test/msg -m "Hello JandBeyond!"

Subscribe & receive all messages from channel “test/msg”

$ mosquitto_sub -t test/msg

MQTT Broker

Raspberry Pi


Installation

$ sudo apt-get install mosquitto mosquitto-clients

Publish message to channel “test”

$ mosquitto_pub -d -t test -m "Hello JandBeyond!"

Subscribe & receive all messages from channel “test”

$ mosquitto_sub -t test

MQTT Broker

Onion Omega


Installation on OpenWRT

$ opkg update
$ opkg install mosquitto| mosquitto-client

Publish message to channel “test/msg”

$ mosquitto_pub -m 'Hello JandBeyond!' -t test

Subscribe & receive all messages from channel “test”

$ mosquitto_sub -v -t test

MQTT Client

MQTT.fx (written in Java)


Download

$ wget http://www.jensd.de/apps/mqttfx/1.1.0/mqttfx-1.1.0-64bit.deb

Installation

$ sudo dpkg -i mqttfx-1.1.0-64bit.deb

MQTT Client for Android

Some MQTT Client Apps in Google Playstore


MyMQTT


MQTT Client


MQTT Dashboard

Various

python: Mosquitto Python module

arduino: PubSubClient - Arduino Client for MQTT


php:
bluerhinos/phpMQTT Simple PHP class to connect/publish/subscribe to an MQTT broker

McFizh/libMQTT Simple MQTT library for PHP with support for MQTT version 3.1.1, TLS.

6. Demo

Sorry...

I do not have sheets for the demo...
You had to be there!


ok, there's a video at Youtube

Questions?


Presentation: http://slides.db8.nl



Peter Martin
e-mail: info at db8.nl
twitter: @pe7er
presentation: https://db8.nl

Photo Credits 1/3

Photo Credits 2/3

Photo Credits 3/3