Part 1 - Unit5
Part 1 - Unit5
• 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.
• 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