6. Courses agenda – Day4
1. Working with CRUD, Database and ORM
2. Making http server
3. Developing Web with framework – flask
4. Access GPIOs in Raspberry PI
5. Control LCD in Raspberry PI
6. Project – using Flask and python access to I/O to make a
simple control center of Raspberry pi via web
24. SQLAlchemy - Creating a Database
A mapper code create the variables as we create
Each column of the table in the database when we create a column we need to pass in some attribute in it
30. Exercise - 1
• Implement a db_setup.py by using
configure/class/table/mapper we told
previously
1. Setup a new database called
employee.db
2. Create 2 tables – one named Employee
and another names Address by ORM
3. Create the column with the proper
datatype and its foreign key
relationship
4. Open employee.db by DB Browser for
SQLite http://sqlitebrowser.org/ to
check if you created correctly
92. GPIO
• Can be configured to be input (read HIGH/LOW – 3.3v or 0 v) or output
• a 40-pin header marked J8, arranged as 2x20 pins All the GPIO pins can be reconfigured to provide
alternate functions, UART, SPI, PWM, I²C and so providing a total of 17 GPIO pins
http://elinux.org/RPi_Low-level_peripherals
https://pinout.xyz/pinout/pin22_gpio25
96. GPIO
RPi.GPIO
a small py library that take some of the complexity out of driving the GPIO pins
#1.Import GPIO module
import RPi.GPIO as GPIO
#2.Pin numbering
GPIO.setmode(GPIO.BOARD)
# or
GPIO.setmode(GPIO.BCM)
mode = GPIO.getmode()
#3.Setup up a channel
GPIO.setup(channel, GPIO.IN or GPIO.OUT)
# Multiple channel
chan_list = [11,12]
GPIO.setup(chan_list, GPIO.OUT)
#INPUT or OUTPUT
# END - cleanup
GPIO.cleanup()
$ wget http://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.6.0.tar.gz
$ tar zxf RPi.GPIO-0.6.0.tar.gz
$ cd RPi.GPIO-0.6.0
$ sudo python setup.py install
100. Python Concurrency
Concurrency :
1) Doing more than one thing at a time.
Example :
1) A network server that communicates with several hundred clients
all connected at once
2) A big number crunching job that spreads its work across multiple
CPUs
106. Python Concurrency
import time
import threading
class CountdownThread(threading.Thread):
def __init__(self,count):
threading.Thread.__init__(self)
self.count = count
def run(self):
while self.count > 0:
Print("Counting down", self.count)
self.count -= 1
time.sleep(5)
return
inherit from Thread
and redefine run()
This code executes
in the thread
First way: threads are defined by a class
107. Python Concurrency
t1 = CountdownThread(10) # Create the thread object
t1.start() # Launch the thread
t2 = CountdownThread(20) # Create another thread
t2.start() # Launch
First way: threads are defined by a class
108. Python Concurrency
def countdown(count):
while count > 0:
print "Counting down", count
count -= 1
time.sleep(5)
t1 = threading.Thread(target=countdown,args=(10,))
t1.start()
Second way: threads are defined by a class