Remote Data Acquisition and IoT
Transcript of 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
2
Outline•Remote data acquisition•Communication pattern in IoT•MQTT protocol
•Programming examples
3
IoT Reference Model
https://insightaas.com/new-iot-reference-model-creates-common-tongue/
4
sensor field
Remote Data Acquisition• Sensor nodes communicate to a gateway via radio links
radio linksRemote
monitoring
Network
gateway
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
6
Content-Centric Networking Model
Internet
Data
InterestInterest
Data
Topic + Data
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
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
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
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
11
MQTT vs. HTTP•HTTP
•MQTT
request
response
broker
Icon made by Smashicons from www.flaticon.com
subscribe
publishpublishsubscribe
publishsubscribe
publish
12
MQTT Client Tools•PC (Windows/macOS/Linux)
◦ Chrome app: MQTTLens◦ Command-line tool: Mosquitto
•Android◦ MyMQTT◦ IoT MQTT Panel
• iOS◦ MQTTool
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
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
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
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/#
17
MQTT: Stack
MQTT
SSL (optional)
TCP
IP
Application
default port: 1883 default port:
8883
18
MQTT Connection and Keep AliveMQTTClient
MQTTBroker
CONNECTCONNACK
SUBSCRIBESUBACK
PUBLISH
PUBLISH
PINGREQPINGRESP
⋮
⋮
⋮
⋮
Connectionestablishment
Regularactivities
Keep alive
DISCONNECT⋮
Disconnection
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/
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
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
22
Scenario 1 – Publishing Data
Internet
MQTT Broker
periodically publishlight status
subscribe for light status
Light sensor
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
24
Code: I/O Setup•Create an ADC object associated with Pin 36
from machine import Pin, ADC
ldr = ADC(Pin(36))
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)
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)
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)
28
Scenario 2 – Subscribing
Internet
MQTT Broker
- subscribe for lamp control- set lamp status
publish LED value
Connect USB lamp here
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
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
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)
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()
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
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
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