# 1dv527 ## Onsdag 210301 - [MicroPython](http://micropython.org/) - [MQTT](http://mqtt.org/) - Platforms, local and online - LPWAN, TTN, LoRa, SigFox - [TIG Stacks](https://hackmd.io/kf2VG07gSZOSXlS5c4ERGg) --- Inspiration, automating in the cloud - Grafana - Create Grafana alert to Discord bot - Node-RED - MQTT - Webhooks ### Node-RED Either locally with Docker https://nodered.org/docs/getting-started/docker Or, in the cloud (For free!) https://nodered.org/docs/getting-started/ibmcloud - Node-RED dashboard --- ### MicroPython - Official homepage: http://micropython.org/ - Documentation: http://docs.micropython.org/en/latest/ - PyCom docs: https://docs.pycom.io/ (MicroPython + Pycoms libs) - Adafruit has CircuitPython (a fork of MicroPython) - Firmware to the PyCom device. - Legacy, not containing Pybytes library. Older. - Flash with Pybytes, even not using Pybytes. - Contains `pybytes_config.json`. - Provisioning from Pybytes homepage. May interfere with you own code. Disable Pybytes on boot! ```=python import pycom import machine pycom.pybytes_on_boot(True) machine.reset() ``` #### Design - MicroPython is designed to work on small micro-controller platforms. - There are learning material out there. [MicroPython series by unexpected maker](https://youtu.be/5W3WvXAmDJc) - You must first write your code and load it onto the board. - Boards have a flash drive storage. You can copy your Python file(s) to this drive for execution at boot time. `boot.py` and `main.py` - The MicroPython console is called the run, evaluate, print loop, or **REPL**. Makes it easy to get started and to debug (remove errors). - **R**un, **E**valuate, **P**rint, **L**oop (REPL) ![REPL](https://i.imgur.com/vBZcjG5.png) REPL Interface in Atom.io #### Projects Atom/VSCode and PyMakr, USB, WiFi, - **Projects**, you need to work in projects. - Do not use PyBytes **for coding**! No debug. Inconsistent connection. - FW of Expansion board. If you have no problems uploading, it should be OK. ### MQTT - Show MQTT Explorer - JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. ```python c.publish(topic_pub,'{"office_sensor": {"co2":' + str(co2) + ',"voc":'+ str(voc) + ',"bmp P":' + str(bmp_P) + ',"bmp temp":' + str(bmp_T) + ',"dht temp":' + str(dht_T) + ',"dht RH":' + str(dht_RH) + '}}') ``` ```json {"office_sensor": {"co2": 412, "voc": 30, "bmp P": 1012, "bmp temp": 23, "dht temp": 24, "dht RH": 42}} ``` #### What is MQTT MQTT stands for **MQ Telemetry Transport**. It is a publish/subscribe, extremely simple and lightweight messaging protocol, designed for **constrained** devices and **low-bandwidth, high-latency or unreliable networks**. The design principles are to minimise network bandwidth and device resource requirements whilst also attempting to ensure reliability and some degree of assurance of delivery. These principles also turn out to make the protocol ideal of the emerging “machine-to-machine” (M2M) or “Internet of Things” world of connected devices, and for mobile applications where bandwidth and battery power are at a premium. #### MQTT security You can pass a user name and password with an MQTT packet in V3.1 of the protocol. Encryption across the network can be handled with **SSL**, independently of the MQTT protocol itself. Additional security can be added by an application encrypting data that it sends and receives, but this is not something built-in to the protocol, in order to keep it simple and lightweight. - + can be used as a wildcard for a single level of hierarchy. It could be used with the topic above to get information on all computers and hard drives as follows: `sensors/+/temperature/+` As another example, for a topic of "a/b/c/d", the following example subscriptions will match: ``` a/b/c/d +/b/c/d a/+/c/d a/+/+/d +/+/+/+ ``` '#' can be used as a wildcard for all remaining levels of hierarchy. This means that it must be the final character in a subscription. With a topic of "a/b/c/d", the following example subscriptions will match: ``` a/b/c/d # a/# a/b/# a/b/c/# +/b/c/# ``` #### Quality of Service MQTT defines three levels of Quality of Service (QoS). The QoS defines how hard the broker/client will try to ensure that a message is received. Messages may be sent at any QoS level, and clients may attempt to subscribe to topics at any QoS level. This means that the client chooses the maximum QoS it will receive. For example, if a message is published at QoS 2 and a client is subscribed with QoS 0, the message will be delivered to that client with QoS 0. If a second client is also subscribed to the same topic, but with QoS 2, then it will receive the same message but with QoS 2. For a second example, if a client is subscribed with QoS 2 and a message is published on QoS 0, the client will receive it on QoS 0. **Higher levels of QoS are more reliable, but involve higher latency and have higher bandwidth requirements.** **0:** The broker/client will deliver the message once, with no confirmation. **1:** The broker/client will deliver the message at least once, with confirmation required. **2:** The broker/client will deliver the message exactly once by using a four step handshake. [https://mosquitto.org/man/mqtt-7.html ](https://mosquitto.org/man/mqtt-7.html) - Please use [sjolab.lnu.se](sjolab.lnu.se) for testing. - List of [public MQTT brokers](https://mntolia.com/10-free-public-private-mqtt-brokers-for-testing-prototyping/). No need of starting one on your own computer. ### Webhook - User-defined callback over HTTP - https://requestbin.com/ - cURL or Postman (Chrome) - Because webhooks use HTTP, they can be integrated into web services without adding new infrastructure. - A POST request being the method that allows the transfer of information in the body of the request through HTTP. - Include a body to the request, it's usually a simple JSON object. - JSON, https://www.json.org/json-en.html ### Platforms - Online platforms. - You send you data directly from your device to the internet. - Local platform (which can be hosted in the cloud ...) - You send the data to a local platform. Either directly over LAN, or via internet. #### Local platforms - The TIG-stack. Telegraf, Influx and Grafana. #### Online services, Platforms https://hackmd.io/@lnu-iot/cloud-platforms - [Pybytes](https://pybytes.pycom.io/) - [Adafriut IO](https://io.adafruit.com/frahlg/dashboards/welcome-dashboard) - [Cayenne MyDevices](https://cayenne.mydevices.com) - [Blynk IoT](https://github.com/vshymanskyy/blynk-library-python) Libraries for MicroPython exist. - [Ubidots (STEM edition)](https://ubidots.com/stem/). Seems to have good support for Pycom. - [Thingsboard](https://thingsboard.io/), can be run locally with [Docker](https://thingsboard.io/docs/user-guide/install/docker/). - [Thingsspeak](https://thingsspeak.com), Matlabs cloud - [Thinger.io](hhttps://thinger.io/) - [Iottweet.com](https://iottweet.com/) Not tested, seems to be free. - [https://freeboard.io/](https://freeboard.io/) Freeboard. Used to be free, not anymore ... - [AWS IoT Things Graph](https://aws.amazon.com/iot-things-graph/) - [Azure IoT](https://azure.microsoft.com/en-us/overview/iot/) - [Google Cloud IoT](https://cloud.google.com/solutions/iot) ### Wireless ![](https://i.imgur.com/2j7wy6V.png) Credit: Marco Zennaro #### LoRa - 10 km range, ~ 10y battery life - Gateways, forward packages using TCP/IP - Build/buy gateway - register to TTN. - **TTN is a MQTT broker!** - OTAA Over-The-Air Activation and ABP (Activation By Personalisation) ![thethingsnetwork.org ](https://i.imgur.com/m4nwzd7.png) thethingsnetwork.org ![thethingsnetwork.org ](https://i.imgur.com/IZtPp89.jpg) thethingsnetwork.org ![ttnmapper.org](https://i.imgur.com/4a0idz0.png) ttnmapper.org ![https://www.frugalprototype.com/technologie-lora-reseau-lorawan/](https://i.imgur.com/MU8blaL.png) https://www.frugalprototype.com/technologie-lora-reseau-lorawan/ ![](https://i.imgur.com/9DF3lN6.png) LoRaWAN-protocol specifications, LPWAN-Technologies for IoT (2020) ![](https://i.imgur.com/KFU1vGJ.png) LoRaWAN-protocol specifications, LPWAN-Technologies for IoT (2020) ![](https://i.imgur.com/U65TV9l.png) :::info Info regarding Pycom devices: ```device_class``` sets the LoRaWAN device class. Can be either ```LoRa.CLASS_A``` or ```LoRa.CLASS_C``` ::: ![https://www.newieventures.com.au/blogtext/2018/2/26/lorawan-otaa-or-abp](https://i.imgur.com/sL6g9dR.png) https://www.newieventures.com.au/blogtext/2018/2/26/lorawan-otaa-or-abp ![](https://i.imgur.com/diZn3BJ.png) ![](https://i.imgur.com/dIUxxBc.png) #### SigFox - 0G network - Same frequencies as LoRa. Different, ultra-narrowband instead of LoRa which uses a broader spectrum. - Low throughput, limited 144/messages day. - Top-down approach. The company owns all of its technology. - Each message transmitted 3 times in 3 different frequencies.