Nptel: Implementation of Iot With Raspberry Pi: Part 2
Nptel: Implementation of Iot With Raspberry Pi: Part 2
Nptel: Implementation of Iot With Raspberry Pi: Part 2
PT
Dr. Sudip Misra
Associate Professor
N
Department of Computer Science and Engineering
IIT KHARAGPUR
Email: smisra@sit.iitkgp.ernet.in
Website: http://cse.iitkgp.ac.in/~smisra/
Internet Of Things
Creating an interactive environment
EL
Network of devices connected together
PT
N
Introduction to Internet of Things 2
IOT: Remote Data Logging
EL
Control the network remotely
PT
N
Introduction to Internet of Things 3
IOT: Remote Data Logging
System Overview:
A network of Temperature and humidity sensor connected with
EL
Raspberry Pi
Read data from the sensor
PT
Send it to a Server
Save the data in the server
N
Introduction to Internet of Things 4
IOT: Remote Data Logging (contd..)
Requirements
DHT Sensor
EL
4.7K ohm resistor
PT
Jumper wires
Raspberry Pi
N
Introduction to Internet of Things 5
DHT Sensor
Digital Humidity and
Temperature Sensor (DHT)
EL
PIN 1, 2, 3, 4 (from left to
right)
PIN 1- 3.3V-5V Power
PT
supply
PIN 2- Data
PIN 3- Null
N
PIN 4- Ground
EL
Connect pin 2 of DHT sensor to any
input pins of Raspberry Pi, here we
have used pin 11
PT
Connect pin 4 of DHT sensor to the
ground pin of the Raspberry Pi
N
Introduction to Internet of Things 7
Read Data from the Sensor
Adafruit provides a library to work with the DHT22 sensor
EL
Use the function Adafruit_DHT.read_retry() to read data from the sensor
PT
N
Source: ADAFRUIT DHTXX SENSORS, Lady Ada, 2012-07-29
Code Output
EL
PT
N
Introduction to Internet of Things 9
Sending Data to a Server
EL
Establish connection between the server and the client
Send data from the client to the server
PT
N
Introduction to Internet of Things 10
Sending Data to a Server (contd..)
Socket Programming:
Creates a two-way communication between two nodes in a network
EL
The nodes are termed as Server and Client
Server performs the task/service requested by the client
PT
N
Introduction to Internet of Things 11
Sending Data to a Server (contd..)
Creating a socket:
s = socket.socket (SocketFamily, SocketType, Protocol=0)
EL
SocketFamily can be AF_UNIX or AF_INET
PT
SocketType can be SOCK_STREAM or SOCK_DGRAM
Protocol is set default to 0
N
Source: PYTHON NETWORK PROGRAMMING, TutorialsPoint
Server:
s = socket.socket() # creating a socket object
EL
host = socket.gethostname() # local machine name/address
port = 12321 # port number for the server
s.bind((host, port)) # bind to the port
PT
s.listen(5) # waiting for the client to connect
while True:
c, addr = s.accept() # accept the connection request from the client
print ‘Connected to', addr
c.send(‘Connection Successful')
c.close()
N
#close the socket
Source: PYTHON NETWORK PROGRAMMING, TutorialsPoint
EL
s = socket.socket() # creating a socket object
host = socket.gethostname() # getting local machine name
port = 12345 # assigning a port
PT
s.connect((host, port))
print s.recv(1024)
s.close
N
Source: PYTHON NETWORK PROGRAMMING, TutorialsPoint
def sensordata():
EL
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
PT
sensor = Adafruit_DHT.AM2302
humidity, temperature = Adafruit_DHT.read_retry(sensor,17)
return(humidity, temperature)
N
This function returns the values from the DHT sensor
EL
while (1):
h,t = sensordata()
message = str(h)+','+str(t)
PT
#Send data
print >>sys.stderr, 'sending "%s"' % message
EL
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Bind the socket to the port
server_address = ('10.14.3.194', 10001)
PT
sock.bind(server_address)
while True:
data, address = sock.recvfrom(4096)
with open(“Datalog.txt","a") as f:
mess=str(data)
f.write(mess)
print mess
N
f.close()
EL
The server receives the data
from the client and saves it in
a text file DataLog.txt
PT
N
Introduction to Internet of Things 18
EL
PT
N
Introduction to Internet of Things 19
EL
Implementation of IoT with Raspberry Pi: Part 3
PT
Dr. Sudip Misra
Associate Professor
N
Department of Computer Science and Engineering
IIT KHARAGPUR
Email: smisra@sit.iitkgp.ernet.in
Website: http://cse.iitkgp.ac.in/~smisra/
Internet Of Things
Creating an interactive environment
EL
Network of devices connected together
PT
N
Introduction to Internet of Things 2
IOT: Remote Data Logging
EL
Processing the data
PT
Respond to the network
N
Introduction to Internet of Things 3
IOT: Remote Data Logging
System Overview:
A network of Temperature and humidity sensor connected with
EL
Raspberry Pi
Read data from the sensor
PT
Send it to a Server
Save the data in the server
Data Splitting
Plot the data
N
Introduction to Internet of Things 4
IOT: Remote Data Logging (contd..)
Requirements
DHT Sensor
EL
4.7K ohm resistor
PT
Jumper wires
Raspberry Pi
N
Introduction to Internet of Things 5
DHT Sensor
Digital Humidity and
Temperature Sensor (DHT)
EL
PIN 1, 2, 3, 4 (from left to
right)
PIN 1- 3.3V-5V Power
PT
supply
PIN 2- Data
PIN 3- Null
N
PIN 4- Ground
EL
Connect pin 2 of DHT sensor to any
input pins of Raspberry Pi, here we
have used pin 11
PT
Connect pin 4 of DHT sensor to the
ground pin of the Raspberry Pi
N
Introduction to Internet of Things 7
Read Data from the Sensor
Use the Adafruit library for DHT22 sensor to read the sensor
data
EL
PT
N
Introduction to Internet of Things 8
Sending Data to a Server
EL
Establish connection between the two
Send data from the client to the server
PT
Save the data in a file
N
Introduction to Internet of Things 9
Data Processing
EL
Data splitting/filtering
Data plotting
PT
N
Introduction to Internet of Things 10
Data Processing
Data splitting/filtering:
Data from the client is saved in a text file
The values are separated by a comma(‘ , ’)
EL
message = str(h)+','+str(t)
Split() function can be used to split a string into multiple strings depending on the type of
PT
separator/delimiter specified.
Example:
Data= ‘sunday,monday,tuesday’ #Data is a string with 3 words separated by a comma
Data.split(“,”)
[‘sunday’,’monday’,’tuesday’] N # split the data whenever a “,” is found
# Gives 3 different strings as output
EL
Plot(x,y): plots the values x and y
xlabel(‘X Axis'): Labels the x-axis
PT
ylabel(‘Y Axis'): Labels the y-axis
title("Simple Plot"): Adds title to the plot
N
Source: MATPLOTLIB, John Hunter, Darren Dale, Eric Firing, Michael Droettboom and the Matplotlib development team, 2012 - 2016
EL
myplot.plot([1,2,3,4])
myplot.ylabel(‘Y-Axis’)
PT
myplot.show()
By default the values are taken for y-axis, values for x-axis are generated automatically starting
from 0
N
Source: MATPLOTLIB, John Hunter, Darren Dale, Eric Firing, Michael Droettboom and the Matplotlib development team, 2012 - 2016
Basic Plot:
EL
PT
N
Introduction to Internet of Things 14
Data Proceessing (contd..)
EL
grid(): Enable or disable axis grids in the plot
ion(): turns on the interactive mode
PT
subplot(): Adds subplot in a figure
Close(): Close the current figure window
N
Scatter(): make a scatter plot of the given points
Source: MATPLOTLIB, John Hunter, Darren Dale, Eric Firing, Michael Droettboom and the Matplotlib development team, 2012 - 2016
EL
def sensordata(): while (1):
GPIO.setmode(GPIO.BOARD) h,t = sensordata()
GPIO.setwarnings(False) message = str(h)+','+str(t) #Send data
PT
sensor = Adafruit_DHT.AM2302 print >>sys.stderr, 'sending "%s"' % message
sent = sock.sendto(message, server_address)
humidity, temperature =
finally:
Adafruit_DHT.read_retry(sensor,17)
N
return(humidity, temperature)
print >>sys.stderr, 'closing socket'
sock.close()
EL
ax.plot(hum,i, c='b', marker=r'$\Phi$')
hum=data.split(",")[0] plt.xlabel('Humidity ($\%$)')
tem=data.split(",")[1] ax.grid()
print 'temp='+(str(tem))+'iter='+str(i) fig.show()
PT
plt.ion() fig.canvas.draw()
fig=plt.figure(num=1,figsize=(6,6))
plt.title(' IoT Temperature and Humidity Monitor') sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ax = fig.add_subplot(121)
EL
while True:
data, address = sock.recvfrom(4096)
with open("DataLog.txt","a") as f:
PT
mess=str(data)
f.write(mess)
coverage_plot(mess,i)
print mess
i+=1
f.close() N
Introduction to Internet of Things 18
Output
The Reading from the sensor is
sent to the Server and saved in
a text file.
EL
Two different plots for
temperature and humidity
data
PT
N
Introduction to Internet of Things 19
Output
EL
PT
N
Introduction to Internet of Things 20
EL
PT
N
Introduction to Internet of Things 21
EL
Software-Defined Networking – Part I
Restructuring the Current Network Infrastructure
PT
Dr. Sudip Misra
Associate Professor
N
Department of Computer Science and Engineering
IIT KHARAGPUR
Email: smisra@sit.iitkgp.ernet.in
Website: http://cse.iitkgp.ac.in/~smisra/
EL
PT
User II
User I
N
Introduction to Internet of Things 2
Overview of Current Network
OSPF Protocol executing
at the switches
EL
PT
User II
User I
N
Introduction to Internet of Things 3
Overview of Current Network
OSPF Protocol executing
at the switches
EL
PT
User II
User I
N
the switch has been attacked!
EL
PT
User II
User I
N
the switch has been attacked!
needs to route through an alternate path!
Present: No centralized control.
app
app
EL
OS
OS hardware app
hardware OS
app
PT
OS hardware
app
hardware
OS app
hardware OS Switches forward traffic
EL
Switches are required to configure according to the installed
operating system (OS).
PT
Centralized control is not feasible in traditional network.
N
Introduction to Internet of Things 7
Limitations in Current Network
Routing, mobility
EL
Thousands app app app
management, etc.
lines of
Operating system
PT
code Cost-expensive
EL
OS OS
hardware
hardware
hardware
PT
hardware app app
OS OS
hardware
N hardware
EL
Operating system
PT
packet packet packet
N
forwarding
hardware
forwarding
hardware
forwarding
hardware
EL
2008: The idea of software-defined network is originated from OpenFlow project
(ACM SIGCOMM 2008).
PT
2009: Stanford publishes OpenFlow V1.0.0 specs.
June 2009: Nicira network is founded.
N
March 2011: Open Networking Foundation is formed.
Oct 2011: First Open Networking Summit. Many Industries (Juniper, Cisco
announced to incorporate.
EL
PT
N -
-
Notes
Northbound API
Southbound API
(via OpenFlow)
EL
Control the entire network including individual switches
PT
Communication between the application, control, and data
planes are done through APIs
N
Introduction to Internet of Things 13
Components/Attributes of SDN
Hardware switches
Controller
EL
Applications
PT
Flow-Rules
Application programming interfaces (APIs)
N
Introduction to Internet of Things 14
Current Status of SDN
EL
It is required to change the current network with SDN in a
phased manner.
PT
Operational cost and delay caused due to link failure can be
significantly minimized.
N
Introduction to Internet of Things 15
Challenges
Rule placement
Controller placement
EL
PT
N
Introduction to Internet of Things 16
Rule Placement I
EL
Traditionally, Routing Table in every switch (L3 switch/router). SDN
maintains Flow Table at every switch.
PT
Flow-Rule: Every entry in the Flow Table.
Each rule has a specific format, which is also defined by a
N
protocol (e.g., OpenFlow).
EL
PT
N
Example of a flow-rule based on OpenFlow protocol
Source: http://networkstatic.net/wp-content/uploads/2013/06/OFP_normal_rules.png
EL
Limited number of rules can be inserted.
Fast processing is done using TCAM at the switches.
PT
TCAM is very cost-expensive.
N
Introduction to Internet of Things 19
Rule Placement Challenges II
On receiving a request, for which no flow-rule is present in
the switch, the switch sends a PACKET-IN message to the
Controller
EL
controller.
The controller decides a suitable
PT
flow-rule for the request.
The flow-rule is inserted at the switch.
N
Typically, 3-5ms delay is involved in a
new rule placement
EL
How to define rules, so that less number of PACKET-IN
messages are sent to controller.
PT
N
Introduction to Internet of Things 21
OpenFlow Protocol I
EL
different number of match-fields.
PT
N
Source: http://networkstatic.net/wp-content/uploads/2013/06/OFP_normal_rules.png
Different match-fields
Source IP
EL
Destination IP
Source Port
PT
Priority
etc.
N
Introduction to Internet of Things 23
OpenFlow Protocol III
EL
All rules are deleted from the switch at hard timeout.
This can used to reset the switch.
PT
Soft timeout
If NO flow is received associated with a rule for a particular time, the
rule is deleted. N
This is used to empty the rule-space by deleting an unused rule.
EL
OpenFlow is a protocol used to communicate between data-plane and
control-plane.
PT
We may have other protocols for this purpose. However, OpenFlow is
the only protocol present today.
N
Introduction to Internet of Things 25
OpenFlow Switch Software
Indigo: Open source, it runs on Mac OS X.
LINC: Open source, it runs on Linux, Solaris, Windows, MacOS, and
EL
FreeBSD.
Pantou: Turns a commercial wireless router/access point to an
PT
OpenFlow enabled switch. OpenFlow runs on OpenWRT.
Of13softswitch: User-space software switch based on Ericsson
TrafficLab 1.1 softswitch.
N
Open vSwitch: Open Source, it is the MOST popular one present
today.
Basics of SDN
Challenges present in SDN
EL
Rule Placement with OpenFlow
PT
Controller Placement – to be discussed in next lecture
N
Introduction to Internet of Things 27
EL
PT
N
Introduction to Internet of Things 28
EL
Software-Defined Networking – Part II
Restructuring the Current Network Infrastructure
PT
Dr. Sudip Misra
Associate Professor
N
Department of Computer Science and Engineering
IIT KHARAGPUR
Email: smisra@sit.iitkgp.ernet.in
Website: http://cse.iitkgp.ac.in/~smisra/
EL
layers
Rule Placement – TCAM and Delay
PT
OpenFlow protocol – flow-rule and math-fields
N
Introduction to Internet of Things 2
APIs in SDN
Southbound API
Used to communicate between control layer and infrastructure layer.
EL
OpenFlow protocol is used.
Northbound API
PT
Used to communicate between control layer and application layer.
Standard APIs are used.
East-Westbound APIs
N
Used to communicate among multiple controllers in the control layer.
EL
The controllers must be able to handle all incoming requests
from switches.
PT
Rule should be placed without incurring much delay.
Typically, a controller can handle 200 requests in a second
N
(through a single thread).
EL
Physically, they are connected to the switches in multi-hop distance.
If we have a very small number of controllers for a large
PT
network, the network might be congested with control
packets (i.e., PACKET-IN messages).
N
Introduction to Internet of Things 5
Flat Architecture
EL
PT
Packet-IN
Flow-Rule
N
Introduction to Internet of Things 6
Hierarchical (tree) Architecture
EL
PT
N
Introduction to Internet of Things 7
Ring Architecture
EL
PT
N
Introduction to Internet of Things 8
Mesh Architecture
EL
PT
User II
User I
N
Introduction to Internet of Things 9
Control Mechanisms
Distributed
The control decisions can be taken in a distributed manner
EL
Ex: each subnetwork is controlled by different controller
Centralized
PT
The control decisions are taken in a centralized manner.
Ex: A network is controlled by a single controller.
N
Introduction to Internet of Things 10
Backup Controller
EL
Replica of the main controller is created
If the main controller is down, backup controller controls the network
PT
to have uninterrupted network management.
N
Introduction to Internet of Things 11
Security I
EL
Proxy
HTTP
PT
Intrusion detection system (IDS)
N
12
Security II
EL
PT
Example of potential data plane ambiguity to implement the policy chain
N
Firewall-IDS-Proxy in the example topology.
Simulator/Emulator
Infrastructure deployment – MUST be supported with OpenFlow
EL
Controller placement – MUST support OpenFlow
Remote – controller can be situated in a remote place, and communicated
PT
using IP address and port number
Local
N
Introduction to Internet of Things 14
Switch Deployment
Mininet
Used to create a virtual network with OpenFlow-enabled switches
EL
Based on Python language
Supports remote and local controllers
PT
N
Introduction to Internet of Things 15
Controller Configuration Software
Pox
Nox
EL
FloodLight
PT
OpenDayLight [Popular!]
ONOS [Popular!]
N
Introduction to Internet of Things 16
Summary
EL
Control message overhead may be increased due to
additional number of packets (PACKET-IN messages).
PT
Unified network management is possible using SDN, while
leveraging global view of the network.
N
Introduction to Internet of Things 17
EL
PT
N
Introduction to Internet of Things 18
EL
Software-Defined IoT Networking – Part I
Recent Advances of SDN in IoT
PT
Dr. Sudip Misra
Associate Professor
N
Department of Computer Science and Engineering
IIT KHARAGPUR
Email: smisra@sit.iitkgp.ernet.in
Website: http://cse.iitkgp.ac.in/~smisra/
EL
PT
Source: https://image.slidesharecdn.com
N Source: http://www.luxhotels.info/p/46800/internet-of-things-iot/
EL
making
Visibility of network resources – network management is
PT
simplified based on user, device and application-specific
requirements
N
Intelligent traffic pattern analysis and coordinated decisions
EL
PT
N
Source: https://image.slidesharecdn.com
EL
Control of end-devices, such as sensors
and actuators
PT
N
Introduction to Internet of Things 5
SDN for IoT III
EL
Rule-placement at access devices,
while considering mobility and
PT
heterogeneity of end-users
N
Introduction to Internet of Things 6
SDN for IoT IV
EL
Rule-placement and traffic
engineering at backbone networks
PT
N
Introduction to Internet of Things 7
SDN for IoT V
EL
Flow classification and enhanced
security at data center networks
PT
N
Introduction to Internet of Things 8
Wireless Sensor Network I
Challenges
Real-time programming of sensor nodes
EL
Vendor-specific architecture
Resource constrained – heavy computation cannot be performed
PT
Limited memory – cannot insert too many control programs
N
Introduction to Internet of Things 9
Wireless Sensor Network II
Opportunities
Can we program the sensor nodes in real-time?
EL
Can we change the forwarding path in real-time?
Can we integrate different sensor nodes in a WSN?
PT
N
Introduction to Internet of Things 10
Software-Defined WSN I
EL
Forward the sensed data if exceeds a certain value
ID-centric data forwarding
PT
Forward the sensed data based on the ID of the source node
N
Real-life implementation of such method NOT done
EL
Sensor management
Multiple sensors can be implemented in a single sensor board
PT
Sensors can be used depending on application-specific requirements
Delay management
Delay for sensing can be changed dynamically in real-time
N
Active-Sleep Management
States of active and sleep mode can be changed dynamically
Soft-WSN
Topology Management
EL
Node-specific management – forwarding logic of a particular sensor can
be modified
PT
Network-specific management
Forward all traffic of a node in the network
Drop all traffic of a node in the network
N
Experimental results show that network performance can be improved using
software-defined WSN over traditional WSN
EL
PT
N
Packet delivery ratio in the network increases using Soft-WSN
compared to the traditional WSN.
EL
PT
N
Number of replicated data packets is reduced using Soft-WSN over
the traditional WSN.
EL
PT
N
Number of control messages in the network is higher using Soft-WSN over the
traditional WSN. This is due to the PACKET-IN message in the network. Each time a
node receives a new packet, it asks the controller for getting adequate forwarding logic.
EL
Flow-table for rule placement at sensor nodes is designed
Any programming language can be used through API to program the
PT
nodes in real-time
N
Introduction to Internet of Things 17
SDN-WISE Protocol Stack
Sensor node includes
IEEE 802.15.4 protocol
EL
Micro control unit (MCU)
Above IEEE 802.15.4 stack,
PT
Forwarding layer consists of
Flow-rules.
INPP – In Network Packet
Processing N
Source: Galluccio et al., IEEE INFOCOM ’15
EL
SDN-based applications
Network performance can be improved significantly using
PT
SDN-based approaches over the traditional approaches
N
Introduction to Internet of Things 19
EL
PT
N
Introduction to Internet of Things 20