Iot 1-5 Units Notes (Elective)
Iot 1-5 Units Notes (Elective)
INTERNET OF THINGS
(IoT)
IoT and M2M - Deference between Iot and M2M - SDN and NFV for lot - IoT systems
management - SNMP - YANG - NETOPEER
IoT physical servers & cloud computing - WAMP - Xively cloud for IoT - python Web
application frame work - Amazon web services for IoT.
• Characteristics of IoT
• IoT Protocols
Definition of IoT
A dynamic global network infrastructure with self-configuring capabilities based on standard
and interoperable communication protocols where physical and virtual "things" have identities,
physical attributes, and virtual personalities and use intelligent interfaces, and are seamlessly
integrated into the information network, often communicate data associated with users and their
environments.
Characteristics of IoT
• Dynamic & Self-Adapting
• Self-Configuring
• Unique Identity
• Exchange data with other connected devices and applications (directly or indirectly)
• Collect data from other devices and process the data locally
• Send the data to centralized servers or cloud-based application back-ends for processing the data
• Perform some tasks locally and other tasks within the IoT infrastructure, based on temporal and
space constraints.
Generic block diagram of an IoT Device
An IoT device may consist of several interfaces for connections to other devices, both wired and
wireless.
• Audio/video interface
IoT Protocols
Link Layer
• 802.3 – Ethernet
• 802.11 – WiFi
• 802.16 – WiMax
• 802.15.4 – LR-WPAN
• 2G/3G/4G
• Network/Internet Layer
• IPv4
• IPv6
• 6LoWPAN
• Transport Layer
• TCP • UDP
• Application Layer
• HTTP
• CoAP
• WebSocket
• MQTT
• XMPP
• DDS
• AMQ
Logical Design of IoT
Logical design of an IoT system refers to an abstract representation of the entities and processes
without going into the low-level specifics of the implementation.
An IoT system comprises of a number of functional blocks that provide the system the capabilities
for identification, sensing, actuation, communication, and management.
Request-Response communication model
• Request-Response is a communication model in which the client sends requests to the server and the
server responds to the requests. • When the server receives a request, it decides how to respond,
fetches the data, retrieves resource representations, prepares the response, and then sends the response
to the client.
• Publishers are the source of data. Publishers send the data to the topics which are managed by the
broker. Publishers are not aware of the consumers.
• When the broker receives data for a topic from the publisher, it sends the data to all the subscribed
consumers.
Push-Pull communication model
Push-Pull is a communication model in which the data producers push the data to queues and the
consumers pull the data from the queues. Producers do not need to be aware of the consumers.
• Queues help in decoupling the messaging between the producers and consumers.
• Queues also act as a buffer which helps in situations when there is a mismatch between the rate at
which the producers push data and the rate rate at which the consumers pull data.
• Exclusive Pair is a bidirectional, fully duplex communication model that uses a persistent
connection between the client and server.
• Once the connection is setup it remains open until the client sends a request to close the connection.
• Client and server can send messages to each other after connection setup.
REST-based Communication APIs
Representational State Transfer (REST) is a set of architectural principles by which you can design
web services and web APIs that focus on a system’s resources and how resource states are addressed
and transferred.
• The REST architectural constraints apply to the components, connectors, and data elements, within a
distributed hypermedia system.
WebSocket APIs allow bi- directional, full duplex communication between clients and servers.
Exclusive Pair is a bidirectional, fully duplex communication model that uses a persistent
connection between the client and server.
• Once the connection is setup it remains open until the client sends a request to close the connection.
• Client and server can send messages to each other after connection setup.
• Device: An IoT device allows identification, remote sensing, actuating and remote monitoring
capabilities. You learned about various examples of IoT devices in section.
• Resource: Resources are software components on the IoT device for accessing, processing, and
storing sensor information, or controlling actuators connected to the device. Resources also include
the software components that enable network access for the device.
• Controller Service: Controller service is a native service that runs on the device and interacts with
the web services. Controller service sends data from the device to the web service and receives
commands from the application (via web services) for controlling the device.
• Database: Database can be either local or in the cloud and stores the data generated by the IoT
device.
• Web Service: Web services serve as a link between the IoT device, application, database and
analysis components. Web service can be either implemented using HTTP and REST principles
(REST service) or using WebSocket protocol (WebSocket service).
• Analysis Component: The Analysis Component is responsible for analyzing the IoT data and
generate results in a form which are easy for the user to understand.
• Application: IoT applications provide an interface that the users can use to control and monitor
various aspects of the IoT system. Applications also allow users to view the system status and view
the processed data.
IoT LevelS
A level-1
IoT system has a single node/device that performs sensing and/or actuation, stores data, performs
analysis and hosts the application.
• Level-1 IoT systems are suitable for modeling low- cost and low-complexity solutions where the
data involved is not big and the analysis requirements are not computationally intensive.
IoT Level-2
A level-2 IoT system has a single node that performs sensing and/or actuation and local analysis.
• Level-2 IoT systems are suitable for solutions where the data involved is big, however, the primary
analysis requirement is not computationally intensive and can be done locally itself.
IoT Level-3
• A level-3 IoT system has a single node. Data is stored and analyzed in the cloud and application is
cloud- based.
• Level-3 IoT systems are suitable for solutions where the data involved is big and the analysis
requirements are computationally intensive.
IoT Level-4
A level-4 IoT system has multiple nodes that perform local analysis. Data is stored in the cloud and
application is cloud-based.
• Level-4 contains local and cloud- based observer nodes which can subscribe to and receive
information collected in the cloud from IoT devices.
• Level-4 IoT systems are suitable for solutions where multiple nodes are required, the data involved
is big and the analysis requirements are computationally intensive.
IoT Level-5
• A level-5 IoT system has multiple end nodes and one coordinator node.
• Coordinator node collects data from the end nodes and sends to the cloud.
• Level-5 IoT systems are suitable for solutions based on wireless sensor networks, in which the data
involved is big and the analysis requirements are computationally intensive.
IoT Level-6
• A level-6 IoT system has multiple independent end nodes that perform sensing and/or actuation and
send data to the cloud.
• The analytics component analyzes the data and stores the results in the cloud database.
• The centralized controller is aware of the status of all the end nodes and sends control commands to
the nodes.
Domain Specific IoT
•Home Automation
•Cities
•Environment
•Energy
•Retail
•Logistics
•Agriculture
•Industry
•Health & Lifestyle
Home Automation
Smart Lighting
• Control lighting by remotely (mobile or web applications)
• Smart Appliances
• Provide status information to the users remotely
• Intrusion Detection
• Use security cameras and sensors (PIR sensors and door sensors)
• Detect intrusions and raise alerts
• The alerts form: an SMS or an email sent to the user
• Smoke/Gas Detectors
• Use optical detection, ionization, or air sampling techniques to detect the smoke
• Gas detectors can detect harmful gases
• Carbon monoxide (CO)
• Liquid petroleum gas (LPG)
• Raise alerts to the user or local fire safety department
Cities
• Smart Parking
• Detect the number of empty parking slots
• Send the information over the internet and accessed by smartphones
• Smart Roads
• Provide information on driving conditions, traffic congestions, accidents
• Alert for poor driving conditions
• Structural Health Monitoring
• Monitor the vibration levels in the structures (bridges and buildings)
• Advance warning for imminent failure of the structure
• Surveillance
• Use the large number of distributed and internet connected video surveillance cameras
• Aggregate the video in cloud-based scalable storage solutions
• Emergency Response
• Used for critical infrastructure monitoring
• Detect adverse events
Environment
Weather Monitoring
• Collect data from several sensors (temperature, humidity, pressure, etc.)
• Send the data to cloud-based applications and storage back-ends
• Air Pollution Monitoring
• Monitor emission of harmful gases (CO2, CO, NO, NO2 , etc.)
• Factories and automobiles use gaseous and meteorological sensors
• Integration with a single-chip microcontroller, several air pollution sensors, GPRS-modem, and a
GPS module
• Noise Pollution Monitoring
• Use a number of noise monitoring stations
• Generate noise maps from data collected
• Forest Fire Detection
• Use a number of monitoring nodes deployed at different locations in a forests
• Use temperature, humidity, light levels, etc.
• Provide early warning of potential forest fire
• Estimates the scale and intensity
• River Floods Detection
• Monitoring the water level (using ultrasonic sensors) and flow rate (using the flow velocity sensors)
• Raise alerts when rapid increase in water level and flow rate is detected.
Energy
• Smart Grids
• Collect data regarding electricity generation, consumption, storage (conversion of energy into other
forms), distribution, equipment health data
• Control the consumption of electricity
• Remotely switch off supply
• Renewable Energy Systems
• Measure the electrical variables
• Measure how much the power is fed into the grid
• Prognostics
• Predict performance of machines or energy systems
• By collect and analyze the data from sensors.
Retail
• Inventory Management
• Monitoring the inventory by the RFID readers
• Tracking the products
• Smart Payments
• Use the NFC
• Customers store the credit card information in their NFC-enabled
• Smart Vending Machines
• Allow remote monitoring of inventory levels
• Elastic pricing of products
• Contact-less payment using NFC
• Send the data to the cloud for predictive maintenance
• The information of inventory levels
• The information of the nearest machine in case a product goes out of stock in a machine
Logistics
• Route Generation & Scheduling
• Generate end-to-end routes using combination of route patterns
• Provide route generation queries
• Can be scale up to serve a large transportation network
• Fleet Tracking
• Track the locations of the vehicles in real-time
• Generate alerts for deviations in planned routes
• Shipment monitoring
• Monitoring the conditions inside containers
• Using sensors (temperature, pressure, humidity)
• Detecting food spoilage
• Remote Vehicle Diagnostics
• Detect faults in the vehicle
• Warn of impending faults
• IoT collects the data on vehicle (speed, engine RPM, coolant temperature)
• Generate alerts and suggest remedial actions
Agriculture
• Smart Irrigation
• Use sensors to determine the amount of moisture in the soil
• Release the flow of water
• Using predefined moisture levels
• Water Scheduling
• Green House Control
• Automatically control the climate logical conditions inside a green house
• Using several sensors to monitor
• Using actuation devices to control
• Valves for releasing water and switches for controlling fans
• Maintenance of agricultural production
Industry
• Machine Diagnosis
• Sensors in machine monitor the operating conditions
• For example: temperature & vibration levels
• Collecting and analyzing massive scale machine sensor data
• For reliability analysis and fault prediction in machines
• Indoor Air Quality Monitoring
• Use various gas sensors
• To monitor the harmful and toxic gases (CO, NO, NO2 , etc.)
• Measure the environmental parameters to determine the indoor air quality
• Temperature, humidity, gaseous pollutants, aerosol
Machine-to-Machine (M2M)
An M2M area network comprises of machines (or M2M nodes) which have embedded hardware
modules for sensing, actuation and communication.
• Various communication protocols can be used for M2M local area networks such as ZigBee,
Bluetooh, ModBus, M-Bus, Wirless M-Bus, Power Line Communication (PLC), 6LoWPAN, IEEE
802.15.4, etc.
• The communication network can use either wired or wireless networks (IP- based).
• While the M2M area networks use either proprietary or non-IP based communication protocols, the
communication network uses IP-based networks.
M2M gateway
Since non-IP based protocols are used within M2M area networks, the M2M nodes within one
network cannot communicate with nodes in an external network.
To enable the communication between remote M2M area networks, M2M gateways are used
Difference between IoT and M2M
Communication Protocols
• M2M and IoT can differ in how the communication between the machines or devices happens.
• M2M uses either proprietary or non-IP based communication protocols for communication within
the M2M area networks.
• The "Things" in IoT refers to physical objects that have unique identifiers and can sense and
communicate with their external environment (and user applications) or their internal physical states.
• M2M systems, in contrast to IoT, typically have homogeneous machine types within an M2M area
network.
• While the emphasis of M2M is more on hardware with embedded modules, the emphasis of IoT is
more on software.
• M2M data is collected in point solutions and often in on-premises storage infrastructure.
• In contrast to M2M, the data in IoT is collected in the cloud (can be public, private or hybrid cloud).
• Applications
• M2M data is collected in point solutions and can be accessed by on-premises applications such as
diagnosis applications, service management applications, and on- premisis enterprise applications.
• IoT data is collected in the cloud and can be accessed by cloud applications such as analytics
applications, enterprise applications, remote diagnosis and management applications, etc..
• Software-based SDN controllers maintain a unified view of the network and make confi guration,
management and provisioning simpler.
• The underlying infrastructure in SDN uses simple packet forwarding hardware as opposed to
specialized hardware in conventional networks.
• NFV is complementary to SDN as NFV can provide the infrastructure on which SDN can run.
• VNF is a software implementation of a network function which is capable of running over the NFV
Infrastructure (NFVI).
• NFVI includes compute, network and storage resources that are virtualized.
• NFV Management and Orchestration focuses on all virtualization-specific management tasks and
covers the orchestration and life-cycle management of physical and/or software resources that support
the infrastructure virtualization, and the life-cycle management of VNFS.
• Managed Device
Limitations of SNMP
• SNMP is stateless in nature and each SNMP request contains all the information to process the
request. The application needs to be intelligent to manage the device.
• SNMP is a connectionless protocol which uses UDP as the transport protocol, making it unreliable
as there was no support for acknowledgement of requests.
• MIBs often lack writable objects without which device configuration is not possible using SNMP.
• Retrieving the current configuration from a device can be difficult with SNMP.
NETOPEER
Netopeer is a set of NETCONF tools built on the libnetconf library.
It allows operators to connect to their NETCONF-enabled devices as well as developers to
allow control their devices via NETCONF.
With the experiences from Netopeer, we have moved our activities to work on next
generation of this NETCONF toolset based on libyang library.
Netopeer is mature enough to be used as a replacement of the original Netopeer tools.
Therefore, the Netopeer is no more developed neither maintained.
UNIT III
IoT Design Methodology that includes:
Applying this to our example of a smart home automation system, the purpose and requirements for
the system may be described as follows:
• Purpose : A home automation system that allows controlling of the lights in a home remotely using
a web application.
• Behavior : The home automation system should have auto and manual modes. In auto mode, the
system measures the light level in the room and switches on the light when it gets dark. In manual
mode, the system provides the option of manually and remotely switching on/off the light.
• System Management Requirement : The system should provide remote monitoring and control
functions.
• Data Analysis Requirement : The system should perform local analysis of the data.• Application
Deployment Requirement : The application should be deployed locally on the device, but should be
accessible remotely.
• Security Requirement : The system should have basic user authentication capability.
• Auto
Controls the light appliance automatically based on the lighting conditions in the room
• Light
When Auto mode is off, it is used for manually controlling the light appliance.
When Auto mode is on, it reflects the current state of the light appliance.
Unit-IV
• Introduction to Python
• Installing Python
• Python Data Types & Data Structures
• Control Flow
• Functions
• Modules
• Packages
• File Input/Output
• Date/Time Operations
• Classess
Python
Python is a general-purpose high level programming language and suitable for providing a solid
foundation to the reader in the area of cloud computing.
Python supports more than one programming paradigms including object-oriented programming
and structured programming
2. Interpreted Language
Python is an interpreted language and does not require an explicit compilation step. The Python
interpreter executes the program source code directly, statement by statement, as a processor or
scripting engine does.
3. Interactive Language
Python provides an interactive mode in which the user can submit commands at the Python prompt
and interact with the interpreter directly.
Python – Benefits
1. Easy-to-learn, read and maintain
Python is a minimalistic language with relatively few keywords, uses English keywords and has
fewer syntactical constructions as compared to other languages.
Reading Python programs feels like English with pseudo-code like constructs. Python is easy to
learn yet an extremely powerful language for a wide range of applications.
Procedure oriented paradigm allows programs to be written around procedures or functions that
allow reuse of code. Procedure oriented paradigm allows programs to be written around objects that
include both data and functionality.
3.Extendable
Python is an extendable language and allows integration of low-level modules written in languages
such as C/C++. This is useful when you want to speed up a critical portion of a program.
4 Scalable
• Due to the minimalistic nature of Python, it provides a manageable structure for large programs.
5.Portable
Python is an interpreted language, programmers do not have to worry about compilation, linking
and loading of programs. Python programs can be directly executed from source.
Python has a broad library support and works on various platforms such as Windows, Linux, Mac,
etc.
Python – Setup
Windows
• For the examples and exercise in this book, you would require Python 2.7 which can be
directly downloaded from: http://www.python.org/ftp/python/2.7.5/python-2.7.5.msi
• Once the python binary is installed you can run the python shell at the command prompt
using > python.
Linux
#Install Dependencies
sudo apt-get install build-essential
sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-
dev libc6-dev libbz2-dev
#Download Python
wget http://python.org/ftp/python/2.7.5/Python-2.7.5.tgz
tar -xvf Python-2.7.5.tgz
cd Python-2.7.5
#Install Python
./configure make sudo make install
Numbers
Number data type is used to store numeric values. Numbers are immutable data types, therefore
changing the value of a number data type results in a newly allocated object.
Example
#Integer
>>>a=5
>>>type(a)
#Floating Point
>>>b=2.5
>>>type(b)
#Long
>>>x=9898878787676L
>>>type(x)
#Complex >>>y=2+5j
>>>y
(2+5j)
>>>type(y)
<type ‘complex’>
>>>y.real
2 >>>y.imag 5
Example 2
#Addition
>>>c=a+b
>>>c
7.5 >>>type(c)
#Subtraction
>>>d=a-b
>>>d
2.5 >>>type(d)
type<’float’>
#Multiplication
>>>e=a*b
>>>e 12.5
>>>type(e)
type<’float’>
Strings
A string is simply a list of characters in order. There are no limits to the num number.
Example
#Create string
>>>s="Hello World!"
>>>type(s)
#String concatenation
>>>t="This is sample program."
>>>r = s+t
>>>r
’Hello World!This is sample program.’
#Get length of string
>>>len(s)
12
#Convert string to integer
>>>x="100"
>>>type(s)
<type ‘str’>
>>>y=int(x)
>>>y
100
Lists
List a compound data type used to group together other values. List items need not all have the same
type. A list contains items separated by commas and enclosed within square brackets.
Tuples
A tuple is a sequence data type that is similar to the list. A tuple consists of a number of values
separated by commas and enclosed within parentheses. Unlike lists, the elements of tuples cannot be
changed.
#Create a Tuple
>>>fruits=("apple","mango","banana","pineapple")
>>>fruits
(’apple’, ’mango’, ’banana’, ’pineapple’)
>>>type(fruits)
#Get length of tuple
>>>len(fruits)
4
Dictionaries
Dictionary is a mapping data type or a kind of hash table that maps keys to values. Keys in a
dictionary can be of any data type, though numbers and strings are commonly used for keys. Values in
a dictionary can be any data type or object.
Example
#Create a dictionary
>>>student={’name’:’Mary’,’id’:’8776’,’major’:’CS’}
>>>student
{’major’: ’CS’, ’name’: ’Mary’, ’id’: ’8776’}
>>>type(student)
<type ‘dict’>
#Get length of a dictionary
>>>len(student)
3
#Get the value of a key in dictionary
>>>student[’name’]
’Mary’
#Get all items in a dictionary
>>>student.items()
[(’gender’, ’female’), (’major’, ’CS’), (’name’, ’Mary’),
(’id’, ’8776’)
Type Conversions
Examples
#Convert to string
>>>a=10000
>>>str(a)
’10000’
#Convert to int
>>>b="2013"
>>>int(b)
2013
#Convert to float
>>>float(b)
2013.0
Control Flow – if statement
• The if statement in Python is similar to the if statement in other languages.
>>>a = 25**5
>>>if a>10000:
print "More"
else:
print "Less"
if a>10000:
if a<1000000:
print”Between 10k and 100k”
else:
print”More than 100k”
This behavior is different from the for statement in other languages such as C in which an
initialization, incrementing and stopping criteria are provided.
Example
Example
If a function with default values is called with fewer parameters or without any parameter, the default
values of the parameters are used.
>>>def displayFruits(fruits=[’apple’,’orange’]):
print "There are %d fruits in the list" % (len(fruits))
for item in fruits:
print item
#Using default arguments
>>>displayFruits()
apple
orange
>>>fruits = [’banana’, ’pear’, ’mango’]
>>>displayFruits(fruits)
banana
pear
mango
Python functions can have variable length arguments. The variable length arguments are passed to as
a tuple to the function with an argument prefixed with asterix (*)
Example
>>>def student(name, *varargs):
print "Student Name: " + name
for item in varargs:
print item
>>>student(’Nav’)
Student Name: Nav
>>>student(’Amy’, ’Age: 24’)
Student Name: Amy
Age: 24
>>>student(’Bob’, ’Age: 20’, ’Major: CS’)
Student Name: Bob
Age: 20
Major: C
Modules
Python allows organizing the program code into different modules which improves the code
readability and management.
• A module is a Python file that defines some functionality in the form of functions or classes. •
Modules can be imported using the import keyword.
• Modules to be imported must be present in the search path.
Example
#student module - saved as student.py
def averageGrade(students):
sum = 0.0
for key in students:
sum = sum + students[key]['grade']
average = sum/len(students)
return average
def printRecords(students):
print "There are %d students" %(len(students))
i=1
for key in students:
print "Student-%d: " % (i)
print "Name: " + students[key]['name']
print "Grade: " + str(students[key]['grade'])
i = i+1
Packages
Python package is hierarchical file structure that consists of modules and subpackages.
Packages allow better organization of modules related to a single application environment.
File Handling
Python allows reading and writing to files using the file object.
• The open(filename, mode) function is used to get a file object.
• The mode can be read (r), write (w), append (a), read and write (r+ or w+), read-binary (rb), write-
binary (wb), etc.
• After the file contents have been read the close function is called which closes the file object.
Example
# Example of reading a certain number of bytes
>>>fp = open('file.txt','r')
>>>fp.read(10)
'Python sup' >>>fp.close()
Date/Time Operation
Python provides several functions for date and time access and conversions.
• The datetime module allows manipulating date and time in several ways.
• The time module in Python provides various time-related functions.
Example
# Examples of manipulating with date
>>>from datetime import date
>>>now = date.today()
>>>print "Date: " + now.strftime("%m-%d-%y")
Date: 07-24-13
>>>print "Day of Week: " + now.strftime("%A")
Day of Week: Wednesday
>>>print "Month: " + now.strftime("%B")
Month: July
>>>then = date(2013, 6, 7)
>>>timediff = now – then
>>>timediff.days
47
Classes
Python is an Object-Oriented Programming (OOP) language. Python provides all the standard features
of Object Oriented Programming such as classes, class variables, class methods, inheritance, function
overloading, and operator overloading.
• Class
A class is simply a representation of a type of object and user-defined prototype for an object
that is composed of three things: a name, attributes, and operations/methods.
• Instance/Object
Object is an instance of the data structure defined by a class.
• Inheritance
Inheritance is the process of forming a new class from an existing class or base class.
• Function overloading
Function overloading is a form of polymorphism that allows a function to have different
meanings, depending on its context.
• Operator overloading
Operator overloading is a form of polymorphism that allows assignment of more than one
function to a particular operator.
A "Thing" in Internet of Things (IoT) can be any object that has a unique identifier and which can
send/receive data (including user data) over a network (e.g., smart phone, smart TV, computer,
refrigerator, car, etc. ).
IoT devices are connected to the Internet and send information about themselves or about their
surroundings (e.g. information sensed by the connected sensors) over a network (to other devices or
servers/storage) or allow actuation upon the physical entities/environment around them remotely.
A home automation device that allows remotely monitoring the status of appliances and controlling
the appliances.
An industrial machine which sends information abouts its operation and health monitoring data to a
server.
A wireless-enabled wearable device that measures data about a person such as the number of steps
walked and sends the data to a cloud-based services.
• Sensing
Sensors can be either on-board the IoT device or attached to the device.
• Actuation
IoT devices can have various types of actuators attached that allow taking actions upon the
physical entities in the vicinity of the device.
• Communication
Communication modules are responsible for sending collected data to other devices or cloud-
based servers/storage and receiving data from other devices and commands from remote applications.
Analysis and processing modules are responsible for making sense of the collected data.
Raspberry Pi
• Raspberry Pi is a low-cost mini-computer with the physical size of a credit card.
• Raspberry Pi runs various flavors of Linux and can perform almost all tasks that a normal desktop
computer can do.
• Raspberry Pi also allows interfacing sensors and actuators through the general purpose I/O pins.
• Since Raspberry Pi runs Linux operating system, it supports Python "out of box”
Linux on Raspberry Pi
• Raspbian
• Arch
• Pidora
• RaspBMC
• OpenELEC
Raspberry Pi Interfaces
• Serial
The serial interface on Raspberry Pi has receive (Rx) and transmit (Tx) pins for
communication with serial peripherals.
• SPI
Serial Peripheral Interface (SPI) is a synchronous serial data protocol used for communicating
with one or more peripheral devices.
• I2C
The I2C interface pins on Raspberry Pi allow you to connect hardware modules. I2C interface
allows synchronous data transfer with just two pins - SDA (data line) and SCL (clock line).
UNIT V
• WAMP – Auto Bahn for IoT
• Python for Amazon Web Services
• Python for MapReduce
• Python Packages of Interest
• Python Web Application Framework – Django
• Development with Django .
WAMP – Concepts
• Transport:
• Session:
• Client:
Clients are peers that can have one or more roles. In publish-subscribe model client can have
following roles:
Publisher:
Publisher publishes events (including payload) to the topic maintained by the Broker.
Subscriber:
Subscriber subscribes to the topics and receives the events including the payload.
In RPC model client can have following roles:
Caller: Caller issues calls to the remote procedures along with call arguments.
Callee: Callee executes the procedures to which the calls are issued by the caller and returns the
results back to the caller.
• Router: Routers are peers that perform generic call and event routing. In publish-subscribe model
Router has the role of a Broker:
Broker: Broker acts as a router and routes messages published to a topic to all subscribers subscribed
to the topic.
Dealer: Dealer acts a router and routes RPC calls from the Caller to the Callee and routes results from
Callee to Caller.
• AutoScaling Service
• Launch Configuration
Launch configuration contains instructions on how to launch new instances including the AMI-ID,
instance type, security groups, etc.
• AutoScaling Group
After creating a launch configuration, it is then associated with a new AutoScaling group.
AutoScaling group is created by calling conn.create_auto_scaling_group.
The settings for AutoScaling group such as the maximum and minimum number of instances in the
group, the launch configuration, availability zones, optional load balancer to use with the group.
Example:
• AutoScaling Policies
• After creating an AutoScaling group, the policies for scaling up and scaling down are defined.
• In this example, a scale up policy with adjustment type ChangeInCapacity and scaling_ad justment
= 1 is defined.
• Similarly a scale down policy with adjustment type ChangeInCapacity and scaling_ad justment = -1
is defined.
Example:
#Creating auto-scaling policies
scale_up_policy = ScalingPolicy(name='scale_up',
adjustment_type='ChangeInCapacity',
as_name='My-Group',
scaling_adjustment=1,
cooldown=180)
scale_down_policy = ScalingPolicy(name='scale_down',
adjustment_type='ChangeInCapacity',
as_name='My-Group',
scaling_adjustment=-1,
cooldown=180) conn.create_scaling_policy(scale_up_policy)
conn.create_scaling_policy(scale_down_policy)
• CloudWatch Alarms
• With the scaling policies defined, the next step is to create Amazon CloudWatch alarms that trigger
these policies.
• The scale up alarm is defined using the CPUUtilization metric with the Average statistic and
threshold greater 70% for a period of 60 sec.
The scale up policy created previously is associated with this alarm. This alarm is triggered when the
average CPU utilization of the instances in the group becomes greater than 70% for more than 60
seconds.
• The scale down alarm is defined in a similar manner with a threshold less than 50%.
#Connecting to CloudWatch
cloudwatch = boto.ec2.cloudwatch.connect_to_region(REGION,
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY)
alarm_dimensions = {"AutoScalingGroupName": 'My-Group'}
#Creating scale-up alarm
scale_up_alarm = MetricAlarm( name='scale_up_on_cpu', namespace='AWS/EC2',
metric='CPUUtilization', statistic='Average',
comparison='>', threshold='70',
period='60', evaluation_periods=2,
alarm_actions=[scale_up_policy.policy_arn],
dimensions=alarm_dimensions)
cloudwatch.create_alarm(scale_up_alarm)
#Creating scale-down alarm
scale_down_alarm = MetricAlarm( name='scale_down_on_cpu', namespace='AWS/EC2',
metric='CPUUtilization', statistic='Average',
comparison='<', threshold='40',
period='60', evaluation_periods=2,
alarm_actions=[scale_down_policy.policy_arn],
dimensions=alarm_dimensions)
cloudwatch.create_alarm(scale_down_alarm)
Amazon S3 – Python
• The RDS region, AWS access key and AWS secret key are passed to this function.
• After connecting to RDS service, the conn.create_dbinstance function is called to launch a new RDS
instance.
• The input parameters to this function include the instance ID, database size, instance type, database
username, database password, database port, database engine (e.g. MySQL5.1), database name,
security groups.
Example
• The map function reads the data from the standard input (stdin) and splits the tab-limited data into
document-ID and contents of the document.
• The map function emits key-value pairs where key is each word in the document and value is the
document-ID.
Example
• The key-value pairs emitted by the map phase are shuffled to the reducers and grouped by the key.
• The reducer reads the key-value pairs grouped by the same key from the standard input (stdin) and
creates a list of document-IDs in which the word occurs.
• The output of reducer contains key value pairs where key is a unique word and value is the list of
document-IDs in which the word occurs.
Example
JavaScript Object Notation (JSON) is an easy to read and write data-interchange format.
JSON is used as an alternative to XML and is is easy for machines to parse and generate.
JSON is built on two structures - a collection of name-value pairs (e.g. a Python dictionary)
and ordered lists of values (e.g.. a Python list).
• XML
XML (Extensible Markup Language) is a data format for structured document interchange.
The Python minidom library provides a minimal implementation of the Document Object Model
interface and has an API similar to that in other languages.
• SMTPLib
Simple Mail Transfer Protocol (SMTP) is a protocol which handles sending email and routing e-
mail between mail servers. The Python smtplib module provides an SMTP client session object that
can be used to send email.
• NumPy
NumPy is a package for scientific computing in Python. NumPy provides support for large multi-
dimensional arrays and matrices
• Scikit-learn
Scikit-learn is an open source machine learning library for Python that provides implementations
of various machine learning algorithms for classification, clustering, regression and dimension
reduction problems.
Python Web Application Framework
Django is an open source web application framework for developing web applications in Python.
• A web application framework in general is a collection of solutions, packages and best practices
that allows development of web applications and dynamic websites.
• Django is based on the Model-Template-View architecture and provides a separation of the data
model from the business rules and the user interface.
• Django provides a unified API to a database backend.
• Thus web applications built with Django can work with different databases without requiring any
code changes.
• With this fiexibility in web application design combined with the powerful capabilities of the
Python language and the Python ecosystem, Django is best suited for cloud applications.
Django Architecture
• Model
The model acts as a definition of some stored data and handles the interactions with the
database. In a web application, the data can be stored in a relational database, non-relational database,
an XML file, etc.
A Django model is a Python class that outlines the variables and methods for a particular type
of data.
• Template
In a typical Django web application, the template is simply an HTML page with a few extra
placeholders.
Django’s template language can be used to create various forms of text files (XML, email,
CSS, Javascript, CSV, etc.)
• View
The view ties the model to the template. The view is where you write the code that actually
generates the web pages. View determines what data is to be displayed, retrieves the data from the
database and passes the data to the template.
PREPARED BY
Mrs.M.SUBHA.,MCA.,M.Phil.,
Assistant Professor & Head,
Mr.P.KARUPPUSAMY.,M.Sc.,M.Phil.,
Assistant Professor,
Department of Computer Science,
Shree Venkateshwara Arts & Science (Co-education) College,
Gobi-638 455.