Skip to main content

Implementation Of IOT with Python

   HOME             UNIT 1           UNIT 2          UNIT 3         UNIT 4           UNIT 5


Implementation Of Internet of Things with Python


Generally, prototypes or real-life Internet of Things (IoT) systems have to be designed and developed swiftly and competently. Whenever this occurs, two activities instantly come to life: One is to program the IoT devices, and another is to organize a backend to interact with these devices.

In both activities, we can utilize the Python programming language for their development. Or we can utilize a functional and practical edition of MicroPython in order to work on devices with small computing resources, and accordingly, at a very low price.

In the following tutorial, we will understand the use of Python in programming Internet of Things (IoT) devices and create a backend for them to work.


Understand the importance of the Internet of Things

The term "Internet of Things" was first coined in the year 1999 by Kevin Ashton. Ever since the importance and scale of IoT have exploded, one of the chief indicators is that the market size of the IoT was $151 billion in 2018, with a steady increment year after year. As per the predictions of marketers, the IoT market could cross the $561 billion mark by 2022.

Back in the day, we could explain IoT with examples as shown below: "We can utilize the phone to turn a light bulb on and off in the room."

Nowadays, hardly anyone would be amazed by a smart electricity meter that transmits readings of the consumption of the electricity, uploads that information to the cloud, and produces monthly bills sent directly to the e-mail.

IoT is increasingly utilized across industries in order to streamline processes and make them more efficient. For instance, manufacturing production lines and agriculture are great examples of various industries taking benefit of the different features of IoT. In the particular scenario of agriculture, IoT helps in coordinating harvesters with trucks that have elevators to handle grains efficiently.


Why use Python in the Internet of Things?

For many developers, Python is considered as the language of preference in the market. It is simple to learn, has clean syntax, and has a large online community supporting it. Python becomes a great choice when it comes to IoT. We can either use it for the backend side of development or the software development of devices. Moreover, Python is available to work on Linux devices, and we can make use of MicroPython for microcontrollers.

Python is the coding language that we can use to reduce the volume of data that we need to deal with, accessible in the cloud. Python recognizes the needs regardless of whether we create the IoT project from scratch or interact with actuators, sensors, and accessories.

Some of the many benefits of working with Python for IoT devices are a large number of libraries for all types of platforms and the speed it offers at which we can develop the code.

Python is a great ally for developing device prototypes. Even if we rewrite some of the scripts while producing to C, C++ or Java to improve performance, the system will generally function perfectly in Python.

What are the best solutions for IoT in Python?

Some of the best solutions for IoT in the Python programming language are as follows:

 

1.       Python on Raspberry Pi

2.     Python on PyBoard

3.     ESP8266, ESP32 with Micropython 

We will discuss each solution in brief.


Python on Raspberry Pi

The primary objective of running Python on an IoT device that pops up in mind is grabbing the Raspberry Pi from the table. Python is pre-installed in the operating system, and the only objective left for us is to write the coding script.

In this scenario, we can control the I/O ports on the expansion bar of the Raspberry Pi. Fortunately, the board supports wireless communication (Bluetooth and WiFi) and Ethernet. We can also connect a monitor to the HDMI output, a specialized 3.2" 320x240 TFT LCD, or a low energy consumption E-Ink 2.13" 250x122 display for Raspberry Pi.


There are controllers available in a large variety of computing power and budgets. We can choose these controllers for the IoT system - ranging from the fast Raspberry Pi 4 Model B 8 GB to the smallest Raspberry Pi Zero, all supporting the Python programming language. In case of necessity, we can install the earlier version of Python2.7 for past compatibility.

Let us consider the following snippet of Python code where we have used the GPIO Zero library in order to control the I/O ports.

Example:

 1.       # importing the required modules

2.     from gpiozero import Button

3.     from time import sleep 4.

5.     # creating an object of Button

6.     the_button = Button(2) 7.

8.     # using the if-else statement

9.     while True:

10.          if the_button.is_pressed:

11.                   print("Button Pressed")

12.           else:

13.                  print("Button Released")

14.           sleep(1)


Explanation:

The above example demonstrates the receiving and processing of the signals by pressing the button on the second pin at the moment of release.

 

The benefits of utilizing this approach are the availability of a large variety of development utilities, libraries and communications for the most complex devices based on Raspberry Pi involving video processing from cameras.

Python on PyBoard

Another great solution for Python in IoT devices is the PyBoard with an STM32F405RG microcontroller.


The PyBoard is considered a compact as well as a powerful electronics development board. It works on MicroPython. The PyBoard connects to the PC through USB, providing us with a USB flash drive to store the Python scripts and a serial Python prompt (a REPL) for instant programming. This works with Windows, MacOS, and Linux.

PyBoard executes MicroPython, which is a lightweight implementation of the standard CPython interpreter. The official documentation also says: "MicroPython is a lean and efficient implementation of the Python 3 programming language that includes a small subset of the Python standard library and is optimized to run on microcontrollers and in constrained environments. The MicroPythonpyboard is a compact electronic circuit board that runs MicroPython on the bare metal, giving you a low-level Python operating system that can be used to control all kinds of electronic projects. MicroPython is packed full of advanced features such as an interactive prompt, arbitrary precision integers, closures, list comprehension, generators, exception handling and more. Yet it is compact enough to fit and run within just 256k of code space and 16k of RAM."

MicroPython is an entire rewrite of the Python (version 3.4) programming language to fit and execute on a microcontroller. It involves various optimization for efficiency and consumes quite less RAM.

MicroPython executes bare-metal on the PyBoard, necessarily providing us with an Operating System based on Python. The in-built pyb module consists of functions and classes in order to control the peripherals available on the board, like I2C, UART, ADC, DAC, and SPI.

The board's dimensions are impressive, taking up around two quarters, 33mm x 43mm and weighing only 6 grams.

ESP8266, ESP32 with MicoPython

Another option could be using ESP8266 and ESP 32 to run Python. We have to create a device based on the Internet of Things with low power consumption, great capabilities, and integration with wireless Wi-Fi networks. More precisely, we can use MicroPython.

Once we installed Python on the system, we can use the pip installer in the command line in order to install the esptool module. The syntax for the same is shown below:

Syntax:

1. $ pip install esptool


The installation procedure of the MicroPython is pretty easy. We can download the firmware from the website and install it with the help of esptool, not forgetting to format the board before installing it.

We can also use one of the IDEs used for developing with MircoPython. The complete procedure of development is carried out on a working computer, and then it is compiled and saved in the memory of an ESP8266 or ESP32 microcontroller.

Let us consider the following example to see how simple the script might look like:

Example:


1.       # importing the required modules

2.     from machine import Pin

3.     import time 4.

5.     # creating an object of Pin

6.     ledPin = Pin(2, Pin.OUT) 7.

8.     # using some functions

9.     while True:

10.          ledPin.on()

11.            time.sleep(1)

12.           ledPin.off()

13.          time.sleep(1)

Explanation:

In   the   above    snippet   of   code,   we   have   imported   the Pin module   from the machine library along with the time module. We have then created an object of Pin and execute some functions on it.

MicroPython imposes many restrictions compared to regular Python; however, in general, we can easily write the necessary functionality on the client-side and execute it effectively on ESP microcontrollers. This option is relatively more cost-effective than buying PyBoard.

Comments