Escolar Documentos
Profissional Documentos
Cultura Documentos
PICADEMY
TRAINING
http://www.themagpi.com
24
Welcome to issue 24 of The MagPi magazine.
This months issue is packed cover to cover with something for just about everyone!
We kick off with Daniel Pelikans Raspberry Pi Scope, an article which describes in detail how to use
the Raspberry Pi as a 1 0 MSPS scope. We follow this with the concluding part of Michael Petersens
Weather Balloon series, where he looks at the code used in their Multi-Sensor Array before moving onto
a great article looking how to build a spectrophotometer using the Raspberry Pi and Wolfram language.
John Shovic shares with us his penultimate article, part five, of Project Curacao. John describes
actually deploying the monitor and also reveals some results from within the first 8 weeks of its data
collection. He finishes the article with some suggestions of future upgrades and we look forward to part
six to see how this project has evolved further.
We have a MagPi exclusive interview with Carrie Anne Philbin from the foundation on the first Picademy
at Raspberry Towers and we pay homage to 1 980s LOGO with an article looking at the use of Python
with the module Turtle to produce similar graphics. Allen Heard, Head of IT at Ysgol Bryn Elian High
School describes his fantastic Tech-Dojo events inspiring tomorrows programmers and we start a new
series looking at improving our understanding of Wi-Fi in Richard Wenners education article
Raspberry Spy. We finish off by returning to C++ cache looking at object-object communication.
We had better begin...
Contents
4 PISCOPE
Using the Raspbery Pi as a scope
CURACAO
1 8 PROJECT
Part 5: Deployment and results
WOLFRAM ANALYSIS: MEASURING LIGHT ABSORPTION
22 DIY
chemistry lab: building a spectrophotometer
26 PICADEMY
An interview with Carrie Anne Philbin
AND TURTLE GRAPHICS
32 PYTHON
Bringing Testudines out of the '80s
SNIFFING
36 PACKET
Raspberry Spy Part 1 : understanding Wi-Fi networks
42 C++ CACHE
48 FEEDBACK
Have your say about The MagPi
http://www.themagpi.com
KERNEL MODULE
Daniel Pelikan
Guest Writer
sure that the time between each sample point is the same.
Without a realtime operating system, this is not
guaranteed.
After a lot of tests and a lot of reading about interrupts and
process control in Linux, I decided to write a Linux kernel
module to try to solve the realtime readout problem.
#! /bi n/bash
# A scri pt to setup the Raspberry Pi for a kernel bui l d
FV=` zgrep " * fi rmware as of" /usr/share/doc/raspberrypi -bootl oader/changel og. Debi an. gz | head -1 | awk ' { pri nt $5 }' `
mkdi r -p k_tmp/l i nux
wget https: //raw. gi thub. com/raspberrypi /fi rmware/$FV/extra/gi t_hash -O k_tmp/gi t_hash
wget https: //raw. gi thub. com/raspberrypi /fi rmware/$FV/extra/Modul e. symvers -O k_tmp/Modul e. symvers
HASH=` cat k_tmp/gi t_hash`
wget -c https: //gi thub. com/raspberrypi /l i nux/tarbal l /$HASH -O k_tmp/l i nux. tar. gz
cd k_tmp
tar -xzf l i nux. tar. gz
KV=` uname -r`
sudo mv raspberrypi -l i nux* /usr/src/l i nux-source-$KV
sudo l n -s /usr/src/l i nux-source-$KV /l i b/modul es/$KV/bui l d
sudo cp Modul e. symvers /usr/src/l i nux-source-$KV/
sudo zcat /proc/confi g. gz > /usr/src/l i nux-source-$KV/. confi g
cd /usr/src/l i nux-source-$KV/
sudo make ol dconfi g
sudo make prepare
sudo make scri pts
Scope-drv.c (1/10)
Bash script
source file defines the GPIO connections that are used to
connect to the ADC. For this article, a six bit ADC was
used. Therefore, six GPIO connections are needed per
ADC:
/* Number of sampl es to capture */
#defi ne SAMPLE_SIZE
10000
Scope-drv.c (3/10)
/* Defi ne GPIO Pi ns */
/* ADC 1 */
#defi ne BIT0_PIN 7
#defi ne BIT1_PIN 8
#defi ne BIT2_PIN 9
#defi ne BIT3_PIN 10
#defi ne BIT4_PIN 11
#defi ne BIT5_PIN 25
/* ADC 2 */
#defi ne BIT0_PIN2
#defi ne BIT1_PIN2
#defi ne BIT2_PIN2
#defi ne BIT3_PIN2
#defi ne BIT4_PIN2
#defi ne BIT5_PIN2
17
18
22
23
24
27
(BCM2708_PERI_BASE + 0x00101000)
Scope-drv.c (5/10)
/* take sampl es */
whi l e(counter<SAMPLE_SIZE){
dataStruct. Buffer[counter++] = *(gpi o. addr + 13);
}
/* enabl e IRQ */
l ocal _fi q_enabl e();
l ocal _i rq_enabl e();
/* Store the ti me di fference i n ns */
dataStruct. ti me =
ti mespec_to_ns(&ts_stop) - ti mespec_to_ns(&ts_start);
ScopeBufferStop = ScopeBufferStart+
si zeof(struct DataStruct);
Scope-drv.c (6/10)
return SUCCESS;
Scope-drv.c (8/10)
Scope-drv.c (9/10)
Data acquisition
Once the ADC has been connected and the kernel module
has been loaded, data can be read from the ADC by
connecting to the device file associated with the kernel
module. To connect to the kernel module, another
program is needed. This program could be written in
several different programming languages. For this article,
C++ was chosen. Create a new file called readout. cpp
and add the C++ given below and on the next page.
#i ncl ude
#i ncl ude
#i ncl ude
#i ncl ude
<i ostream>
<cmath>
<fstream>
<bi tset>
i nt mai n(){
//Read the RPi
struct DataStructRPi dataStruct;
unsi gned char *ScopeBufferStart;
unsi gned char *ScopeBufferStop;
unsi gned char *buf_p;
buf_p=(unsi gned char*)&dataStruct;
ScopeBufferStart=(unsi gned char*)&dataStruct;
ScopeBufferStop=ScopeBufferStart+
si zeof(struct DataStructRPi );
std: : stri ng l i ne;
std: : i fstream myfi l e (" /dev/chardev" );
i f(myfi l e. i s_open()){
whi l e(std: : getl i ne(myfi l e, l i ne)){
for(i nt i =0; i <l i ne. si ze(); i ++){
i f(buf_p>ScopeBufferStop)
std: : cerr<<" buf_p out of range! " <<std: : endl ;
*(buf_p)=l i ne[i ] ;
buf_p++;
}
}
myfi l e. cl ose();
}
el se std: : cerr<<" Unabl e to open fi l e" <<std: : endl ;
Install
}
return 0;
Michael Petersen
Guest Writer
Library installation
12
needed.
For a version 1 Model B Raspberry Pi, you must
edit the source code by entering:
cd src
nano bcm2835. c
13
14
Reading I 2C sensors
The MSA sensors are divided into two different
groups: (1 ) analog and (2) digital sensors.
The analog sensors are all wired through an I 2C
ready LTC2495 ADC. The digital sensors for the
MSA were all chosen to be I 2C accessible. With
all of the sensors communicating over the I 2C
bus, we were able to free up the remaining
GPIOs for other purposes.
Reading analog sensors
15
16
Date: 11/09/13
ti me, reg_temp, xtemp, i temp, i pressure, i humi di ty
, dust_v, xhumi di ty, batt_vol tage,
12: 59: 18, 37. 12, 23. 81, 33. 02, 768. 89, 22. 78,
0. 0000, 41. 57, 7. 53,
12: 59: 23, 37. 00, 23. 81, 33. 02, 768. 85, 22. 55,
0. 0000, 38. 78, 7. 53,
12: 59: 28, 36. 88, 23. 88, 33. 29, 768. 83, 22. 32,
0. 0000, 40. 37, 7. 53,
. . . . . .
Coming up
We are currently working on a balloon borne
optical sensor for measuring PM2.5 aerosols.
The new sensor will require a vacuum pump to
maintain a steady flow rate of 3LPM. A feedback
loop will be designed to control a heater for
maintaining a relative humidity of less than
50%RH. The Raspberry Pi will generate a PWM
signal to regulate both temperature and flow rate.
We have already begun prototyping critical
components.
PROJECT CURACAO
John Shovic
Guest Writer
System description
Project Curacao consists of four subsystems. A
Raspberry Pi Model A is the brains and the overall
controller. The Power Subsystem was described in
part 1 , the Environmental Sensor Subsystem in
part 2 and the Camera Subsystem was shown in
part 3. Part 4 described the software running the
Raspberry Pi and the Arduino Battery Watchdog.
Results of shipping
The box arrived in our luggage on March 3, 201 4.
Airport security (TSA) inspected the box twice, but
aside from picking up the box with the outside
18
Deployment
We spent ten days finishing off some software and
preparing to put the box up the tower. Understand,
however, this was in addition to going to the beach,
dancing, coffee with the neighbors and some very
nice parties. Eventually, we did put up the box. The
box and wind turbine was deployed on the tower on
March 1 3, 201 4.
19
20
From: ********@gmail.com
Subject: ProjectCuracao Fan ON(TMP)
Date: April17,2014at12:08PM
Fan turning ON: State: ot:36.50 it:38.10
oh:34.50 ih:49.40 sv:4.77
September Upgrades
We are planning a maintenance trip to Curacao.
The planned upgrades are:
What's Next?
Our final article, Part 6 in Autumn 201 4, will
describe the upgrades and the required changes to
the Project Curacao system. This has been a large
project and we want to thank all of the people and
companies that helped make this possible.
More discussion on Project Curacao at:
http://switchdoc.blogspot.com
21
WOLFRAM ANALYSIS
Measuring light absorption
Guest Writer
Introduction
I was in the beverage isle of the supermarket one
day and was looking at the soft drinks. One of
the drinks, a "Watermelon Punch" had listed on it
Red 40 as one of the ingredients. Red 40, also
known as Allura Red AC or E1 29, was phased
out of the UK by the Food Standards Agency in
2009 because some research showed that it may
induce hyperactivity in children. The food dye is
still allowed in the USA market, but if questions
have been raised about the safety of the dye, I
would like to know how much of it I'm consuming.
I am an analytical chemistry professor and one of
the fun parts about my job is that I get to teach
students how to play with instruments: we use
them, we break them, we repair them and
sometimes we even build them. One major area
of analytical chemistry is called spectroscopy,
which uses light (or more broadly, the
electromagnetic spectrum) to explore the
properties of atoms, molecules and materials. A
spectroscopic instrument commonly found in
22
Robert J. LeSuer
Supplies
All of the electronic parts should be available at
your local electronics store or an online retailer. I
use either Newark, http://www.newark.com, or
Adafruit, http://www.adafruit.com:
Green LED (20 mA max)
CdS photoresistor [Adafruit #1 61 , Newark #95F9039]
1 00 uF electrolytic capacitor
The science equipment I borrowed from my
personal lab, but retailers such as Edmund
Scientifics, http://www.scientificsonline.com, and
the
Labware
section
of
Amazon,
http://goo.gl/qyfqiy are good sources for the
following items:
Spectrophotometer cuvettes
Graduated cylinder (1 0 to 25 mL)
Th e sci en ce
2000x
0.00050
15
3000x
0.00033
20
4000x
0.00025
25
5000x
0.00020
The software
Now that the Wolfram Language is available to
all owners of the Raspberry Pi, we have at our
disposal a comprehensive system for data
acquisition, analysis and visualisation. I will
describe one way to use Mathematica as the
front end of the spectrophotometer. We need
several functions to (a) discharge the capacitor,
(b) read the status of the GPIO pin connected to
24
The analysis
By measuring each sample five times and
plotting the average output versus the inverse of
the dilution factor, I obtained the results shown
on the right. The linear relationship means I can
be fairly confident in the results of the unknown
measurement. The equation for a straight line is
= m + c; so by rearranging I can obtain an
25
PICADEMY
An interview with
Carrie Anne Philbin
Colin Deady
& Tim Cox
MagPi Writers
26
27
28
like weather
see
an
immediate
difference
in
- Allen Heard
29
is
not
just
about
textual
important
education."
if
it
is
to
work
within
- Ed Dourass
30
teachers
who
are
finding
help
to
build
network of teachers"
valuable
the
Picademy
self-support
- Graham Hastings
Bringing Testudines
out of the '80s
Paul Sutton
Guest Writer
32
turtle.forward(150)
turtle.left(90)
ts = turtle.getscreen()
ts.getcanvas().postscript(file=fname)
turtle.exitonclick()
for x in range(0,72):
turtle.left(5)
for n in range(0,4):
turtle.forward(150)
turtle.left(90)
http://en.wikipedia.org/wiki/Logo_(programming_
language)
http://en.wikipedia.org/wiki/Turtle_graphics
http://docs.python.org/2/library/turtle.html
33
SCHOOL REPORT
Allen Heard
Guest Writer
34
35
PACKET SNIFFING
Richard Wenner
Guest Writer
36
Wi-Fi sniffing
It is relatively straight-forward to sniff your Wi-Fi
network, or a network that you have explicit
permission to sniff. All that is needed is a USB
Wi-Fi dongle, which can be either connected
directly to the Raspberry Pi or via a powered
USB hub. Although these dongles are small and
low cost, they are packed with technology.
Twenty five years ago, when I first started
designing this type of equipment, it was difficult
to fit the electronics into something the size of a
very large box of matches.
Today everything fits onto a
board almost the same size as
the USB connector!
I f the Wi-Fi dongle is plugged directly into your
Raspberry Pi, keep the keyboard connected but
you can remove any mouse. We are not going to
use the GUI interface, so a mouse is not
necessary.
37
cd ki smet-2013-03-R1b
make
i nstal l
i nstal l
i nstal l
i nstal l
i nstal l
l i bncurses5-dev
l i bpcap-dev
l i bpcre3-dev
l i bnl -3-dev
l i bnl -genl -3-dev
38
Kismet configuration
The final few lines that appear following the build
refer you to the README file. This is worth
reading. Enter:
cat README | l ess
Wireshark support
Kismet works in partnership with Wireshark, a
network protocol analyser used for network
troubleshooting, analysis and education. Let's
install that now:
apt-get i nstal l wi reshark
to:
INTERFACES=eth0
HOTPLUG_INTERFACES=
Using Kismet
Once the Raspberry Pi reboots, log in as the 'pi'
user and enter:
sudo -s
ki smet
39
Greater range
The light from a bulb or LED is boosted into a
beam by the reflector inside a car headlight or
bicycle torch.
40
Dossier
We have covered a large amount in this article
and you should have a very effective device for
monitoring Wi-Fi signals. In the next article we
will learn how to analyse the data we have
collected.
6 - Communication between
objects
SKILL LEVEL : ADVANCED
W. H. Bell
MagPi Writer
This month's article is a continuation of the introduction to C++ classes given in Issue 23 of The MagPi.
In this article communication between objects is introduced.
Object-object communication
Objects are instances of a class. This means that if two objects of a class are created, they occupy two different
memory locations. In some cases, it can be very useful to define a class with one or more data members that are
objects of a different class. A class that contains other objects as data members can easily call the member
functions of the objects it contains, in a similar manner as the mai n() function called the member functions of a
simple class in Issue 23. However, for an object that is assigned as a data member to call the member functions
of the object that contains it, it should call the correct instance in memory. This can be achieved by using the
thi s pointer to pass a pointer to the data member object.
42
#i fndef CHILD_H
#defi ne CHILD_H
cl ass Parent;
cl ass Chi l d {
publ i c:
Chi l d(Parent *); // Construct a Chi l d wi th a Parent poi nter
voi d run(voi d); // A member functi on to cal l the Parent cl ass
pri vate:
Parent *m_parent; // A data member to store a poi nter to the Chi l d' s Parent.
};
#endi f
The Chi l d class has a simple constructor that accepts a pointer of Parent class type, a public member function
run() that takes no arguments and one data member to store a pointer to the Parent object.
Create another file called Chi l d. cpp and add:
#i ncl ude <i ostream>
#i ncl ude " Parent. h"
#i ncl ude " Chi l d. h"
usi ng namespace std;
Chi l d: : Chi l d(Parent *parent):
m_parent(parent) {
}
voi d Chi l d: : run() {
cout << " Parent frame di mensi ons = {"
<< m_parent->x() << " , "
<< m_parent->y() << " }" << endl ;
}
This file contains the implementation of the constructor and the run() member function. The constructor sets
the value of the private data member pointer, using the input Parent pointer. The member function prints the x
and y values of the Parent object on the screen.
Now create a fill called Parent. h and add:
#i fndef PARENT_H
#defi ne PARENT_H
cl ass Chi l d;
cl ass Parent {
publ i c:
Parent(unsi gned i nt x, unsi gned i nt y); // Construct a Parent frame wi th some di mensi ons.
~Parent(voi d); // Destructor to cl ean up the parti cl e poi nter
voi d run(voi d); // Cal l a member functi on i n the Chi l d cl ass
unsi gned i nt x(voi d) { return m_x; } // Get the x di mensi on
43
This class contains a constructor that takes x and y dimensions, a destructor to delete the Chi l d pointer, a
run() member function to create a Chi l d object and call its run() function, accessor functions to return the
values of m_x, m_y and the m_chi l d , and private data members that contain a Chi l d pointer and dimensions x
and y. Since the Parent class declaration includes a pointer of Chi l d type, the Chi l d class must be known to
the compiler beforehand. If the private data member contained a Chi l d object (rather than a pointer to a Chi l d
object), it would be necessary to include the Chi l d header file. However, since a pointer is used it is enough to
tell the compiler that Chi l d is a class. It is a good idea to do this where possible, since it speeds up the
precompilation process.
Create another file called Parent. cpp and append:
#i ncl ude " Parent. h"
#i ncl ude " Chi l d. h"
Parent: : Parent(unsi gned i nt x, unsi gned i nt y):
m_chi l d(0),
m_x(x),
m_y(y) {
}
Parent: : ~Parent() {
i f(m_chi l d) del ete m_chi l d;
}
voi d Parent: : run() {
i f(! m_chi l d) { // Onl y create a chi l d i f there i sn' t one al ready
m_chi l d = new Chi l d(thi s);
}
m_chi l d->run();
}
This is the implementation of the Parent class. It contains the implementation of the constructor, destructor and
run() member function. It is not necessary to implement the accessor functions, since they are already
implemented in the Parent. h header file. In the Parent constructor, the private data members are initialised in
the same order as they are defined in the Parent. h file. If they were not in the same order, a warning message
would be reported during compilation. The Parent destructor checks to see if the m_chi l d pointer is null. If it
is not null, it deletes the object associated with the pointer. If the pointer is null, the run() function creates a new
Chi l d pointer on the heap. Then the run() function calls the run() function in the Chi l d class. Notice that
the Chi l d class is instantiated by calling the Chi l d class constructor that takes a Parent pointer as an
argument. In this case, the thi s pointer is used to pass a pointer to the current Parent object to the Chi l d
object.
44
This example program has two more files that are needed to get it working. First, create a file called mai n. cpp
and add the mai n() function given below:
#i ncl ude " Parent. h"
i nt mai n() {
Parent *parent = new Parent(200, 150);
parent->run();
del ete parent;
return 0;
}
The mai n() function creates an instantiation of the Parent class on the heap, calls its run() member function
and then deletes the Parent object. The Parent constructor is passed the x and y dimensions as 200 and
150 . Similar to the Chi l d. cpp and Parent. cpp files, the header file that contains the class declaration is
included to allow the code to be compiled.
The last thing needed to build the example code in a straight forward manner is the Makefi l e . Create a file
called Makefi l e in the same directory as the other source files ( Chi l d. h , Chi l d. cpp , Parent. h ,
Parent. cpp , mai n. cpp ) and add to it:
CC=g++
TARGET=pc
OBJ ECTS=mai n. o Parent. o Chi l d. o
$(TARGET): $(OBJ ECTS)
@echo " ** Li nki ng Executabl e"
$(CC) $(OBJ ECTS) -o $(TARGET)
cl ean:
@rm -f *. o *~
verycl ean: cl ean
@rm -f $(TARGET)
%. o: %. cpp
@echo " ** Compi l i ng C++ Source"
$(CC) -c $(INCFLAGS) $<
to run the example code. As an exercise, create a Parent object inside the Chi l d class and access its run()
member function instead. The values will not be the same as the other Parent object returns, since the two
objects are separate instances in memory.
45
Then this section of The MagPi is for you! We aim to list Raspberry Jam events in your area, providing
you with a Raspberry Pi calendar for the month ahead.
Are you in charge of running a Raspberry Pi event? Want to publicise it?
Email us at: editor@themagpi.com
Bristol Digimakers
When: Saturday 1 4th June 201 4, 1 0.30am to 4.30pm
Where: At-Bristol, Anchor Road, Harbourside, Bristol, BS1 5DB, UK
A series of community technology events aimed at children (7-1 7), parents and teachers. An
introduction to 'making' in the digital world. http://www.eventbrite.com/e/1 1 031 1 45453
46
The MagPi is a trademark of The MagPi Ltd. Raspberry Pi is a trademark of the Raspberry Pi Foundation. The MagPi magazine is
collaboratively produced by an independent group of Raspberry Pi owners, and is not affiliated in any way with the Raspberry Pi
Foundation. It is prohibited to commercially produce this magazine without authorization from The MagPi Ltd. Printing for non commercial
purposes is agreeable under the Creative Commons license below. The MagPi does not accept ownership or responsibility for the content
or opinions expressed in any of the articles included in this issue. All articles are checked and tested before the release deadline is met but
some faults may remain. The reader is responsible for all consequences, both to software and hardware, following the implementation of
any of the advice or code printed. The MagPi does not claim to own any copyright licenses and all content of the articles are submitted with
the responsibility lying with that of the article writer. This work is licensed under the Creative Commons Attribution-NonCommercialShareAlike 3.0 Unported License. To view a copy of this license, visit:
http://creativecommons.org/licenses/by-nc-sa/3.0/
Alternatively, send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041 , USA.