Re Remote Data Acquisition and IoT 01219335 Data Acquisition and - - PowerPoint PPT Presentation

re remote data acquisition and iot
SMART_READER_LITE
LIVE PREVIEW

Re Remote Data Acquisition and IoT 01219335 Data Acquisition and - - PowerPoint PPT Presentation

Re Remote Data Acquisition and IoT 01219335 Data Acquisition and Integration Chaipo Chaiporn J n Jaik aikae aeo De Department of f Computer Engineering Ka Kasetsart Unive versity Materials partly taken from lecture slides by Karl and


slide-1
SLIDE 1

Re Remote Data Acquisition and IoT

Chaipo Chaiporn J n Jaik aikae aeo De Department of f Computer Engineering Ka Kasetsart Unive versity

01219335 Data Acquisition and Integration

Revised 2020-09-02 Materials partly taken from lecture slides by Karl and Willig

slide-2
SLIDE 2

2

Ou Outline

  • Remote data acquisition
  • Communication pattern in IoT
  • MQTT protocol
  • Programming examples
slide-3
SLIDE 3

3

sensor field

Re Remote Data Acquisition

  • Sensor nodes communicate to a gateway via radio links

radio links

Remote monitoring Network

gateway

slide-4
SLIDE 4

4

Da Data-Ce Centri ric Networki rking

  • Typical networking interaction is node-centric
  • Client/server, peer-to-peer
  • Desirable properties for IoT applications
  • Decoupling in space – neither sender nor receiver may need to

know their partner

  • Decoupling in time – ”answer” not necessarily directly triggered by

“question”, asynchronous communication

slide-5
SLIDE 5

5

Da Data-Ce Centri ric Networki rking Mo Model

Internet

D a t a Interest Interest D a t a

slide-6
SLIDE 6

6

Pu Publish/Subscribe Interaction

  • Idea
  • Entities can publish data under certain names
  • Entities can subscribe to updates of such named data
  • Variations
  • Topic-based
  • Content-based

Software bus Publisher 1 Publisher 2 Subscriber 1 Subscriber 2 Subscriber 3

slide-7
SLIDE 7

7

MQ MQTT T Protocol

  • Message Queue Telemetry Transport
  • Light-weight messaging protocol designed for connecting

edge IoT devices

  • Uses publish/subscribe pattern to interconnect devices
  • Operates on top of TCP, but works well with intermittently

connected links

slide-8
SLIDE 8

8

MQ MQTT T Operation

MQTT Broker

T

  • p

i c :

  • f

f i c e / r

  • m

2 / t e m p P a y l

  • a

d : 2 6

p u b l i s h

Topic: office/room2/temp

s u b s c r i b e

Topic: office/room2/temp Payload:26

s u b s c r i b e

T

  • p

i c :

  • f

f i c e / + / t e m p Topic: office/room2/temp Payload: 26

MQTT clients

slide-9
SLIDE 9

9

Ad Advantages of MQTT

  • Decouples publishers and subscribers
  • Allows message reception without long polling
  • Uses very short message headers
  • Supports multiple quality of services
  • Allows NAT traversal
slide-10
SLIDE 10

10

MQ MQTT T vs.

  • s. HTTP

TP

  • HTTP
  • MQTT

request response broker Icon made by Smashicons from www.flaticon.com

subscribe publish publish subscribe publish subscribe p u b l i s h

slide-11
SLIDE 11

11

MQ MQTT T Cl Client Tools

  • PC (Windows/macOS/Linux)
  • Chrome app: MQTTLens
  • Command-line tool: Mosquitto
  • Android
  • MyMQTT
  • IoT MQTT Panel
  • iOS
  • MQTTool
slide-12
SLIDE 12

12

MQ MQTT T Cl Client Librari ries

  • Eclipse Paho Project
  • Various implementations for many platforms and languages
  • MicroPython’s umqtt module
  • Light-weight MQTT client implementation
  • Two variations
  • umqtt.simple
  • umqtt.robust
  • Already built into standard MicroPython firmware
slide-13
SLIDE 13

13

Pu Public MQTT Brokers

  • HiveMQ
  • broker.hivemq.com
  • Mosquitto
  • test.mosquitto.org
  • Many more can be found on
  • https://github.com/mqtt/mqtt.github.io/wiki/public_brokers
slide-14
SLIDE 14

14

MQ MQTT T Topic Naming

  • A topic name consists of one or more topic levels

(UTF-8 strings), separated by a forward slashes

  • E.g.,
  • Topics are case-sensitive
  • ku/cpe is not the same as Ku/Cpe

ku/cpe/room204/temperature

topic level topic level topic level topic level

slide-15
SLIDE 15

15

Su Subscrip iption ion W Wild ildcar ards

  • Wildcards may be used for subscription
  • Single-level wildcard (+)
  • Replaces only one topic
  • Multi-level wildcard (#)
  • Replaces many topic levels
  • MUST be placed at the end of a topic
  • Some best practices are described here:
  • https://www.hivemq.com/blog/mqtt-essentials-part-5-mqtt-

topics-best-practices

ku/cpe/+/temperature ku/cpe/#

slide-16
SLIDE 16

16

MQ MQTT: Stack

MQTT SSL (optional) TCP IP Application

default port: 1883 default port: 8883

slide-17
SLIDE 17

17

MQ MQTT T Co Connection and Keep Alive

MQTT Client MQTT Broker

CONNECT CONNACK SUBSCRIBE SUBACK PUBLISH PUBLISH PINGREQ PINGRESP

⋮ ⋮ ⋮ ⋮

Connection establishment Regular activities Keep alive

DISCONNECT

Disconnection

slide-18
SLIDE 18

18

Qualit Quality o

  • f S

f Ser ervic ice (Qo e (QoS) Le ) Levels els

  • QoS 0 - at most once
  • Fire and forget
  • QoS 1 - at least once
  • Msg gets resent until PUBACK

is received

  • QoS 2 - exactly once
  • Achieved through four-way

handshake

https://www.hivemq.com/blog/mqtt-essentials-part-6-mqtt-quality-of-service-levels/

slide-19
SLIDE 19

19

Las Last W Will & ill & T Testame ament (L (LWT)

  • IoT devices may get disconnected ungracefully
  • Unstable wireless connection
  • Battery depletion
  • A client can publish a LWT in advance
  • In case of unexpected disconnection of the client, all

subscribed clients will get a message from the broker

slide-20
SLIDE 20

20

Re Retained Messages

  • A newly subscribed client may have missed previously

published data

  • A message published with the retain flag set will be

delivered to a newly subscribed client

slide-21
SLIDE 21

21

Sc Scenar ario 1 io 1 – Pu Publishing Data

Internet

MQTT Broker

periodically publish light status

subscribe for light status

Light sensor

slide-22
SLIDE 22

22

De Devi vice Op Operation

  • n
  • Connect KidBright to the local WiFi (e.g., KUWIN)
  • Connect to a local MQTT broker
  • Measure and publish light value every 2 seconds to the

topic ku/cpe/room204/light

slide-23
SLIDE 23

23

Co Code: I/O Setup

  • Create an ADC object associated with Pin 36

from machine import Pin, ADC ldr = ADC(Pin(36))

slide-24
SLIDE 24

24

Co Code: Wi WiFi Co Connection

  • Create WLAN connection with

WLAN class in the network module

  • Replace SSID and PASS with

appropriate values

  • wlan.connect() method is not

blocking, so the code keeps checking wlan.isconnected() to ensure connectivity before further proceeding

  • Use on-board red LED (IO2) for

WiFi connection indicator

import time import network from machine import Pin led_wifi = Pin(2, Pin.OUT) led_wifi.value(1) # turn it off wlan = network.WLAN(network.STA_IF) wlan.active(True) print("*** Connecting to WiFi...") wlan.connect("SSID","PASS") while not wlan.isconnected(): time.sleep(0.5) print("*** Wifi connected") led_wifi.value(0)

slide-25
SLIDE 25

25

Co Code: MQ MQTT T Br Broker r Co Connection

  • Replace UNIQUE-ID and BROKER with appropriate values
  • mqtt.connect() blocks until a connection is successfully

established

  • Use on-bard green LED (IO12) to indicate broker connection

from umqtt.robust import MQTTClient led_iot = Pin(12, Pin.OUT) led_iot.value(1) # turn it off mqtt = MQTTClient("UNIQUE-ID","BROKER") print("*** Connecting to MQTT broker...") mqtt.connect() print("*** MQTT broker connected") led_iot.value(0)

slide-26
SLIDE 26

26

Co Code: Publish shing Light Values

  • mqtt.publish() method requires strings or byte arrays for both

topic and payload arguments

while True: value = ldr.read() print("Publish light:", value) mqtt.publish("ku/cpe/room204/light", str(value)) time.sleep(2)

slide-27
SLIDE 27

27

Sc Scenar ario 2 io 2 – Su Subscrib ibin ing

Internet

MQTT Broker

  • subscribe for lamp control
  • set lamp status

publish LED value

Connect USB lamp here

slide-28
SLIDE 28

28

De Devi vice Op Operation

  • n
  • Connect KidBright to the local WiFi (e.g., KUWIN)
  • Connect to a local MQTT broker
  • Connect USB lamp to KidBright's USB port
  • Subscribe to ku/cpe/room204/lamp and use the

published value to change the lamp status

  • Payload is 0 → turn the lamp off
  • Payload is 1 → turn the lamp on
  • Other payloads should be properly ignored
slide-29
SLIDE 29

29

Co Code: I/O O Se Setup

  • USB-switch output is connected

to IO25

  • Logic 1 à Power off
  • Logic 0 à Power on

from machine import Pin lamp = Pin(25, Pin.OUT) lamp.value(1) # turn it off

slide-30
SLIDE 30

30

Co Code: Wi WiFi Se Setup

  • Connect to WiFi with

appropriate SSID and PASS

  • Again, use the on-board red

LED for WiFi connection indicator

import time import network from machine import Pin led_wifi = Pin(2, Pin.OUT) led_wifi.value(1) # turn it off wlan = network.WLAN(network.STA_IF) wlan.active(True) print("*** Connecting to WiFi...") wlan.connect("SSID","PASS") while not wlan.isconnected(): time.sleep(0.5) print("*** Wifi connected") led_wifi.value(0)

slide-31
SLIDE 31

31

Co Code: MQ MQTT T and Subsc scri ription

  • Replace UNIQUE-ID and BROKER

with appropriate values

  • Call mqtt.set_callback()

method to set the callback function before making any subscription

  • Call mqtt.check_msg()

regularly to check for incoming messages

  • sub_callback() is called

automatically with byte arrays in topic and payload arguments

from umqtt.robust import MQTTClient led_iot = Pin(12, Pin.OUT) led_iot.value(1) # turn it off def sub_callback(topic,payload): if topic == b"ku/cpe/room204/lamp": try: lamp.value(1-int(payload)) except ValueError: pass mqtt = MQTTClient("UNIQUE-ID","BROKER") print("*** Connecting to MQTT broker...") mqtt.connect() print("*** MQTT broker connected") led_iot.value(0) mqtt.set_callback(sub_callback) mqtt.subscribe(b"ku/cpe/room204/lamp") while True: mqtt.check_msg()

slide-32
SLIDE 32

32

Ex Exer ercise 4. ise 4.1: 1: D Dimmable Io immable IoT Lamp Lamp

  • Modify the last example so that user can control the

brightness of the USB-powered lamp by publishing a value between 0-100 to ku/daq2020/<nickname>/lamp

  • Payload is 0 → Lamp is completely off
  • Payload is 100 → Lamp is completely on
  • Payload is any value between 0 to 100 → Lamp’s brightness is

adjusted accordingly (the greater, the brighter)

  • Other payloads should be properly ignored without crashing the

program

slide-33
SLIDE 33

33

Furt Further R her Rea eadi ding ngs

  • HiveMQ’s MQTT Essentials
  • https://www.hivemq.com/mqtt-essentials/
  • MQTT standard v3.1.1
  • http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-
  • s.pdf
  • MQTT standard v5.0
  • https://docs.oasis-open.org/mqtt/mqtt/v5.0/mqtt-v5.0.pdf
  • umqtt’s source code and API reference
  • https://github.com/micropython/micropython-

lib/tree/master/umqtt.simple

slide-34
SLIDE 34

34

Co Conclusi sion

  • IoT devices are typically resource-constrained and
  • perating under unreliable Internet connections
  • Data-centric communication where devices are decoupled

in both time and space is desirable for IoT

  • MQTT is a light-weight and disconnection-tolerant protocol

that allows devices to interact with pub/sub pattern