Slide 1

Slide 1 text

Creating IoT Applications Using Python

Slide 2

Slide 2 text

Intro CEO @ AgileVision Software Engineer Studied at CNTU some time ago ;)

Slide 3

Slide 3 text

Why Python? • Easy to learn • Easy to use • Lots of useful libraries • Go-to option for the ML/BigData • Loved by engineers and scientists (proven by Stackoverflow and TIOBE index)

Slide 4

Slide 4 text

IoT Basics

Slide 5

Slide 5 text

What is IoT? • A network of interconnected devices(things) • Each thing has its own compute module running a software and can be connected with multiple sensors or other devices • Things can be controlled or monitored remotely, not necessarily from the same physical network or location • Approach when the data is being preprocessed by a thing and then sent to the cloud/hub/control software is called edge computing.

Slide 6

Slide 6 text

Typical IoT Setup Temperature Sensor Humidity Sensor Pump Controller Thing with three sensors, compute module and LTE/Wi-Fi module IoT Hub/Broker Client

Slide 7

Slide 7 text

MQTT Protocol • Pub/sub based • Designed to be energy-efficient • Authentication and authorisation are required by the standard, but are implementation-specific • Transport level security is recommended by the standard. TLS is suggested

Slide 8

Slide 8 text

TLS • Communication security protocol • Assumes there is a third-party authority which can be trusted to issue certificates (CA, certificate authority) • Can be used as a secure transport protocol for the MQTT

Slide 9

Slide 9 text

IoT Broker • Server software used to connect MQTT clients • Provides authentication, authorization and secure encryption • Provides QoS and other features

Slide 10

Slide 10 text

Meet ESP32 The queen of low-power devices

Slide 11

Slide 11 text

ESP32 at a glance • Ultra Low-Power • Wi-Fi and Bluetooth Chip • Many supported interfaces and integrations • A ton of developer kits and tutorials!

Slide 12

Slide 12 text

It’s coding time! Let’s get our hands dirty!

Slide 13

Slide 13 text

We’ll need • Mosquitto MQTT broker • Python 3.7 • paho-mqtt client • ESP32-based devkit, we are using M5Stack

Slide 14

Slide 14 text

Getting Mosquitto 1. Download a distribution from https://mosquitto.org/ 2. Launch as a service or as an application

Slide 15

Slide 15 text

Checking everything up and running In one terminal window, execute the following command: mosquitto_sub -h localhost -t "testme" -v In another one, execute the following command mosquitto_pub -h localhost -t "testme" -m Hello This will send a “Hello” message to the “testme” topic. If everything was configured correctly, the window running the mosquitto_sub command will show the “Hello” message.

Slide 16

Slide 16 text

Setting up M5Stack • Download Arduino IDE • Download and install UART driver • Check the connection

Slide 17

Slide 17 text

Establishing Wi-Fi and MQTT connection void setup() { M5.begin(); WiFi.begin(ssid, pass); waitForWifi(); if (!mqttClient.connect( broker, port)) { // Infinite loop on failure while (1); } mqttClient.subscribe(topic); }

Slide 18

Slide 18 text

Receiving messages if (messageSize) { M5.…setTextColor(0x51D); M5.…print( “Received a message with topic ‘"); M5.…print( mqttClient.messageTopic()); M5.…print("', length "); M5.…print(messageSize); M5.…print(" bytes:\n”); while( mqttClient.available()) { M5.…print( (char)mqttClient.read()); } }

Slide 19

Slide 19 text

We’ve just seen some really awesome stuff! •Easy Wi-Fi configuration •MQTT connection using several lines of code •Device is not running any OS, by the way!

Slide 20

Slide 20 text

Reading sensor data and sending it via MQTT float temperature = TH02.ReadTemperature(); mqttClient.beginMessage( tempTopic ); mqttClient.print( "Temperature is: “ ); mqttClient.print(temperature); mqttClient.endMessage();

Slide 21

Slide 21 text

More awesomeness! •Temperature sensor data readings •Publishing to another MQTT topic

Slide 22

Slide 22 text

How it works ESP32 mosquitto_sub

Slide 23

Slide 23 text

It’s Python time! We could use any other language, BTW

Slide 24

Slide 24 text

Subscribing to a topic import paho.mqtt.client as mqtt from matplotlib import pyplot from matplotlib.animation import FuncAnimation client.on_message = on_message client.connect("test.mosquitto.org") client.subscribe("agilevision.io/ demo/temp0") def update(frame): global temperature client.loop() client.publish("agilevision.io/ demo", “GET"); x.append(datetime.now()) y.append(temperature) line.set_data(x, y) figure.gca().relim() figure.gca().autoscale_view() return line,

Slide 25

Slide 25 text

Summary • IoT device and application are very broad terms. It can be whatever • MQTT is a really simple protocol • Arduino IDE can be a great tool for extremely fast prototyping, even for non-Arduino devices • Pick a right tool, not the “only true” tool for the task. No need to reinvent the wheel, especially for prototyping

Slide 26

Slide 26 text

Questions?