Python For IoT
Python For IoT
PROGRAMMING IN
IOT
Shashika Lokuliyana
Beginning Python 1
General Information
Beginning Python 2
The Python Shell
■ Because Python is interpretive, you can do simple things with the shell
■ In the graphical shell on Linux, double-click on LXTerminal or use the python IDE
■ At the prompt, type Python
■ You should have a >>> prompt
■ Type in:
print(“hello, world”)
■ You have written your first Python program
■ Keep the shell up; we’ll be using it
Beginning Python 3
The Python Shell
■ This is good for simple calculations but not for real programming
■ For programming, we’ll use Idle
■ There are two versions: Idle for Python 2.7 and Idle3 for Python 3.2
■ For most of what we do, we’ll have to use 2.7 because Bluetooth doesn’t seem to
work with 3.2
■ You’ll run as the “superuser” because otherwise you won’t have access to the GPIO
pins
■ Idle will give you access to a shell but also to an IDE for writing and saving programs
Beginning Python 4
Python Modules
Beginning Python 5
Python Modules
■ Press F5
■ It will ask you to save the file before you run it
■ Save it to your home directory as HelloWorld.py
■ You must provide the .py extension
■ If you want to run it outside of the development environment simply type:
python HelloWorld.py
■ Note that Linux is case sensitive
Beginning Python 6
Variables
Beginning Python 7
Comments
Beginning Python 8
Operators
Beginning Python 9
Expressions
■ When integers and reals are mixed, the result is a real number.
– Example: 1 / 2.0 is 0.5
Beginning Python 10
Math Functions
Beginning Python 11
Relational Operators
Beginning Python 12
Logical Operators
Beginning Python 13
The if Statement
■ Syntax:
if <condition>:
<statements>
x = 5
if x > 4:
print “x is greater than 4”
print “This is not in the scope of the if”
Beginning Python 14
The if Statement
Beginning Python 15
The if/else Statement
if <condition>:
<statements>
else:
<statements>
Beginning Python 16
The for Loop
■ This is similar to what you’re used to from C or Java, but not the same
■ Syntax:
for variableName in groupOfValues:
<statements>
■ variableName gives a name to each value, so you can refer to it in the statements.
■ groupOfValues can be a range of integers, specified with the range function.
■ Example:
for x in range(1, 6):
print x, "squared is", x * x
Beginning Python 17
Range
■ The range function specifies a range of integers:
range(start, stop) - the integers between start (inclusive)
and stop (exclusive)
■ It can also accept a third value specifying the change between values.
range(start, stop, step) - the integers between start (inclusive)
and stop (exclusive) by step
Beginning Python 18
The while Loop
Beginning Python 19
Exercise
■ Write a Python program to compute and display the first 16 powers of 2, starting
with 1
■ Do this in the Python shell
Beginning Python 20
Strings
■ String: A sequence of text characters in a program.
■ Strings start and end with quotation mark " or apostrophe ' characters.
■ Examples:
"hello"
"This is a string"
"This, too, is a string. It can be very long!"
■ A string may not span across multiple lines or contain a " character.
"This is not
a legal String."
"This is not a "legal" String either."
Beginning Python 21
Strings
Beginning Python 22
Indexing Strings
■ As with other languages, you can use square brackets to index a string as if it were
an array:
name = “Arpita Nigam”
print(name, “starts with “, name[0])
Beginning Python 23
String Functions
Beginning Python 24
Byte Arrays and Strings
Beginning Python 25
Other Built-in Types
Beginning Python 26
Tuples
Beginning Python 27
Lists
Beginning Python 28
Dictionaries
Beginning Python 29
Sets
■ Sets are similar to dictionaries in Python, except that they consist of only keys with
no associated values.
■ Essentially, they are a collection of data with no duplicates.
■ They are very useful when it comes to removing duplicate data from data collections.
Beginning Python 30
Writing Programs
Beginning Python 31
Writing Functions
■ Define a function:
def <function name>(<parameter list>)
■ The function body is indented one level:
def computeSquare(x)
return x * x
# Anything at this level is not part of the function
Beginning Python 32
Error Handling
Beginning Python 33
Error Handling
Beginning Python 34
Using the GPIO Pins
■ The Raspberry Pi has a 40-pin header, many of which are general-purpose I/O pins
■ Include the library:
import RPi.GPIO as GPIO
■ Set up to use the pins:
GPIO.setmode(GPIO.BOARD)
Beginning Python 35
Using the GPIO Pins
■ The GPIO.BCM option means that you are referring to the pins by the "Broadcom SOC
channel" number, these are the numbers after "GPIO" in the green rectangles around the
outside of the diagram:
Beginning Python 36
Using the GPIO Pins
Beginning Python 37
Using the GPIO Pins
Beginning Python 38
Using the GPIO Pins
■ Output to GPIO:
if cmd=='LAMPON':
cmdlist["LAMPSTATUS"] = True;
GPIO.output(LAMP, True) # turn on the light
Beginning Python 39
Programming Exercise
■ Write a Python program that blinks an LED at a rate of 1 second on, one second off
■ To do this, you’ll need to use the idle3 environment
Beginning Python 40
Python File I/O
■ You can read and write text files in Python much as you can in other languages, and
with a similar syntax.
■ To open a file for reading:
try:
configFile = open(configName, "r")
except IOError as err:
print(“could not open file: “ + str(err))
Beginning Python 41
Python File I/O
while 1:
line = configFile.readline()
if len(line) == 0:
break
Beginning Python 42
Python File I/O
■ You can also read all lines from a file into a set, then iterate over the set:
lines = file.readlines()
for line in lines:
print(line)
file.close()
Beginning Python 43
Python File I/O
Beginning Python 44