MagPi10.pdf

(39907 KB) Pobierz
ISSUE 10 ­ MAR 2013
Get printed copies at
themagpi.com
H
a
p
B
i
r
t
h
d
a
y
y
!
p
A Magazine for Raspberry Pi Users
This Issue...
WebIOPi Framework
Expansion boards
Backup SD cards
Scratch fractals
BASH basics
Charm
Win a 512Mb
Model B
Raspberry Pi
& case!
Raspberry Pi is a trademark of The Raspberry Pi Foundation.
This magazine was created using a Raspberry Pi computer.
ht
t
t
t
p:
:
/
//
www.
.
t
t
hemagpi
i
.
.
com
h p www hemagp com
10
Welcome to isssue 10,
Thank you to all of those who have ordered volume one (issues 1-8). After
some printing delays, the cordex binder for volume 1 has been submitted for
rapid production. The final pdf files needed for printing each of the magazines
are nearing completion. We expect to be releasing pdfs for printing this week.
We are very grateful for your continued patience. Such delays are not expected
with volume 2, since all of the articles are already in the correct format for
printing.
In this month's issue there is a great selection of hardware and software
projects. We are pleased to present part 2 of the WebIOPi and backup articles,
new programming langauges and our regular Scratch and Python pages. For
those looking for automation solutions, there is one article with a whole range
of extension boards.
We are on the look out for willing volunteers to help with layout, testing and
proof reading activities.
If you can dedicate some time, please email
editor@themagpi.com.
Chief Editor of The MagPi
The MagPi Team
Ash Stone
- Chief Editor / Administration
Chris 'tzj' Stagg
- Administration
Matt '0ther0judge0'
- Website / Administration
Tim 'meltwater' Cox
- Administration
Lix
- Administration
Aaron Shaw
- Page Design / Graphics
Bryan Butler
- Page Design & Theme / Graphics
Ian McAlpine
- Page Design / Graphics
Isa McKenty
- Page Design
Simon Johnson
- Page Design
Steve Drew
- Page Design
W.H. Bell
- Page Design / Administration
Mark Robson
- Proof Reading
Michael Beaucage
- Graphics
2
Contents
CAM - PART 2
4
REMOTE CONTROLLED ROBOT
REST Framework (WebIOPi)
Robot remote control with raspberry Pi
COCKTAIL OF EXPANSION BOARDS
8
A
selection of different expansion boards for interfacing projects
A
MONTH'S
1 1
THIS
512mb Rasp.
COMPETITION
from PC Supplies LTD
Win a
Pi model B and case,
- PART 2
1 2
BACKING UP
card images safe and restoring backups
Keeping the SD
GAFFER
1 6
BASH
some lashup
TAPE
with the Bourne-again shell
Learn
scripts
1 9
WHAT'S ON GUIDE
in your area
Find this month's events
TO CHARM
20
INTRODUCTION
to get coding with the Raspberry Pi
Encouraging others
23
C++ CACHE
streams, reading and writing files
Introducing C++
PART
26
SCRATCH PATCH - GPIO CONTROL
Pi with a
2
Celebrate the anniversary of the Raspberry
LEDborg candle
28
SCRATCH FRACTALS
Scratch
Generate fractal images with
33
PYTHON PIT
client-server model for parallel calculations
Using a simple
36
FEEDBACK FROM READERS
HAPPY BIRTHDAY RASPBERRY PI!
http://www.themagpi.com
3
WebIOPi - Raspberry Pi REST framework
Remote Controlled
Robot Cam, Part II
DIFFICULTY : ADVANCED
Building the interface
Building your own interface is also easy, and is
based on a HTML file embedding some
Javascript. You only have to load the webiopi.js
file from your HTML file to use the WebIOPi
power. Create a new index.html file next to your
Python script:
<html >
<head>
<ti tl e>CamBot</ti tl e>
<scri pt type=" text/j avascri pt"
src=" /webi opi . j s" ></scri pt>
<scri pt type=" text/j avascri pt" >
// J avascri pt code goes here
</scri pt>
</head>
<body>
<di v i d=”box" al i gn=" center" >
</di v>
</body>
</html >
Eric PTAK
Guest Writer
In the script section, we add an init function to
build the interface using WebIOPi library. It
contains many functions to ease creation of
buttons that control GPIO. Here we use a basic
button to call a different function on press and
release. Each function calls a different macro on
the server. Don’t forget to register the init
function to WebIOPi. It will be called when
everything is loaded and ready.
functi on i ni t() {
var button =
webi opi (). createButton(
" bt_up" ,
// i d
" /\\" ,
// l abel
go_forward, // press
stop);
// rel ease
$(" #box" ). append(button);
}
functi on go_forward() {
w(). cal l Macro(" go_forward" );
}
functi on stop() {
w(). cal l Macro(" stop" );
}
webi opi (). ready(i ni t);
4
Take note of the starting slash when loading
webiopi.js, to ensure it will be searched in the
root of the server or it may be not found.
I added an empty script section; we will use the
WebIOPi JS library here. There is also a div box,
which will contain controls.
Be careful that webiopi() is a function and a
reserved word that need brackets to return the
WebIOPi object. You can use w() to short the
webiopi() call.
It’s now time to start the server and enjoy the
interface. Open a terminal in the folder you
created Python and HTML files and execute the
script:
$ sudo python yourscri pt. py
Resetti ng camera setti ngs
Camera bri ghtness l evel i s 0
Camera contrast l evel i s 255
Camera saturati on l evel i s 255
Camera gai n l evel i s 255
Savi ng i mage to: snap. j pg
If uvccapture returns without error, we can
continue to stream the webcam.
I use MJPG-STREAMER, which is really easy to
use. It gives me a 320x240@25fps pass-through
MJPEG stream over HTTP. I tried FFMPEG but
it takes the RAW output of the webcam to
encode it in MJPEG with a framerate under 5fps.
You can download MJPG-STREAMER at
http://sourceforge.net/projects/mjpg-streamer/
You will also need libjpeg8-dev you can install
using aptitude/apt-get.
Uncompress and build MJPG-STREAMER using
make command. Then execute it:
$ . /mj pg_streamer - i " . /i nput_uvc. so
–r 320x240 –f 25" - o " . /output_http. so
–n –p 8001" &
Open a browser to the webiopi to control the
chassis. Hold the button to go forward and
release it to stop. The last piece missing is the
webcam.
Add a webcam stream
There are many possibilities to stream a
webcam, which may depend on the model you
have. In my case, I have a recent webcam which
outputs both RAW and MJPEG formats up to
1280x720@30fps.
First, check your webcam is installed with a
terminal:
$ l susb
[. . . ]
Bus 001 Devi ce 005: ID 046d: 0825
Logi tech, Inc. Webcam C270
$ l s /dev/vi deo*
/dev/vi deo0
Back to HTML file, add a img tag with src set to
http://raspberrypi:8001/?action=stream replacing
raspberrypi by your Pi’s IP. You can also directly
try the URL in your browser.
...
<i mg
src=" http: //raspberrypi : 8001/?acti on=s
tream" >
</body>
</html >
Then, to check it’s working, you can install
uvccapture using apt-get or aptitude and take a
single snapshot:
$ uvccapture - v
Usi ng vi deodevi ce: /dev/vi deo0
Savi ng i mages to: snap. j pg
Image si ze: 320x240
Taki ng snapshot every 0 seconds
Taki ng i mages usi ng mmap
Conclusion
With this article, you learned how to install
WebIOPi and how to use it in your own Python
scripts to write macros you can call from the web.
5
Zgłoś jeśli naruszono regulamin