0% found this document useful (0 votes)
8 views

Part 1 - Unit5

Raspberry pi

Uploaded by

Varad Kadtan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Part 1 - Unit5

Raspberry pi

Uploaded by

Varad Kadtan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

CherryPy

• What is CherryPy?
• According to their website, CherryPy is a Pythonic, object-oriented web framework.
• CherryPy gives developers the power to build web applications as if they were building any object-oriented
Python program. In true Python style, CherryPy programs have less code and are developed in less time than
other web frameworks.
• Installing CherryPy
sudo pip3 install cherrypy
Creating a simple web page using CherryPy

• import cherrypy
• class HelloWorld():
• @cherrypy.expose
• def index(self):
• return "Hello Raspberry Pi!"
• cherrypy.quickstart(HelloWorld())
• From the output to the shell you should be able to observe the ip address and
port that CherryPy is running on, http://127.0.0.1:8080.
• @cherrypy.expose, exposes the method index that happens to correspond to the root of
• The web server. When we load our web page using the loopback address (127.0.0.1) and
• port that CherryPy is running on (8080), the index method is served up as the page
MULTIPLE FUNCTION
• import cherrypy
• class HelloWorld():
• @cherrypy.expose
• def index(self):
• return "Hello Raspberry Pi!"
• @cherrypy.expose
• def sayHello(self, myFriend=" my friend"):
• return "Hello " + myFriend
• cherrypy.quickstart(HelloWorld())
• TO GET SECOND ONE ONLY EXECUTE
• http://127.0.0.1:8080/sayHello
Static pages
• //static.html
• <html>
• <body>
• This is a static HTML page.
• </body>
• </html
//StaticPage.py.
• import cherrypy
• class StaticPage():
• @cherrypy.expose
• def index(self):
• return open('static.html')
• cherrypy.quickstart(StaticPage())
Bootstrap
• Bootstrap is a free front-end framework for faster and easier web development
• Bootstrap is the most popular HTML, CSS, and JavaScript framework for developing
responsive, mobile-first websites.
• Bootstrap includes HTML and CSS based design templates for typography, forms,
buttons, tables, navigation, modals, image carousels and many other, as well as optional
JavaScript plugins
• Bootstrap also gives you the ability to easily create responsive designs
• Responsive web design is about creating web sites which automatically adjust themselves
to look good on all devices, from small phones to large desktops.
• Bootstrap 4 CDN
• If you don't want to download and host Bootstrap 4 yourself, you can include it from a
CDN (Content Delivery Network).
• MaxCDN provides CDN support for Bootstrap's CSS and JavaScript. You must also
include jQuery:
Bootstrap
• A card is a flexible and extensible content container. It
includes options for headers and footers, a wide variety
of content, contextual background colors, and
powerful display options.

• EXAMPLE:Create lists of content in a card with a flush


list group.
• <div class="card" style="width: 18rem;">
• <div class="card-header">
• Featured
• </div>
• <ul class="list-group list-group-flush">
• <li class="list-group-item">Cras justo odio</li>
• <li class="list-group-item">Dapibus ac facilisis in</li>
• <li class="list-group-item">Vestibulum at eros</li>
• </ul>
Bootstrap Container

• In Bootstrap, container is used to set the content's margins dealing


with the responsive behaviors of your layout. It contains the row
elements and the row elements are the container of columns (known as
grid system).
• Containers are a fundamental building block of Bootstrap that contain,
pad, and align your content within a given device or viewport.
• The container class is used to create boxed content.
• jQuery is a fast, small, and feature-rich JavaScript library. It makes
things like HTML document traversal and manipulation, event
handling, animation, and Ajax much simpler with an easy-to-use API
that works across a multitude of browsers.
Building a Home Security Dashboard
• Creating our dashboard using CherryPy
• Displaying sensory data on our dashboard
• To complete this project, the following will be required:
• A Raspberry Pi Model 3 (2015 model or newer)
• A USB power supply
• A computer monitor
• A USB keyboard
• A USB mouse
• A breadboard
• A DHT11 temperature sensor
• A latching push-button, switch, or key switch
• A PIR sensor
• An active buzzer
• A Raspberry Pi camera module
Using the DHT11 to find temperature and
humidity
• The DHT11 temperature and humidity sensor is a low-cost hobbyist-grade sensor, capable
• of providing basic measurements. The DHT11 comes in two different versions, the four-pin
• model and the three-pin model.
• The library we will be using to read DHT11 data, the Adafruit DHT library, does not come
• pre-installed on Raspbian (as of the time of writing). To install it, we will clone the library's
• GitHub project and build it from the source.
• Open up a Terminal window, and type the following command to use git and download
• the source code (at the time of writing, git came pre-installed with Raspbian):
• git clone https://github.com/adafruit/Adafruit_Python_DHT.git
• cd Adafruit_Python_DHT
• You will be in the source code directory.
• Build the project with the following command:
• sudo python3 setup.py install
CIRCUIT AND CODE
• let's wire up the circuit. Connect the DHT11 sensor
to the Raspberry Pi as follows:
• GND from DHT11 to GND on the Raspberry Pi
• VCC on DHT11 to 5V DC on the Raspberry Pi
• Signal on the DHT11 to GPIO pin 19
• import Adafruit_DHT
• dht_sensor = Adafruit_DHT.DHT11
• pin = 19
• humidity, temperature =
Adafruit_DHT.read_retry(dht_sensor, pin)
• print(humidity)
• print(temperature)
Using the Pi camera to take a photo

• Install a Raspberry Pi camera module onto the Raspberry Pi through the CSI camera port
• from picamera import PiCamera
• from time import sleep
• pi_cam = PiCamera()
• pi_cam.start_preview()
• sleep(5)
• pi_cam.capture('/home/pi/myimage.png')
• pi_cam.stop
Creating our dashboard using
CherryPy(security-dashboard.py)
• We use the conf dictionary to pass configuration data to the cherrypy
• quickstart method. This configuration data allows us to use the static files led.css,
• intruder.png, all-clear.png, and not-armed.png with our CherryPy server.
• The configuration information states that the CSS and image files are located in
• the directories named styles and images, respectively. These directories are both located
• in the /home/pi directory.
• The following is a screenshot of the files in the images directory (be sure to place your files
• in the correct directories):
Displaying sensory data on our dashboard

• To provide our dashboard data, we will create a new Python file called SecurityData.py
• where we will store the SecurityData class.
Circuit
• We will build our first version of the home security
dashboard with a DHT11 temperature
• and humidity sensor, a PIR sensor, and a latching button
• The circuit connects as follows:
• GND from DHT11 to GND
• VCC on DHT11 to 5V DC
• Signal on the DHT11 to GPIO pin 19
• GND from PIR sensor to GND
• VCC on PIR sensor to 5V DC
• Signal on PIR sensor to GPIO pin4
• One end of the latching button to GPIO pin 8
• The other end of the latching button to GND
• Pi camera module to CSI port (not shown)
SecurityData.py
SECURITY DATA
• You should get an output to the shell indicating the • def getSecurityImage(self):
temperature and humidity level in the room, an on or off
indicating the position of the switch, and the current time. • if not(self.switch.is_pressed):
Try turning the switch on and off to see if the value changes • self.detected_message = ''
in the output.
• return "/not-armed.png"
• Our first conditional statement checks to see whether the
circuit is armed (switch is in the on position). If it's not • elif self.motion_sensor.motion_detected:
armed, then all that we need to do is set the detected message
to nothing, and return a reference to the not-armed.png file • self.pi_cam.resolution = (500, 375)
(/not-armed.png was defined in the configuration • self.pi_cam.capture("/home/pi/images/intruder.png")
information that we set up in the security-dashboard.py file).
• self.detected_message = "Detected at: " +
• If we take a look at the code in the SecurityDashboard class
(security-dashboard.py file), we can see that the • self.getTime()
getSecurityImage method is called near the bottom of the
generated HTML code: • return "/intruder.png"
• <div class="card-footer" align="center"> • else:
• <img src=""" + self.securityData.getSecurityImage() + """/> • self.detected_message = ''
• <p>""" + self.securityData.getDetectedMessage() + """</p> • return "/all-clear.png"
• </div>
With intruder
• Open up a web browser, and navigate to the
following address:
• http://127.0.0.1:8080
• With the switch in the off position, you should see
the following dashboard screen1
• Now, turn on the switch, and stand back so that the
PIR sensor does not detect you. You
• should see a screen2- You will notice that the LED
in the Armed section now turns to a flashing red

You might also like