Python Programming Recipes For IoT Applications
Python Programming Recipes For IoT Applications
Jivan S. Parab ·
Madhusudan Ganuji Lanjewar ·
Marlon Darius Sequeira · Gourish Naik ·
Arman Yusuf Shaikh
Python
Programming
Recipes for IoT
Applications
Transactions on Computer Systems
and Networks
Series Editor
Amlan Chakrabarti, Director and Professor, A. K. Choudhury School of
Information Technology, Kolkata, West Bengal, India
Transactions on Computer Systems and Networks is a unique series that aims
to capture advances in evolution of computer hardware and software systems
and progress in computer networks. Computing Systems in present world span
from miniature IoT nodes and embedded computing systems to large-scale
cloud infrastructures, which necessitates developing systems architecture, storage
infrastructure and process management to work at various scales. Present
day networking technologies provide pervasive global coverage on a scale
and enable multitude of transformative technologies. The new landscape of
computing comprises of self-aware autonomous systems, which are built upon a
software-hardware collaborative framework. These systems are designed to execute
critical and non-critical tasks involving a variety of processing resources like
multi-core CPUs, reconfigurable hardware, GPUs and TPUs which are managed
through virtualisation, real-time process management and fault-tolerance. While AI,
Machine Learning and Deep Learning tasks are predominantly increasing in the
application space the computing system research aim towards efficient means of
data processing, memory management, real-time task scheduling, scalable, secured
and energy aware computing. The paradigm of computer networks also extends it
support to this evolving application scenario through various advanced protocols,
architectures and services. This series aims to present leading works on advances
in theory, design, behaviour and applications in computing systems and networks.
The Series accepts research monographs, introductory and advanced textbooks,
professional books, reference works, and select conference proceedings.
Jivan S. Parab · Madhusudan Ganuji Lanjewar ·
Marlon Darius Sequeira · Gourish Naik ·
Arman Yusuf Shaikh
Python Programming
Recipes for IoT Applications
Jivan S. Parab Madhusudan Ganuji Lanjewar
School of Physical and Applied Sciences School of Physical and Applied Sciences
Goa University Goa University
Taleigao, Goa, India Taleigao, Goa, India
© The Editor(s) (if applicable) and The Author(s), under exclusive license to Springer Nature
Singapore Pte Ltd. 2023, corrected publication 2023
This work is subject to copyright. All rights are solely and exclusively licensed by the Publisher, whether
the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse
of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and
transmission or information storage and retrieval, electronic adaptation, computer software, or by similar
or dissimilar methodology now known or hereafter developed.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication
does not imply, even in the absence of a specific statement, that such names are exempt from the relevant
protective laws and regulations and therefore free for general use.
The publisher, the authors, and the editors are safe to assume that the advice and information in this book
are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or
the editors give a warranty, expressed or implied, with respect to the material contained herein or for any
errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional
claims in published maps and institutional affiliations.
This Springer imprint is published by the registered company Springer Nature Singapore Pte Ltd.
The registered company address is: 152 Beach Road, #21-01/04 Gateway East, Singapore 189721,
Singapore
Foreword
v
vi Foreword
In conclusion, I would like to say that Python is an excellent language for devel-
oping IoT applications, and this book will provide one with the knowledge and
tools one need to get started. Whether you are building a simple sensor network or a
complex machine learning application, the recipes in this book will guide you through
the process. So, let’s dive in and start building some awesome IoT applications with
Python.
The original version of this book was revised. The series editor name was removed in XML of
chapters 1, 2 and 6. The correction of the book is available at https://doi.org/10.1007/978-981-19-
9466-1_7.
Preface
We are pleased to present this book titled “Python Programming Recipes for
IoT Applications” to the readers. As we all know, Python is powerful, yet flex-
ible, easy to learn, and can be adapted to work with most of the microcontroller-
based environments. Simplicity serves as a great asset to Python, which allows it
to flourish on every platform. The coding flexibility and dynamic nature of Python
help developers to create intelligent Internet of Things (IoT) devices.
So, we thought this is the right time to bring such a book to the market which
explains the basic glimpses of Python followed by three different embedded platforms
and their configuration setup. Various IoT applications with FoG and cloud-based
computation on these platforms are explained in a very simple and understandable
manner so that the readers can start programming on any of the platforms of their
choice with ease.
The detailed chapter-wise flow is as follows:
Chapter 1 “Python Programming and IoT” discusses the basics of Python language
such as features of Python, Integrated Development Environment (IDE), data types,
and so on. It also discusses comparison of Python with C and C++ language. The
C/C++ programming languages dominates embedded systems programming. On the
other hand, Python has many strengths that make it a great programming language for
embedded systems. This chapter also gives the overview of IoT and its applications.
Chapter 2 provides detailed step-wise configuration setup of Raspberry Pi,
MicroPython Pyboard, and NVIDIA Jetson Nano This chapter also covers the details
of all above boards including block diagram, features available in these boards, and
functions of each block.
Chapter 3 gives the detailed implementation steps of simple IoT applications using
Raspberry Pi such as controlling LED blinking, OLED display interface, camera
interface, and motor control (DC motor, stepper motor, and servo motor).
Chapter 4 gives the detailed implementation steps of MicroPython Pyboard for
IoT applications such as home automation, smart e-waste bin, industrial environment
monitoring, green house monitoring, and aquaculture monitoring.
Chapter 5 focusses on FoG and cloud computing with NVIDIA Jetson Nano
board. It covers the introduction and the model architecture of both the FoG and
vii
viii Preface
ix
Contents
xi
xii Contents
Gourish Naik Former Dean Faculty of Natural Science & Former Head Department
of Electronics obtained his Ph.D. from the Indian Institute of Science, Bangalore
(1987) and served the institute as a research associate in the areas of Optoelectronics
xiii
xiv About the Authors
and Communication until 1993. He was associated with the Goa University for
27 years. He is the founding head of the University Instrumentation Centre and
established fiber optic LAN and wireless communication networks at Goa University.
He was also the coordinator of DEITI (an educational broadcast studio supported by
Indian Space Research). His other commitments are regulating the digitization center
at Goa University to support the various digital repository projects like DIGITAP
(Digital Repository for Fighter Aircrafts Documentation) of the Indian Navy, the
Million Book project of the Ministry of Information Technology and Antarctica
Study Center (NCAOR). He has to his credit around 60 research papers published
in international journals and has presented research works at various national and
international forums. He has delivered several keynote addresses and has been invited
to talks at various institutes and also authored five books on embedded systems and
allied areas published by CRC Press, Springer, Lambert, etc. He was a member of
the Goa State Rural Development Authority and also an advisor for the Directorate
of Education. He was a governing body member of the engineering college of Goa
and also a member of the faculty board of Goa University.
Abstract There are number of books that cover Python programming in detail. This
chapter is not exhaustive; instead, it briefly describes Python’s programming basics
that will give a good hold for programmers to write the programs. It answers the
question of whether Python will replace C/C++? This chapter also briefly introduces
IoT and Python for embedded and IoT applications.
© The Author(s), under exclusive license to Springer Nature Singapore Pte Ltd. 2023 1
J. S. Parab et al., Python Programming Recipes for IoT Applications, Transactions
on Computer Systems and Networks, https://doi.org/10.1007/978-981-19-9466-1_1
2 1 PYTHON Programming and IoT
Python has evolved as such a popular programming language with several releases,
and with every release, some new features are added in it.
Python versions:
• The first Python code was published with labeled version 0.9.0 by Guido Van
Rossum in February 1991.
• Python 1.0 was manumitted in 1994 with features such as lambda, map, filter, and
reduce, followed by Python 1.5 and 1.6 in the years 1997 and 2000 respectively.
• Python 2.0 was released with addon features such as comprehensions, garbage
collection systems. Python 2 also has intermediate version 2.1 till 2.7.
• Similarly, on December 3, 2008, Python 3.0 (also called “Py3K”) was manumitted.
Python 3 was specially developed to rectify the basic flaws of the earlier versions.
From 2008 till date several releases of Python 3 have come, from version 3.1 till
3.11 which was released in February 2023.
The answer to the question “can Python replace C/C++?” is NO. C and C++ forms the
basis of every programming. Python is built using C by keeping web programming
in mind. So there is no possibility that Python will replace fundamental languages
like C or C++ at least not in the near future.
Overall Python is better than C/C++ in terms of its simplicity, readability,
writability, and very simple syntax, while C/C++ is tedious and time-consuming,
prone to errors, and not easy to understand . Although C/C++ is better in terms of
performance, speed and has enormous application areas.
Python offers enormous error-checking option than C. Being a very high-level
programming language, it has various data types, arrays, and dictionaries already
built-in that would have cost someone many days to implement effectively in C.
Python gives the flexibility to break up the program into small modules which later
can be leveraged in another Python programs.
For embedded system code development, the programmer prefers C/C++ language
due to faster runtime code. When it comes to runtime speed Python may be less
efficient than C/C++, however, runtime speed is sometimes not that important when
development speed is considered, which is good for Python. Now in recent times,
Python is extensively used for embedded systems explained in Sect. 1.4.
Python programming language is easy to learn because of its simple syntax and huge
library packages. Before starting with the basics of Python programming one has to
1.3 Overview of Python Programming 3
Anaconda Installation
Anaconda is an incredible collection of Python packages, tools, resources, and IDEs.
This package comprises several tools which are helpful for the data scientist to
exploit Python’s unbelievable strength. Anaconda is a free and open-source individual
edition available for developers. The installation steps are given below:
Step 1: Visit the web page https://www.anaconda.com/products/individual-dto and
download the individual edition of the Anaconda. Click on ’Download’ button as
shown in Fig. 1.2a.
Step 2: Search.exe file (Anaconda3-2021.05-Windows-x86_64) in Download direc-
tory. Double click on it → click on ’Run’ → click on ’Next’ (Fig. 1.2b) to continue
→ click on ’Agree’ (Fig. 1.2c) → select ’Just me’ and then ’Next’ (Fig. 1.2d)
→ choose the location for installation (Fig. 1.2e) and click on ’Next’ → click on
’Register Anaconda3 as my default Python’ (Fig. 1.2f) → click on ’Install’ button.
Step 3:After completion of installation process (Fig. 1.2g) click on ’Next’ → click
on ’Next’ button to finish the process as shown in Fig. 1.2h and then ’Finish’ button
(Fig. 1.2i).
IDEs are used for developing the codes for various applications. In this book, authors
have used Thonny and PyCharm IDEs for developing various applications. The
installation process of these two IDEs is depicted as follows.
Thonny IDE Installation
Thonny is an easy and simple IDE to use and it comes with a built-in Python 3.7.
The downloading and installation process of Thonny IDE is given below:
Step 1: Visit https://thonny.org/ and choose OS as Windows, Mac, or Linux depending
on your requirement. Here the installation steps of Thonny for Windows are shown
below.
Step 2: Download the Thonny for Windows → search Thonny-3.3.13 (or latest
version) file in the Download folder → double click on it → accept terms and
conditions and then click on ’Next’ → select the location (destination) → click on
’Next’ → select create desktop icon → select start menu folder and click on ’Next’
→ click on ’Install’ → installation process will start and after installation click on
’Finish’. The full installation flow is depicted in Fig. 1.3a–e.
PyCharm IDE installation
The step-by-step process of how to download and install PyCharm IDE on Windows
is as follows:
Step 1: Visit the website https://www.jetbrains.com/pycharm/download/ to download
PyCharm → click the ’DOWNLOAD’ link under the community section as shown
in Fig. 1.4a.
Step 2: Run the.exe file (pycharm-community-2021.2.2 or latest version) to install
PyCharm → the setup wizard will start and then click ’Next’ as shown in Fig. 1.4b.
Step 3: Installation path can be changed if required or click on ’Next’ to continue
(Fig. 1.4c).
Step 4: Create Desktop shortcut and then click on ’Next’ (Fig. 1.4d).
Step 5: Choose the start menu folder (Fig. 1.4e) → keep JetBrains selected → click
on ’Install’ → wait until the installation process is finished.
Step 6: After finishing the installation, a message screen will display as shown in
Fig. 1.4f → click on ’Finish’.
1.3.2 Glimpses of Python Programming
Python is a fantastic language for beginners and helps to develop various applications.
Following are some of the important characteristics which make this language very
powerful:
• Python supports Object-Oriented Programming (OOP) as well as it is structured
programming.
• It supports scripting language.
1.3 Overview of Python Programming 7
lines and indentations, quotations, different data types, operators, functions, etc. are
outlined. After navigating through the following section, the reader will be able to
write their first Python code for any desired applications.
Identifiers
An identifier refers to the name used to represent a variable, function, module, or class
in Python. It must always begin with an uppercase letter (A–Z) or lowercase letter
(a–z) or an underscore, and then can continue with a letter, underscore, and digits
(0–9). We cannot use special characters such as @, $, and % in building an identifier.
It must be noted that Python is a case-sensitive language and care must be taken to
construct the identifier accordingly. For example, var and VAR are considered two
separate identifiers in Python.
Reserved Words
Reserved words cannot be used as identifiers or variables or constants. The Python
keywords are always constructed using lowercase letters. Table 1.1 gives the
keywords which are used in the Python language.
Indentation in Python
Unlike other languages, a block of code under functions and flow control in Python
is not specified using braces or brackets. Instead, Python relies on line indentations,
which are rigidly applied in the language.
One can use a variable number of spaces to specify indentations, but this number
must be consistent in the entire block. An example showing proper indentation is
shown below:
a=10
b= 10
if (a==b):
print("Yes")
else:
print("No")
Multi-line Statements
Usually a statement is terminated by a new line in Python. However Python permits
to continue line by using a line continuation character(\):
history=10
geography=9
maths=2
10 1 PYTHON Programming and IoT
science=3
Marks = history + geography +maths + science
print(Marks)
However, statement which are contained in {}, [], or () do not require the line
continuation character.
Employees= [‘John’, ’kevin’, ’Wenzel’, ’Albert’, ’Peter’]
In Python, one can use the semicolon ( ; ) character to type multiple statements
on the same line provided neither of the statement starts a new code block.
print("hello"); a = 50; a = a + 34
Quotation
A string in Python is specified using quotes. Single, double, or triple quotes are used
to specify literals provided the same type of quotes are used to terminate the string.
However, triple double quotes allow spanning a string across multiple lines.
name = ’Haston’
message= "Hello World of Python programming"
long_message = """We are going to land on the island tomorrow after
the sunset"""
Comments
In Python, a comment is indicated to the interpreter by a hash sign (# ) and this
character must not be contained in a string literal. The characters placed after the #
till the end of the line makes up the comment. The Python interpreter will ignore all
the characters after # character.
# This line is a comment, below code prints hello Python on the screen
print(“hello world”) # command to print
The Python interpreter ignores the triple single quoted string, as such can be used
as a multiline comments.
’ ’ ’Comment can span multiple Lines ’ ’ ’
the ’True’ value of an expression. The code below prints the type of the variable z
which is <class ‘bool’>:
z = True
print(type(z))
Numeric
Numeric data types are one of the most extensively used data types in Python. It is
further subdivided into integers, float, and complex types. Integers can be positive,
negative, and whole numbers including zero which do not have a fractional part and
have unlimited precision. Integers can be specified with binary, octal, and hexadec-
imal values. The numbers 78, -58, and 0 are examples of valid integers in Python. It
must be noted that leading zeros are not allowed in non-zero numbers.
Floating-point data type are positive and negative real numbers that have a frac-
tional part. The fractional part is specified after the decimal point. One can use a
decimal point representation or scientific representation to specify a floating-point
number. For example, 1000.47 is decimal point representation and 1.00047E3 is in
scientific representation. The scientific representation is particularly useful to show
numbers having many digits.
The third type of number which Python supports is the Complex number. It has a
real part and
√ the imaginary part. The imaginary part of the number is multiplied by j
(j refer to −1). For example, for the complex number 3 + 2j, the 3 is the real part
and the 2j is the imaginary part. Care must be taken to use j or J as the imaginary
component, the use of any other character results in a syntax error. The code below
shows the use of integer, float, and complex numbers:
# The numerical types in Python
# Use of integer data type in Python
int1 = 15
int2 = 2500
int3 = -25
# We can use binary, octal, and hexadecimal values to
represent integer variables
12 1 PYTHON Programming and IoT
Python Dictionary
Dictionary in Python is similar to a hash table. They operate similar to hashes or
associative arrays which are part of the Perl programming language. Dictionary is a
pair of key and value. Any valid type in Python can be specified as a key, however
numbers and strings are preferred over others. Whereas any arbitrary Python object
can be a value for the key.
It must be noted that curly braces ({ }) are used to indicate a dictionary whereas
square braces ([]) are used to assign and access.
dictionary={}
dictionary[’one’]="Test one"
dictionary [2] ="test two"
smalldictionary ={’name’:’tony’,’srno’:789,’section’:’research’}
print(dictionary[’one’]) # Displays value of key ‘one’
print(dictionary [2]) # Displays value of key 2
print(smalldictionary) # Displays entire dictionary
print(smalldictionary.keys()) # Displays the keys
print(smalldictionary.values())# Displays the values
Sequence Type
Sequence type in Python refers to an ordered set. Strings, tuples, and lists are the
popular sequence data types used in Python.
Python Strings
A string is a set of characters that are specified within quotation marks. We can use
single or double quotes as a pair to contain a string. Python allows to access a subset
of string using the slice operator [] or [:]. This needs to be done using the index which
begins at 0. The ’+’ operator is used to concatenate two strings, and the ’*’ operator
is used to repeat the string as shown in below example:
string =’Hello Python !’
print(string) # Displays whole string
print(string[0]) # Displays first character in string
print(string[1:8]) # Displays 1st to 7th index characters
print(string[3:]) # starting from 3rd index is Displayed
1.3 Overview of Python Programming 13
After executing above code the output is shown below: Hello Python ! H ello Py lo
Python ! The index can begin with 0 and end with (length -1). The use of concatenate
is shown below:
print(string *3) # Displays string 3 times
print(string +" World") # concatenated string is Displayed
Python Lists
Python has powerful compound data types, one of them is the List data type. The
items within the list are specified by a comma separator. All the items are contained
within square brackets. A Python list allows entering different data types within the
list in contrast to arrays in other programming languages.
list =[’albert’, 145, 3.14, ’raman’, 36]
smalllist =[654, ’bose’]
print(list) # Shows the list
print(list[0]) # Shows only the the 1st element of the list
print(list[0:4]) # Shows elements starting from 1st till 4th
print(list[2:]) # Shows elements starting from 3rd till end
print(smalllist*4) # Shows the list 4 times
print(list + smalllist) # Prints the concatenated list
Python Tuples
Yet another powerful data type in Python is a tuple. This again stores a sequence of
data and is similar to a list in its operation. In a tuple, commas are used to separate
the items contained within it. However, as compared to a list, the items within a tuple
are enclosed within parentheses.
Here we summarize the main differences between a list and a tuple. Square
brackets ([]) are used to enclose items in a list whereas parentheses (( )) are used to
enclose items in a tuple. In the case of a list, we can change the elements and size,
but in a tuple, we cannot change the elements and size. The tuple is a read-only list.
tuple =(’albert’,145,3.14,’raman’,36)
smalltuple =(654,’bose’)
print(tuple) # prints the tuple
print(tuple[0]) # Displays only the the1st element of the tuple
print(tuple[0:4]) # elements are Displayed from 1st till 4th
print(tuple[2:]) # Displays elements from 3rd till end
print(smalltuple*4) # Displays the smalltuple 4 times
print(tuple + smalltuple) # Displays the concatenated tuple and
smalltuple
Set
Python programming language enables us to use the concept of set. A set is defined
as a collection of items that are not in order. The items must be enclosed in curly
braces ({}) and commas are used to separate the items. The interpreter automatically
14 1 PYTHON Programming and IoT
removes duplicate items. The basic operations such as set intersection, union, differ-
ence, and symmetric difference are allowed in Python. Indexing is not allowed for a
set as the items are not ordered.
a = {4,5,5,6,6,6} # a set with repeated values,
b = {4,7,8} # set b
c = a.union(b) # set c is union of a and b print (c)
d = a.intersection(b) # set d is intersection of a and b print (d)
e = a.difference(b) # set e is difference of a from b print(e)
f = a.symmetric_difference(b) # set f is symmetric difference
print(f)
The above lines of code explain how to assign values to variables. The value 1000,
3.14, and “Albert” are assigned to the variables speed, pi, and name, respectively and
then printed.
We can also handle multiple assignments in the Python language. In the statement
below, the variables j, k, and l are initialized to a value 15.
j=k=l=15
and ’*’ is the operator. In Python, operators are classified into various types which
are intended for specific purposes. The different categories of the operators are listed
below:
• Arithmetic.
• Comparison (Relational).
• Assignment.
• Logical.
• Bitwise.
• Membership.
• Identity.
Arithmetic Operators
The arithmetic operators are used to perform various arithmetic operations on the
operands. Table 1.2 explains all the arithmetic operators assuming that the variables
x and y are the operands on whom the operators are applied upon.
Comparison Operators
Operators which compare the operands on each of its sides and find the relation
between them are comparison operators. Another name for comparison operators is
relational operators. Table 1.3 gives the different comparison operators.
Assignment Operators
These operators are used to assign a value to the variable on the left of the command.
Different assignment operators are given in Table 1.4.
Bitwise Operators
Bitwise operators perform bit-by-bit operations. If we assume that x = 126 and y =
31. Then their binary representation will be 0111 1110 and 0001 1111, respectively.
The bitwise operators in Python take the two operands and perform the desired
operation on every corresponding bit of two operands. For bitwise AND, it takes
the two operands and performs a bitwise AND operation. For bitwise OR, it takes
the two operands and performs a bitwise OR. Similarly, for bitwise XOR, it takes
the two operands and performs a bitwise XOR. In a bitwise complement, it takes
the operand and performs a bitwise compliment. In addition, there are bitwise shift
left and shift right operators. In the left shift, the bits of the operand on the left are
shifted left by the value of the right operand. Similarly, in the right shift, the bits of
the operand on the left are shifted right by the value of the right operand. Table 1.5
gives the different bitwise operators.
Logical Operators
There are three logical operators, namely, NOT, AND, and OR which are supported
by Python. These are used to make a logical decision.
In logical AND, if both the operands are true or non-zero, then only the result will
be True. In logical OR, if either of the operands are True or non-zero then the output
is True. In logical NOT, the output is the reversal of the logical state of the original
operand. Table 1.6 gives the logical operators in Python.
Membership Operators
A membership operator is used in Python to check if the element is a member of a
sequence, such as tuples, lists, or strings. The ’in’ operator is used for this purpose
and will evaluate to True if it finds a variable in the mentioned sequence and False
another time. The ’not in’ evaluates to true if it does not find a variable in the specified
sequence and false otherwise. In the code below, ’A’ is a member of the string and
hence the print statement outputs True. In the second statement as ’s’ is not a member
of the string and hence the print statement outputs True.
a = ’Albert’
print(’A’ in a ) # Results in True
print(’s’ not in a) # Results in True
Identity Operators
Identity operators are used for checking the memory locations of two objects. The
’is’ operator results to True if the variables on either side of the operator point to
the same object or False another time. The ’is not’ evaluates to False if the variables
on either side of the operator point to the same object and True another time. In the
code below the first print statement outputs a False and the second print statement
outputs a True as in both cases the values are identical (Guzdial and Ericson 2015).
a = 10
b = 10
x = ’Albert’
y = ’Albert’
print(a is not b) #Results in False
print(x is y) #Results in True
1.3.5 Python—Decision-Making
In programming, decision-making is done by anticipating a condition to occur in
the execution process and then specifying a list of commands to be executed, if that
condition occurs.
Various decision structures are employed which evaluate expressions that produce
a TRUE or FALSE result. A set of actions have to be performed if the outcome is
TRUE and a different set of actions have to be performed if the outcome is FALSE.
Figure 1.6 shows the general control flow in most programming languages when
a decision has to be made by the program.
Python has various decision-making statements such as if…statements, if…else
statements and if…elif… else statements.
An ’if’ statement is made up of a boolean expression followed by single or multiple
statements. An ’if…else’ statement has an additional ’else’ statement executed when
the boolean expression results in a FALSE value. In an ’if…elif…else’ statement
the ’elif’ is short for ’else if’. It allows us to check for multiple expressions. If all
the conditions are FALSE, the body of else is executed. The below Python program
illustrates the use of the above decision-making structure:
# if statement in Python
a = 50
if(a==50):
print("the value is 50")
Loops
In a programming laguage, statements are executed in a sequential manner. A
programmer may be faced with a situation where he needs to execute a block of code
multiple times. Python provides several control structures which allow the above
situation. A loop in Python is a control structure used to run a block of statements
multiple times.
The loop control structures in Python are listed below:
• while loop
• for loop
• nested loop
The ’while’ loop repeats a block of statements when a condition evaluates to a
TRUE value. The condition is tested before the execution of the block of statements.
The ’for’ loop is a concise way of writing a ’while’ loop, as such it is said to
abbreviate the code section which is used to update the loop variables. The nested
loop structure uses multiple loops inside any other ’while’or ’for’ loop.
Loop Control Statements
A loop control statement changes the normal loop execution. It must be noted that
when execution has left a scope, all automatic objects that were created in that scope
are destroyed.
The control statements which are provided are
• break statements
• continue statement
• pass statement
The ’break’ is used to terminate the loop and the program control is transferred to
the first statement after the loop. The ’continue’ is used to skip the rest of the body of
1.3 Overview of Python Programming 21
the loop and to immediately re-evaluate the condition before reiterating. The ’pass’
in Python results in no operation . It may be used by the user to avoid error when
empty code is syntactically not allowed in cases such as loops, function definitions,
etc.
1.3.6 Functions
A function is defined as a reusable block of related and organized code that can be used
to perform a task. A function is principally used to provide modularity. A function
called multiple times as such promotes the reusability of code. Python language has
a repository of a large number of built-in functions such as print(),input() etc. In
addition, Python also allows users to define a function to perform a user-defined
task, such functions are called as user-defined functions.
Function Definition in Python
The following set of rules needs to be followed to define a function:
• The keyword ’def’ followed by the function name and parentheses ’()’ is used to
define a function block.
• Input arguments are placed within parentheses.
• An optional first statement in a function is the documentation string of the function
or docstring.
• Indentation must be followed in a function and the indentation begins after the
“:”.
• A function has a ’return [expression]’ statement. Its main task is to exit a function
or passing back an expression to the caller. We can use return statements without
arguments. Below is the example of a function definition:
Syntax
#Example
def multiply(a):
"function to multiply"
ans = a * a
return [ans]
val = multiply(50)
print(" the value of multiplication",val)
1.3.7 Modules
A module is used to arrange code in order to make it more readable. Grouping similar
code into modules makes it easier to comprehend and distribute the code. A module
is a Python object containing randomly named characteristics that may be bound and
referenced. A module contains function definition, classes, and variables. One can
include runnable code in a module.
22 1 PYTHON Programming and IoT
When the interpreter comes across an import statement, it imports the module if
it is on the search path. A search path is a directory list that the interpreter looks
over before importing a module. To import the module test.py, use the following
commands:
#!/usr/bin/Python
import test
test.square(15)
Can Python be Used for Embedded Systems? Over the years, the C/C++
programming languages have dominated the embedded systems programming.
On the other side, Python has many strengths that make it a great language for
embedded programming.
Python isn’t only the most popular language, it’s also the fastest growing language
for embedded computing. May be that sounds silly, but the reality is that Python has
started eating into C/C++’s space.
Python can be used in embedded, small, or minimal hardware devices, depending
on how limiting the devices actually are.
The question arises: Can Python be used to program microcontrollers?
MicroPython is an efficient and optimized implementation of the Python 3
programming language that allows the programming of microcontrollers. MicroPy-
thon is a small subset of the Python standard library and is optimized to run on
microcontrollers and in constrained environments.
We will also talk about PyBoard, a microcontroller board specifically designed to
be programmed using MicroPython.
The term IoT, i.e. “Internet of Things” (IoT) was initially coined by Kevin Ashton
in 1999, which later became widely accepted throughout the world. IoT is a mesh
of physical objects or people called “things” that are encapsulated with software,
hardware, network connectivity, and sensors, which admits these objects to gather
and barter the data. IoT makes everything “smart” and “easy” by improving aspects
24 1 PYTHON Programming and IoT
facet of individuals’ life with the potential of data collection, Artificial Intelligence
algorithm, and connectivity. There are basically four basic components of an IoT
system, namely, sensors, connectivity, data processing, and user interface as shown
in Fig. 1.7.
Sensors: Sensors/devices are important parts of IoT system which help to gather live
data from the surrounding. This data is complex and varied in nature. Sometimes a
device may have other types of sensors which perform some kind of intelligent task
apart from sensing.
Connectivity: Connectivity is another component of IoT. All the data gathered from
the sensors is sent to a cloud. These sensor data will be sent to the cloud by using
several communication protocols such as Bluetooth, wireless, NFC, RFID, radio
protocols, and Wi-Fi-Direct.
Data Processing: Once the data is gathered and uploaded on to the cloud, the various
available data processing software performs required processing algorithm.
User Interface: After performing the processing on data, the processed data must be
made available to the end user in some way. The end user gets these data on his smart
device. Sometimes user from distant place can control the various devices through
IoT.
IoT helps to create opportunities to connect and interact with physical world by using
sensors and Internet. IoT system allows devices to be controlled remotely over the
Internet. The interconnection of these various devices helps in automizing the entire
system. This results in enhanced accuracy, system efficiency, and economic benefit
with minimal human intervention.
IoT devices find extensive set of applications into consumer, commercial, indus-
trial, and infrastructure spaces (Vongsingthong and Smanchat 2014; Jump up to 2015,
Perera et al. 2015). Below are the list of few applications of IoT.
Energy Applications: IoT find its extensive use in energy sector. The energy saving
is very important, so one has to utilize it very effectively. Individuals and organiza-
tions are finding out various ways to minimize and control the energy consumption.
With advancement in IoT technologies it makes it possible to monitor the energy
consumption at the device level, individuals house level, electric grid level, and also
at distribution level. It also helps to monitor the system performance.
Smart Homes/Smart City: This is one of the most frequently used practical appli-
cations of IoT. Smart homes system ensures both convenience and home security.
Under smart city, it allows Internet access to people. IoT helps to manage traffic,
waste management, water distribution, electricity management, etc. This will help
to give some kind of convenience to the city management administration.
Healthcare Application: In today’s world health monitoring is very important. In
present state, people need to monitor their own health at regular intervals. Smart
devices on patients continuously updates the data on cloud and notifies this to the
hospital/doctor in case of emergency. So, if a patient is coming to the hospital in
case of emergency, by the time he or she reaches the hospital his health report is
diagnosed by doctors and the hospital can quickly take the treatment decision which
is very much crucial.
Education: IoT plays an important role in education. IoT in education system means
better connectivity for learning which helps to fulfill the gaps in the education sector.
Today, many schools and colleges have IoT embedded into their day-to-day learning.
It improves the overall quality of education.
Air and Water Pollution: IoT also plays an important role in monitoring the environ-
mental pollution. There are various sensors available which help to detect pollution
in the air and water. By employing IoT system, one can automize and reduce the
human intervention in monitoring.
Agriculture: Agriculture is the main stay of our economy. Smart agriculture is very
important these days as farmers will have ready information about various agricul-
tural parameters. IoT device installed at remote places measures various parameters
such as soil moisture, chemical application, dam levels, and provides this information
to the farmer in real time.
26 1 PYTHON Programming and IoT
Transportation: In transportation sector, IoT has changed the entire facet of the
same. Now, the market has seen introduction of automated cars with embedded
sensors, which can sense the traffic and switch off automatically. IoT also helps
to identify the location of free parking slot. Some cars are embedded with sensors
which can monitor the current health status of the vehicle, so that one doesn’t face
any issues while traveling.
Marketing your product: IoT has ventured into the marketing industry, using IoT,
companies can now analyze and respond to customer preferences by providing the
desired content and solutions. IoT helps enhance the business strategies in the real
time.
Conclusion:
Python programming and IoT chapter gives the brief introduction to Python and
history of various Python versions. As we all know, compared to other programming
languages, C/C++ is preferred by programmers for embedded applications. But in
recent past Python is extensively used for embedded application and it is eating the
C/C++ space. Developers shouldn’t be surprised when they see Python cropping up
and beginning to play a vital role in embedded system development. An overview
of the Python programming with detailed installation steps of various Python IDEs
such as Thonny, Pycharm, and Anaconda is also provided. There’s a lot of noise
at the moment about the IoT and its impact on everything. The coding flexibility
and dynamic nature of Python helps developers in creating intelligent IoT devices.
Introduction to IoT, its working, and few examples are covered nicely in this chapter.
References
Beazley D, Jones BK (2013) Python cookbook: recipes for mastering python, vol 3. O’Reilly Media,
Inc.
Guzdial MJ, Ericson B (2015) Introduction to computing and programming in Python. Pearson
Jump up to: The enterprise internet of things market. Business Insider. 25 February 2015. Accessed
26 June 2015
Kuhlman D (2012) A python book: beginning python, advanced python, and python exercises.
Section 1.1. Archived from the original (PDF) on 23 June 2012
Perera C, Liu CH, Jayawardena S (2015) The emerging internet of things marketplace from an
industrial perspective: a survey. IEEE Trans Emerg Top Comput 3(4):585–598. arXiv:1502.
00134. Bibcode:2015arXiv150200134P. https://doi.org/10.1109/TETC.2015.2390034. ISSNS
2168-6750. S2CID 7329149
Pilgrim M, Willison S (2009) Dive into Python 3, vol 2. Springer
Vongsingthong S, Smanchat S (2014) Internet of things: a review of applications & technologies
(PDF). Suranaree J Sci Technol
Chapter 2
Configuring Raspberry Pi,
MicroPython Pyboard, and Jetson Nano
for Python
Abstract In today’s world one need not spend much time in configuring and getting
acquainted with any embedded platform. The main focus should be towards quick
launch of any designed application to market. Keeping these points in mind authours
have provided the detailed steps involved in configuration and programming the
platform. Here, authours have focused on three different embedded Platforms such
as Raspberry pi Board for basic interface application, MicroPython Py board for IoT
applications and NVIDIA Jetson Nano board for Machine learning and deep learning
Applications using Python programming with FoG/cloud Computing.
© The Author(s), under exclusive license to Springer Nature Singapore Pte Ltd. 2023 27
J. S. Parab et al., Python Programming Recipes for IoT Applications, Transactions
on Computer Systems and Networks, https://doi.org/10.1007/978-981-19-9466-1_2
28 2 Configuring Raspberry Pi, MicroPython Pyboard, and Jetson Nano …
the processor range from 0.7 to 1.2 GHz based on the various models of the Raspberry
Pi. SDRAM is provided with capacities ranging from 256 MB to 1 GB. Popular serial
interfaces such as the SPI, I2C, I2S, and UART are provided on the Raspberry Pi.
The different versions of the Raspberry Pi are Raspberry Pi Zero, Raspberry Pi 1
A, Raspberry Pi 1 A+, Raspberry Pi 1 B, Raspberry Pi 1 B+, Raspberry Pi 2 B,
Raspberry Pi 3 B, Raspberry Pi 3 B+, and Raspberry Pi 4.
Out of the above versions of Raspberry Pi, more prominently used Raspberry Pi
models and their features are given in Table 2.1.
Raspberry Pi 3 B+ Hardware
The hardware on the Raspberry Pi 3 B+ is highlighted in Fig. 2.2. Important hardware
features are explained here. “Raspberry Pi 3 ModelB+”. https://static.raspberrypi.
org/files/product-briefs/Raspberry-Pi-Model-Bplus-Product-Brief.pdf
Fig. 2.3 a Raspberry website, b Raspberry imager, c Raspberry OS selection, d choose storage for
OS installation, e microSD card or storage selection, and f OS installation
2.1 Raspberry Pi Board Features 31
Step 7: The final step is to hit the ’Flash’ button and then the balenaEtcher will start
the flashing process as shown in Fig. 2.5.
Booting Raspberry Pi for the First Time
Step 1: Insert your microSD card into Raspberry Pi.
Step 2: Connect power, keyboard, mouse, and monitor to Raspberry Pi.
Step 3: After booting you get “Welcome to the Raspberry Pi” dialog on the display,
as shown in Fig. 2.6.
Step 4: Quick setup of Raspberry Pi:
(i) On the next screen you will be asked to choose your Country (Fig. 2.7a),
Language, and Timezone.
(ii) Set account password as shown in Fig. 2.7b.
(iii) Followed by this, select a Wireless Network and its network password.
(iv) Set up the desktop screen by clicking ’Next’ as shown in Fig. 2.7c.
(v) Finally it shows setup complete as shown in Fig. 2.7d.
Now the Raspberry Pi is ready to use.
2.2 MicroPython Pyboard Features 33
The MicroPython Pyboard is a small form factor development board designed to run
MicroPython to realize various IoT projects. MicroPython is versatile and provides
an interactive prompt, locks, generators, list understanding, exception handling, etc.
Below is the specification of Pyboard V1.1: “Quick reference for the Pyboard”.
https://docs.micropython.org/en/latest/Pyboard/quickref.html
Pyboard V1.1 Specifications:
• STM32F405RG microcontroller (clock frequency 168 MHz Cortex-M4 core)
• 1024 KB flash ROM, 192 KB RAM
• Power from microUSB connector
• MMA7660 3-axis accelerometer
• MicroSD card slot
• 5 GPIO on the bottom, 24 GPIO on the left and right, Real-Time Clock (RTC)
• Three 12-bit analog-to-digital converters (ADC)
• Two 12-bit digital-to-analog (DAC) converters
• One user and one reset switch, four LEDs
• Wide input voltage range from 3.6 V to 16 V, 3.3 V low dropout voltage regulator
• Upgrade firmware through DFU bootloader in ROM
34 2 Configuring Raspberry Pi, MicroPython Pyboard, and Jetson Nano …
Fig. 2.7 a Set country, language, and timezone; b set a new password; c set up screen for desktop;
and d setup complete
Fig. 2.10 a Connect P1 (DFU) and 3V3 pins and b connection of P1 (DFU) and 3V3 pins using
jumper
Update Firmware
To update the Pyboard’s interpreter the following steps need to be followed:
Step 1: Disconnect the power supply from the Pyboard.
Step 2: Short P1 (DFU) pin to power (3V3) pins using jumper or wire as shown in
Fig. 2.10 a and b.
Step 3: Connect the Pyboard to the computer through the USB port.
Step 4: Download the firmware file from MicroPython website as shown in Fig. 2.11
(https://micropython.org/download/pybv1/). Carefully select the firmware based on
the version of Pyboard used (PYBv1.0 or PYBv1.1). Several firmware versions are
created with distinct functionalities for each Pyboard version.
Step 5: Download the latest stable firmware file for Pyboard having extension ’.dfu’.
The ’dfu-util’ will be used to flash the Pyboard with the new firmware. The installation
process is different for OS like Windows, Linux, etc.
The installation process for Windows OS:
Windows makes it difficult to install non-signed drivers, so use a special program to
install such drivers.
Step 1: Download and install the Zadig utility from https://zadig.akeo.ie/. Open the
utility as shown in Fig. 2.12. Make sure that Pyboard is plugged in and it is in DFU
mode (with P1 and 3.3V shorted).
Step 2: In the Zadig utility, select Options → ‘List All Devices’. From the drop-
down menu, select ’STM32 BOOTLOADER’. Zadig utility recommends replacing
2.2 MicroPython Pyboard Features 37
the ’STTub30’ driver with ’WinUSB’, as shown in Fig. 2.13. If you are unable to see
the installed driver then select WinUSB. Successful installation is shown in Fig. 2.13.
Step 3: From http://dfu-util.sourceforge.net/releases/dfu-util-0.9-win64.zip down-
load the latest release of dfu-util firmware. Unzip the folder and open a command
prompt, as shown in Fig. 2.14. Navigate to the unzipped folder. If the unzipped file
is saved in the Downloads directory, then use the following command (Fig. 2.14) or
give the correct path of saved files:
cd Downloads\dfu-util-0.9-win64\dfu-util-0.9-win64.
To upload the latest firmware to the Pyboard (Fig. 2.11), run the following command:
dfu-util --alt 0 -D
<path to .dfu>
<path to .dfu>
is the complete path of the downloaded Pyboard firmware, as shown in Fig. 2.14
Step 4: If the firmware was successfully uploaded, you should get a message that
ends with ’done parsing DfuSe file’ as shown in Fig. 2.15.
Step 5: To test Pyboard, connect it to USB port of a machine. Open the ’Device
Manager’, and identify the’ Port (COM and LPT)’. If it shows ’USB Serial Device’,
then the driver is installed and working properly, as shown in Fig. 2.16. Manu-
ally install the driver if you see a yellow exclamation mark(!). Right-click on the
’device’ → choose ’Properties’ → choose ’Driver Install’. Select the ’PYFLASH’
drive (Fig.2.17) to launch the file explorer. If you have not removed the Pyboard flash
memory, the ’.inf’ driver should be saved in the flash. Click ’OK’ → ’Next’, and the
driver should be installed. The USB Serial Device listed under Ports (COM & LPT).
Write down or copy the COM name (e.g. COM6), as shown in Fig. 2.16.
Jetson Nano SBC is a powerful AI platform for embedded and machine learning
applications. Applications such as image classification, object detection, and pattern
recognition can leverage the parallel computing capability of the Jetson Nano SBC.
Jetson Nano SBC can be configured in 5 or 10 Watts mode and has a small form
factor as such can be readily used in innovative IoT applications.
The Jetson Nano SBC runs on 64-bit ARM Cortex-A57 quad-core processor at a
clock frequency of 1.43 GHz. The GPU is built on NVIDIA Maxwell architecture
with 128 CUDA cores. The GPU can achieve 472 GFLOPs. The memory on Jetson
Nano consists of 4 GB 64-bit LPDDR4 RAM in conjunction with an eMMC with
16 GB of storage to run Tegra Linux. Jetson Nano SBC is a carrier board consisting
of a 260-pin SODIMM connector in which the Jetson Nano module can be inserted
as shown in Fig. 2.18. The various interfaces embedded on the Jetson Nano module
are made available on the Jetson Nano SBC. These include MIPI-CSI camera socket,
Gigabit Ethernet port, four USB 3.0 sockets, microSD card slot, HDMI and display
port, and Power-over-Ethernet (PoE). The 40-pin header on the SBC offers GPIO,
I2C, I2S, SPI, PWM, and UART. The specification of the Jetson Nano Developer kit
is listed below (NVIDIA 2019):
• ARM Cortex-A57 quad-core processor
• 128 CUDA core GPU based on NVIDIA Maxwell architecture
• 4 GB 64-bit LPDDR4 at 1600 MHz and 25.6 GB/s
2.3 Jetson Nano Board Features 41
• 16 GB eMMC 5.1
• 12 lanes MIPI-CSI-2 D-PHY 1.1
• Gigabit Ethernet
• eDP 1.4 and HDMI 2.0
• M.2 Key E Expansion slot for Wi-Fi
• Interface options—GPIO, I2 C, I2 S, SPI, UART
• MicroSD Storage
The Jetson Nano Developer Kit can be powered by a micro USB 5 V supply with a
current rating of greater than 3A. The supply must be greater than 4.75 V to eliminate
the brownout condition. There is a DC barrel Jack that can be used to supply a 5 V
input and must have a current rating greater than 4A. Make sure that a jumper is put
on the J48 header before you use the DC barrel jack for giving the supply voltage.
The Jetson Nano is by no means a stripped-down version of other NVIDIA offering
such as Jetson TX2, Jetson Xavier, and Jetson AGX. It is designed to leverage the
acceleration offered by the deep learning, graphics, and computer vision libraries
included in the JetPack SDK and Deepstream SDK.
or more for machine learning applications. In addition, we require a micro USB power
supply (5VDC and 3A), keyboard, mouse, and HDMI display.
We require a PC with a built-in SD card reader or an external SD card adapter to
perform read and write operations on the microSD card. The image for the Jetson
Nano Developer Kit can be downloaded from the link: (https://developer.nvidia.com/
jetson-nano-sd-card-image). Make sure you have a free memory of more than 6 GB
on your PC for a successful download. The steps needed to format and flash the image
on to the microSD card will vary based on the OS used. The steps to be followed on
a Windows machine are given below.
Formatting the MicroSD Card
Step 1: SD card formatter is required initially to format the card. Download
it using the link: https://www.sdcard.org/downloads/formatter/sd-memory-card-for
matter-for-windows-download/. After downloading install the software.
Step 2: Select the drive associated with your microSD card as shown in Fig. 2.19.
Step 3: Select the quick format option under formatting options. Leave the volume
label field blank.
Step 4: Finally click the ’format’ button to begin the process.
Once the Jetson Nano is successfully booted for the first time, you will see a
screen with all the keyboard shortcuts as shown in Fig. 2.24.
Fig. 2.25 Jetson nano developer kit without the SODIMM module
Fig. 2.26 Jetson nano development board a) after NIC screwed in place, b) after the SODIMM
module is screwed in place
image of Jetson Nano after the installation. The installation of the NIC is complete and
the Jetson Nano can be now powered up for the first boot with wireless connectivity.
Conclusion:
In this chapter, Python is explored with three different embedded platforms such
as Raspberry Pi, MicroPython Py, and Jetson Nano. Raspberry Pi is single-board
48 2 Configuring Raspberry Pi, MicroPython Pyboard, and Jetson Nano …
computer which acts as mini personal computer. Various Raspberry Pi models are
briefly compared and detailed features of Pi 3 B+ model are discussed. Two different
methods of OS flashing on Pi are given in detail. Also the detailed steps involved to
boot the Pi board first time are provided.
The microPython Pyboard is a small form factor board designed specifically to
run MicroPython to realize various IoT projects. The detailed feature specification
of Pyboard is listed. Configuration of MicroPython Pyboard is not an easy job.
The detailed configuration steps of MicroPython Pyboard are provided starting with
loading the firmware on Pyboard.
Jetson Nano SBC is a powerful AI platform for embedded and machine learning
applications. Here the detailed board specifications are listed. Handling the Jetson
Nano is not trivial and hence, the book provides the detailed steps for board
configuration and setup.
References
Abstract All over the world, people use the Raspberry Pi to learn the coding
skills, build simple projects, implement simple clusters and a little bit of Edge
computing. Raspberry Pi is considered as a low-cost pocket-sized minicomputer used
in various applications. In this chapter, we have focused on hands on implementation
of few simple applications such as, Interfacing LEDs, Organic LED (OLED) display,
Camera Interfacing to capture video/pictures, different types of motor control and
lastly implementation of Bluetooth with mobile .To implement these examples, we
have used Thonny Python IDE which is already integrated with Raspbian OS.
© The Author(s), under exclusive license to Springer Nature Singapore Pte Ltd. 2023 49
J. S. Parab et al., Python Programming Recipes for IoT Applications, Transactions
on Computer Systems and Networks, https://doi.org/10.1007/978-981-19-9466-1_3
50 3 Simple Applications with Raspberry Pi
Fig. 3.1 GPIO pins of physical (BOARD) and BCM. a Actual backside physical pin assignments
and b WiringPi
If the package exists, then there is no need to install; otherwise, it can be installed
using ‘apt-get’.
3.1 Blinking of LED 51
wget https://pypi.python.org/packages/source/R/RPi.GPIO/RPi.
GPIO-.5.11.tar.gz
Step 2: The downloaded file is in ’.tar’ format, so extract the archive to a new folder
by using the following command:
cd RPi.GPIO-0.5.11
Fig. 3.2 a Interfacing of four LEDs with GPIO of Raspberry Pi and b flowchart for LED blinking
# To blink 4 -LED
Fig. 3.3 a PWM signals with 0–100% duty cycle, b circuit for LED interfacing to Raspberry Pi,
and c flowchart of the Python program
3.2 OLED Display Interface 55
To interact with the outside world and make the display more attractive as well as
readable, OLED is interfaced to Raspberry Pi Board. It supports I2C communication
through pins 3 (SDA) and 5 (SCL). Raspberry Pi pin numbers 1 (3.3 V) and 9 (GND)
are used to power up the OLED, as shown in Fig. 3.4. The hardware connection details
are shown in Fig. 3.5.
Figure 3.4 shows the interfacing circuit diagram of SSD1306 I2C OLED display
with the Raspberry Pi. Power supply 3.3 V (pin 1) is connected to VCC pin of the
OLED display. The SDA of the Raspberry Pi is connected to the OLED display’s
SDA pin. The SCL is connected with the OLED display’s SCL pin. The GND of the
Raspberry Pi is connected with the GND pin of the OLED display. The following
steps are required to display message on OLED display.
Step 1: Connect power supply to the Raspberry Pi.
Step 2: I2C set up on Raspberry Pi:
Check if the I2C bus is activated on Raspberry Pi. Click on Raspberry Pi icon(left
side corner) → preferences → Raspberry Pi configuration, as shown in Fig. 3.6a.
After clicking on Raspberry Pi configuration, window will open as shown in Fig. 3.6b.
The configuration window has the System, Display, Interfaces, Performance, and
Localization tabs. Click on the interfaces tab and enabled the I2C (Fig. 3.6b).
Step 3: The I2C activation on Raspberry Pi can also be enabled using the command
terminal. Open the command terminal and enter the following command:
sudo raspi-config
This command will open the Raspberry Pi Software Configuration Tool. Use
arrow keys to scroll down and select Interface options as shown in Fig. 3.7a and
3.2 OLED Display Interface 57
press enter → the pop-up window will open as shown in Fig. 3.7b → then select the
interface options → I2C option as shown in Fig. 3.7c. After this, message will be
displayed as shown in Fig. 3.7d, then click on “yes” to enable I2C → Click “Ok”
followed by clicking on option “Finish” as shown in Figs. 3.7e and 3.6f, respectively.
After enabling the I2C, the next step is to install the libraries required to access
OLED module.
Step 1: Update and upgrade Raspberry Pi with help of the following command:
Step 2: Check the I2C hex address using the following command (Fig. 3.8):
i2cdetect -y 1
import time
import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
# Pi pin configuration:
RST = 24
# These lines are used with SPI:
DC = 23
SPI_PORT = 0
SPI_DEVICE = 0
# library initialization.
disp1.begin()
(a)
(b)
Fig. 3.11 Flowchart for displaying text, rectangle, etc., on OLED display
# ellipse is drawn.
draw.ellipse((x, top , x+shape_width, bottom), outline=255, fill=0)
x += shape_width+padding
# rectangle is drawn.
draw.rectangle((x, top, x+shape_width, bottom), outline=255, fill=0)
x += shape_width+padding
# triangle is drawn.
draw.polygon([(x, bottom), (x+shape_width/2, top), (x+shape_width,
bottom)], outline=255, fill=0)
x += shape_width+padding
# Draw an X.
draw.line((x, bottom, x+shape_width, top), fill=255)
draw.line((x, top, x+shape_width, bottom), fill=255)
x += shape_width+padding
# Display image.
disp1.image(image)
disp1.display()
One can also try the alternate method of picamera installation using Python’s
pip tool. The steps are given below:
Step 1: Enter the following commands to install picamera library by using Python’s
pip tool:
sudo pip install picamera
Step 2: Enter the following command to use the classes in the picamera array module.
sudo pip install "picamera[array]"
Step 3: Enter the following command to upgrade your installation when new releases
are made:
sudo pip install -U picamera
Step 3: Close the Camera Port: To close the port, push the top of the plastic clip
back into the place as shown in Fig. 3.13c.
Step 4: To enable Camera interface on Raspberry Pi: Power up the Raspberry Pi
→ go to the main menu → open the Raspberry Pi Configuration tool (Fig. 3.14a)
→ Select the Interfaces tab and ensure that the camera is enabled (Fig. 3.14b).
Step 5: Testing Camera Module Via the Command Line: Open a terminal window
and enter the following command to take a still picture and save it on the Desktop
(Fig. 3.14c):
raspistill -o Desktop/image.jpg
When the above command runs, you can see the camera preview opens up for 5
seconds before a still picture is taken. Look for the picture file icon on the Desktop,
and double-click to open the picture.
The entire flow diagram for capturing the images using Picamera is shown below
in Fig. 3.15.
Python Code to Capture Image Using Camera Module
The Python “picamera” library allows to control Camera Module.
Step 1: Open a Thonny Python IDE → Create a new file and save it as test_camera.py
or any other suitable name as per your choice but make sure to avoid naming it as
“picamera.py” and enter the code given below.
3.3 Camera Interfacing 65
Fig. 3.14 a,b Camera enabling process on Raspberry Pi. c Terminal window to enter the command
Step 2: Save and run the above program. The camera will take one picture every
5 s. Once the fifth image is taken, check the desktop or filepath to find the captured
images (Fig. 3.16).
#Python Program to Add Text on the Captured Image
Sometimes, it is required to put some text on the captured image. An example of this
can be putting a time stamp on it. Here, we have written the code which captures the
image and puts the desired text on it. The flow diagram for displaying text on camera
captured image is shown in below Fig. 3.17a.
3.3 Camera Interfacing 67
#In this program we will add text “Hello Friends!” on captured image.
After executing the above code in Thonny IDE, the text is imposed on image as
shown in Fig. 3.17b.
#Python Program to Record Video by Using Camera Module
We have already learned how to take images by using camera module. Now, we will
see how to record the video by using the same camera module and executing the
below code in Thonny IDE.
68 3 Simple Applications with Raspberry Pi
(a)
(b)
Fig. 3.17 a flowchart for displaying text on camera-captured image. b Text on picamera captured
image
3.4 Motor Control (DC Motor, Stepper Motor, and Servo Motor) 69
In this section, authors have explored various types of motor interfacing such as DC
Motor, Stepper Motor, and Servo Motor with Raspberry Pi.
DC (Direct Current) Motor Control
The working principle of DC motor is that, when a magnetic field and an electric field
interact, a mechanical force is produced. This is known as motoring action. There
are two types of DC motors, standard and Brushless DC motors.
The DC Motor Speed Control Using the PWM Technique
The speed of DC motor is directly proportional to the supply voltage; if voltage is
reduced from 12 to 6 V, then the speed becomes half that of the original speed of
DC motor. But in practice, for changing the speed of a DC motor we cannot go on
changing the supply voltage all the time. The voltage provided to the DC motor must
be adjusted to control the speed at different torque levels (Weber 1965). The speed of
the DC motor can be controlled by using PWM technique by varying the duty cycle
of applied signals.
As we have already seen how to use PWM in Sect. 3.1 (control LEDs using
PWM). Here, we will directly use the PWM to control the speed of DC motor. GPIO
pin can source a maximum of 15 mA and the sum of currents from all 26 GPIO Pins
should not exceed 50 mA. Raspberry Pi has a provision of +5 V and +3.3 V power
output pins on the board for connecting other modules and sensors. This power rail is
also giving power to the processor. Drawing high current from this power rail affects
the processor. One can draw 100 mA safely from the +3.3 V rail. To avoid this
loading effect, a separate power source is used for DC motor. The motor Driver IC
L293D module is used to drive the motors. L293D is a powerful IC that can control
direction and speed of two DC motors running with supply voltage ranging from 4.5
to 36 V.
70 3 Simple Applications with Raspberry Pi
Truth Table
The truth Table 3.1 provides the logical conditions to rotate motor in either clock-
wise or anti-clockwise direction. Table 3.2 shows the pin connections of motor with
Raspberry Pi having L293 motor driver,
Figure 3.19a and b show the circuit diagram and photo of motor interfaced to
Raspberry Pi, respectively. Figure 3.19c gives the flowchart for controlling DC motor.
Type the below code in Thonny IDE and execute the same.
Fig. 3.19 a Circuit diagram for DC motor interfacing with Raspberry Pi, b hardware connection,
and c flowchart
3.4 Motor Control (DC Motor, Stepper Motor, and Servo Motor) 73
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
Enable1=10
input1=12
input2=18
GPIO.setup(Enable1,GPIO.OUT)
GPIO.setup(input1,GPIO.OUT)
GPIO.setup(input2,GPIO.OUT)
pwm=GPIO.PWM(Enable1,100)
pwm.start(0)
Rotation=input(" Enter c/C for clockwise & a/A for Anticlockwise: ")
duty_cycle=int(input("Enter Duty Cycle from 1 to 100: "))
pwm.start(duty_cycle)
delay=0.1
if Rotation=='c' or Rotation=='C':
try:
while True :
sleep(delay)
exceptKeyboardInterrupt:
pass
74 3 Simple Applications with Raspberry Pi
try:
while True:
GPIO.output(input1,True)
GPIO.output(input2,False)
GPIO.output(Enable1,True)
sleep(delay)
exceptKeyboardInterrupt:
pass
else:
print("Wrong Entry")
pwm.stop()
GPIO.cleanup()
PWM signal will set the angle of the servo. This can differ from servo to servo,
as normally it is from 2.5 to 12.5%. To calculate the duty cycle for the desired angle,
divide it by 18, then add the lowest available value, and, in this case, it is 2.5.
The formula to calculate the duty cycle is as follows:
Duty_cycle = (set_angle/18) + 2.5
So, for 90 degrees, 7.5% duty cycle and, for 180 degrees, 12.5%.
One can easily rotate the arm at a fixed angle by just varying the duty cycle.
Figure 3.20a–c shows the Tower Pro SG90 9G servo Motor interfaced to Raspberry
Pi with duty cycle. Figure 3.20d shows the flowchart for controlling servo motor.
Type the below code in Thonny IDE and execute the same.
# program to control Servo motor
Fig. 3.20 a Tower Pro SG90 9G servo Motor Duty Cycle and PWM Period, b duty cycle with
respect to the angle, c circuit diagram of servo motor interfacing with Raspberry Pi, and d flowchart
for controlling servo motor
3.4 Motor Control (DC Motor, Stepper Motor, and Servo Motor) 77
Stepper Motor
A stepper motor is a device that translates a DC voltage pulse train into a mechanical
rotation of its shaft in a proportionate manner (Fraser 1994). Stepper motor is made
up of mainly two parts, a stator and a rotor. Stator is of coil winding and rotor is
mostly permanent magnet or ferromagnetic material. Stepper motors are generally
used for position control. One of the best things about these motors is that they can be
positioned accurately and one “step” at a time. They are a special type of brushless
motor that divide a full rotation into a number of equal “steps”. They are usually
found in desktop printers, 3D printers, CNC milling machines, and anything else
that requires precise positioning control. The speed of rotation depends upon the rate
at which the control signals (Duty Cycle) are applied. A driver IC ULN2003 is used
to drive the stepper motor as GPIOs of Raspberry Pi are not able to provide sufficient
drive current.
In the stepper motor, continuous and limited angle rotation is obtained by
providing sequential steps. Mostly, there are two step sequences, i.e. full step and
half step used to rotate stepper motor as shown in Fig. 3.21. In the full-step mode,
at a time two coils are excited, while, in the half-step sequence, motor moves half
of its basic step angle. Tables 3.4 and 3.5 tabulate the full-step mode and half-step
mode coil energizing sequence, respectively.
Fig. 3.22 a internal gears of the stepper motor and b pin number and wire colors of the stepper
motor
Table 3.6 tabulates the stepwise sequence of the rotate motor in the clockwise
direction. Figure 3.24 shows (a) circuit diagram, (b) photo of hardware connection,
and (c) flowchart to control stepper motor.
Type the below code in Thonny IDE and execute the same to control stepper
motor.
80 3 Simple Applications with Raspberry Pi
Table 3.6 The stepwise sequence of the rotate motor in the clockwise direction
Motor wire color Sequence to rotate in clockwise direction
Step 1 Step 2 Step 3 Step 4 Step 5 Step 6 Step 7 Step 8
Orange 0 0 1 1 1 1 1 0
Yellow 1 0 0 0 1 1 1 1
Pink 1 1 1 0 0 0 1 1
Blue 1 1 1 1 1 0 0 0
Red 1 1 1 1 1 1 1 1
pins=[10,11,12,13]
# Full step Clock wise sequence reverse can be used for anticlockwise
full_step=[[0,1,1,0],
[1,1,0,0],
[1,0,0,1],
[0,0,1,1]]
# half step Clock wise sequence reverse can be used for anticlockwise
half_step=[[0,1,1,0],
[1,1,1,0],
[1,1,0,0],
[1,1,0,1],
[1,0,0,1],
[1,0,1,1],
[0,0,1,1],
[0,1,1,1]]
3.5 Raspberry Pi and Mobile Interface Through Bluetooth 81
seq=input("Enter f/F for full step & h/H for half Step: ")
print("finished")
82 3 Simple Applications with Raspberry Pi
Fig. 3.24 a circuit diagram of the stepper motor connected with Raspberry Pi, b hardware
connection of motor and Raspberry Pi, and c flowchart of the program
3.5 Raspberry Pi and Mobile Interface Through Bluetooth 83
Step 3: Upgrade to the latest version of bluedot using the following command:
sudo pip3 install bluedot—upgrade
Figure 3.26 shows the circuit diagram of the Raspberry Pi interfaced with Relay
for controlling the appliances.
#Python Program to Control the Appliances Using Bluetooth
import os
from bluedot import BlueDot
bd=BlueDot()
while True:
bd.wait_for_press()
GPIO.output(pin, HIGH) # to turn on the appliance
bd.wait_for_release()
GPIO.output(pin, LOW) # to turn off the appliance
3.5 Raspberry Pi and Mobile Interface Through Bluetooth 85
Fig. 3.26 a Circuit diagram of Raspberry Pi interfaced with Relay and b flowchart for controlling
relay using BlueDot app
After executing the above program in Thonny IDE, open the Blue Dot app on your
smartphone as shown in Fig. 3.27a. After connecting smartphone with Rasberry Pi,
it will show a blue circle as shown in Fig. 3.27b. When the blue circle is pressed, the
appliance connected to GPIO 17 (Physical Pin = 11) through relay will turn on.
Conclusion:
Raspberry Pi is an SBC installed with Raspbian OS. Various peripherals such as
keyboard, mouse, and display are connected to the Raspberry Pi, which makes this
system act as a mini personal computer. Raspberry Pi is popularly used for real-
time Image Video Processing, IoT-based applications, and Robotics applications. In
this chapter, authors have implemented Python code for controlling LEDs by using
simple delay and PWM. Detailed steps involved in accessing the GPIO along with
a connection diagram and execution are given. A simple I2C-based Adafruit SSD
1306 OLED is interfaced with Pi board and is also covered in a simple manner.
For image and video processing, camera interfacing plays an important role. Here,
authors have given the interfacing of CSI camera to Pi board in a simple manner. At
the end, motor control and IoT-based home appliance control using mobile phone is
covered in detail.
86 3 Simple Applications with Raspberry Pi
Exercise:
(1) Connect 5 LEDs to Raspberry Pi. Configure the Raspberry Pi in BCM mode and
write a program to blink LEDs as shown in the below pattern with an interval
of 1 s between each pattern in a continuous cycle.
(2) Interface a switch and a led with Raspberry Pi. When the switch is turned ON
the LED intensity should increase from Low to High and remain High. When
the switch is turned OFF the intensity of LED should reduce gradually from
High to Low and turn OFF(Hint: Refer to the below figure).
(3) Blink Eeight LEDs with different duty cycles using Raspberry Pi.
(4) Display string “Hello World”, integer and floating-point number on OLED
display.
References 87
(5) Interface a couple of stepper motors/DC with Raspberry Pi. Program it to run in
full-step mode in a clockwise direction for 5 s followed by 10 s in anti-clockwise
direction.
(6) Write Python code to control two servo motors.
References
Christ RD, Wernli RL (2014) Power and telemetry. In: Christ RD, Wernli RL (eds) The ROV manual
(Second Edition), Butterworth-Heinemann, pp 141–161. https://doi.org/10.1016/B978-0-08-098
288-5.00007-5
Ferrari M, Ferrari G (2007) Controlling motors. Building robots with LEGO mindstorms NXT,
syngress, 2007, pp 41–59. https://doi.org/10.1016/B978-159749152-5/50008-5
Fraser CJ (1994) 2 - Electrical and electronics principles. In: Smith EH (ed) Mechanical engineer’s
reference book (Twelfth Edition), Butterworth-Heinemann, p 2-1-2-57. https://doi.org/10.1016/
B978-0-7506-1195-4.50006-3
Kaur R, Kaur M, Kaur J (2017) Bluetooth technology. Int J Eng Comput Sci 5(3).http://www.ijecs.
in/index.php/ijecs/article/view/702
Weber HF (1965) Pulse-width modulation DC motor control. IEEE Trans Ind Electron Control
Instrum IECI 12(1):24–28. https://doi.org/10.1109/TIECI.1965.229545
Chapter 4
MicroPython PyBoard for IoT
Abstract In this chapter, readers will learn how to program Pyboard for IoT appli-
cations using MicroPython. MicroPython is a member of the Python interpreter’s
family. MicroPython Programming language is a subset of the Python to fit and
execute on a microcontroller. It has several optimizations to ensure that it works
effectively and consumes little RAM. Hence MicroPython is perfect for embedded
systems and IoT applications. Here, authors have focused on hands on implemen-
tation of few simple IoT applications such as home automation, smart e-waste bin,
industrial environmental monitoring, green house monitoring, and aquaculture moni-
toring. Before implementing the IoT applications, one has to learn how to Install,
run and debug the Micropython using PyCharm IDE.
© The Author(s), under exclusive license to Springer Nature Singapore Pte Ltd. 2023 89
J. S. Parab et al., Python Programming Recipes for IoT Applications, Transactions
on Computer Systems and Networks, https://doi.org/10.1007/978-981-19-9466-1_4
90 4 MicroPython PyBoard for IoT
Nowadays, home automation has become more popular, and smart systems are
being implemented practically in every home. Home automation, often referred to as
“Smart Home Technology”, which uses technology to automate your home. Home
automation allows you to control almost every aspect of your home through the IoT.
The concept of a smart home has gained popularity in recent years as technology made
life easier (Majeed et al. 2020). Almost everything has gone digital and automated
(Stolojescu-Crisan et al. 2021). The IoT concept, conceptualizes the idea of remotely
connecting and monitoring real-world items (things) over the internet (Madhesh et al.
2020). One of the most touted benefits of home automation is providing peace of mind
to homeowners, allowing them to monitor/control their homes remotely, countering
dangers such as forgetting to turn off geyser or a front door left unlocked or lights/fan
left on. Figure 4.1 shows the basic smart home system.
4.1 Home Automation 91
Here, the authors implemented home automation system with two approaches,
first one with Pyboard and second one with ESP32 as IoT device, using Blynk app.
The reason for using ESP32 is that Pyboard is not included in Blynk app to remotely
control the appliances. Here, four electrical appliances such as fan, refrigerator, lights,
and air condition (AC) are controlled using IoT device. This system has ESP32
configured as IoT device to remotely control the above appliances. The electrical
appliances in the house are connected through relay module to Pyboard/ESP32. The
OLED display interface is used to show the status of appliances connected to the
Pyboard/ESP32.
Here, a 0.96-inch OLED SSD1306 display is used. The OLED display will
communicate with the Pyboard/ESP 32 via the I2C protocol.
First Approach:
Home Appliances Control with Pyboard:
The four electrical appliances (fan, AC, refrigerator, and light) will be controlled
by Pyboard through the relay as shown in Fig. 4.2a. Figure 4.2b shows the entire
flowchart for controlling the four appliances. Table 4.1 tabulates the connection of
Pyboard with switches, OLED, and relays.
Implementation and Configuration Steps:
Step 1: Copy MicroPython OLED ssd1306 library from the following link:
https://github.com/micropython/micropython/blob/master/drivers/display/ssd
1306.py
Step 2: Open PyCharm IDE → paste the code in PyCharm IDE and save it as
ssd1306.py.
92 4 MicroPython PyBoard for IoT
Fig. 4.2 a Circuit diagram of home automation and b Flowchart for controlling four devices
4.1 Home Automation 93
Step 3: Make sure the connections are according to Fig. 4.2 and enter the following
code in ’main.py’:
# Program to control home appliances
import time
import ssd1306
import machine
from pyb import Pin
from micropython import const
width = const (128)
height= const (64)
ssd1306_scl= Pin('X9', Pin.OUT_PP)
ssd1306_sda= Pin('X10', Pin.OUT_PP)
i2c_ssd1306=machine.I2C(scl=ssd1306_scl, sda=ssd1306_sda)
oled = ssd1306.SSD1306_I2C(width, height, i2c_ssd1306)
oled.fill(0)
p_in1 = Pin('X1',Pin.IN, Pin.PULL_UP)
p_in2 = Pin('X2', Pin.IN, Pin.PULL_UP)
p_in3 = Pin('X3', Pin.IN, Pin.PULL_UP)
p_in4 = Pin('X4', Pin.IN, Pin.PULL_UP)
p_out1 = Pin('Y1', Pin.OUT, Pin.OUT_PP )
p_out2 = Pin('Y2', Pin.OUT_PP)
p_out3 = Pin('Y3', Pin.OUT_PP)
p_out4 = Pin('Y4', Pin.OUT_PP)
while (1):
oled.fill(0)
if p_in1.value() == False:
p_out1.high()
oled.text('AC-ON', 0, 0)
oled.show()
else:
p_out1.low()
oled.text('AC-OFF', 0, 0)
oled.show()
if p_in2.value() == False:
p_out2.high()
oled.text('Fan-ON', 0, 10)
oled.show()
else:
p_out2.low()
oled.text('Fan-OFF', 0, 10)
oled.show()
if p_in3.value() == False:
p_out3.high()
oled.text('Refrigerator-ON', 0, 20)
oled.show()
94 4 MicroPython PyBoard for IoT
else:
p_out3.low()
oled.text('Refrigerator-OFF', 0, 20)
oled.show()
if p_in4.value() == False:
p_out4.high()
oled.text('Light-ON', 0, 30)
oled.show()
else:
p_out4.low()
oled.text('Light-OFF', 0, 30)
oled.show()
time.sleep(0.5)
(2) Edit the boot file (boot.py) of ESP32: File → open → Select ’MicroPython’ →
open ’boot.py’ and paste the below code and save it.
(3) Enter the below code in ’main.py’ file with correct authentication token received
over email.
The flowchart for home automation system using Blynk app is shown in Fig. 4.4a.
Run the ’main.py’ program and the obtained output is shown in Fig. 4.4b.
In the present days, a rapid increase in urbanization and per capita income has led to
an increase in municipal solid waste generation. Society creates an unhygienic envi-
ronment for its citizens with respect to waste generation. This rapid generation of
waste leads to various infectious diseases in the environment. A smart waste manage-
ment (SWM) system ensures real-time monitoring of collection and transportation of
waste. The SWM ensures that waste is collected on time and that the cost of entire
operation is kept to a minimum (Zeb et al. 2019). To deal with various sorts of waste,
including biological, industrial, and home waste, a variety of techniques are used
(Rahman et al. 2020). Technologies such as global positioning system (GPS), radio
frequency identification (RFID), global system for mobile communications (GSM),
machine-to-machine (M2M) communication, and IoT, as well as innovative mobile
4.2 Smart e-waste Bin 97
Fig. 4.4 a Flowchart for home automation system using Blynk app and b Output status of home
appliance
98 4 MicroPython PyBoard for IoT
and web-based applications, can be used to improve and smoothen the ground-level
mechanism for waste collection, processing, and recycling. Hence, the authors have
implemented a smart e-waste bin using an IoT platform, which makes waste manage-
ment convenient and very efficient. Here, Pyboard with ESP 8266 is configured as
IoT node which updates the bin’s current status, whether the bin is empty or full, on
to the ThingSpeak cloud.
The smart bin system has a proximity sensor FC-45 for checking the status of
bin, ESP8266 for WiFi connectivity, and OLED to display the bin status. All these
modules are interfaced to Pyboard as shown in Fig. 4.5.
The proximity sensor uses an infrared (IR) transmitter and a receiver to detect
an object. Here, three IR sensor modules are used to give the status of the bin. The
output of these proximity sensors is connected to the Pyboard pins X1, X2, and X3.
The bottom IR sensor is connected to the X1 pin, middle IR sensor to X2 pin, and
top IR sensor to the X3 pin of Pyboard.
As Pyboard does not have WiFi feature, ESP8266 Serial WiFi Module is used
for WiFi connectivity to upload the status of smart bin on ThingSpeak cloud. The
ESP8266 WiFi Module is a self-contained SOC with an integrated TCP/IP protocol
stack that allows any microcontroller to connect to a WiFi network for accessing
internet. The pin-out details for interfacing Pyboard with 8266 WiFi model are given
in Table 4.3.
After successful hardware setup, the next step is configuration and code
implementation.
Implementation and configuration:
4.2 Smart e-waste Bin 99
Table 4.3 ESP8266 WiFi module pin description and pin connection with Pyboard
8266 Pin Description Pyboard pins
VCC Power pin = 3.3v 3.3 V
GND Ground GND
Rx Receive serial data from another device Tx
Tx Transfer serial data to other devices Rx
CH_En Chip enable pin, connected to 3.3 V 3.3 V
GPIO 0 General-purpose input–output pin used as a normal GPIO pin and Not connected
also used to enable the ESP8266 programming mode
GPIO 2 Used as a GPIO pin Not connected
(1) In this application, smart bin status is sent to ThingSpeak. Hence, authors have
named the channel ’Smart Bin’. More than one field of data can be activated
by checking the box next to Field option. The authors have created one field,
namely ’Bin status’ (Fig. 4.7). After this, click on ’save channel’ button to save
the details.
(2) Add widget by clicking on ’Add widget’ and select ’Gauge’ → click on ’Next’
→ enter the information → Gauge created as shown in Fig. 4.8.
Fig. 4.10 a pywifi library details and b Flowchart for e-waste bin
4.2 Smart e-waste Bin 103
Fig. 4.11 ThingSpeak write channel feed access and channel status
while 1:
rst_pyb = Pin('X11', Pin.OUT)
rst_pyb.low()
pyb.delay(20)
rst_pyb.high()
pyb.delay(500)
wifi_mode = 3
Pyboard_wifi.set_mode(wifi_mode)
pyb.delay(50)
Pyboard_wifi.connect(ssid='AndroidAP93E9', psk='uxbm0411')
pyb.delay(50)
oled.fill(0)
oled.text('WiFi Connected', 0, 0)
oled.show()
pyb.LED(4).on() #BLUE LED ON
Pyboard_wifi.start_connection(protocol='TCP',dest_ip='184.106.153.149',
dest_port=80, debug=True)
if p_in1.value() == True and p_in2.value() == True and p_in3.value() == True:
oled.text('Bin is Empty', 0, 10)
bin='0'
oled.show()
if p_in1.value() == False and p_in2.value() == True and p_in3.value() == True:
oled.text('Bin is less than Half', 0, 10)
bin='33'
oled.show()
if p_in1.value() == False and p_in2.value() == False and p_in3.value() == True:
oled.text('Bin is Half', 0, 10)
bin='66'
oled.show()
if p_in1.value() == False and p_in2.value() == False and p_in3.value() == False:
oled.text('Bin is Full', 0, 10)
bin='100'
oled.show()
#Copy write channel feed from Thingspeak website as shown in figure 4.11
Pyboard_wifi.send('GET
https://api.thingspeak.com/ update? api_key=HKFRK1JCH5BMOCQ8&field1= '
+ str(bin) + ' HTTP/1.0\r\nHost:192.168.43.176\r\n\r\n', debug=True)
pyb.delay(1000)
4.3 Industrial Environmental Monitoring 105
After entering the above code, one has to run the ’main.py’ program and the status
of the bin will be displayed on OLED display as well as on ThingSpeak cloud shown
in Fig. 4.12.
Environmental monitoring is essential for protecting both human health and the
ecosystem. Industries are the backbone of today’s contemporary society, allowing for
mass production of commodities to meet the needs of an ever-increasing population.
Unfortunately, industrial pollution has a detrimental impact on the planet we live
in. Hence, it’s our responsibility to give our next generation a greener world for a
better life. Governments around the globe have created norms and regulations to
monitor the industries and to keep the pollution under check. Routine environmental
monitoring data can also be used to validate and compare results about chemical
behavior based on laboratory or field investigations (Artiola and Brusseau 2019).
106 4 MicroPython PyBoard for IoT
In this application, the authors have named the channel ’Industrial Environmental
Monitoring’ and seven fields are created as shown in Fig. 4.14. After this, click on
’save channel’ button to save the details.
Obtain the API Key
To send data to ThingSpeak, a unique API key is required, which is used in ’main.py’
code to upload parameters onto the ThingSpeak server. Navigate to ’API Keys’ header
under the newly created channel to get the unique API key. After completing these
steps, the channel is ready to receive the parameters. Enter the below code in ’main.py’
and save it on Pyboard. The flowchart for industrial environmental monitoring system
is shown in Fig. 4.14b.
(a)
Fig. 4.14 a Channel settings and b Flowchart for Industrial Environmental Monitoring system
108 4 MicroPython PyBoard for IoT
(b)
Pyboard_wifi.send('GET
https://api.thingspeak.com/update?api_key=7RZ93ORH43F2RYP7&field1=' +
str(temp)+'&field2='+ str(hum) +'&field3='+ str(Out1)+'&field4='+
str(Out2)+'&field5='+ str(Out3)+'&field6='+ str(Out4)+'&field7='+ str(Out5)+'
HTTP/1.0\r\nHost: 192.168.43.176\r\n\r\n', debug=True)
pyb.delay(1000)
After entering the above code, one has to execute the ’main.py’ program and the
industrial environmental parameters will be displayed on OLED display as well as
on ThingSpeak cloud as shown in Fig. 4.15.
Flora such as flowers and vegetables are grown in a greenhouse. Greenhouses warm
up during the day as sunlight passes through them, heating the plants, soil, and
structure. Greenhouses protect crops from a variety of illnesses, notably these are
soil-borne and splash onto plants in the rain. The greenhouse effect is a natural
phenomenon that is advantageous to humans. To achieve optimal plant growth, the
system must be continuously monitored and managed, for example, temperature,
moisture, soil humidity, light intensity, and so on (Environmental and Pollution
Science 2019). Many farmers fail to profit from greenhouse crops because they
are unable to control two critical elements that influence plant development and
output. The temperature of the greenhouse should be maintained at a specified level.
Crop transpiration and condensation of water vapor on various greenhouse surfaces
can all be caused by high humidity. This greenhouse monitoring and management
system comes to the rescue in the face of such obstacles. The design and implemen-
tation of several sensors for greenhouse environment monitoring, such as humidity,
temperature, light condition, and soil moisture, are discussed here.
The authors have implemented greenhouse monitoring systems using IoT. The
designed system monitors parameters such as temperature, humidity, water level,
and light. The pin-out details for interfacing Pyboard with sensors are given in Table
4.5. The temperature and humidity sensors detect temperature and humidity, the soil
moisture sensor detects water level, and the LDR sensor detects light. The detailed
interfacing diagram is shown in Fig. 4.16.
Configuration of ThingSpeak Channel
After creating the channel as shown in earlier Sect. 4.2, click on ’New Channel’
button which will subsequently ask for the ’Name and Description’ of the data you
want to upload on this channel as shown in Fig. 4.17.
In this application, the authors have named the channel ’Greenhouse Moni-
toring’ and four fields are created as shown in Fig. 4.17a. After this, click on ’save
channel’ button to save the details. Figure 4.17b shows the flowchart for greenhouse
monitoring system.
(a)
Fig. 4.17 a Channel settings and b flowchart for greenhouse monitoring system
4.4 Greenhouse Monitoring 113
(b)
import pywifi
temp_hum = dht.DHT22(Pin('X1'))
while 1:
rst_pyb = Pin('X11', Pin.OUT)
rst_pyb.low()
pyb.delay(20)
rst_pyb.high()
pyb.delay(500)
Pyboard_wifi = pywifi.ESP8266(1, 115200)
wifi_mode = 3
Pyboard_wifi.set_mode(wifi_mode)
pyb.delay(50)
Pyboard_wifi.connect(ssid='AndroidAP93E9', psk='uxbm0411')
pyb.delay(50)
pyb.LED(4).on() #BLUE LED ON
pyb.delay(2000)
temp_hum.measure()
temp = temp_hum.temperature()
hum = temp_hum.humidity()
LDR = pyb.ADC('X2')
Out1 = LDR.read() # read an analog value
Moisture = pyb.ADC('X3') # create an analog object from a pin
Out2 = Moisture.read() # read an analog value
mois = ((Out2 - 3700) * (100 - 1) / (1300- 3700) +1)
Pyboard_wifi.send('GET
https://api.thingspeak.com/update?api_key=E3SLU3MT69BN8G3V&fiel
d1=' + str(temp)+'&field2='+ str(hum) +'&field3='+ str(Out1)+ '&field4='+
str(mois)+' HTTP/1.0\r\nHost: 192.168.43.176\r\n\r\n', debug=True)
pyb.delay(1000)
After entering the above code, one has to execute the ’main.py’ program and the
greenhouse monitoring parameters will be displayed on OLED display as well as on
ThingSpeak cloud shown in Fig. 4.18.
Table 4.6 Pin connection of Pyboard with sensors for aquaculture monitoring
Pyboard OLED Turbidity pH TDS DS18B20
Y9 SCL – – – –
Y10 SDA – – – –
Y11 – Output of circuit – – –
Y12 – – SIG – –
X7 – – – Aout –
X8 – – – – Sensor output
4.5 Aquaculture Monitoring 117
(a)
(b)
width = const(128)
height = const(64)
ssd1306_scl = Pin('Y9', Pin.OUT_PP)
ssd1306_sda = Pin('Y10', Pin.OUT_PP)
i2c_ssd1306 = machine.I2C(scl=ssd1306_scl, sda=ssd1306_sda)
oled = ssd1306.SSD1306_I2C(width, height, i2c_ssd1306)
import pywifi
while True:
rst_pyb = Pin('X11', Pin.OUT)
rst_pyb.low()
pyb.delay(20)
rst_pyb.high()
pyb.delay(500)
Pyboard_wifi = pywifi.ESP8266(1, 115200)
wifi_mode = 3
Pyboard_wifi.set_mode(wifi_mode)
pyb.delay(50)
Pyboard_wifi.connect(ssid='AndroidAP93E9', psk='uxbm0411')
pyb.delay(50)
pyb.LED(4).on() # BLUE LED ON
pyb.delay(2000)
oled.fill(0)
adc = pyb.ADC('Y12') # Ph sensor
adc1 = pyb.ADC('Y11') #Turbidity sensor
val1 = adc1.read()
adc2 = pyb.ADC('X8') # TDS sensor
val2 = adc2.read()
ds_pin = Pin('X7') # temperaute sensor
sensorSum = 0
for i in (0, 50):
sensorValue = adc.read()
sensorSum = sensorSum + sensorValue
sensorValue = sensorSum / 50
ph = 7 - 1000 * (sensorValue - 372) * 3.3/59.16/4095
scaled_value = (val1 - 60) * (0.2 - 5) / (2200 - 60) + 5
V = val2 * (3.3 / 4095.0)
tdsValue = (133.42 / V * V * V - 255.86 * V * V + 857.39 * V) * 0.5
ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))
roms = ds_sensor.scan()
ds_sensor.convert_temp()
time.sleep(1)
120 4 MicroPython PyBoard for IoT
oled.show()
Pyboard_wifi.start_connection(protocol='TCP',dest_ip='184.106.153.149',
dest_port=80,debug=True)
Pyboard_wifi.send('GET
https://api.thingspeak.com/update?api_key=UBY89DB50V2UPGGU&field1=' + str(ph) +
'&field2=' + str(scaled_value) + '&field3=' + str(tdsValue) + '&field4=' + str(a)
+ ' HTTP/1.0\r\nHost: 192.168.43.176\r\n\r\n', debug=True)
pyb.delay(1000)
After entering the above code, one has to execute the ’main.py’ program and the
greenhouse monitoring parameters will be displayed on OLED display as well as on
ThingSpeak cloud shown in Fig. 4.21.
Conclusion:
PyCharm supports various boards including Pyboard. To start with, MicroPython
installation and testing steps of Pyboard in PyCharm IDE are given in detail. The
various applications, such as, home automation, smart e-waste bin, industrial envi-
ronmental monitoring, greenhouse monitoring and aquaculture monitoring are imple-
mented to track and control remotely. There is no WiFi support on Pyboards; hence,
authors have interfaced external WiFi module. Home automation system is first
implemented with switches and appliances interfaced directly to Pyboard. The same
application is implemented with ESP32 and controlled remotely with Blynk app. For
other applications, the authors have provided detailed interfacing and configuration
steps to monitor/control remotely through ThingSpeak cloud.
Exercise
(1) Blink eight LEDs using Pyboard
(2) Design a system to control six appliances with switches connected to ESP32.
Also, display the status of each appliance on the OLED screen.
(3) Interface a temperature sensor to ESP32. Using a relay, build a system to turn
ON/OFF the fan when temperature rises/decreases of a particular threshold
temperature and monitor the temperature.
(4) Assume there are two rooms, Room1 and Room2. Each room has a temperature
sensor and a fan respectively. Design a system that can monitor the temperature
of each room separately and turn ON/OFF the fan in the respective room if
temperature crosses the threshold.
(5) Using turbidity sensor along with ESP32, build a system to monitor cleanliness
of water. If the value of turbidity is higher than a set threshold, give out an
alarm signaling the need for water replacement/cleaning. The system can then
be applied to monitor cleanliness of water in aquariums.
(6) Interface five IR sensors to get the accurate status of the e-waste bin by placing
appropriately and displaying the status on ThingSpeak Cloud.
(7) Interface other industrial sensors to monitor Environmental parameters.
References
https://doi.org/10.1016/j.jksuci.2020.08.016.
https://doi.org/10.1016/B978-0-12-814719-1.00010-0.
https://doi.org/10.1016/B978-0-12-386454-3.01041-1.
Artiola JF, Brusseau ML (2019) The role of environmental monitoring in pollution science. In:
Brusseau ML, Pepper IL, Gerba CP (eds) Environmental and pollution science, 3rd edn. Academic
Press, pp 149–162. https://doi.org/10.1016/B978-0-12-814719-1.00010-0
Covaci A (2014) Environmental fate and behavior. In: Wexler P (ed) Encyclopedia of toxicology,
3rd edn. Academic Press, pp 372–374. https://doi.org/10.1016/B978-0-12-386454-3.01041-1
Environmental and Pollution Science (Third Edition) (2019) Academic Press, pp 149–162
Madhesh A, Kowsigan M, Khishore R, Balasubramanie P (2020) IoT based home automation and
security system. Int J Adv Sci Technol 29(9s):2733–2739. http://sersc.org/journals/index.php/
IJAST/article/view/15439
Majeed R, Abdullah NA, Ashraf I, Zikria YB, Mushtaq MF, Umer M (2020) An intelligent, secure,
and smart home automation system. Sci Program. ID 4579291, 14 p. https://doi.org/10.1155/
2020/4579291
122 4 MicroPython PyBoard for IoT
Nasir OA, Mumtazah S (2020) IOT-based monitoring of aquaculture system. Matter: Int J Sci
Technol 6(1):113–137. https://doi.org/10.20319/mijst.2020.61.113137
Rahman MW, Islam R, Hasan A, Bithi NI, Hasan MM, Rahman MM (2020) Intelligent waste
management system using deep learning with IoT. J King Saud Univ-Comput Inf Sci. https://doi.
org/10.1016/j.jksuci.2020.08.016
Stolojescu-Crisan C, Crisan C, Butunoi B-P (2021) An IoT-based smart home automation system.
Sensors 21:3784. https://doi.org/10.3390/s21113784
Sujin JS et al (2021) IOT based greenhouse monitoring and controlling system. J Phys: Conf Ser
1916 012062
Zeb A, Ali Q, Saleem MQ, Awan KM, Alowayr AS, Uddin J, Iqbal S, Bashir F (2019) A proposed
IoT-enabled smart waste bin management system and efficient route selection. J Comput Netw
Commun. ID 7043674, 9 p. https://doi.org/10.1155/2019/7043674
Chapter 5
FoG and Cloud Computing with Jetson
Nano
Abstract FoG and cloud paradigms are helping the IoT devices to offload the
computing in order to reduce power consumption and increase battery life. The
Jetson Nano SBC is an exciting platform which can act as a FoG node and as an
intermediate layer between the cloud and IoT devices. The Jetson Nano module is a
small Artificial Intelligence (AI) computer that has the performance and power effi-
ciency needed to run modern AI workloads, multiple neural networks in parallel and
process data from several sensors simultaneously. The ongoing chapter introduces
the concepts of FoG and Cloud computing along with its role in IoT. At the end, this
chapter covers implementation of FoG and Cloud computing.
Keywords FoG · Cloud · Patient monitoring · Home security · Fire alert system ·
Home safety lock · Surveillance
Cloud computing has gained widespread acceptance due to the services such as
data storage, computing, etc. offered by it at a very affordable cost. To achieve this,
centralized data centers are offered by big players. More businesses are able to adopt
cloud computing for their operations due to the ready adaptability and reduced cost of
cloud services (Chiang and Zhang 2016). This offloads a lot of workload and in turn
increases productivity (Khan et al. 2017). To keep up with the market needs, cloud
computing was able to evolve and offers better performance, security, and reliability.
Today, cloud is able to offer various models for services such as Infrastructure as a
Service (IaaS), Platform as a Service (PaaS), and Software as a Service (SaaS).
The world has seen an unprecedented rise in devices connected to the Internet over
the decade. According to Cisco system inc, there are around 50 billion connected
devices (Evans 2011). Naturally, there is high demand for quality cloud services
and IoT devices. Since the overall operation of cloud data servers is centralized,
it proves to be a hindrance for IoT devices spread over a large geographic area.
Problems such as network congestion and high latency are common occurrences in
such scenarios. FoG computing is an extension of the existing cloud platform which
© The Author(s), under exclusive license to Springer Nature Singapore Pte Ltd. 2023 123
J. S. Parab et al., Python Programming Recipes for IoT Applications, Transactions
on Computer Systems and Networks, https://doi.org/10.1007/978-981-19-9466-1_5
124 5 FoG and Cloud Computing with Jetson Nano
was put forward by Cisco to resolve the issue faced and elaborated on above. Cisco
was the first player to formally put forward the concept of FoG computing (Tang
et al. 2015). An additional layer called the FoG layer is introduced in between the
cloud data centers and the end-users. In principle, this layer is physically much closer
to the end-user or the IoT nodes. It helps to achieve the decentralization of the data
centers.
FoG computing together with cloud computing and IoT devices (End devices)
forms three-tier architecture as shown in Fig. 5.1 (Taneja and Davy 2016). The
bottom Tier 1 consists of the End /IoT devices which are the generators of the
data. The IoT devices are equipped with sensors that generate the raw data. Tier
2 is the FoG computing layer. This computing layer generally consists of devices
such as gateways, routers, switches, etc. which can provide processing and storage
of information. Hence, this layer is sometimes referred to as FoG intelligence. If
the devices which form the part of the FoG computing layer wishes to send data
to the cloud they can do so on a periodic basis. In FoG computing architecture,
not every data packet is redirected to the cloud, instead all real-time analysis and
latency sensitive applications have a dependency to run from the FoG layer itself.
Tier 3 is named as cloud computing layer, also referred to as the cloud intelligence,
which leverages modern infrastructure such as data centers to provide enormous
storing and processing capabilities to the lower layers.
Dastjerdi et al. defined FoG computing as a distributed computing environment
extending the services of cloud computing to one-hop distance from the user (Dast-
jerdi et al. 2016). FoG network consists of multiple FoG nodes, which communicates
with one another and also in sync with the cloud data center. FoG computing and cloud
computing work together to enhance the quality and performance of any system. In
this type of computing, one utilizes a network of FoG nodes which are placed at just
one leap from the end-user or IoT device. This allows the local data processing on
the FoG node rather than on the cloud data center which may be placed geographi-
cally at a distant location. In addition to data processing, FoG node may also provide
networking between the end-users (edges) and the cloud data centers. The features
like communication, mobility, and computing offered by FoG node aim to provide
low latency.
FoG Computing Essential Characteristics
This section briefly discusses the essential characteristics of FoG computing. If
a device possesses storage, computing capacity, and network connectivity, it can
operate as a FoG node. They can be deployed anywhere within a network. Examples
of such devices are routers, switches, embedded servers and industrial controllers,
and cameras for video surveillance (Atlam et al. 2018). FoG Computing in general
should have the below characteristics:
• Location awareness and low latency: FoG computing features location awareness
and can be set up in any location. As FoG nodes are closer to the end devices,
FoG computing gives low latency operation (Atlam et al. 2018).
• Scalability: One of the main features of FoG computing is distributed computing
and storage networks which work as end devices/sensor networks. Additionally,
FoG computing supports the ready addition of new end devices/sensor networks
(Atlam et al. 2018).
• Geographical distribution: Services and applications given by FoG nodes are
distributed and can be deployed anywhere, in contrast to the services of centralized
data servers.
• Heterogeneity and Interoperability: FoG nodes or end devices are designed by
different manufacturers but still possess the ability to work on different platforms
and across different service providers (Atlam et al. 2018).
• Real-time interactions: FoG computing gives a real-time interaction between FoG
nodes rather than the batch processing model provided by the cloud (Atlam et al.
2018).
126 5 FoG and Cloud Computing with Jetson Nano
• Support for mobility: Mobile devices can be connected directly in FoG applica-
tions (Atlam et al. 2018).
In FoG computing, the facilities provided by the data centers of cloud computing
are taken to the edge of the network. FoG nodes offer limited computing, storing,
and networking capabilities. These capabilities are provided in a distributed manner
that spans the Edge/IoT devices and the traditional cloud data centers. Here the sole
objective is to give minimal latency for time-sensitive application running on the
Edge/IoT devices (Atlam et al. 2018; Shi et al. 2015).
The architecture for FoG is made up of six layers as shown in the figure 5.2
(Mukherjee et al. 2018; Aazam and Huh 2014, 2015; Muntjir et al. 2017) namely,
physical and virtualization, monitoring, pre-processing, temporary storage, security,
and transport layer.
The lowest layer is the physical and virtualization layer and is made up of virtual
sensor networks along with physical and wireless sensor networks. In this layer,
myriad types of sensors are geographically distributed for environment sensing which
generates raw data. This raw data is passed to the next layers via gateways for further
processing and filtering (Liu et al. 2017).
The next layer is the monitoring layer which monitors the resource utilization
and the availability of FoG nodes along with the other network elements. This layer
is also responsible for monitoring services, performance, power consumtion, and
tracking the status of applications running on FoG nodes (Mukherjee et al. 2018;
Aazam and Huh 2015).
Preprocessing and temporary storage layer handles the tasks of data management.
The data forwarded by the lower layers are analyzed, trimmed, and filtered to extract
meaningful information. This preprocessed data in the below layer is then required
to be temporarily stored in the next layer (Aazam and Huh 2015; Muntjir et al. 2017).
The encryption/decryption of the data on the FoG nodes is handled at the security
layer. Additional integrity mechanism is also applied to the data to protect the data
from being tampered.
The topmost layer of the architecture is the transport layer where the data is
uploaded to the cloud to generate more useful services (Aazam and Huh 2015; Muntjir
et al. 2017). As the FoG nodes are constrained by limited resources, the protocols
used for communication on the FoG nodes have to be lightweight, efficient, and
customizable (Aazam et al. 2014; Marques et al. 2017).
5.3 Introduction to Cloud Computing 127
resources needs. The rapid elasticity gives the users/customers an impression of the
availability of unlimited resources.
Metering service: A metering capability is incorporated by the service provider
in order to automatically control and optimize the resource utilization. The metering
is done for the resources such as bandwidth, storage, processing, etc. This provides
transparency to both the consumer and service provider.
Cloud Computing Service Models
The services provided by cloud computing have evolved into three typical service
models, namely, (1) Infrastructure as a Service, (2) Platform as a Service, and (3)
Software as a Service as shown in Fig. 5.3. One finds a hierarchical organization
of these top three models based on the level of abstraction of the capabilities made
available by individual layers (Kumar and DuPree 2011).
Infrastructure as a Service (IaaS): In the IaaS scheme, a consumer acquires
processing, storage, networking, and other resources from the service provider. Here,
the consumer is free to utilize the acquired resources at his discretion by running or
deploying arbitrary software of his choice which can include applications and oper-
ating systems. In IaaS, the service provider gives to the consumer a large number
of virtual resources by dividing a very large physical resource of the infrastruc-
ture (). The most popular IaaS service providers in the market are IBM, Microsoft,
Rackspace, NTT, Oracle, Fujitsu, and Amazon (Marston et al. 2011).
Platform as a Service (PaaS): In PaaS cloud model, a user/consumer develops
applications to run on the cloud infrastructure. The applications are developed using
programming languages, services, tools, and libraries supported by the provider
(Mell and Grance 2011). The user is not given the control or management of the
cloud infrastructure, but given the control of configuration settings for the application
hosting environment and applications. A PaaS provides a cloud developer freedom
to design, build, test and deploy custom applications (). Leading providers offering
PaaS are Amazon, Microsoft, Alibaba, Google, IBM, and Rackspace (Voorsluys et al.
2011).
5.4 Cloud Computing Architecture 129
Software as a Service (SaaS): As the name suggests the user/consumer uses the
applications provided by the cloud service provider. These applications are running
on the cloud infrastructure. The applications are accessed using a client device
running a thin client interface such as a browser or a program interface. The manage-
ment and control of the cloud infrastructure are not done by the user/consumer, while a
limited user specific application configuration setting is controlled by the user (Mell
and Grance 2011). Therefore, user/consumer enjoys abstraction from all the finer
details of the current applications running behind the scenes (Arasaratnam 2011; ).
Leading SaaS providers are Salesforce, Microsoft, SAP, Oracle, Adobe Systems, and
IBM.
Deployment Models
There are four different deployment models such as Private, Public, Community, and
Hybrid cloud.
Private cloud: The infrastructure is exclusively reserved for use by the single orga-
nizations who’s services are given to a large number of consumers, namely, business
units. The cloud infrastructure may be housed on the premises of the organization or
off premises (Mell and Grance 2011).
Community cloud: The infrastructure is exclusively reserved for a particular
community of users from organizations that have common concerns. The cloud
infrastructure may be housed on the premises of the organization or off premises
(Mell and Grance 2011).
Public cloud: The infrastructure is not reserved for specific users and is open to
the general public.
Hybrid cloud: Here, two or more distinct cloud deployment models such as
community, public, or private are combined together. However, the individual entities
remain unique.
The cloud architecture is split into two parts, namely, frontend and backend. Fig. 5.4
depicts an internal architectural view of cloud computing.
Cloud computing architecture consists of client infrastructure, application,
service, runtime, storage, infrastructure, administration, and security (ITCandor
2018).
Frontend: The cloud architecture’s frontend refers to the cloud’s client infrastruc-
ture, which includes all user interfaces and applications.
Backend: Backend refers to the cloud itself which is used by the service provider.
It controls resources and implements security measures. It also contains massive
storage, virtual applications, virtual computers, traffic management techniques,
deployment models, and so on.
130 5 FoG and Cloud Computing with Jetson Nano
The advent of IoT has seen smart devices permeating every home, vehicle, and
workplace with connectivity to the Internet. Due to technological advancements, the
things which surround us are potentially getting connected to the Internet and hence
there is a huge stress on the current Internet and cloud infrastructure in place.
The conventional approach is to utilize a centralized cloud for processing which
is localized at one site. But due to the proliferation of IoT devices, the sheer amount
of data generated by these devices is increasing day by day. The existing cloud
infrastructure will be severely strained by this massive explosion of data [29].
FoG computing allows computing to happen via IoT devices and only pushes
relevant data to the cloud. The FoG brings the cloud closer to the objects that generate
and act on IoT data. The FoG nodes can be deployed anywhere with a network
between cloud and IoT devices. They analyze the most time-sensitive data at the
network edge instead of sending vast amounts of IoT data to the cloud. Only required
data can be sent to the cloud for historical analysis and longer term storage. It is very
important to point out that FoG does not eliminate the cloud but complements it to
improve the efficiency (Schneider and Sunyaev 2016).
The FoG and cloud computing has become ubiquitous in the modern computing
scenario. On a regular basis, FoG and cloud service providers are adding exciting
features to the existing platforms. Here, authors have brought out the salient features
of both the paradigms especially relating to IoT with various applications. Under
cloud computing, authors have implemented a Patient Monitoring system to monitor
Oxygen Saturation, Pulse, and Body Temperature. FoG computing is leveraged for
Home Security in which Home Surveillance, Home Safety Lock, and Fire Alert
systems are implemented.
In modern society, the incidence of chronic diseases has increased due to different risk
factors such as dietary habits, physical inactivity, alcohol consumption, etc. It is said
that in the next 10 years, deaths from chronic diseases will increase by 17%. If these
diseases are not monitored and treated early, they can lead to several complications
and even pose threat to life. Therefore, monitoring of health parameters is of utmost
importance (Schneider and Sunyaev 2016).
132 5 FoG and Cloud Computing with Jetson Nano
This application demonstrates the Patient Monitoring system. Here, various health
parameters such as Oxygen Saturation (SpO2), Pulse Rate, and Body Temperature
are monitored using appropriate sensors. The main heart of this system is Jetson Nano
SBC, which is interfaced with these sensors, as shown in Fig. 5.5. The Oxygen Satu-
ration and Pulse Rate are obtained from the MAX30102 sensor which is interfaced
with the Jetson nano SBC using I2C. The Body Temperature is obtained using the
MLX90614 IR Temperature Sensor which is also interfaced using I2C. The health
parameters are updated to ThingSpeak Cloud for monitoring from any geographic
location. ThingSpeak is an open IoT platform for monitoring patients data online.
Table 5.1 tabulates the pin connection of MAX30102 and MXL90614 with Jetson
Nano.
MAX30102 is an integrated pulse oximetry and heart-rate sensor. It integrates two
LEDs (IR and Red), a photodetector, optimized optics, and low-noise analog signal
processing to detect pulse oximetry and heart-rate signals. It is fully configurable
through software registers, and the digital output data is stored in a 32-deep FIFO
within the device. It also has an ambient light cancellation (ALC), 18-bit sigma delta
ADC, and discrete time filter. It has an ultra-low-power operation which makes it
ideal for battery operated systems. MAX30102 operates within a supply range of 1.7
to 2 V. The same sensor can be used for various applications such as fitness assistant,
wearable devices, etc.
There are two I2C ports available on the J41 Header of the Jetson Nano SBC.
Here, authors have used the I2C-1 for both the sensors as shown in Fig. 5.5. Pin 3
and Pin 5 of J41 header is SDA and SCL of I2C-1, respectively. One can test if the
I2C device is properly connected by using the below command.
sudo i2cdetect –y –r 1
# heartrate_monitor.py
from max30102 import MAX30102
import hrcalc
import threading
import time
import numpy as np
class HeartRateMonitor(object):
"""
A class that encapsulates the max30102 device into a thread
"""
LOOP_TIME = 0.01
def run_sensor(self):
sensor = MAX30102()
ir_data = []
red_data = []
bpms = []
if len(ir_data) == 100:
bpm, valid_bpm, spo2, valid_spo2 = hrcalc.calc_hr_and_spo2(ir_data,
red_data)
if valid_bpm:
bpms.append(bpm)
while len(bpms) > 4:
bpms.pop(0)
5.6 Examples of FoG and Cloud Computing 135
self.bpm = np.mean(bpms)
if (np.mean(ir_data) < 50000 and np.mean(red_data) < 50000):
self.bpm = 0
if self.print_result:
print("Finger not detected")
if self.print_result:
print("BPM: {0}, SpO2: {1}".format(self.bpm, spo2))
self.print_spo2 = spo2
self.print_bpm = self.bpm
time.sleep(self.LOOP_TIME)
sensor.shutdown()
def start_sensor(self):
self._thread = threading.Thread(target=self.run_sensor)
self._thread.stopped = False
self._thread.start()
Fig. 5.6 Name and description of channel for Patient Monitoring system
After completing these steps the channel is ready to send the body parameters.
Fig. 5.8 gives the flow chart of Patient Monitoring System with Cloud.
Step 4:
Create a file ’main.py’ in the project folder with the below code.
print('sensor starting...')
hrm = HeartRateMonitor(print_raw=args.raw, print_result=(not args.raw))
hrm.start_sensor()
try:
time.sleep(args.time)
except KeyboardInterrupt:
print('keyboard interrupt detected, exiting...')
hrm.stop_sensor()
print('sensor stoped!')
print(hrm.print_bpm,hrm.print_spo2)
bus1 = smbus.SMBus(1)
sensor = MLX90614(bus1, address=0x5a)
print ("bodyTemperature :", sensor.get_object_1())
body_temp = sensor.get_object_1()
bus1.close()
print(response.status,response.reason)
data = response.read()
conn.close()
In the Heart Rate and SpO2 section of the code, authors instantiate an object of
the class ’HeartRateMonitor’ found in ’heartrate_monitor.py’. The thread is initiated
and terminated by calling ’start_sensor’ and ’stop_sensor’, respectively. When the
thread is active one can access ‘bpm‘ to get beats per minute (BPM). The user has
to wait for a few seconds to get a reliable BPM value. Also, make sure that there is
minimum movement as the sensor is very sensitive to it. The program is designed in
such a way that a keyboard interrupt (CNTRL + C) can stop the sensor and transit
to the next section of the code. The last reading for Pulse rate and Oxygen saturation
is written into ’hrm.print_bpm’ and ’hrm.print_spo2’ variables, respectively, which
are then sent to ThingSpeak Cloud.
Step 5: To run the above ’main.py’ use the below command.
python3 main.py
The output after execution is shown on the terminal as in Fig. 5.9 and also the
data visualization on the ThingSpeak server is shown in Fig. 5.10.
Certainly, the advent of the IoT and FoG computing paradigm has made it available for
user to efficiently approach the problem of Home security. The authors have leveraged
5.6 Examples of FoG and Cloud Computing 139
various security hardware such as PIR sensors, Surveillance cameras, Smart locks,
etc. to safeguard property and human life from potential life threatening situations.
In this section, authors have implemented Home Surveillance, Home Safety Lock,
and Fire alert system. The programming of the IoT nodes is done by Thonny IDE
using MicroPython programming as detailed in below steps.
Step 1: Download the Thonny IDE for windows from the link: https://thonny.org
Step 2: Download ESP 32 Firmware from the link https://micropython.org/dow
nload/esp32/
Step 3: Install appropriate MicroPython Firmware on ESP32:
140 5 FoG and Cloud Computing with Jetson Nano
Home Surveillance system consists of IoT device ESP32-CAM having PIR sensor
interfaced to it. IoT device communicates with Jetson Nano SBC which is configured
as FoG node as shown in Fig. 5.11. Intruder activity is sensed by a PIR sensor and an
image of the activity is captured by a camera interfaced with IoT device. The captured
image is sent over Wi-Fi using MQTT protocol to the Jetson Nano SBC. The Jetson
Nano SBC( FoG node) stores the received image. The Jetson upon reception of the
image will intimate the house owner with a prompt WhatsApp message to his mobile
phone using the Twilio cloud python package. The FoG node will also upload the
same image to the Google Drive cloud so that the house owner can monitor the house
in real time. The detailed conceptual and pin diagram are shown in Fig. 5.11. Table
5.2 depicts the pin connection of PIR sensor to ESP32 CAM.
IoT Device (ESP32-CAM)
The ESP32-CAM is a fully-featured microcontroller that has an integrated video
camera and microSD card socket. It is easy to use, and perfect for various IoT
applications requiring a camera with advanced functions like image tracking and
recognition.
Here,2 megapixel camera module OV2640 is used, which gives outputs in various
formats such YUV422, YUV420, RGB565, RGB555, and 8-bit compressed data.
This ESP32-CAM doesn’t have USB port to program hence one has to use an FTDI
adapter.
The IoT device is interfaced with HC-SR 501 (PIR sensor). The PIR sensor is
used to detect animal/human movement. This pyroelectric PIR sensor detects motion
based on emitted infrared radiation.
Programming the ESP32-CAM:
It is important to note that the FTDI adapter must be set for a 5V VCC output with
proper jumper settings, as we are powering the ESP32-CAM using the 5 V as shown
in Fig. 5.12.
To upload the firmware, one has to short GPIO 0 pin to Ground on ESP32 - CAM.
This connection is required only for firmware upload to ESP32-CAM. For switching
to programming mode remove the short between GPIO 0 and ground.
The ESP32-CAM firmware required for this application can be downloaded
from the link https://github.com/lemariva/micropython-camera-driver/tree/master/
firmware. The firmware upload and programming for ESP32-CAM are done using
MicroPython in the Thonny IDE.
In order to configure ESP32-CAM for MQTT protocol, one requires MicroPython
MQTT client(umqtt) and camera package which can be downloaded using the steps
outlined in Sect. 3.2 of Chapter 3. Now the ESP32-CAM is ready to capture the
image and implement the MQTT protocol. Flowchart for configuring ESP32-CAM
module for Home security is shown in Fig. 5.13.
Create a ’main.py’ file in Thonny IDE and enter the below code in it. Then click
the ’run’ button to execute the code. Make sure before running the code, the Wi-Fi
5.6 Examples of FoG and Cloud Computing 143
Fig. 5.13 Flowchart for configuring ESP32s-CAM module for Home security
hotspot created on FoG node is active. Here ESP32-CAM is the client which will
publish the captured image to MQTT topic named as ’Image’.
camera.init(0, format=camera.JPEG)
144 5 FoG and Cloud Computing with Jetson Nano
#Attach external interrupt to GPIO13 and rising edge as an external event source
PIR_Interrupt.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt)
while True:
if Motion_status:
print('Motion is detected!')
buf = camera.capture()
c.publish(TOPIC, buf)
time.sleep_ms(100)
Motion_status = False
One also requires the ’paho-mqtt’ library , which can be installed using the below
command.
sudo apt-get install python3-pip
sudo pip3 install paho-mqtt
The Jetson FoG node is now ready for MQTT protocol communication.
Enabling Wi-Fi Hotspot on FoG node:
To enable the hotspot on the FoG node, select the settings menu on Jetson user
interface. Under setting menu → Select ’Wi-Fi’ → on the right-hand side, hover
over the three horizontal lines ( ) → Turn on Wi-Fi hotspot.
It is not possible to access the Internet over wireless when hotspot is active. Hence
a wired connectivity is provided for the Jetson Nano SBC to connect to the Internet
for uploading the image to Google Drive and sending a message using Twilio cloud.
5.6 Examples of FoG and Cloud Computing 145
Now the system is ready to send and receive notifications on WhatsApp via the
Twilio cloud platform.
Google Drive Configuration
In this implementation, the surveillance image is stored on to Google Drive. To do
this, user need to install the Pydrive Python module using the following command.
pip install pydrive
To upload the image to Google Drive using python, one need an active google
cloud account. User need to get the authentication files for Google Service API, so
that the Python code can access Google Drive. To do that, one need to follow the
below steps.
Step 1: Create a new project in Google Developer Console by clicking
’CREATE PROJECT’ with the proper name of the project. The link for the
146 5 FoG and Cloud Computing with Jetson Nano
same is https://console.cloud.google.com/cloud-resource-manager?organizationId=
0&authuser=1&supportedpurview=project.
Step 2: Next step is to Enable APIs and their Services by clicking the ’ENABLE
APIS AND SERVICES’. This will bring you to the API library. Then Search ’Google
Drive’ → Click the ’Google Drive API’ icon → Then click ’ENABLE’, which will
enable Google Drive API service.
Step 3: Creation of credentials: click on ’credentials’ and select the options shown in
Fig. 5.16 and click ’done’. Next hover over to ’create credentials’ and select ’OAuth
Client ID’ as shown in Fig. 5.17.
Step 4: Next click on the button ’configure consent screen’ → Navigate to OAuth
consent screen → select ’External’ and then click ’Create’.
5.6 Examples of FoG and Cloud Computing 147
On the next screen enter the ’app name’ and the ’user support email id’ as well
as the developer contact information (developer email id) and then click ’save’ and
’continue’. On the following screen, click on ’save and continue’ as no changes are
required. Next add a test user’s email id and click ’add’ → then click ’save and
continue’. The next screen displays the summary of the OAuth consent.
Step 5: Next under Credential on left panel click ’Create Credentials’ → select
OAuth Client ID → Provide the settings as shown in Fig. 5.18 and click ’create’.
Now the OAuth Client ID is created.
Step 6: After creating the OAuth client ID → click on ’download’ button to get
.JSON file. This downloaded JSON file is required for Python code to access Google
Drive. After downloading the .JSON file, rename it to ’client_secrets.json’ and make
sure that it is placed in the same folder where the ’main.py’ is present.
Step 7: The Python code for home surveillance running on Jetson Nano SBC (FoG
node) requires the user to enter the google account and password. After authentication
is completed browser displays ’The authentication flow has completed’ and initiates
the uploading of the image onto Google Drive.
The surveillance image sent by ESP32-CAM is received by FoG node will be
saved in the folder named as ’photos’ in the project folder of the FoG node. The same
image is also uploaded to Google Drive in its root directory for remote monitoring.
The flow chart for configuring Jetson Nano for Home security is given in Fig. 5.19.
Step 8: Create a ’main.py’ to enter the below Python code on the Jetson Nano SBC
and run it using the below command.
python3 main.py
148 5 FoG and Cloud Computing with Jetson Nano
After successfully running the code the Twilio message notification is sent to the
registered mobile number of the house owner as shown in Fig. 5.20.
5.6 Examples of FoG and Cloud Computing 149
Fig. 5.19 Flow chart for using Twilio cloud and Google Drive from Jetson Nano
import time
import paho.mqtt.client as mqtt
#twilio imports
import os
from twilio.rest import Client
# setuptwilio
# setuptwilio ends
# twilio message
message = twilio_client.messages.create(body='Motion Detected : Image saved on GooogleDrive',from_=from_whatsapp_number,to=to_whatsapp_number)
print(message.sid)
t = drive.CreateFile({'title': time_str+'.jpg'})
t.SetContentFile(os.path.join(path, time_str+'.jpg'))
t.Upload()
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect('localhost', 1883, 60)
client.loop_forever()
Safety is the most important requirement for any home. More than ever before, a need
is felt for a home safety lock to safeguard one’s house and the belonging therein.
With the advent of IoT, one can give the connectivity to the door lock.
Here, authors have designed a smart home safety lock that communicates with the
owner. It also informs the owner of an authentic entry into the house, or if an intruder
is trying to enter the house. The solenoid lock is placed with IoT node configured on
152 5 FoG and Cloud Computing with Jetson Nano
Fig. 5.21 Conceptual and pin diagram for home safety lock
Fig. 5.22 Flow chart for home safety lock on ESP32 Wi-Fi module
5.6 Examples of FoG and Cloud Computing 153
ESP32 Wi-Fi module. When PIR sensor detects motion, the IoT node prompts the
user to enter the password. If the password is valid, only then the lock will open and
the user is allowed to enter the house. The status of valid/invalid entry is informed
to the Jetson Nano SBC configured as FoG node using MQTT protocol. The FoG
node in turn sends an alert message to the house owner using Twilio cloud platform
on WhatsApp. The conceptual diagram and pin diagram is shown in Fig. 5.21. Table
5.3 depicts the pin connection for the home safety lock.
To Configuring MQTT on ESP 32 Wi-Fi Module:
To configure, follow the steps already given in Sect. 5.6.2.1. This includes connecting
to the hotspot hosted by Jetson Nano SBC.
Here, ESP32 is the client and publishes the sensor data to a topic called ’pass-
word_verify’. FoG node, i.e. the Jetson Nano SBC will be the broker and client. A
Python MQTT client running on the FoG node will subscribe to the ’password_verify’
topic and collect the results. After setting up the MQTT protocol on the IoT node, it
is ready for communication. The flow chart for configuring ESP32 for Home Safety
Lock is given in Fig. 5.22.
154 5 FoG and Cloud Computing with Jetson Nano
Fig. 5.23 Flow chart for configuring Home Safety Lock using Jetson Nano
Create a ’main.py’ file in Thonny IDE and enter Python code. Then click the ’run’
button to execute the code.
5.6 Examples of FoG and Cloud Computing 155
KEY_UP = const(0)
KEY_DOWN = const(1)
keys = [['1', '2', '3', 'A'], ['4', '5', '6', 'B'], ['7', '8', '9', 'C'], ['*', '0', '#', 'D']]
# Pin names
rows = [13,12,14,27]
cols = [26,25,33,32]
def init():
for row in range(0,4):
for col in range(0,4):
row_pins[row].value(0)
if col_pins[col].value() == KEY_UP:
key = KEY_UP
row_pins[row].value(0)
# return the key state
return key
SERVER = '10.42.0.1' # MQTT Server Address (Change to the IP address of your Pi)
CLIENT_ID = 'ESP32'
156 5 FoG and Cloud Computing with Jetson Nano
TOPIC = b'password_verify'
#Attach external interrupt to GPIO13 and rising edge as an external event source
PIR_Interrupt.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt)
password = ['1','0','0','0']
input_pass = ['0','0','0','0']
print(input_pass)
if input_pass == password:
print('yes')
lock.value(0)
5.6 Examples of FoG and Cloud Computing 157
sleep(10)
lock.value(1)
Motion_status = False
else:
print('No')
lock.value(1) # dont open door
t = 0.0 # 0.0 indicates authentication not done
if isinstance(t, float): # Confirm sensor results are numeric
msg = (b'{0:3.1f}'.format(t))
client.publish(TOPIC, msg) # Publish sensor data to MQTT
print(msg)
else:
print('Invalid sensor readings.')
sleep(5)
Motion_status = False
Upon a valid password entry, the IoT node will unlock the lock and at the same
time send an MQTT message ’1’ to the Jetson Nano SBC (FoG Node) indicating
the same. If the intruder tries to enter the invalid password, the IoT node will send
an MQTT message as ’0’ to indicate that it is an invalid entry and will not open the
lock.
To configure the Jestson Nano (FoG Node) for MQTT and Twilio cloud follow
the steps given in Sect. 5.6.2.1. The flow chart for configuring Jetson Nano for Home
Safety Lock is given in Fig. 5.23.
Create a ’main.py’ and enter Python code and run using the below command.
python3 main.py
After successfully running the code, the Twilio message notification is sent to the
registered WhatsApp mobile number of the house owner.
#twilio imports
import os
from twilio.rest import Client
# setuptwilio
twilio_client = Client('AC230101cb426d251b82479e33a992c149','1d55483a6ab87ed20eac8531d70650c4')
# this is the Twilio sandbox testing number
from_whatsapp_number='whatsapp:+14155238886'
client = mqtt.Client()
client.on_connect = on_connect # Specify on_connect callback
client.on_message = on_message # Specify on_message callback
client.connect('localhost', 1883, 60) # Connect to MQTT broker (also running on Jetson).
Once the code is executed on Jetson SBC (FoG node), it will receive a message
from the IoT node using MQTT protocol. If the message received indicates an
authentic entry, it will inform the owner of the same. If there is invalid entry, it
indicates that the intruder is trying to enter.
Fire is one of the most dangerous threats that home and business owners need to take
into account. In the unfortunate case of a fire, there is a risk of losing the majority
of your belongings. A lack of safety precautions may result in material and financial
losses, as well as death. As a result, having a fire alert system installed is a great
way to keep premises safe. This method provides early alert of the hazard, allowing
adequate time to evacuate the premises and contact authorities before the fire spreads
out of control.
Here, the authors have designed a Fire Alert system to mitigate such situation. In
this implementation, authors have used the ESP32-CAM module configured as IoT
device. MQ-4 Sensor Module is interfaced to detect the smoke in the air. The MQ-
4 can detect smoke concentrations anywhere from 200 to 10,000 ppm. It provides
analog as well as digital output corresponding to the concentration of the gases in
the air.
When the IoT node detects a fire, it will inform the Jetson Nano SBC (FoG
node) using Wi-Fi over MQTT protocol. On reception of a message from the IoT
node, the FoG node will inform the house owner by a WhatsApp message using the
Twilio Cloud Platform, that there is a fire in the house for preventive measures. The
5.6 Examples of FoG and Cloud Computing 159
Fig. 5.24 Conceptual and pin diagram for Fire Alert system
conceptual diagram and pin diagram for Fire Alert system are shown in Fig. 5.24.
Table 5.4 depicts the pin connection for the Fire Alert system.
To Configuring MQTT on ESP32-CAM
To configure ESP32-CAM module for MQTT follow the steps already given in
Sect. 5.6.2.1. Here, ESP32 is the client and publishes the sensor data to a topic called
’smoke_alert’. FoG node, i.e. the Jetson Nano SBC will be the broker and client. A
Python MQTT client running on the FoG node will subscribe to the ’smoke_alert’
topic and collect the results. After setting up the MQTT protocol on the IoT node it is
ready for communication. The flow chart for the above process is given in Fig. 5.25.
Create a ’main.py’ file in Thonny IDE and enter the Python code . Then click the
’run’ button to execute the code.
160 5 FoG and Cloud Computing with Jetson Nano
Fig. 5.25 Flow chart for configuring ESP 32 Wi-Fi module for Fire Alert system
5.6 Examples of FoG and Cloud Computing 161
import network
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect("jetson-desktop", "11111111")
SERVER = '10.42.0.1' # MQTT Server Address (Change to the IP address of your Pi)
CLIENT_ID = 'ESP32'
TOPIC = b'smoke_alert'
while True:
if MQ4 value() == 0:
smoke_status = True
if smoke_status:
print('smoke is detected!')
To Configure the Jestson Nano (FoG Node) for MQTT and Twilio follow the steps
given in Sect. 5.6.2.1. Now both the nodes are ready for communication. The flow
chart for configuring Jetson Nano for the Fire Alert system is given in Fig. 5.26.
162 5 FoG and Cloud Computing with Jetson Nano
Fig. 5.26 Flow chart for configuring Jetson Nano for Fire Alert system
5.6 Examples of FoG and Cloud Computing 163
Next Create a ’main.py’ with the below code on the Jetson Nano SBC and run it
using the below command
python3 main.py
#twilio imports
import os
from twilio.rest import Client
# setuptwilio
twilio_client =
Client('AC230101cb426d251b82479e33a992c149','1d55483a6ab87ed20eac8531d70650c4');
# this is the Twilio sandbox testing number
from_whatsapp_number='whatsapp:+14155238886'
# setuptwilio ends
client = mqtt.Client()
client.on_connect = on_connect# Specify on_connect callback
client.on_message = on_message# Specify on_message callback
client.connect('localhost', 1883, 60) # Connect to MQTT broker
After successfully running the code, the Twilio message notification is sent to the
registered mobile number of the house owner that there is the fire in the house and
necessary action has to be taken.
164 5 FoG and Cloud Computing with Jetson Nano
Conclusion:
NVIDIA® Jetson Nano™ lets you bring incredible new capabilities to millions
of small and power-efficient AI systems. It opens new worlds of embedded IoT
applications. Jetson Nano is a powerful computer that lets run multiple codes for
various applications. In this chapter, the authors have given a brief introduction to FoG
and Cloud computing along with its architectures, characteristics, service models,
and various models deployment. It also covers the role of FoG and Cloud computing
in IoT applications. The authors have provided the detailed implementation steps
for the Patient Monitoring system with Cloud, wherein the system monitors Oxygen
saturation, Pulse, and Body Temperature. Authors have also implemented Home
Security system such as Home Surveillance, Home Safety Lock, and Fire alert system
by providing detailed steps. This Home Security system is implemented with Jetson
Nano as a FoG node.
Exercise:
(1) Similar to Sect. 5.6.2.3 (Fire alert system), using the ESP32-CAM module,
design a system that reads the temperature from the DHT22 sensor and updates
it over the FoG in regular intervals of 30 minutes.
(2) Modify the system mentioned in Sect. 5.6.1 (Patient Monitoring System with
Cloud) to work on Raspberry Pi instead of Jetson Nano.
(3) Referring to Sect. 5.6.1 (Patient Monitoring System with Cloud) interface a
MAX30102 Heart Rate sensor with Jetson Nano to monitor the patient’s
heart rate continuously. If the heart rate deviates outside the normal range and
send an alert message via Twilio Cloud.
(4) Interface ESP32-CAM module with 2 PIR sensors referring to Sect. 5.6.2.2
(Home Safety Lock). Also interface a light source through relay module. Write
a Python program which can make the ESP32-CAM module to take inputs from
both the PIR sensors. If both the PIR sensors detect movement, then send the
signals indicating the same to Jetson Nano and turn on the light source. Further,
send the message “Movement detected, Light On” through Twilio cloud to
WhatsApp number.
(5) Modify the setup in Sect. 5.6.2.1 (Home Surveillance) to capture images
in interval of 10 minutes if there is no movement detected by the PIR sensor.
(6) To enhance the Home Security, interface fingerprint sensor module with system.
(7) Increase the number of IoT nodes with smoke sensors in Fire Alert system to
detect fire in multiple rooms.
References 165
References
Aazam M, Huh EN (2014) Fog computing and smart gateway based communication for cloud of
things. In: Proceedings of the 2014 international conference on future internet of things cloud,
FiCloud 2014, Barcelona, Spain, 27–29 August 2014, pp 464–470
Aazam M, Huh EN (2015) Fog computing micro datacenter based dynamic resource estimation
and pricing model for IoT. In: Proceedings of international conference on advanced information
network applications, AINA 2015, pp 687–694
Aazam M, Hung PP, Huh E (2014) Smart gateway based communication for cloud of things.
In: Proceedings of the 2014 IEEE ninth international conference on intelligent sensors, sensor
networks and information processing, Singapore, 21–24 April 2014, pp 1–6
Arasaratnam O (2011) Introduction to cloud computing. In: Halpert B (ed) Auditing cloud
computing, a security and privacy guide. Wiley, Hoboken, NJ, pp 1–13
Atlam HF, Walters RJ, Wills GB (2018) Fog computing and the internet of things: a review. Big
Data Cogn Comput 2:10. https://doi.org/10.3390/bdcc2020010
Cha H-J, Yang H-K, Song Y-J (2018) A study on the design of fog computing architecture using
sensor networks. Sensors 18:3633. https://doi.org/10.3390/s18113633
Chandrasekaran K (2015) Essentials of cloud computing. http://www.crcnetbase.com/isbn/978148
2205442
Chiang M, Zhang T (2016) Fog and IoT: an overview of research opportunities. IEEE Internet
Things J 3(6):854–864
Dastjerdi AV, Gupta H, Calheiros RN, Ghosh SK, Buyya R (2016) Fog computing: principles,
architectures, and applications. In: Buyya R, Vahid Dastjerdi A (eds) Internet of things. Morgan
Kaufmann, pp 61–75. https://doi.org/10.1016/B978-0-12-805395-9.00004-6
Evans D (2011) The internet of things how the next evolution of the internet is changing every-
thing. https://www.cisco.com/c/dam/en_us/about/ac79/docs/innov/IoT_IBSG_0411FINAL.pdf.
Accessed 13 Nov 2021
Fog Computing and Its Role in the Internet of Things. https://readwrite.com/2020/10/30/fog-com
puting-and-its-role-in-the-internet-of-things/
ITCandor (2018) Distribution of cloud platform as a service (PaaS) market revenues worldwide
from 2015 to June 2018, by vendor. https://www.statista.com/statistics/540521/worldwidecloud-
platform-revenue-share-by-vendor/
Iyer B, Henderson JC (2010) Preparing for the future: understanding the seven capabilities of cloud
computing. MIS Q Exec 9(2):117–131
Khan S, Parkinson S, Qin Y (2017) Fog computing security: a review of current applications and
security solutions. J Cloud Comput. 6(1):19
Kumar N, DuPree L (2011) Protection and privacy of information assets in the cloud. In: Halpert
B (ed) Auditing cloud computing, a security and privacy guide. Wiley, Hoboken, NJ, pp 97–128
Liu Y, Fieldsend JE, Min G (2017) A framework of fog computing: architecture challenges and
optimization. IEEE Access 4:1–10
Marques B, Machado I, Sena A, Castro MC (2017) A communication protocol for fog computing
based on network coding applied to wireless sensors. In: Proceedings of the 2017 IEEE Inter-
national symposium on high performance computer architecture, Vosendorf, Austria, 24–28
February 2017, pp 109–114
Marston S, Li Z, Bandyopadhyay S, Zhang J, Ghalsasi A (2011) Cloud computing—the business
perspective. Decis Support Syst 51(1):176–189
Mell P, Grance T (2011) The NIST definition of cloud computing. National Institute of Standards
and Technology, NIST Special Publication (SP), pp 800–145. https://doi.org/10.6028/NIST.SP.
800-145
Mukherjee M, Shu L, Wang D (2018) Survey of fog computing: fundamental, network applications,
and research challenges. IEEE Commun Surv Tutor
166 5 FoG and Cloud Computing with Jetson Nano
Muntjir M, Rahul M, Alhumyani HA (2017) An analysis of internet of things (IoT): novel architec-
tures, modern applications, security aspects and future scope with latest case studies. Int J Eng
Res Technol 6:422–447
Ara R, Rahim MA, Roy S, Prodhan UK (2020) Cloud computing: architecture, services, deployment
models, storage, benefits and challenges. Int J Trend Sci Res Dev 4(4):837–842
Schneider S, Sunyaev A (2016) Determinant factors of cloud-sourcing decisions: reflecting on the
IT outsourcing literature in the era of cloud computing. J Inf Technol 31(1):1–31
Shi Y, Ding G, Wang H, Roman HE, Lu S (2015) The fog computing service for healthcare. In:
Proceedings of the 2015 2nd international symposium on future information and communication
technologies for ubiquitous healthcare (Ubi-HealthTech), Beijing, China, 28–30 May 2015, pp
1–5
Sunyaev A (ed) (2020) “Cloud computing,” in internet computing: principles of distributed systems
and emerging internet-based technologies. Springer International Publishing, Cham, pp 195–236.
https://doi.org/10.1007/978-3-030-34957-8_7
Taneja M, Davy A (2016) Resource aware placement of data analytics platform in fog computing.
Proc Comput Sci 97:153–156. https://doi.org/10.1016/j.procs.2016.08.295
Tang B, Chen Z, Hefferman G, Wei T, He H, Yang Q (2015) A hierarchical distributed Fog computing
architecture for big data analysis in smart cities. In: Proceedings of the ASE Big Data & Social
Informatics 2015. ACM, p 28
Voorsluys W, Broberg J, Buyya R (2011) Introduction to cloud computing. In: Buyya R, Broberg
J, Goscinski A (eds) Cloud computing. Wiley, Hoboken, NJ, pp 3–42
Chapter 6
Machine Learning (ML) in IoT
with Jetson Nano
Abstract Machine Learning (ML) is a fast growing field and has been deployed
in various IoT applications. When used in conjunction, they can deliver insights
which are otherwise hidden in large data. One of the popular platforms used for
ML in IoT is Jetson Nano SBC. It boasts of NVIDIA Maxwell architecture with
128CUDA cores, which make running ML algorithms efficient. This chapter lays the
foundation for ML in a systematic manner by covering concepts such as what is AI,
Deep Learning (DL) and ML. To provide a hands-on approach on ML, the authors
have carefully curated three applications in the field namely, Pattern Recognition
using ML with Cloud, Object Classification using ML with FoG, and Prediction of
unknown Glucose concentration using ML at Edge. After completion of the chapter,
the reader will have a firm grasp on the skills required to implement ML applications
on the IoT platform.
Artificial Intelligence (AI) was born in the 1950s, when Alan Turing in his pioneering
paper titled “Computing Machinery and Intelligence (1950)” posed the question,
“whether computers could be made to think?”, a question whose answers is still
being explored today. At its core, AI is the branch of computer science that seeks to
answer Turing’s question in the affirmative. It is an attempt to recreate or reproduce
human intellect in machines. As such, AI is a general field that encompasses ML and
DL as depicted in Fig. 6.1.
Many scientists have believed for a long time, that human-level AI could be
achieved. The programmers tried to code a sufficiently extensive set of explicit rules
for manipulating knowledge. This approach is called symbolic AI and was dominant
from the 1950s to the 1980s (Levesque 2017). The experts system boom in the 1980s
is proved to be the golden period of symbolic AI.
© The Author(s), under exclusive license to Springer Nature Singapore Pte Ltd. 2023 167
J. S. Parab et al., Python Programming Recipes for IoT Applications, Transactions
on Computer Systems and Networks, https://doi.org/10.1007/978-981-19-9466-1_6
168 6 Machine Learning (ML) in IoT with Jetson Nano
Machine Learning
ML is a marriage of two fields, namely, computer science and statistics which can be
used to solve fuzzy tasks. The original question posed by Alan Turing opens the door
to a new programming paradigm. In conventional programming and symbolic AI,
the output is generated based on the data provided to the input rules (program). With
ML, input data, as well as the expected output from the data is given, which produces
a set of rules (program). These rules can then be applied to new data to produce the
original output. Both the approaches are depicted in Fig. 6.2. Supervised learning,
unsupervised learning, and reinforcement learning are the three most commonly
prevalent forms of ML algorithms.
Supervised Learning:
In supervised learning, train the machine using data that is well “labeled”. Super-
vised learning is used for problems related to regression and classification. The
most commonly used supervised learning algorithms are Support-Vector Machines
(SVM), Linear Regression, Logistic Regression, Naive Bayes, Linear Discriminant
Analysis (LDA), Decision Trees (DT), K-Nearest Neighbor algorithm (KNN), Neural
Networks-Multilayer Perceptron (MLP), etc. (Algorithms and of Machine Learning:
Prediction of Brand Loyalty. 2019).
6.2 Concepts of Machine Learning (ML) and Deep Learning (DL) 169
Unsupervised Learning:
Unsupervised learning is a technique of ML in which the algorithm is not given any
labels or scores for training the data. Unsupervised learning is used for dimension
reduction and clustering. Few commonly used unsupervised learning algorithms
are K-means clustering, KNN, Hierarchal clustering, Anomaly detection, Neural
Networks (NN), Principle Component Analysis (PCA), Independent Component
Analysis (ICA), Apriori algorithm, etc.
Reinforcement Learning:
Reinforcement Learning is an ML technique in which a model learns to perform
the actions based on the feedback. For each accurate action, the model gets posi-
tive feedback, and for each inaccurate action, the model gets a negative feedback.
Here, the model learns automatically using feedbacks without any labeled data,
unlike in supervised learning. Few commonly used reinforcement methods are
Monte Carlo, Q-learning, State–Action–Reward–State–Action (SARSA), Deep Q
Network (DQN), Deep Deterministic Policy Gradient (DDPG), Q-Learning with
Normalized Advantage Functions (NAF), etc. Fig. 6.3 represents the ML techniques
in pictorial form.
Deep Learning (DL):
DL is a subfield of ML to solve sophisticated problems by extracting the hidden
information by using the deeper layers. The high performance of DL can be realized
when trained on the huge amount of data, which is not possible with conventional
ML.
DL algorithms analyze data with a logic similar to how a human would conclude.
DL applications do this by employing a layered structure of algorithms known as
an Artificial Neural Network (ANN). The architecture of ANN is inspired by the
biological neural network of the human brain, resulting in a learning process that is
significantly superior to that of ordinary ML models.
170 6 Machine Learning (ML) in IoT with Jetson Nano
Fig. 6.4 depicts a rudimentary ANN architecture. The left most layer is known
as the ‘Input layer’, while the right most layer is known as the ‘Output layer’. The
layers between the input and output layer are referred to as ‘Hidden layers’. The
weights of the hidden layers are tuned so that the network is able to achieve the
desired goal. Deeper the network, the more hidden layers it has between the input
and output levels. In general, a Deep Neural Network (DNN) is an ANN with one or
more hidden layers.
DL has made drastic strides for applications involving huge data and high
computing power to process these data. However, with the advent of cloud computing
Fig. 6.4 A simple ANN with Input layer, two Hidden layers and Output layer
6.3 Pattern Recognition Using ML with Cloud 171
Pattern Recognition has piqued the interest of researchers all around the world.
A pattern can be physically detected or computationally observed by using
ML algorithms.
Pattern recognition systems can recognize familiar patterns quickly and accu-
rately. Here, the labeled training data is used. Pattern recognition applications include
image processing, voice, fingerprint identification, aerial picture interpretation,
optical character recognition (OCR) and even medical imaging.
To understand the pattern recognition application, the authors have implemented
classification of different shapes. There are in particular two classes: Quadrilaterals
and Triangles. During the training phase, authors have fed the algorithm with 123
images of each class. For recognition, the model takes the image from a specified
location and provides it as an input to the trained SVM ML model. ML model then
predicts the class to which the image belongs and sends the message on WhatsApp
via ‘Twilio’ cloud. The conceptual diagram for pattern recognition with the cloud is
shown in Fig. 6.5. The SVM achieved pattern recognition accuracy of more than 90%.
172 6 Machine Learning (ML) in IoT with Jetson Nano
Fig. 6.6 Flow chart for the SVM model training and validation
file = '/home/jetson/Desktop/Project/Pattern_recog'
categories = ['Quadrilateral', 'Triangle']
data = []
features = []
labels = []
# Data preparation
6.3 Pattern Recognition Using ML with Cloud 175
# Splitting total dataset into training and testing set with 80:20 ratio.
X_train, X_test, Y_train, Y_test = train_test_split(features, labels, test_size=0.20)
#Fitting and training the model on training set.
model = SVC(C=1, kernel='poly', gamma='auto',degree=4,coef0=1.5)
model.fit(X_train, Y_train)
# Prediction
Y_pred = model.predict(X_test)
Identification of unknown (Test) Image: This section allows user to test unknown
shape (quadrilateral or triangle) using above trained SVM model.
Save the Python code as ‘main.py’ in ‘Pattern_recog’ directory and execute the
code. This code takes unknown image which is saved in ‘Test’ folder for identification
and sends the result to WhatsApp via Twilio cloud. The detailed flow chart for the
test image recognition is shown in Fig. 6.7. In present scenario, the authors tested
two unknown images and identified result is shown in Fig. 6.8.
176 6 Machine Learning (ML) in IoT with Jetson Nano
# This is main.py
import os
import numpy as np
import cv2
import pickle
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from twilio.rest import Client
test = []
path = '/home/jetson/Desktop/Project/Pattern_recog/Test'
for img in os.listdir(path):
img_path = os.path.join(path, img)
image_shape = cv2.imread(img_path, 0)
image_shape = cv2.resize(image_shape , (100, 100))
image = np.array(image_shape).flatten()
image = image.reshape(1,-1)
pick = open('model.sav','rb')
model = pickle.load(pick)
result = model.predict(image)
if result == [0]:
print('Prediction is : Quadrilateral')
message = client.messages.create(body='Prediction is
Quadrilateral',from_='whatsapp:+14155238886',to='whatsapp:+917057504996')
if result == [1]:
print('Prediction is : Triangle')
message = client.messages.create(body='Prediction is
Triangle',from_='whatsapp:+14155238886',to='whatsapp:+91XXXXXXXXXX')
print(message.sid)
178 6 Machine Learning (ML) in IoT with Jetson Nano
Fig. 6.10 Flow chart for configuring IoT node for image capture
Step 2: Save the below code in ESP32-CAM as ‘main.py’ using Thonny IDE and
execute the same by clicking the run button to send the image via MQTT.
180 6 Machine Learning (ML) in IoT with Jetson Nano
#main.py
from machine import Pin #importing classes
from umqtt.simple import MQTTClient
import time
import os
import camera
SERVER = '10.42.0.1' # MQTT Server Address (Change to the IP address of your Pi)
CLIENT_ID = 'esp32-camera'
TOPIC = b'Image'
camera.init(0, format=camera.JPEG)
buf = camera.capture()
c.publish(mqtt_config[TOPIC], buf)
time.sleep_ms(100)
file = '/home/jetson/Desktop/Project/Object_classify'
categories = ['Py_board', 'Raspberry_pi','Jetson_nano']
data = []
features = []
labels = []
# Data preparation
6.4 Object Classification Using ML with FoG 183
model = KNeighborsClassifier(n_neighbors=3)
model.fit(X_train, Y_train)
# Prediction
Y_pred = model.predict(X_test)
pick = open('model_obj.sav','wb')
pickle.dump(model,pick)
pick.close()
Step 7: Right-click inside the directory and click on ‘open in terminal’ (To open
terminal in working directory). Execute the code by typing ‘sudo python3 model.py’.
After execution, trained model having pickle file with extension ‘.sav’ will be saved
in the ‘Object_classify’ directory.
Step 6: Save the following code as ‘main.py’ in ‘Object_classify’ directory
and execute. This code receives an image from IoT node (ESP-32CAM) through
MQTT protocol. This received image is then saved in ‘Test’ directory. The flowchart
for image classification is given in Fig. 6.12. The image is then classified and the
classification result is sent on the WhatsApp via Twilio cloud as shown in Fig. 6.13.
184 6 Machine Learning (ML) in IoT with Jetson Nano
# main.py
"""This is a program for image classification. There are three
classes, namely Raspberry Pi, Py Board and Jetson Nano. """
import os
import numpy as np
import cv2
import pickle
from sklearn.model_selection import train_test_split
from twilio.rest import Client
import paho.mqtt.client as mqtt
pick = open('model_obj.sav','rb')
model = pickle.load(pick)
result = model.predict(image)
if result == [0]:
print('Prediction is : Py Board')
t_message = t_client.messages.create(body='Prediction is Py Board',
from_='whatsapp:+14155238886',
to='whatsapp:+91XXXXXXXXXX')
elif result == [1]:
print('Prediction is : Raspberry Pi')
t_message = t_client.messages.create(body='Prediction is Raspberry Pi',
from_='whatsapp:+14155238886',
to='whatsapp:+91XXXXXXXXXX')
elif result == [2]:
print('Prediction is : Jetson Nano')
t_message = t_client.messages.create(body='Prediction is JesonNano',
from_='whatsapp:+14155238886',
to='whatsapp:+91XXXXXXXXXX')
print("message sent")
186 6 Machine Learning (ML) in IoT with Jetson Nano
client = mqtt.Client()
client.on_connect = on_connect# Specify on_connect callback
client.on_message = on_message# Specify on_message callback
client.connect('localhost', 1883, 60) # Connect to MQTT broker.
client.loop_forever()
For longevity of life, one has to monitor body parameters regularly which also helps
to avoid serious complications caused by various diseases such as diabetes, cardio-
vascular diseases, etc. Diabetes is a serious condition that is characterized by high
blood glucose levels. If this level remains high for a long duration it leads to several
health problems such as kidney failure, stroke, amputation of limbs, heart attack,
blindness, etc. Hence, it is of utmost importance to monitor blood glucose regularly
for diabetes patients. There are Glucose monitoring meters available in markets, but
one needs to put a drop of blood by pricking the finger. Since it involves pricking,
it causes pain and can lead to infection. To circumvent this problem, a non-invasive
method is required such as optical absorption signatures from human tissues and
presently, an extensive amount of research is being carried out in this particular area.
In this application, Jetson Nano SBC acts as an Edge node on which the Multilayer
perceptron (MLP) model is deployed to accurately predict the glucose concentration.
Edge computing is a distributed framework wherein client data is processed as near to
the source as feasible at the network’s perimeter. To consider the data set for prediction
from the human blood sample one requires ethical clearance. Therefore, we have
worked on laboratory samples instead of human tissues. The samples are prepared
by mixing five major blood constituents as glucose, ascorbate, alanine, lactate, and
urea with a physiologically relevant concentration in the normal range. Authors have
created database of 57 samples having absorption signatures at three wavelengths,
namely, 2246 nm, 2280 nm, and 2308 nm using Spectrophotometer Jasco V770.
MLP Model deployed on Edge is trained with 45 samples and 12 samples are
used for validating the model, i.e. database split ratio is of 80:20. The MLP model
6.5 Prediction of Unknown Glucose Concentration Using ML at EDGE 187
Step 3:
Create a directory named ‘Project’ in your desired location (Authors have chosen
Desktop). Open the ’Project’ directory and create a subdirectory named as
‘Glucose_pred’. This directory shall hold dataset, trained model, and program for
the aforementioned application.
Step 4: Navigate to ‘Glucose_pred’ directory that was preciously created.
Step 5: Save the following code as ‘model.py’ in ’Glucose_pred’ directory. The flow
chart for glucose prediction is given in Fig. 6.14.
188 6 Machine Learning (ML) in IoT with Jetson Nano
"""Initialisedatset """
y_pred=model.predict(x_test1)
print("The Score with ", (r2_score(y_pred, y_test)))
test=x_test1[0]
arr = np.array(x_test1[0])
actual=np.array(y_test)
# Convert 1D array to a 2D numpy array of 2 rows and 3 columns
test = np.reshape(arr, (1, 3))
actual[0]
predicted_all=model.predict(x_test1)
print(predicted_all)
6.5 Prediction of Unknown Glucose Concentration Using ML at EDGE 191
predicted=model.predict(test)
predicted=model.predict(wave_values)
print ("Predicted glucose is ")
print(predicted)
Step 6: Run the above code using the following command in the Jetson Nano terminal
(make sure the terminal is opened from the working directory).
sudo python3 model.py
After Successful execution the predicted results are shown on the Jestson terminal.
Conclusion:
ML is a fast growing field and has been deployed in various IoT applications. To start
with, the authors have given the brief introduction of AI, DL, and ML. Authors have
implemented three applications on Jetson Nano platform, namely, Pattern Recogni-
tion using ML with Cloud, Object Classification using ML with FoG and Prediction
of unknown Glucose concentration using ML at Edge. The detailed implementation
steps of all three applications are provided for ready prototyping for readers.
Exercise:
(1) With reference to the example in the Sect. 6.3 (Pattern Recognition using ML
with Cloud), try running the program with different resolution of the images.
E.g.: 200 × 200, 150 × 150, and 50 × 50 and see the change in prediction
accuracy of the trained model.
Hint: Change cv2.resize(image_shape, (100, 100)) to desired resolution.
(2) For the activity mentioned in the Sect. 6.3 (Pattern Recognition using ML with
Cloud) examine the change in result when number of shapes in the dataset is
increased from 2 to 3. E.g.: Quadrilateral, Triangle, and Circular/Elliptical. Also
follow up with sending the result over to Twilio cloud.
(3) In the Sect. 6.4 (Object Classification using ML with FoG), explore the
possibility of replacing the FoG node, i.e. Jetson Nano with Raspberry Pi.
(4) In the Sect. 6.4 (Object Classification using ML with FoG), try to create your
own dataset and evaluate the performance with various ML models.
(5) Investigate how changing parameters such as C, degree and coefficient affects
the accuracy of system mentioned in Sect. 6.3 (Pattern Recognition using ML
with Cloud). Further determine the accuracy that can be achieved by using KNN
classifiers.
(6) Build a dataset using your own glucose sensor and pass it to the above glucose
prediction model and determine the prediction accuracy.
192 6 Machine Learning (ML) in IoT with Jetson Nano
References
https://www.simplilearn.com/10-algorithms-machine-learning-engineers-need-to-know-article
https://machinelearningmastery.com/transfer-learning-for-deep-learning/
https://www.ibm.com/cloud/learn/deep-learning/
(2019) Supervised learning algorithms of machine learning: prediction of brand loyalty. IJITEE
8:3886–3889. https://doi.org/10.35940/ijitee.J9498.0981119
Levesque HJ (2017) Common sense, the turing test, and the quest for real AI: Reflections on natural
and artificial intelligence. MIT Press
Correction to: Python Programming
Recipes for IoT Applications
Correction to:
J. S. Parab et al., Python Programming Recipes for IoT
Applications, Transactions on Computer Systems
and Networks, https://doi.org/10.1007/978-981-19-9466-1
The original version of the book was inadvertently published with incorrect author
names in online version.
The correction chapters and the book have been updated with these changes.
© The Author(s), under exclusive license to Springer Nature Singapore Pte Ltd. 2023 C1
J. S. Parab et al., Python Programming Recipes for IoT Applications, Transactions
on Computer Systems and Networks, https://doi.org/10.1007/978-981-19-9466-1_7