Вы находитесь на странице: 1из 7

RUBRICS

Sl no Dimension Marks
1 Information search
and Collection
2 Attendance

3 Skills acquired
4 Presentati on
5 Repot presentation
TOTAL
INTRODUCTION
When you buy a Raspberry Pi, you are essentially buying an assembled printed circuit board. It does not
even include a power supply or operating system. The recipes in this chapter are concerned with getting
your Raspberry Pi set up and ready for use. Because the Raspberry Pi just uses standard USB keyboards
and mice, most of the setup is pretty straightforward, so you will concentrate only on those tasks that are
specific to the Raspberry Pi. 1.1 Selecting a Model of Raspberry Pi Problem There are many models of
Raspberry Pi and you are not sure which to use. Solution If you want a Raspberry Pi for general use, then
you should buy a Raspberry Pi 3 or 2 model B. With four times as much memory and a quad-core
processor, it will cope with most tasks much better than the Pi Zero or model A+ with their single
processors. The Raspberry Pi 3 model B has the great advantage of having WiFi built in, so there’s no
need for an extra USB WiFi adapter. If, on the other hand, you are embedding a Raspberry Pi in a project
for a single purpose, then using a model A+ or Pi Zero and saving a few dollars may well be an option.

NETWORKING
The Raspberry Pi is designed to be connected to the Internet. Its ability to communicate
on the Internet is one of its key features and opens up all sorts of possible uses,
including home automation, web serving, network monitoring, and so on.
The connection can be wired through an Ethernet cable (at least in the case of the
model B), or the Pi can use a USB WiFi module to provide a network connection.
Having a connected Raspberry Pi also means that you can connect to it remotely
from another computer. This is very useful in situations where the Raspberry Pi itself
is inaccessible and does not have a keyboard, mouse, and monitor attached to it.
This chapter gives you recipes for connecting your Raspberry Pi to the Internet and
Finding Your Way Around the GPIO Connector
Be sure to check out the accompanying video for this recipe at
You need to connect electronics to the GPIO connector, but first you need to know
more about what all the pins do.
There have actually been three versions of the Raspberry Pi GPIO connector. Two 26-
pin layouts for the original Raspberry Pi and one 40-pin layout that came in with the
Raspberry Pi “+” models.
The quick way to distinguish the boards is knowing that if you have one of
the very first revision 1 boards, it has a black audio socket. The revision 2 boards have
a blue audio socket.
Leaving the GPIO Pins in a Safe State
Problem
You want all the GPIO pins to be set to inputs whenever your program exits so that
there is less of a chance of an accidental short on the GPIO header, which could damage
your Raspberry Pi.
Solution
finally: construction and the GPIO.cleanup method.
The blink example can be rewritten to exit safely as shown below.
The file for the code is called led_blink_safe.py.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
try:
while (True):
GPIO.output(18, True)
time.sleep(0.5)
GPIO.output(18, False)
time.sleep(0.5)
finally:
print("Cleaning Up!")
GPIO.cleanup()
Discussion
If cleanup is not called or the Pi is not rebooted, then pins set to be outputs will
remain as outputs after the program has finished. If you were to start wiring up a new
project, unaware of this problem, your new circuit might accidentally short a GPIO
output to one of the supply rails or another GPIO pin in the opposite state.
A typical scenario where this might happen would be if you were to connect a push
switch, connecting a GPIO pin that you had configured as an output and HIGH to
GND, such as in

Make a Buzzing Sound


Problem
You want to make a buzzing sound with the Raspberry Pi.
Solution
Use a piezo-electric buzzer connected to a GPIO pin.
Most small piezo buzzers work just fine using the arrangement shown in
The one I used is an Adafruit-supplied component
You can connect the buzzer pins directly to the Raspberry Pi using female-to-female
headers
These buzzers use very little current. However, if you have a large buzzer or just want
to play it safe, then put a 470Ω resistor between the GPIO pin and the buzzer lead.
The Internet of Things
The Internet of Things (IoT) is the rapidly growing network of devices (things) connectedto the Internet. That
doesn’t just mean more and more computers using
browsers, but actual appliances and wearable and portable technology. This includes
all sorts of home automation from smart appliances and lighting, to security systems
and even Internet-operated pet feeders, as well as lots of less practical but fun
projects.
In this chapter, you will learn how your Raspberry Pi can participate in the Internet
of Things in various ways.
15.1 Controlling GPIO Outputs Using a Web Interface
Problem
You want to control GPIO outputs using a web interface to your Raspberry Pi.
Solution
Use the bottle Python web server library to create an HTML web
interface to control the GPIO port.

Here’s the web_control.py program:


from bottle import route, run
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
led_pins = [18, 23, 24]
led_states = [0, 0, 0]

switch_pin = 25
GPIO.setup(led_pins[0], GPIO.OUT)
GPIO.setup(led_pins[1], GPIO.OUT)
GPIO.setup(led_pins[2], GPIO.OUT)
GPIO.setup(switch_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def switch_status():
state = GPIO.input(switch_pin)
if state:
return 'Up'
else:
return 'Down'
def html_for_led(led):
l = str(led)
result = " <input type='button' onClick='changed(" + l +
")' value='LED " + l + "'/>"
return result
def update_leds():
for i, value in enumerate(led_states):
GPIO.output(led_pins[i], value)
@route('/')
@route('/<led>')
def index(led):
if led >= '0' and led <= '9':
led_num = int(led)
led_states[led_num] = not led_states[led_num]
update_leds()
response = "<script>"
response += "function changed(led)"
response += "{"
response += " window.location.href='/' + led"
response += "}"
response += "</script>"
response += '<h1>GPIO Control</h1>'
response += '<h2>Button=' + switch_status() + '</h2>'
response += '<h2>LEDs</h2>'
response += html_for_led(0)
response += html_for_led(1)
response += html_for_led(2)
return response
try:
run(host='0.0.0.0', port=80)
finally:
print('\nCleaning up')
GPIO.cleanup()

Arduino and Raspberry Pi


Setting Up PyFirmata to Control an Arduino from a
Raspberry Pi
Problem
You want to use an Arduino as an interface board for your Raspberry Pi.
Solution
Connect the Arduino to a USB socket of the Raspberry Pi so that the computer can
communicate and send power to the Arduino.
Next, install the Firmata sketch onto the Arduino and the PyFirmata onto your Raspberry
Pi. This entails installing the Arduino IDE, so if you haven’t already done so
The Arduino IDE includes Firmata, so all you have to do to install Firmata onto your
Arduino board is to upload a sketch. You will find the sketch at File→Examples→Firmata→
StandardFirmata.
Once Firmata is installed, the Arduino waits for communication from the Raspberry
Pi.
Now you need to install PyFirmata, the other half of the link. This requires the use of
the PySerial library, so follow to install this.
You can now download and install PyFirmata by using these commands:
$ git clone https://github.com/tino/pyFirmata.git
$ cd pyFirmata
$ sudo python setup.py install
You can try out the PyFirmata library from the Python console. Enter the following
commands to turn on the built-in LED on Arduino pin 13 (marked with an L) and
then turn it off again.
$ sudo python
Python 2.7.3 (default, Jan 13 2013, 11:20:46)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyfirmata
>>> board = pyfirmata.Arduino('/dev/ttyACM0')
>>> pin13 = board.get_pin('d:13:o')
>>> pin13.write(1)

>>> pin13.write(0)

>>> board.exit()

Discussion
The preceding code first imports the PyFirmata library and then makes an instance of
Arduino called board, using the USB interface (/dev/ttyACM0) as its parameter. You
can then gain a reference to one of the Arduino pins (in this case, 13) and set it to be
a digital output. The d is for digital, 13 is the pin number, and o is for output.
To set the output pin high, use write(1), and to set it low, use write(0). You can also
use True and False in place of 1 and 0.

Python Basics
You need to use Python but are unsure about which version to use.
Solution
Use both. Use Python 3 until you face a problem that is best solved by reverting to
version 2.
Discussion
Although the latest version of Python is Python 3 and has been for years, you will
find that a lot of people stick to Python 2. In fact, in the Raspbian distribution, both
versions are supplied and version 2 is just called Python, whereas version 3 is called
Python 3. You even run Python 3 by using the command python3. The examples in
this book are written for Python 3 unless otherwise stated. Most will run on both
Python 2 and Python 3 without modification.

Вам также может понравиться