Connecting Things

Created by Miroslav Biňas / mirek

[![Namakaný deň 2020](images/namakany.den.2020.png)](http://namakanyden.sk/)
[![Hack Košice 2020](images/hack.kosice.2020.png)](https://hackkosice.com/)
## M2M
![ESP32](images/esp32.png)
![ESP32 Block Diagram](images/esp32.block.diagram.svg) * notes: * https://en.wikipedia.org/wiki/ESP32
## WiFi Support * _802.11 b/g/n_ * IEEE 802.11 standard security features all supported, including WFA, WPA/WPA2 and WAPI * can operate as an _Access Point_ notes: * https://en.wikipedia.org/wiki/ESP32
## The `network` Module
[![IFTTT](images/ifttt.png)](https://ifttt.com)
![IFTTT Recipe](images/ifttt-recipe.png)]
## What is MQTT? * [MQTT](https://en.wikipedia.org/wiki/MQTT) (Message Queue Telemetry Transport) * lightweight publish-subscribe protocol that is used on top of TCP/IP * designed to _M2M_ communications * uses _publisher/subscriber pattern_ and _MQ model_ * becoming one of the main protocols for _IoT_ Notes: * https://pagefault.blog/2017/03/02/using-local-mqtt-broker-for-cloud-and-interprocess-communication/ * https://www.youtube.com/watch?v=eS4nx6tLSLs * http://www.steves-internet-guide.com/mqtt/
![Publish-subscribe Architecture](images/publish-subscribe.architecture.png) Notes: * https://blog.securitycompass.com/publish-subscribe-threat-modeling-11add54f1d07
## The Basics * broker * topics * publisher * subscriber * message

IoT Example

MQTT Overview
[![Mosquitto](images/logo-mosquitto.png)](https://mosquitto.org/)
## [Eclipse Mosquitto™](https://mosquitto.org/) * an open source lightweight MQTT broker * implements the MQTT protocol versions 3.1 and 3.1.1 * install with `sudo apt install mosquitto` `sudo apt install mosquitto-clients`
## `mosquitto_pub` * client for publishing simple messages * syntax: `mosquitto_pub -h host -t topic -m message` Notes: * man mosquitto_pub
## `mosquitto_sub` * client for subscribing to topics * syntax: `mosquitto_sub -h host -t topic [-F format]` * example: `mosquitto_sub -h localhost -t '#' -F "%t: %p"` Notes: * man mosquitto_sub
## Topics * string used by broker to filter messages * consist of one/more topic levels * topic level separator is '`/`' * topics don't need to be initialized first * cAsE sEnsItIvE * allows to use wildcards when subscribing: * '`#`' - multi level * '`+'` - single level Notes: * https://www.hivemq.com/blog/mqtt-essentials-part-5-mqtt-topics-best-practices
Slovensko/Košice/Letná 9/IoT Lab
home/ground/livingroom/temperature
## MQTT and ESP32 * package `umqtt`
## [IBM Watson IoT](https://quickstart.internetofthings.ibmcloud.com/#/)
## Get Ready for Watson * no direct support for _Watson_ * install appropriate package with `upip` * _MicroPython_ version of `pip` * packages for _MicroPython_ have prefix `micropython-` * install `micropython-watson-iot` package: ```python import upip upip.install('micropython-watson-iot') ```
## ESP32 with Watson ```python from watson_iot import Device device = Device(device_id='esp32-dht22', device_type='my-device-type', token='my-device-token') device.connect() device.publishEvent('temperature', {'degrees': 30.7, 'unit': 'C'} ) ```
## The Final Code ```python # imports ... sensor = DHT11(Pin(23)) device = Device(device_id='esp32-with-dht22', device_type='type', token='token') device.connect() while True: sensor.measure() temp = sensor.temperature() device.publishEvent('temperature', {'degrees': temp, 'unit': 'C'}) sleep(10) ```
## Questions?