You are on page 1of 18

MFA Design & Technology

IOT Fall 2015


Ayodamola (Ayo) Okunseinde | okuna291@newschool.edu

Intro to Python

History of Python
https://docs.python.org/2/tutorial/introduction.html#

"Python is an interpreted, interactive, object-oriented programming


language".

Python is similar in some ways to Perl and Java. Knowing Perl or Java will
help you learn Python, but it's just as easy to pick it up as a language.
Python is great for both shell and web scripting and is making it's way into
every facet of computing from APIs to video and interactivity.

Guido van Rossum first created Python in the late eighties and early
nineties. It is now maintained by a core development team.

Why Python
Python is Efficient : Iterative processes, good with lists, memory management
Python is Fast : Even if interpretive language can add decorators to speed up. Increasing in performance
Python is Broad : Systems automation, testing, gaming, CGI, and web development. Many modules
Python is Easy : Readable syntax, easy for non-programmer types, plays well with other languages
Python is Fun : Broaden your programing base, show off, reason differently

Basic Python math/strings


On Laptop Terminal type - $ python
>>> 2+2
>>> (5-20*2)/4
>>> 7**2
>>> width = 20
>>> height = 5 * 9
>>> width * height

4
-9
49

900

>>> s="hot"
>>> p="dog"
>> print s+p

hotdog

>>> j = "hotdog wa oishii desu"


>>> print j[10:16]
oishii
>>> len(j)
21

Basic Python list/while


>>> s=[1,4,5,6]
>>> s[1]=20
>>> print s
>>> a, b = 0, 1
>>> while b < 10:
print b
a, b = b, a+b

>>> a, b = 0, 5
>>> while b<1000:
print b
[1, 20, 5, 6]
a,b = b, a+b

1.1.2.3.5.8

5.5.10.15.25

>>> import time


>>> while True:
time.sleep(2)
print "sleeping"

sleeping...

Basic Python if/for


>>> x=int(raw_input(#?))
>>> if x < 0:
print Neg Number
elif x == 0:
print Number is 0
elif x == 1:
print Number is 1
else:
print Other

>>> words = [neko,inu,ratto,sakana]


>>> for w in words:
print w, len (w)
>>> for i in range (5,100,2):
print i
>>> words = [ringo,nashi,hana,ki]
>>> for i in range (len(words)):
print i, words[i]

Basic Python functions


>>> def fib(n): #fibonacci function
a, b =0,1
while a<n:

print a

a,b=b,a+b
>>> fib(2000)

>>> def greeting(n): #hello function


name = raw_input(enter name )
print hello +name+you are + n

Script - ip-email.py
Laptop Terminal

Raspberry Pi

$ cd /home/path/toscript
$ touch ip-email.py
$ sudo nano ip-email.py
paste contents of script into terminal
$ sudo python ip-email.py

ssh and log into pi


$ cd Desktop
$ touch ip-email.py
$ sudo nano ip-email.py
paste contents of ip-email.py into terminal
$ sudo nano /etc/rc.local
write path of script before exit 0
$ sudo python /home/pi/Desktop/ip-email.py

Script - email-read.py, email-notify.py

Python update/GPIO
Install latest python 2.7 and python pip
$ sudo apt-get install python-pip python2.7-dev
Update distribution
$ sudo easy_install -U distribute
Install feedparser
$ sudo pip install feedparser

GPIO

GPIO
http://elinux.org/RPi_Low-level_peripherals#GPIO_Code_examples

GPIO.BOARD

GPIO.BCM

Digital Output - gpio-blink.py

BOARD / BCM
Resistors

Digital Input - gpio-buttons.py

Resistors
Debounce
Pullup
Pulldown
Digital in only

Analog Input - gpio-rc.py

ADC
RC analog input

http://www.raspberrypi-spy.co.
uk/2013/10/analogue-sensors-on-the-raspberry-piusing-an-mcp3008/

https://arduinodiy.wordpress.com/2013/10/19/793/

Analog Output - gpio-angout.py

PWM
Fading LED
Servo

Assignment

You might also like