re remote data acquisition and iot
play

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


  1. 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 Willig Revised 2020-09-02

  2. Ou Outline • Remote data acquisition • Communication pattern in IoT • MQTT protocol • Programming examples 2

  3. Re Remote Data Acquisition • Sensor nodes communicate to a gateway via radio links Network sensor field gateway Remote monitoring radio links 3

  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 4

  5. Data-Ce Da Centri ric Networki rking Mo Model Internet Interest Interest D a a t t a a D 5

  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 Publisher 1 Publisher 2 Software bus Subscriber 1 Subscriber 2 Subscriber 3 6

  7. MQ MQTT T Protocol • M essage Q ueue T elemetry T ransport • 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 7

  8. MQTT MQ T Operation p u b l i s h T o p i c : o MQTT clients f f i c P a e / y r l o MQTT Broker o o a d m 2 : / 2 t e 6 m p e b r i c b s u s Topic: office/room2/temp Topic: office/room2/temp Payload:26 Topic: office/room2/temp p m e b e t i r / c + s / b e u c s i f f o Payload: 26 : c i p o T 8

  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 9

  10. MQ MQTT T vs. s. HTTP TP request • HTTP response • MQTT subscribe publish publish subscribe publish subscribe p u b l i s h broker Icon made by Smashicons from www.flaticon.com 10

  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 11

  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 12

  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 13

  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., ku/cpe/room204/temperature topic level topic level topic level topic level • Topics are case-sensitive ◦ ku/cpe is not the same as Ku/Cpe 14

  15. Subscrip Su iption ion W Wild ildcar ards • Wildcards may be used for subscription • Single-level wildcard (+) ku/cpe/+/temperature ◦ Replaces only one topic • Multi-level wildcard (#) ku/cpe/# ◦ 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 15

  16. MQ MQTT: Stack Application MQTT default port: 1883 default port: SSL (optional) 8883 TCP IP 16

  17. MQ MQTT T Co Connection and Keep Alive MQTT MQTT Client Broker CONNECT Connection CONNACK establishment ⋮ SUBSCRIBE SUBACK ⋮ Regular PUBLISH activities ⋮ PUBLISH ⋮ PINGREQ PINGRESP Keep alive ⋮ DISCONNECT Disconnection 17

  18. Qualit Quality o of 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/ 18

  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 19

  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 20

  21. Sc Scenar ario 1 io 1 – Pu Publishing Data Internet subscribe for light status periodically publish light status Light sensor MQTT Broker 21

  22. De Devi vice Op Operation on • 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 22

  23. Co Code: I/O Setup • Create an ADC object associated with Pin 36 from machine import Pin, ADC ldr = ADC(Pin(36)) 23

  24. Co Code: Wi WiFi Co Connection • Create WLAN connection with import time import network WLAN class in the network module from machine import Pin • Replace SSID and PASS with led_wifi = Pin(2, Pin.OUT) led_wifi.value(1) # turn it off appropriate values wlan = network.WLAN(network.STA_IF) • wlan.connect() method is not wlan.active(True) print("*** Connecting to WiFi...") blocking, so the code keeps wlan.connect("SSID","PASS") checking wlan.isconnected() while not wlan.isconnected(): time.sleep(0.5) to ensure connectivity before print("*** Wifi connected") further proceeding led_wifi.value(0) • Use on-board red LED (IO2) for WiFi connection indicator 24

  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) 25

  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) 26

  27. Sc Scenar ario 2 io 2 – Su Subscrib ibin ing Internet publish LED value - subscribe for lamp control - set lamp status Connect USB MQTT Broker lamp here 27

  28. De Devi vice Op Operation on • 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 28

  29. Co Code: I/O O Se Setup • USB-switch output is connected from machine import Pin to IO25 lamp = Pin(25, Pin.OUT) ◦ Logic 1 à Power off lamp.value(1) # turn it off ◦ Logic 0 à Power on 29

  30. Co Code: Wi WiFi Se Setup • Connect to WiFi with import time import network appropriate SSID and PASS from machine import Pin led_wifi = Pin(2, Pin.OUT) • Again, use the on-board red led_wifi.value(1) # turn it off LED for WiFi connection wlan = network.WLAN(network.STA_IF) indicator 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) 30

  31. Code: MQ Co MQTT T and Subsc scri ription from umqtt.robust import MQTTClient • Replace UNIQUE-ID and BROKER with appropriate values led_iot = Pin(12, Pin.OUT) led_iot.value(1) # turn it off • Call mqtt.set_callback() def sub_callback(topic,payload): method to set the callback if topic == b"ku/cpe/room204/lamp": try: function before making any lamp.value(1-int(payload)) subscription except ValueError: pass • Call mqtt.check_msg() mqtt = MQTTClient("UNIQUE-ID","BROKER") regularly to check for incoming print("*** Connecting to MQTT broker...") mqtt.connect() messages print("*** MQTT broker connected") led_iot.value(0) • sub_callback() is called mqtt.set_callback(sub_callback) automatically with byte arrays in mqtt.subscribe(b"ku/cpe/room204/lamp") topic and payload arguments while True: mqtt.check_msg() 31

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend