Fiot Unit-3
Fiot Unit-3
String
x = ‘This is Python’
print x >>This is Python
print x[0] >>T
print x[2:4] >>is
Tuple
Dictionary
d = {1:‘item','k':2}
statement 1
statement 2
return x # Returning the value
Output:: Hi!
val = greater(10,
100) print(val)
Output:: (100,10)
Introduction to Internet of Things 11
Functions as Objects
Functions can also be assigned and reassigned to the variables.
Example:
def add (a,b)
return a+b
print (add(4,6))
c = add(4,6)
print c
Output:: 10 10
Introduction to Internet of Things 12
Variable Scope in Python
Global variables:
These are the variables declared out of any function , but can be
accessed inside as well as outside the function.
Local variables:
These are the ones that are declared inside a function.
def example():
l_var = 100
print(g_var)
Output:: 10
Introduction to Internet of Things 14
Example showing Variable Scope
var = 10
def example():
var = 100
print(var)
Output:: 100
10
Introduction to Internet of Things 15
Modules in Python
Any segment of code fulfilling a particular task that can be
used commonly by everyone is termed as a module.
Syntax:
import module_name #At the top of the code
for i in range(1,10):
val = random.randint(1,10)
print (val)
Example:
from math import pi
print (pi)
Output:: 3.14159
Read: Write:
PIL is supported till python version 2.7. Pillow supports the 3x version of
python.
# Create a
TCP/IP socket
sock =
socket.socket(
socket.AF_IN
ET,
socket.SOCK_
STREAM)
Introduction to Internet of Things 17
#Receive command
data = connection.recv(1024)
print(data)
sock.close()
# Create a
TCP/IP socket
client_socket
=
socket.socket(
socket.AF_IN
ET,
socket.SOCK_
STREAM)
client_socket.connect(("10.14.88.82", 2017))
Code Snapshot
1
Introduction to Internet of
What is Raspberry
Pi?
2
Introduction to Internet of
Variants and Specifications of
Raspberry Pi
Key features Raspberry pi 3 model B Raspberry pi 2 Raspberry Pi zero
model B
RAM 1GB SDRAM 1GB SDRAM 512 MB SDRAM
CPU Quad cortex [email protected] Quad cortex ARM 11@ 1GHz
A53@900MHz
GPU 400 MHz video core IV 250 MHz video core IV 250 MHz video core IV
Ethernet 10/100 10/100 None
Wireless 802.11/Bluetooth 4.0 None None
Video output HDMI/Composite HDMI/Composite HDMI/Composite
GPIO 40 40 40
3
Introduction to Internet of
Basic Architecture
RAM
ETHERNET USB
4
Introduction to Internet of
Raspberry Pi
Introduction to Internet of
Raspberry Pi GPIO
Source: Raspberry Pi PCB Pin Overview, Wikimedia Commons (Online) Source: Raspberry Pi GPIO, Wikimedia Commons (Online)
Basic Set up for Raspberry
Pi
• HDMI cable.
• Monitor.
• Key board.
• Mouse.
• 5volt power adapter for raspberry pi.
• LAN cable .
• Min- 2GB micro sd card
Basic Set up for Raspberry
Pi
Operating
System
Official Supported OS :
• Raspbian
• NOOBS
Enable SSH
Step1 : Open command prompt and type sudo raspi-config and press enter.
Default installed :
• Python
•C
• C++
• Java
• Scratch
• Ruby
Note : Any language that will compile for ARMv6 can be used with raspberry pi.
Source: Programming languages for Raspberry Pi, eProseed, Lonneke Dikmans, August 07, 2015
Popular Applications
• Media streamer
• Home automation
• Controlling BOT
• VPN
• Light weight web server for IOT
• Tablet computer
Topics Covered
Using GPIO pins
Taking pictures using PiCam
raspistill -o image.jpg
Python Code:
Import picamera
camera = picamera.PiCamera()
camera.capture('image.jpg')
Source: PYTHON PICAMERA, Raspberry Pi Foundation
Mechanical/electromechanical
switch
3 output terminals (left to
right)
NO (normal open):
Common
NC (normal close)
HT
Program: DHT22 with
Pi
import RPi.GPIO as GPIO
from time import sleep
import Adafruit_DHT #importing the Adafruit library
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
sensor = Adafruit_DHT.AM2302 # create an instance of the sensor type
print (‘Getting data from the sensor’)
#humidity and temperature are 2 variables that store the values received from the sensor
Set the GPIO pin connected with the relay’s input pin as output in the sketch
GPIO.setup(13,GPIO.OUT)
Set the relay pin high when the temperature is greater than 30
if temperature > 30:
GPIO.output(13,0) # Relay is active low
print(‘Relay is on')
sleep(5)
GPIO.output(13,1) # Relay is turned off
after delay of 5 seconds
18