Python: How It Helps Run IoT Devices

0
468

Python’s strength lies in its English-like syntax, the nimble MicroPython, and an extensive library of modules. It’s a great tool for prototyping, developing, and operating various IoT devices.

IoT, or the Internet of Things, is a technology we encounter daily. It encompasses various applications, including automatic lights controls based on presence, fan speed and AC temperature adjustments according to outdoor climate conditions, self-driving vehicles, surveillance and security systems, smart electricity meters that send monthly bills directly to your email, production lines, real-time health monitoring, and more.

IoT is defined as a network of electronic devices equipped with sensors, actuators, or accessories that possess processing capabilities through software. These devices can connect, control and exchange data with other systems over the internet or other communication networks in real-time, enhancing decision making processes.

Why Python

Python is widely used in IoT due to its simplicity and versatility. Here are some key reasons for its popularity.

  • Easy to learn and use: Python’s syntax is simple, like the English language. Also, there is a lighter version of Python called MicroPython making it a great fit for small computing resources. MicroPython is a lean and efficient Python 3 programming language implementation that includes a small part of the Python standard library, and is optimised to work on microcontrollers and in limited contexts.
  • Cross-platform compatibility: Python can run on multiple operating systems such as Linux, Windows, MacOS, or Raspberry Pi ensuring compatibility. In most cases, Python is preinstalled.
  • Existing large community: Python boasts a substantial user community actively contributing to the development of tools and support for IoT advancements.
  • Interoperability: Python seamlessly integrates with other languages and protocols like MQTT, HTTP, BLE, and more.
  • Scalability: Python’s modular architecture suits both large and small-scale IoT applications.
  • Extensive library support: Python offers a vast array of tools for machine learning, deep learning, data intensive applications, data analysis, data inference, visualisation, and more. Its huge set of development libraries, tools and frameworks makes development faster and compatible with IoT.
  • Open source: Python is an open source framework, readily available for download at no cost.

Using Python for IoT

Python offers various modules for IoT programming, facilitating development at various stages. Let us explore around 30 modules that assist in IoT development.

  • IoT devices

Arduino and Raspberry Pi are the two most popular controller boards for hardware projects.

  • Raspberry Pi
    • MicroPython is a full Python 3 implementation that works directly on embedded hardware such as the Raspberry Pi Pico.
    • It provides an interactive prompt (the REPL) and a built-in file system, as well as the ability to run commands directly through USB serial.
    • MicroPython’s Pico port offers modules for interacting with low-level chip-specific hardware.
    • Detailed documentation can be found at https://www.raspberrypi.com/documentation/microcontrollers/micropython.html
  • Arduino
    • Arduino is an open source platform comprising hardware and software, allowing quick production of interactive electronics projects.
    • Arduino employs its own programming language, akin to C++; however, for applications that require integration with sensors and other physical devices, Arduino can be used with Python.
    • The Firmata protocol is the standard method to control Arduino from PC. It enables serial communication with microcontrollers from software on a host computer, allowing it to receive digital and analogue inputs and transmit data to digital and analogue outputs.
    • To get started with Arduino and Python, visit https://realpython.com/arduino-python/

IoT system and sensors interaction

  • PySensors: Use ‘pip install PySensors’ to install it.
      • lm-sensors (Linux monitoring sensors) is a free and open source application that provides temperature, voltage, and fan monitoring tools and drivers.
      • The PySensors Python module is used for ctypes bindings for libsensors.so from the lm-sensors project.
      • Reference: https://pypi.org/project/PySensors/
    • esptool: Install using ‘pip install esptool’.
  • esptool is a Python-based, open source, platform-independent software for communicating with the Espressif SoC’s ROM bootloader.
  • The toolbox for working with Espressif chips includes esptool.py, espefuse.py, and espsecure.py.
  • They can, for example, accomplish these tasks:
      • Binary data saved on flash can be read, written, erased, and verified.
      • Can read chip characteristics and other data relating to the chip, such as the MAC address or flash chip ID.
      • The one-time-programmable efuses can be read and written.
      • Make binary executable images that are ready for flashing.
      • Binary images can be analysed, assembled, and merged.
    • Reference: https://pypi.org/project/esptool/
  • pyusb: Install with ‘pip install pyusb’.
    • pyusb simplifies communication with USB devices in Python.
    • It should function without any additional code in any Python >= 3.6 environment with ctypes and a pre-built USB backend library.
    • Reference: https://pypi.org/project/pyusb/
  • pyserial: Use ‘pip install pyserial’ to install it.
    • pyserial is a Python library responsible for serial port access.
    • It provides the same class-based interface for Python running on Windows, OSX, Linux, BSD (potentially any POSIX-compliant system), and IronPython on all supported platforms.
    • This allows it to communicate serially with devices like voltmeters, oscilloscopes, strain gauges, flow meters, actuators, and lights.
    • Reference: https://pypi.org/project/pyserial/
  • pybluez: Install with ‘pip install pybluez2’.
    • This module is responsible for serial port access.
    • It provides the same class-based interface for Python running on Windows, OSX, Linux, BSD (potentially any POSIX-compliant system), and IronPython on all supported platforms.
    • Reference: https://pypi.org/project/pybluez2/
  • gpiozero: Install via ‘pip install gpiozero’.
    • gpiozero provides a straightforward interface to GPIO devices on the Raspberry Pi.
    • Component interfaces are provided to make getting started with physical computing as simple as possible.
    • One may rapidly connect components together with very little code.
    • Reference: https://pypi.org/project/gpiozero/
  • mraa
    • mraa is a Python-compatible structural GPIO library.
    • Eclipse Mraa (Libmraa) is a C/C++ library with Java, Python, and JavaScript bindings for interacting with I/O pins and buses on various IoT and edge systems, providing an organised and reasonable API, where port names/numbering match the board that you are on.
    • The use of Libmraa does not bind you to any particular hardware. Since board detection is done at runtime, you can write portable code that works on all supported platforms. The most appealing aspect of this is that there is only one library for each individual device.
    • The goal is to make it easy for developers and sensor manufacturers to map their sensors and actuators on top of compatible hardware, and to allow high level languages and structures to govern low level communication protocols.
    • Reference: https://github.com/eclipse/mraa/tree/master/examples/python
  • IoT communication protocols
    • sockets: Install using ‘pip install sockets’.
      • TCP/IP and UDP are communication transport layer protocols.
      • sockets package facilitates TCP/IP and UDP communication, which are used to enable networking in IoT devices.
      • Reference: https://pypi.org/project/sockets/
  • paho-mqtt: Install using ‘pip install paho-mqtt’.
    • The MQTT or Message Queue Telemetry Transport protocol is a publish/subscribe messaging transport that is intended for machine-to-machine (M2M)/Internet of Things connectivity, enabling high speed data exchange with low payload.
    • When a minimal code footprint is necessary or bandwidth on the network is at a premium, it is beneficial for communications with faraway locations.
    • Applications can connect to a MQTT broker to publish messages, subscribe to topics, and receive published messages thanks to the client class provided by the Poho library code.
    • Additionally, it offers certain assistance functions to make it simple to publish one-time messages to MQTT servers.
    • Reference: https://pypi.org/project/paho-mqtt/
  • smtplib
    • This protocol handles the sending and routing of emails between mail servers.
    • It is part of Python’s standard library.
    • Reference: https://docs.python.org/3/library/smtplib.html
  • asyncio: Install with ‘pip install asyncio’.
    • The asyncio module provides architecture for building single-threaded concurrent programming with co-routines, multiplexing I/O access through sockets and other resources, running network clients and servers, and other primitives.
    • Reference: https://pypi.org/project/asyncio/
  • IoT database
    • MySQLdb: Install with ‘pip install MySQL-python’.
      • MySQLdb is a popular go-to relational database that aids in the building of remote storage for IoT systems.
      • Reference: https://pypi.org/project/MySQL-python/
    • SQLite
      • SQLite is a C library that provides a lightweight disk-based database that does not require a separate server process and may be accessed using a non-standard variation of SQL.
      • SQLite can be used by IoT to store internal data.
      • The sqlite3 module does not require installation. Since Python 2.5, it has been included in the standard library.
      • Reference: https://docs.python.org/3/library/sqlite3.html
  • IoT graphical user interface
    • Tkinter
      • Python provides numerous GUI (graphical user interface) development choices. Tkinter is the most commonly used of all the GUI techniques.
      • It is a standard Python interface to the Python-supplied Tk GUI toolkit.
      • Python with Tkinter is the quickest and most straightforward approach to construct GUI apps.
      • Reference: https://docs.python.org/3/library/tkinter.html
  • IoT backend
    • Flask: Install via ‘pip install Flask’.
      • Flask is a simple micro framework for building web applications, to build http requests.
      • IoT applications require a web-based interface in order to provide monitoring and controlling devices.
      • Reference: https://pypi.org/project/Flask/
    • websockets: Install using ‘pip install websockets’.
      • websockets is a Python package for creating WebSocket servers and clients. It prioritises consistency, simplicity, resilience, and performance.
      • The WebSocket API is a cutting-edge technology that allows a two-way interactive communication session to be established between a user’s browser and a server. This API sends messages to a server and receives event-driven responses without polling the server for a response.
      • Reference: https://pypi.org/project/websockets/
  • requests: Install with ‘pip install requests’.
    • The requests module allows sending HTTP requests; the HTTP request generates a ‘response object’ containing response data (content, encoding, status, and so on).
    • Supports multipart file uploads, streaming downloads, and connection pooling.
    • Enables automatic decoding and decompression of content.
    • Reference: https://pypi.org/project/requests/
  • aiohttp: Install with ‘pip install aiohttp’.
    • aiohttp provides an asynchronous HTTP client or server framework, including websockets support.
    • Reference: https://pypi.org/project/aiohttp/
  • pushsafer: Install with ‘pip install python-pushsafer’.
    • pushsafer can send and receive push notifications in real-time to iOS, Android, and Windows devices (mobile and desktop), as well as browsers such as Chrome, Firefox, Opera, and others.
    • Reference: https://pypi.org/project/python-pushsafer/
  • IoT data analysis and visualisation
    • Numpy: Install via ‘pip install numpy’.
      • Numpy package helps to power N-dimensional array computing.
      • It’s often used in IoT to read sensor bulk data from the system’s database inherent functions.
      • Reference: https://pypi.org/project/numpy/
  • Pandas: Install with ‘pip install pandas’.
    • Pandas is a Python module that provides quick, versatile, and expressive data structures that are intended to make working with ‘relational’ or ‘labelled’ data simple and natural.
    • It aspires to be the basic high-level building block for doing realistic, real-world data analysis in Python.
    • Reference: https://pypi.org/project/pandas/
  • Matplotlib: Install using ‘pip install matplotlib’.
    • Matplotlib is a comprehensive library for creating static, animated, and interactive visualisations in Python.
    • Reference: https://pypi.org/project/matplotlib/
  • JSON
    • JSON is a data storage and exchange syntax.
    • It has been written in JavaScript object notation.
    • Python includes a library named json that may be used to work with JSON data.
    • Reference: https://docs.python.org/3/library/json.html
  • IoT machine learning and deep learning (AI)
    • Tensorflow: Install with ‘pip install tensorflow’.
      • TensorFlow is a high-performance numerical computing open source software package.
      • Its adaptable architecture enables simple computing deployment over a wide range of platforms (CPUs, GPUs, TPUs), from PCs and clusters of servers to mobile and edge devices for handling non-linear data sets.
      • Reference: https://pypi.org/project/tensorflow/
  • Opencv: Install via ‘pip install opencv-python’.
    • OpenCV is a large open source library for computer vision, machine learning, and image processing, and it currently plays an essential part in real-time operations.
    • Reference: https://pypi.org/project/opencv-python/
  • Pycaret: Install using ‘pip install pycaret’.
    • PyCaret is an open source, low-code machine learning library in Python.
    • When compared to other open source machine learning libraries, PyCaret is a low-code library that may be used to replace hundreds of lines of code with only a few lines. Experiments become significantly faster and more efficient as a result, making it apt to use for IoT apps.
    • Reference: https://pypi.org/project/pycaret/
  • LightGBM: Install using ‘pip install lightgbm’.
    • LightGBM is an abbreviation for Light Gradient Boosting Machine. It provides plenty of memory-efficient yet quick computational power and is capable of handling massive volumes of data.
    • Reference: https://pypi.org/project/lightgbm/
  • IoT cloud integration
    • Azure-CLI: Install using ‘pip install azure-cli’.
      • Azure-CLI provides Python tools for Azure.
      • Reference: https://pypi.org/project/azure-cli/
    • AWS:
      • Client devices can interface with AWS IoT and AWS IoT Greengrass core devices using the Python programming language by using the AWS IoT Device SDK for Python.
      • Reference: https://docs.aws.amazon.com/greengrass/v1/developerguide/IoT-SDK.html

Challenges of using Python for IoT

Python has many advantages, but no programming language is flawless.

  • Python is slower than other programming languages because it is dynamically typed and run line by line.
  • Python’s key limitations are its runtime speed, memory management, and concurrency and parallelism support. When compared to other languages, the runtime speed is poor. Due to the amount of memory Python needs, projects with many objects active in RAM may encounter challenges when using Python.
  • While Python is a good server-side language, it is rarely used on the client-side as working with mobile applications and apps is slow and inconvenient.

IoT is evolving, and the choice of tools to meet its demands is crucial. Several programming languages have proven effective in IoT development. Python has shown itself to be a valuable tool for prototyping, developing, and operating various IoT devices and systems. Its rapid development speed, minimal learning curve, and extensive library set make it indispensable for IoT.

LEAVE A REPLY

Please enter your comment!
Please enter your name here