Thursday, 22 June 2023

IoT with Python


What is IoT 

IoT - Internet of Things, we would encounter this technology on a daily basis - automatic AC or lights switch on and off based on presence, adjust the fan speed or AC temperature based on the climate outside, self-driving vehicle, surveillance and security, smart electricity meter produces monthly bills directly send to email, manufacture production lines, in agriculture while harvesting, real time monitoring of our health such calories, steps and so on where the tech is connected with internet. Kevin Ashton created the term "Internet of Things" in 1999.


IoT is defined as a network of electronic devices with sensors, actuators or accessories having processing ability using software, which can connect, control and exchange data with other systems over internet or other communication networks in real time making improved decision making.

Why Python

Python has wide usage in the field of IoT because of its simplicity and versatility. Let's highlight some of the key reasons here.
  • Easy to learn and use, as the syntax is simple like the English language. Also there is a lighter version of Python called MicroPython making 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, can run on multiple OS such as Linux, Windows, MacOS or Raspberry Pi without worrying about compatibility and mostly python is preinstalled.
  • Existing large community, has many users contributing to the python community building several tools and support for further advancements.
  • Interoperability, can be easily integrated with other languages and protocols such as MQTT, HTTP, BLE and so on.
  • Scalability, the modular architecture can be used for both large or small scale IoT applications.
  • Extensive library support, several tools available for machine learning, deep learning, data intensive application, data analysis, data inference, visualization and also the lighter version of them has been the key reason to use Python in IoT. Having a huge set of development libraries, tools and frameworks making development faster and compatible with IoT.
  • Opensource, zero cost involved as python is opensource framework that is easily available to download.

IoT with Python

Python has various modules that can be utilised for IoT programming. Let us look at 30 modules that aid in the development of IOT at various stages.
  • IoT devices
    • Arduino and Raspberry Pi are the two most popular controller boards available for use in among the various hardware projects.
    • Raspberry Pi
      • MicroPython is a full Python 3 implementation that works directly on embedded hardware such as the Raspberry Pi Pico. 
      • You get an interactive prompt (the REPL) and a built-in filesystem, 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.
      • Very good documentation with all the hand holding steps are provided here : https://www.raspberrypi.com/documentation/microcontrollers/micropython.html
    • Arduino
      • Arduino is an open-source platform made up of hardware and software that enables the 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. 

      • Firmata protocol is the most standard way to control Arduino from PC among the other various options. This protocol facilitates 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.

      • Real python has brilliant material to get started, visit this https://realpython.com/arduino-python/

  • IoT system and sensors interaction
    • PySensors : pip install PySensors
      • 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 : 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 the following:
        • Binary data saved on flash can be read, written, erased, and verified.
        • 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 : pip install pyusb
      • pyusb makes it simple to communicate 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 : pip install pyserial
      • pyserial is a Python library 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.

      • This allows it to communicate serially with devices like voltmeters, oscilloscopes, strain gauges, flow metres, actuators, and lights.

      • Reference: https://pypi.org/project/pyserial/

    • pybluez : pip install pybluez2
      • pybluez, is bluetooth Python extension module that allows Python developers to access Bluetooth resources on the system. 
      • PyBluez is compatible with GNU/Linux, macOS, and Windows.
      • Reference: https://pypi.org/project/pybluez2/
    • gpiozero: pip install gpiozero
      • gpio 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 your 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. Because board detection is done at runtime, you can write portable code that works on all supported platforms. This 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 : 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 : pip install paho-mqtt
      • The MQTT - 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
      • Protocol to handle sending and routing emails between mail servers.
      • It is part of python's standard library.
      • Reference: https://docs.python.org/3/library/smtplib.html
    • asyncio : pip install asyncio
      • The asyncio module provides architecture for building single-threaded concurrent programming with coroutines, multiplexing I/O access through sockets and other resources, running network clients and servers, and other primitives.
      • Using this will enhance the performance of the IoT applications.
      • Reference: https://pypi.org/project/asyncio/
  • IoT database
    • MySQLdb : 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 nonstandard variation of the SQL query language. 
      • SQLite can be used by IoT to store internal data. 
      • The sqlite3 module installation is not required, as 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 : 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 : pip install websockets
      • websockets is a Python package for creating WebSocket servers and clients that 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 to send messages to a server and receive event-driven responses without polling the server for a response.
      • Reference: https://pypi.org/project/websockets/
    • requests : pip install requests
      • requests module allows to send HTTP requests, the HTTP request generates a Response Object that contains all of the response data (content, encoding, status, and so on).
      • Supports multipart file uploads and streaming downloads, connection pooling and keep-alive.

      • Automatic decoding and decompression of content.

      • Reference: https://pypi.org/project/requests/

    • aiohttp : pip install aiohttp
      • aiohttp, provides asynchronous HTTP client or server framework, including websockets support.

      • Reference: https://pypi.org/project/aiohttp/

    • pushsafer : 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 visualization
    • numpy : pip install numpy
      • numpy, package enables an power N-dimensional array computing.
      • Its features are utilised in IoT to read sensor bulk data from the system's database inherent functions.
      • Reference: https://pypi.org/project/numpy/
    • pandas : 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 which is very much suitable for IoT applications.
      • Reference: https://pypi.org/project/pandas/
    • matplotlib : pip install matplotlib
      • Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python.
      • Reference: https://pypi.org/project/matplotlib/
    • JSON 
      • JSON is a data storage and exchange syntax.
      • JSON is text that 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 : 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 to clusters of servers to mobile and edge devices for handling non-linear datasets.
      • Reference: https://pypi.org/project/tensorflow/
    • opencv : 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 operation. 
      • Reference: https://pypi.org/project/opencv-python/
    • pycaret : pip install pycaret
      • PyCaret - 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 use for IoT apps.
      • Reference: https://pypi.org/project/pycaret/
    • lightgbm : pip install lightgbm
      • LightGBM is an abbreviation for Light Gradient Boosting Machine, which 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 : 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 performance and 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.

Conclusion

IoT is evolving, and in order to fulfill the demands of the technology's users, it is necessary to examine which tool can match those demands. Several programming languages have shown to be effective in IoT development. As previously said, IoT using Python has shown to be a useful tool for prototype, development, and operation of various IoT devices and systems. Python's speed of development, minimal learning curve, and vast library set make it indispensable for IoT.


Credits and References:

https://www.datamation.com/wp-content/uploads/2021/04/IOT-4.png
https://www.deepseadev.com/en/blog/iot-with-python-reasons-to-use-it/
https://edu.varistor.in/python-in-iot/
https://www.oodlestechnologies.com/blogs/role-of-python-in-iot-development/
https://svitla.com/blog/internet-of-things-with-python

2 comments:

  1. Very informative article with simple text to understand..

    ReplyDelete

Scarcity Brings Efficiency: Python RAM Optimization

  In today’s world, with the abundance of RAM available, we rarely think about optimizing our code. But sooner or later, we hit the limits a...