+ All Categories
Home > Documents > Remote Data Acquisition and IoT

Remote Data Acquisition and IoT

Date post: 18-Dec-2021
Category:
Upload: others
View: 3 times
Download: 0 times
Share this document with a friend
35
Remote Data Acquisition and IoT Chaiporn Jaikaeo Department of Computer Engineering Kasetsart University 01219335 Data Acquisition and Integration Revised 2021-09-08 Materials partly taken from lecture slides by Karl and Willig
Transcript
Page 1: Remote Data Acquisition and IoT

Remote Data Acquisition and IoT

Chaiporn Jaikaeo

Department of Computer EngineeringKasetsart University

01219335 Data Acquisition and Integration

Revised 2021-09-08Materials partly taken from lecture slides by Karl and Willig

Page 2: Remote Data Acquisition and IoT

2

Outline•Remote data acquisition•Communication pattern in IoT•MQTT protocol

•Programming examples

Page 3: Remote Data Acquisition and IoT

3

IoT Reference Model

https://insightaas.com/new-iot-reference-model-creates-common-tongue/

Page 4: Remote Data Acquisition and IoT

4

sensor field

Remote Data Acquisition• Sensor nodes communicate to a gateway via radio links

radio linksRemote

monitoring

Network

gateway

Page 5: Remote Data Acquisition and IoT

5

Data-Centric Networking• 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

Page 6: Remote Data Acquisition and IoT

6

Content-Centric Networking Model

Internet

Data

InterestInterest

Data

Topic + Data

Page 7: Remote Data Acquisition and IoT

7

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

Page 8: Remote Data Acquisition and IoT

8

MQTT 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

Page 9: Remote Data Acquisition and IoT

9

MQTT Operation

MQTT BrokerTopic: office/room2/tempPayload: 26

publish

Topic: office/room2/temp

subscribe

Topic: office/room2/temp

Payload:26

subscribe

Topic: o

ffice/+/

temp

Topic: o

ffice/ro

om2/temp

Payload:

26

MQTT clients

Page 10: Remote Data Acquisition and IoT

10

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

Page 11: Remote Data Acquisition and IoT

11

MQTT vs. HTTP•HTTP

•MQTT

request

response

broker

Icon made by Smashicons from www.flaticon.com

subscribe

publishpublishsubscribe

publishsubscribe

publish

Page 13: Remote Data Acquisition and IoT

13

MQTT Client Libraries• 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

Page 14: Remote Data Acquisition and IoT

14

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

Page 15: Remote Data Acquisition and IoT

15

MQTT 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/temperaturetopic level topic level topic level topic level

topic level separator

Page 16: Remote Data Acquisition and IoT

16

Subscription Wildcards•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/#

Page 17: Remote Data Acquisition and IoT

17

MQTT: Stack

MQTT

SSL (optional)

TCP

IP

Application

default port: 1883 default port:

8883

Page 18: Remote Data Acquisition and IoT

18

MQTT Connection and Keep AliveMQTTClient

MQTTBroker

CONNECTCONNACK

SUBSCRIBESUBACK

PUBLISH

PUBLISH

PINGREQPINGRESP

Connectionestablishment

Regularactivities

Keep alive

DISCONNECT⋮

Disconnection

Page 19: Remote Data Acquisition and IoT

19

Quality of Service (QoS) Levels•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/

Page 20: Remote Data Acquisition and IoT

20

Last Will & Testament (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

Page 21: Remote Data Acquisition and IoT

21

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

Page 22: Remote Data Acquisition and IoT

22

Scenario 1 – Publishing Data

Internet

MQTT Broker

periodically publishlight status

subscribe for light status

Light sensor

Page 23: Remote Data Acquisition and IoT

23

Device Operation•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

Page 24: Remote Data Acquisition and IoT

24

Code: I/O Setup•Create an ADC object associated with Pin 36

from machine import Pin, ADC

ldr = ADC(Pin(36))

Page 25: Remote Data Acquisition and IoT

25

Code: WiFi 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 timeimport networkfrom 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)

Page 26: Remote Data Acquisition and IoT

26

Code: MQTT Broker 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)

Page 27: Remote Data Acquisition and IoT

27

Code: Publishing 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)

Page 28: Remote Data Acquisition and IoT

28

Scenario 2 – Subscribing

Internet

MQTT Broker

- subscribe for lamp control- set lamp status

publish LED value

Connect USB lamp here

Page 29: Remote Data Acquisition and IoT

29

Device Operation•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

Page 30: Remote Data Acquisition and IoT

30

Code: I/O 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

Page 31: Remote Data Acquisition and IoT

31

Code: WiFi Setup•Connect to WiFi with

appropriate SSID and PASS•Again, use the on-board red

LED for WiFi connection indicator

import timeimport networkfrom 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)

Page 32: Remote Data Acquisition and IoT

32

Code: MQTT and Subscription• 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()

Page 33: Remote Data Acquisition and IoT

33

Conclusion• IoT devices are typically resource-constrained and

operating 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

Page 34: Remote Data Acquisition and IoT

34

Exercise 4.1: Dimmable IoT Lamp•Modify the last example so that user can control the

brightness of the USB lamp by publishing a value between 0-100 to ku/daq2021/<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

Page 35: Remote Data Acquisition and IoT

35

Further Readings•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-

os.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


Recommended