Python for engineering your Internet of Things (IoT) Application framework

By: Varghese Chacko 1 year ago

Python logoPython has already gained it's momentum among IoT developers as it offers readability with syntax without compromising on syntax. In today's era, language doesn't really matter any more when it comes to IoT, ease of writing the code efficiently matter. Large number of available open-source libraries and ability to get more things done with fewer lines of code is an added plus. Python's clean syntax and one to one mapping of it's data built-in structure -dict- to JSON are added advantages. If your application works with database or communicates with JSON data, Python is the right choice. Python is the choice of language for Raspberry Pi, one of the most popular micro controllers in the market. The micro Python is another micro controller optimized for using Python. Few of the advantages of using python includes

  1. Very simple language to learn and is easy to implement and deploy.
  2. It is portable, expandable and embeddable and hence it support most of the single board computers irrespective of operating system and architecture
    Huge developer community support
  3. Few python libraries we used in IoT are:

mraa - mraa is GPIO library for most single board computers that supports Python. The advantage of mraa is that it support most of the devices having GPIO. Since it is written in a high level language, reading or writing to pin is just one liner.

# Example Usage: Reads integer and float value from ADC
import mraa

print(mraa.getVersion())
try:
    x = mraa.Aio(0)  # initialise AIO
    print(x.read())  # read integer value
    print("%.5f" % x.readFloat()) # read float value
except:
    print("Are you sure you have an ADC?")

numpy - in essence is a scientific computing library in python similar to MatLab. We can use numpy to read sensor data from database in bulk and process them.

>>> import numpy as np
>>> a = np.arange(15).reshape(3, 5)
>>> a
array([[ 0, 1, 2, 3, 4],
       [ 5, 6, 7, 8, 9],
       [10, 11, 12, 13, 14]])
>>> a.shape
(3, 5)


matplotlib - It is a data visualization tool. The sensor data is best explained when converted to graphs and charts.

import matplotlib.pyplot as plt
import numpy as np

t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2*np.pi*t)
plt.plot(t, s)

plt.xlabel('time (s)')
plt.ylabel('voltage (mV)')
plt.title('About as simple as it gets, folks')
plt.grid(True)
plt.savefig("test.png")
plt.show()

pandas - Another scientific processing library in python which uses numpy.

import pandas as pd
data = [1,2,3,4,5]
df = pd.DataFrame(data)
print df

Open CV -A great library to be used for image and video processing

import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('messi5.jpg',0)
edges = cv2.Canny(img,100,200)

plt.subplot(121),plt.imshow(img,cmap = 'gray')
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(edges,cmap = 'gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])

plt.show()


tensorflow - Used for numerical computing and machine learning

import tensorflow as tf

# Create a Constant op
# The op is added as a node to the default graph.
#
# The value returned by the constructor represents the output
# of the Constant op.

hello = tf.constant('Hello, TensorFlow!')

# Start tf session
sess = tf.Session()

# Run the op
print(sess.run(hello))


requests - inter process communication over http(s)

>>> import requests

>>> r = requests.get('https://api.github.com/events')
>>> r.text
u'[{"repository":{"open_issues":0,"url":"https://github.com/...

paho-mqtt - Bi-directional async push communication via MQTT protocol