The Internet of Things (IoT) trend is about connecting almost every possible object to the Internet so that it's data can be uploaded, stored, accessed and analyzed. With the IoT, devices are not only sending data to the cloud, but also listening for commands from anyone or anything connected to the Internet. To make this vision possible, sensors and computers are going into types of products that didn't have them before. Now you can flip a light switch in your house by sending 1's and 0's from your office downtown or anywhere else in the world. IoT naturally lends itself to home automation because those 1's and 0's can be sent either by you via a mobile app or by a computer program.
Python at Home
That program can be written in Python and be run on your home computer. This is the approach taken by Home Assistant, which is Free and Open Source home automation software implemented as a Python 3 module. Home Assistant discovers your devices, monitors their state, and controls them based on rules written in text files. The Home Assistant GitHub page offer several already written examples for basic tasks, such as turning on a living room light on a rainy afternoon. Home Assistant is compatible with lots of different protocols and products from many vendors and is meant to be run on a computer connected to the local network, where all the data is stored. This way, the user has full control and possession of the sensor and location data, instead of a cloud service provider. This software also features web-based configuration. Enterprising users can even extend Home Assistant's power by writing additional components in Python.
Python on the Device
IoT devices work their magic because with tiny processors, called microcontrollers, which are built into these products. Microcontrollers are small, simple, low-power and low-performance computers which are made specifically to control other things. The software for almost all MCU's is written in C, C++ or assembly because programs written in these low-level languages can more efficiently manage the limited RAM and CPU power of microcontrollers.
But could microcontrollers run sofware which is written in Python instead? Â
MicroPython brings Python programming to microcontrollers, long the exclusive turf of those lower-level languages. The MicroPython approach is to bring Python's ease of development to microcontrollers by implementing a Python interpreter which is optimized for microcontrollers. Python programs are uploaded to the device by editing the main.py file on target device's flash storage.
MicroPython's development, then is a dramatic departure from traditional embedded (firmware) development done in C/C++, where programs are first written, debugged and compiled on a separate computer before being downloaded to the microcontroller via an interface like JTAG. MicroPython, by contrast, uses the ubiquitous USB interface from programming and debugging.
There are however, some similarities between traditional microcontroller development and MicroPython. As the project's own documentation says, utilizing pointers and paying careful attention to data types are key to efficiently using the limited RAM and processing power available on the microcontroller. Also, just as C/C++ microcontroller code is usually only compatible with the type of processor it was originally written for, MicroPython's compatibility is best on the project's own Pyboard, although MicroPython can run on several other devices.
Cloud is Where the Things and Apps Meet
As we discussed earlier, the standard IoT model is that the devices connect to an application running on the Web and then apps in turn connect to this account app in the cloud. These cloud-based IoT services then are the bridge between the app and the devices. The more feature-packed the cloud service, the more useful the IoT devices become.
Just as with any other emerging trend in technology, some familiar names are hoping to ride this IoT wave because of the opportunity to sell customers on their cloud products. One of the main selling points for these commercial IoT cloud offerings is Python support. IBM, for example has a GitHub page with example Python code which you can use with their IoT platform. You can perform advanced analytics and even write your code with IBM's cloud app.
IBM isn't the only large technology company utilizing Python in their IoT software: Microsoft offers a version of Windows (Windows 10 IoT core) made for low-power platforms like embedded IoT devices. It even runs on the Raspberry Pi! They too have a GitHub page with example Python code which you can deploy to the Windows IoT device and run it. You can even download the IoT version of Windows and an entire development toolchain at no cost. Of course, Microsoft enables easy integration with their Azure cloud platform.
Whether on your home PC, on a commercial cloud platform, on even on the connected device, Python is right at center of the Internet of Things.
Copyright © Python People