the magpi-issue 23

48
8/10/2019 The Magpi-Issue 23 http://slidepdf.com/reader/full/the-magpi-issue-23 1/48 I I S S S S U U E E  2 2 3 3  - -  M M A A Y Y  2 2 0 0 1 1 4 4  M  g  g  f  f  R  p  p  P  U http:  /  /  /www.themagpi.com Raspberry Pi is a trademar k of The Raspberry Pi Foundation. This magazine was created using a Raspberry Pi computer. G G e e t t  p p r r i i n n t t e e d d  c c o o p p i i e e s s  a a t t t t h h e e m m a a g g p p i i . . c c o o m m H H i i g g h h  A A l l t t i i t t u u d d e e  M M e e a a s s u u r r i i n n g g R R e e a a l l  W W o o r r l l d d  M M i i n n e e c c r r a a f f t t B B r r i i c c P P i i  a a n n d d  S S c c r r a a t t c c h h X X M M P P P P  C C h h a a t t  S S e e r r v v e e r r L L i i n n u u x x  T T o o o o l l  S S h h e e d d 1 1 - - W W i i r r e e  S S e e n n s s o o r r s s G G r r o o v v e e P P i i S S O O N N I I C C  P P I I  2 2 . . 0 0 G G e e t t  Y Y o o u u r r  G G r r o o o o v v e e  O O n n

Upload: sunnysigara

Post on 02-Jun-2018

230 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 1/48

IISSSSUUEE 2233 -- MMAAYY 22001144

A A M M

a a g g a a z z i i n n e e f f o o r r R R a a s s p p b b e e r r r r y y P P i i U U s s e e r r s s

hhttttpp:: / / / / wwwwww..tthheemmaaggppii..ccoommRRaassppbbeerrrryy PPii iiss aa ttrraaddeemmaarrk k ooff TThhee RRaassppbbeerrrryy PPii FFoouunnddaattiioonn..TThhiiss mmaaggaazziinnee wwaass ccrreeaatteedd uussiinngg aa RRaassppbbeerrrryy PPii ccoommppuutteerr..

GGeett pprriinntteedd ccooppiieess aatt

tthheemmaaggppii..ccoomm

HHiigghh AAllttiittuuddee MMeeaassuurriinngg

RReeaall WWoorrlldd MMiinneeccrraafftt

BBrriicck k PPii aanndd SSccrraattcchh

XXMMPPPP CChhaatt SSeerrvveerr

LLiinnuuxx TTooooll SShheedd

11--WWiirree SSeennssoorrss

GGrroovveePPii

B I G

B I R T H D A Y

C O M P E T I T I O N

W i n m o r e t h a n

£ 2 , 0 0 0

o f g o o d i e s !

SSOONNIICC PPII 22..00GGeett YYoouurr GGrroooovvee OOnn

Page 2: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 2/48

A s h S t o n e - C h i e f E d i t o r / A d m i n i s t r a t i o n / L a y o u t

W . H . B e l l - I s s u e E d i t o r / L a y o u t / A d m i n i s t r a t i o n

B r y a n B u t l e r - P a g e D e s i g n / G r a p h i c s

I a n M c A l p i n e - L a y o u t / T e s t i n g / P r o o f R e a d i n g

M a t t J u d g e - W e b s i t e / A d m i n i s t r a t i o n

A a r o n S h a w - L a y o u t

T h e M a g P i T e a m

N i c k H i t c h - A d m i n

C o l i n D e a d y - L a y o u t / T e s t i n g / P r o o f R e a d i n g

T i m C o x - T e s t i n g / P r o o f R e a d i n g

C l a i r e P r i c e - L a y o u t / P r o o f R e a d i n g

C h r i s S t a g - T e s t i n g

23

W e l c o m e t o I s s u e 2 3 o f T h e M a g P i m a g a z i n e .

I t ’ s p a r t y t i m e h e r e a t T h e M a g P i t o w e r s , c e l e b r a t i n g o u r s e c o n d b i r t h d a y ! T o m a r k t h i s m i l e s t o n e , T h e

M a g P i i s p l e a s e d t o p r o v i d e a n o t h e r m a s s i v e c h a n c e t o g e t y o u r h a n d s o n s o m e f a n t a s t i c R a s p b e r r y P i

g o o d i e s , w i t h o v e r £ 2 0 0 0 w o r t h o f t a s t y t r e a t s f o r o u r r e a d e r s t o w i n ! T h a n k y o u t o a l l o u r s p o n s o r s w h o

h a v e k i n d l y g i v e n t o w a r d s t h i s m a s s i v e c o l l e c t i o n o f p r i z e s . S e e p a g e s 1 8 - 1 9 f o r m o r e i n f o r m a t i o n .

T h i s m o n t h y o u ’ l l f i n d u s i n t h e c l u b w i t h S o n i c P i . T h e M a g P i h a s a n e x c l u s i v e o f S a m u e l A a r o n ' s

b r a n d n e w r e l e a s e o f S o n i c P i v 2 . 0 a n d h o w i t i s a i d i n g b u i l d t h e u n d e r g r o u n d m u s i c m o v e m e n t o f L i v e

C o d i n g . S a m u e l d e s c r i b e s w h a t i s n e w t o v 2 . 0 a l o n g w i t h s o m e b a s i c s t o g e t y o u u p a n d m i x i n g i n n o

t i m e .

J a c o b M a r s h f r o m M o d M y P i i s b a c k w i t h a n o t h e r g r e a t t u t o r i a l o n p h y s i c a l c o m p u t i n g , t h i s m o n t h

d e s c r i b i n g h o w t o u s e 1 - W i r e t e m p e r a t u r e s e n s o r s . W e l o o k a t h o w t o b u i l d y o u r o w n X M P P c h a t

s e r v e r i n G i a n l u c a ’ s C h a t R o o m a r t i c l e , t h e n B e r n h a r d S u t e r p r o v i d e s t h e n e x t a r t i c l e i n o u r L i n u x t o o l

s h e d s e r i e s w h e r e h e d e s c r i b e s t h e b a s h s h e l l .

M i c h a e l P e t e r s e n b e g i n s h i s t w o - p a r t s e r i e s o n u s i n g t h e R a s p b e r r y P i t o s t u d y a t m o s p h e r i c p o l l u t i o n .

I n t h i s a r t i c l e h e i n t r o d u c e s u s t o t h e m a i n s u b s y s t e m s i n v o l v e d i n t h e m u l t i - s e n s o r a r r a y u s e d i n t h e

r e s e a r c h b a l l o o n s w h i c h a r e s e n t i n t o t h e l o w e r s t r a t o s p h e r e o f U t a h . W e a l s o t a k e a l o o k a t s t a c k a b l e

h a r d w a r e w i t h S a i Y a m a n o o r ' s a r t i c l e a b o u t G r o v e P i .

O u r v e r y o w n W i l l i a m B e l l h a s b e e n w o r k i n g o v e r t i m e t h i s m o n t h w i t h n o l e s s t h a n t h r e e a r t i c l e s . F i r s t ,

h e s h o w s h o w t o i n t e r f a c e B r i c k P i w i t h S c a t c h , t h e n h e d e s c r i b e s h o w t o b r i n g M i n e c r a f t t o t h e r e a l

w o r l d a n d f i n a l l y t h e C + + C a c h e s e r i e s m a k e s a w e l c o m e r e t u r n w i t h a n e x p l a n a t i o n o f c l a s s e s .

W h y n o t h e a d o v e r t o o u r F a c e b o o k p a g e

h t t p : / / w w w . f a c e b o o k . c o m / M a g P i M a g a z i n e a n d l e t

u s k n o w y o u r f a v o u r i t e a r t i c l e o v e r t h e l a s t t w o

y e a r s , o r e v e n w h a t y o u w a n t t o r e a d a b o u t o v e r t h e

n e x t 1 2 m o n t h s !

E n j o y .

C h i e f E d i t o r o f T h e M a g P i

Page 3: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 3/483

4

S T U D Y I N G A T M O S P H E R I C P O L L U T I O N W I T H A M U L T I - S E N S O R A R R A Y

P a r t 1 : I n t r o d u c t i o n t o t h e m a i n s u b s y s t e m s

8

S t a c k a b l e h a r d w a r e e x t e n s i o n b o a r d

G R O V E P I : A D D I N G G R O V E S E N S O R M O D U L E S

P a r t 3 : S c r a t c h i n t e r f a c e w i t h R p i S c r a t c h I O

1 2

B R I C K P I

1 8

O v e r £ 2 0 0 0 o f p r i z e s t o b e w o n i n o u r s e c o n d a n n i v e r s a r y c o m p e t i t i o n

B I G B I R T H D A Y C O M P E T I T I O N

2 0

P a r t 2 : I n t e r f a c i n g M i n e c r a f t w i t h P i F a c e D i g i t a l

M I N E C R A F T P I E D I T I O N

3 4

T u r n y o u r R a s p b e r r y P i i n t o a n X M P P c h a t s e r v e r

C H A T R O O M

P H Y S I C A L C O M P U T I N G

P a r t 2 : U s i n g 1 - W i r e t e m p e r a t u r e s e n s o r s

2 6

4 2

C a m b r i d g e U K , N o r t h S t a f f o r d s h i r e U K , C a r d i f f U K , S a n M a t e o C A U S A , B a t h U K

T H I S M O N T H ' S E V E N T S G U I D E

4 4

P a r t 2 : D i s c o v e r n e w s a m p l e s , s y n t h s , s t u d i o e f f e c t s a n d L i v e C o d i n g

S O N I C P I : G E T Y O U R G R O O V E O N !

P a r t 5 : C l a s s e s

3 0

C + + C A C H E

3 8

P a r t 2 : T a l e s f r o m t h e L i n u x t o o l s h e d - d o n ' t b a s h t h e s h e l l

L I N U X C O M M A N D S

4 8

H a v e y o u r s a y a b o u t T h e M a g P i

F E E D B A C K

h t t p : / / w w w . t h e m a g p i . c o m

C o n t e n t s C o n t e n t s C o n t e n t s

Page 4: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 4/48

S K I L L L E V E L : A D V A N C E D

M i c h a e l P e t e r s e n

G u e s t W r i t e r

S T U D Y I N G A T M O S P H E R I C P O L L U T I O N

A M u l t i - S e n s o r A r r a y f o r A t m o s p h e r i c S c i e n c e

P a r t 1 : I n t r o d u c t i o n t o t h e m u l t i - s e n s o r

a r r a y s u b s y s t e m s

T h i s t w o p a r t s e r i e s d e s c r i b e s t h e d e s i g n a n d

c o n s t r u c t i o n o f a M u l t i - S e n s o r A r r a y ( M S A ) f o r

s t u d y i n g a t m o s p h e r i c p o l l u t i o n i n a n u r b a n i s e d

m o u n t a i n b a s i n ; s p e c i f i c a l l y , t h e r e g i o n a b o v e

S a l t L a k e C i t y , U t a h ( U S A ) . T h e M S A i s f l o w n o n

r e s e a r c h b a l l o o n s b y H A R B O R , a n

u n d e r g r a d u a t e r e s e a r c h g r o u p l o c a t e d a t W e b e r

S t a t e U n i v e r s i t y i n O g d e n , U t a h . T h e M S A

p r o d u c e s a c o l u m n o f m e a s u r e m e n t s f r o m

g r o u n d l e v e l ( a p p r o x . 1 k m a b o v e s e a l e v e l , A S L )

t o t h e l o w e r s t r a t o s p h e r e ( a p p r o x . 3 5 k m A S L ) .

D u r i n g f l i g h t , t h e s y s t e m i s e x p o s e d t o p r e s s u r e s

a s l o w a s 0 . 7 5 m m H g , w h e r e h e a t e x c h a n g e

b e c o m e s d i f f i c u l t e v e n a t t e m p e r a t u r e s o f - 5 0 ° C .

J e t s t r e a m w i n d s c a n e x c e e d s p e e d s o f

2 0 0 k m / h , a p p l y i n g p u n i s h i n g s h o c k a n d v i b r a t i o n

f o r c e s t o o u r e l e c t r o n i c e q u i p m e n t . I n t h i s

e x t r e m e e n v i r o n m e n t , t h e M S A m u s t c o n t i n u e t o

g a t h e r s c i e n t i f i c d a t a f o r a m i n i m u m o f 4 h o u r s .

T h e f i r s t p a r t o f t h i s s e r i e s f o c u s e s o n t h e

h a r d w a r e d e s i g n o f t h e M S A s y s t e m . P a r t 2 w i l l

d e s c r i b e h o w t h e s o f t w a r e w a s d e s i g n e d a n d

i m p l e m e n t e d .

T h e o r y o f o p e r a t i o n

T h e M S A i s a d a t a l o g g i n g c o m p u t e r w i t h a

s t a n d a r d s e t o f b u i l t - i n s e n s o r s a n d a n e x p a n d e d

s e t o f i n p u t s t o a l l o w g u e s t p a c k a g e s t o b e

c o n n e c t e d . E x t e r n a l d e v i c e s s u c h a s L E D s ,

b u z z e r s , p u m p s , a n d h e a t e r s a r e c o n t r o l l e d v i a

e x i s t i n g G P I O p i n s . T h e M S A i s c o m p r i s e d o f s i x

m a j o r s u b s y s t e m s :

1 . R a s p b e r r y P i ( V e r s i o n 1 - M o d e l B )

2 . S y s t e m I n t e r f a c e D a u g h t e r b o a r d

3 . E x t e r n a l S e n s o r B o a r d

4 . E x t e r n a l C o n t r o l s a n d I n d i c a t o r s

5 . P o w e r S u p p l y

6 . E n c l o s u r e s

T h e R a s p b e r r y P i s e r v e s a s t h e p r i m a r y p l a t f o r m

f o r t h e M S A s y s t e m . I t m o n i t o r s i n p u t s ,

c o m m u n i c a t e s w i t h s e n s o r s , p r o c e s s e s a n d

s t o r e s f l i g h t d a t a , a n d c o n t r o l s L E D s , m o t o r s ,

a n d h e a t e r s .

Page 5: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 5/485

S y s t e m I n t e r f a c e D a u g h e r b o a r d

T h e S y s t e m I n t e r f a c e D a u g h t e r b o a r d ( S I D ) l i n k s

t h e R a s p b e r r y P i t o e v e r y t h i n g e l s e i n t h e

s y s t e m . I t a l s o p r o v i d e s r e g u l a t e d p o w e r . T h e

S I D i s w h e r e a n a l o g u e a n d d i g i t a l s e n s o r s a r e

m o u n t e d o r c o n n e c t e d . I t i s e q u i p p e d w i t h t h e

f o l l o w i n g c o m p o n e n t s :

T h e s e n s o r s a r e u s e d t o c a l i b r a t e d a t a a n d h e l p

e l i m i n a t e b a d d a t a w h i c h m a y h a v e b e e n

r e c o r d e d d u r i n g a n e v e n t ; s u c h a s a n i m p a c t ,

e x t r e m e t e m p e r a t u r e , o r h i g h h u m i d i t y . A l l

s e n s o r s c o m m u n i c a t e o v e r t h e a v a i l a b l e I 2 C

b u s , w i t h a n a l o g u e s e n s o r s r o u t e d t o a n I 2 C

c a p a b l e A D C .

T h e r e a r e t w o p r i m a r y s e n s o r c i r c u i t s b u i l t i n t o

t h e S I D . T h e f i r s t i s a f l i g h t d y n a m i c c i r c u i t w h i c h

c o n s i s t s o f a n I 2 C a c c e l e r o m e t e r / g y r o s c o p e

s e n s o r a n d m a g n e t o m e t e r .

T h e s e c o n d i s a n e n v i r o n m e n t a l c i r c u i t

c o m p o s e d o f t e m p e r a t u r e , h u m i d i t y , a n d

p r e s s u r e s e n s o r s .

T h e A D C c a n s u p p o r t u p t o s i x t e e n s i n g l e - e n d e d

a n a l o g u e s e n s o r s , n o t i n c l u d i n g a n i n t e r n a l

t e m p e r a t u r e s e n s o r . T h e I 2 C b u s c a n s u p p o r t

m a n y m o r e d e v i c e s .

W e d e s i g n e d o u r c i r c u i t b o a r d s u s i n g a f r e e

v e r s i o n o f t h e E a g l e P C B D e s i g n S o f t w a r e b y

C a d S o f t . W e m a n u f a c t u r e d t h e b o a r d s i n - h o u s e

u s i n g c o m m e r c i a l l y a v a i l a b l e c h e m i c a l s a n d

p h o t o s e n s i t i v e P C B s . T h e t o p v i e w o f a

c o m p l e t e d S I D b o a r d i s s h o w n a b o v e . T h e

a s s e m b l e d b o a r d m o u n t s t o t h e R a s p b e r r y P i v i a

t h e 2 x 1 3 p i n h e a d e r .

E x t e r n a l s e n s o r s

T h e e x t e r n a l s e n s o r b o a r d c o n s i s t s o f a T M P 1 1 2

t e m p e r a t u r e s e n s o r a n d a n H I H 5 0 3 0 h u m i d i t y

s e n s o r m o u n t e d t o a s m a l l P C B e m b e d d e d i n t h e

e x t e r i o r w a l l o f t h e e x t e r n a l e n c l o s u r e a n d

c o n n e c t e d w i t h a s h o r t h a r n e s s .

E x t e r n a l c o n t r o l s a n d i n d i c a t o r s

E x t e r n a l c o n t r o l s a n d i n d i c a t o r s a l l o w t h e u s e r t o

p o w e r u p a n d s h u t d o w n t h e M S A f r o m o u t s i d e

i t s i n s u l a t e d e n c l o s u r e . L E D s a n d b u z z e r s a l l o w

t h e u s e r t o k n o w t h e s t a t u s o f t h e M S A , w h e t h e r

i t i s i n s t a n d b y o r d a t a l o g g i n g m o d e , a n d a l s o

a l e r t s u n a w a r e p e o p l e o n t h e g r o u n d w h e n i t i s

d e s c e n d i n g n e a r t h e s u r f a c e . A p u l l - p i n s t a r t s a

m i s s i o n t i m e r t o a l l o w s y n c h r o n i s a t i o n o f d a t a

b e t w e e n v a r i o u s i n s t r u m e n t s a t t h e s t a r t o f a

f l i g h t . I t c a n a l s o b e r e - i n s e r t e d t o s a v e d a t a f i l e s

a n d i n i t i a t e a s a f e - s h u t d o w n .

* M P U 6 0 0 0 ( 6 - a x i s a c c e l e r o m e t e r / g y r o s c o p e )

* H M C 5 8 8 3 L ( 3 - a x i s m a g n e t o m e t e r )

* M P X 2 1 0 2 A ( p r e s s u r e s e n s o r )

* M A X 4 2 0 8 ( i n s t r u m e n t a m p l i f i e r )

* H I H 5 0 3 0 ( h u m i d i t y s e n s o r )

* T M P 1 1 2 ( t e m p e r a t u r e s e n s o r )

* C O M - 0 8 7 2 0 ( p u s h b u t t o n )

* M I C 2 9 3 7 A ( 3 . 3 V r e g u l a t o r )

* M I C 2 9 3 7 A ( 5 . 0 V r e g u l a t o r )

* L T C 2 4 9 5 ( 1 6 C h a n n e l A D C )

* D S 3 2 3 1 M ( R e a l - T i m e C l o c k )

T e m p e r a t u r e

S e n s o r

H u m i d i t y

S e n s o r

C o n n e c t o r

( b o t t o m )

Page 6: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 6/48

P o w e r s u p p l y

T h e p o w e r s u p p l y s u b s y s t e m c o m b i n e s s o f t w a r e

a n d h a r d w a r e t o p r o v i d e p o w e r a n d i n i t i a t e a

s a f e s h u t d o w n i n t h e e v e n t o f b a t t e r y f a i l u r e .

T h e h a r d w a r e c o m p o n e n t i s b a s i c a l l y a 7 . 4 V

L i P O b a t t e r y a n d t w o l i n e a r v o l t a g e r e g u l a t o r s

( 5 V a n d 3 . 3 V ) . T h e 5 V r e g u l a t o r s u p p l i e s p o w e r

t o t h e R a s p b e r r y P i a n d h a s a c o u p l e h u n d r e d

m i l l i a m p s a v a i l a b l e f o r o t h e r d e v i c e s . T h e 3 . 3 V

r e g u l a t o r p r o v i d e s v o l t a g e f o r t h e I 2 C b u s a n d a l l

o f t h e o n b o a r d s e n s o r s .

B a t t e r y p o w e r i s a p r e c i o u s c o m m o d i t y o n

b a l l o o n f l i g h t s , a s c a p a c i t y p e r g r a m q u i c k l y e a t s

i n t o m a s s b u d g e t s . F e d e r a l r e g u l a t i o n s l i m i t

b a l l o o n p a c k a g e s t o 5 . 4 k g , w h i c h m u s t b e

d i s t r i b u t e d a m o n g s t v a r i o u s f l i g h t p a c k a g e s ;

i n c l u d i n g r a d i o t r a n s m i t t e r s , c a m e r a s , a n d o t h e r

s c i e n t i f i c i n s t r u m e n t s . T h e M S A s y s t e m w a s

l i m i t e d t o j u s t 4 0 8 g w h i c h m a d e b a t t e r y s e l e c t i o n

c r u c i a l .

T h e R a s p b e r r y P i d r a w s b e t w e e n 3 1 0 m A a n d

3 9 0 m A u n d e r n o r m a l o p e r a t i o n ; t h i s i n c l u d e s t h e

c u r r e n t d r a w o f t h e H D M I o u t p u t c h i p a n d t h e

U S B / E t h e r n e t c h i p s , w h i c h a c c o u n t f o r n e a r l y

o n e h a l f o f t h e t o t a l c u r r e n t c o n s u m p t i o n . T o

p r o l o n g b a t t e r y l i f e d u r i n g f l i g h t , t h e s e c h i p s m u s t

b e d i s a b l e d . T h e c h i p s a r e d i s a b l e d i n t h e M S A

c o d e b y c a l l i n g t w o L i n u x c o m m a n d s .

T h e C c o d e t o s h u t d o w n t h e H D M I v i d e o c h i p i s :

s y s t e m ( " / o p t / v c / b i n / t v s e r v i c e - o f f " ) ;

T h e n e x t l i n e o f c o d e s u s p e n d s t h e

U S B / E t h e r n e t c o n t r o l l e r :

s y s t e m ( " s h - c \ " e c h o 1 >

/ s y s / d e v i c e s / p l a t f o r m / b c m 2 7 0 8 / u s b / b u s s u s p e

n d \ " " ) ;

B o t h o f t h e s e c h i p s a r e r e - e n a b l e d w h e n t h e

M S A i s r e b o o t e d o r p o w e r e d u p f o r t h e f i r s t t i m e .

A f t e r t h e c h i p s a r e d i s a b l e d , t h e a v e r a g e c u r r e n t

d r a w o f t h e R a s p b e r r y P i d r o p s t o 1 8 0 m A .

F a c t o r i n g i n t h e a d d i t i o n a l p o w e r c o n s u m p t i o n o f

t h e s e n s o r s a n d I C s , a t o t a l o f 1 4 . 4 2 m A , a n d t h e

r e q u i r e d b a t t e r y l i f e o f 4 h o u r s , a m i n i m u m

b a t t e r y c a p a c i t y o f 7 7 8 m A h i s r e q u i r e d .

4 h r s x ( 1 8 0 m A + 1 4 . 4 2 m A ) = 7 7 8 m A h

W e s e l e c t e d a 7 . 4 V 1 1 0 0 m A h L i P O b a t t e r y ,

w h i c h p r o v i d e s a m o d e s t a m o u n t o f h e a d r o o m

a n d o n l y c o s t s u s 7 2 g o f o u r 4 0 8 g m a s s b u d g e t .

E n c l o s u r e s

A n i n t e r n a l e n c l o s u r e w a s d e s i g n e d t o p r o t e c t

t h e R a s p b e r r y P i a n d S I D f r o m s h o c k a n d

v i b r a t i o n s a s t h e M S A m a k e s i t s w a y t h r o u g h t h e

j e t s t r e a m a n d u p o n l a n d i n g . I t i s m a d e o f h i g h -

d e n s i t y f o a m w r a p p e d i n a l i g h t w e i g h t f a b r i c . T h e

f a b r i c a c t s a s a h i n g e a n d a l l o w s t h e l i d t o w r a p

a r o u n d t h e s i d e s , w h i c h a r e s e c u r e d w i t h

V E L C R O ® b r a n d h o o k - a n d - l o o p f a s t e r n e r s .

Page 7: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 7/487

T h e i n t e r n a l e n c l o s u r e f i t s s n u g g l y i n t o a 3 0 c m x

3 0 c m x 3 0 c m S t y r o f o a m e x t e r n a l e n c l o s u r e . T h e

e x t e r n a l e n c l o s u r e p r o v i d e s s t r u c t u r a l r i g i d n e s s

f o r t h e e x t e r n a l c o n t r o l s , a n d i n s u l a t e s s e n s i t i v e

c o m p o n e n t s f r o m e x t r e m e e n v i r o n m e n t a l

c o n d i t i o n s .

C o m p l e t e l y a s s e m b l e d , t h e w h o l e u n i t w e i g h s

4 0 6 . 9 g r a m s . I t h a s b e e n s u c c e s s f u l l y t e s t e d o n

f o u r f l i g h t s a n d h a s r e a c h e d a n a l t i t u d e o f 3 4 k m .

I t h a s s u r v i v e d l a n d i n g i n r u g g e d d e s e r t t e r r a i n

a n d a l s o a s p l a s h d o w n i n a r e s e r v o i r .

C o m i n g u p

I n p a r t 2 o f t h i s s e r i e s , I w i l l d i s c u s s h o w t h e

M S A i n i t i a t e s t h e d a t a l o g g i n g p r o g r a m a t

s t a r t u p , c o m m u n i c a t e s w i t h s o m e o f t h e s e n s o r s ,

s t o r e s d a t a , m a n a g e s s a m p l i n g r a t e s , i n t e r f a c e s

w i t h t h e u s e r , a n d r e s p o n d s t o c o n d i t i o n a l

e v e n t s ; s u c h a s l o w b a t t e r y v o l t a g e .

T h e c o d e f o r t h e M S A i s w r i t t e n c o m p l e t e l y i n C

a n d i s f a c i l i t a t e d w i t h t h e a i d o f t h e B C M 2 8 3 5 C

l i b r a r y , m a i n t a i n e d b y M i k e M c C a u l e y a t

h t t p : / / w w w . a i r s p a y c e . c o m . T h e c o d e a l s o t a k e s

a d v a n t a g e o f s e m a p h o r e s a n d p a r a l l e l

p r o g r a m m i n g t o c r e a t e a t i m e r w i t h o u t t h e u s e o f

i n t e r r u p t s .

A c k n o w l e d g e m e n t s

T h e f o l l o w i n g m e m b e r s c o n t r i b u t e d t o t h e d e s i g n

a n d c o n s t r u c t i o n o f t h e M S A .

E l e c t r o n i c s E n g i n e e r i n g S t u d e n t s :

- M i c h a e l P e t e r s e n

- J . W e s l e y M a h u r i n

- J e n i f e r S t o d d a r d

D r . J o h n E . S o h l : D i r e c t o r o f H A R B O R a n d

P r o f e s s o r o f P h y s i c s a t W S U

D r . F o n B r o w n : M e n t o r a n d P r o f e s s o r o f

E l e c t r o n i c s E n g i n e e r i n g a t W S U

D e v e l o p m e n t o f t h e M S A w a s f u n d e d i n p a r t b y

t h e V a l A . B r o w n i n g F o u n d a t i o n , t h e R a l p h N y e

C h a r i t a b l e F o u n d a t i o n , t h e W e b e r S t a t e

U n i v e r s i t y O f f i c e o f U n d e r g r a d u a t e R e s e a r c h ,

a n d t h e U t a h C h a p t e r o f t h e A I A A . H A R B O R

w o u l d l i k e t o g i v e s p e c i a l t h a n k s t o A l R y d m a n

f o r h i s h o s p i t a l i t y a n d f o r m a k i n g t h e D u c h e s n e

M u n i c i p a l A i r p o r t a v a i l a b l e t o u s t o l a u n c h o u r

s y s t e m .

U s e f u l l i n k s

C a d S o f t E A G L E P C B D e s i g n S o f t w a r e :

h t t p : / / w w w . c a d s o f t u s a . c o m / e a g l e - p c b - d e s i g n -

s o f t w a r e

B C M 2 8 3 5 C l i b r a r y :

h t t p : / / a i r s p a y c e . c o m / m i k e m / b c m 2 8 3 5 /

H A R B O R h o m e p a g e :

h t t p : / / p l a n e t . w e b e r . e d u / h a r b o r /

Page 8: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 8/488

SKILL LEVEL : INTERMEDIATE

Sai Yamanoor

M a g P i w r i t e r

GROVEPIA d d i n g g r o v e s e n s o r m o d u l e s

Stackable hardwareextension board

Introduction

T h e G r o v e P i i s a a n e x t e n s i o n b o a r d t h a t a l l o w s

a d d i t i o n a l h a r d w a r e t o b e s t a c k e d o n t o p o f t h e

R a s p b e r r y P i .

h t t p : / / w w w . d e x t e r i n d u s t r i e s . c o m / G r o v e P i /

T h e b o a r d s i m p l i f i e s t h e a r t o f e l e c t r o n i c c i r c u i t

h a c k i n g , s i n c e i t c o m e s w i t h a n o n b o a r d

m i c r o c o n t r o l l e r . T h e e x t e n s i o n b o a r d e n a b l e s

i n t e r f a c i n g t h e R a s p b e r r y P i t o a n e c o s y s t e m o f

m o d u l e s i n c l u d i n g s e n s o r s , a c t u a t o r s a n d

d i s p l a y s , t h r o u g h a s e t o f s t a n d a r d c o n n e c t o r s .

T h e G r o v e P i c o m e s w i t h a n a l o g u e i n p u t s , d i g i t a l

i n p u t / o u t p u t ( I / O ) a n d I

2

C i n t e r f a c e s . T h e

m o d u l e s a r e c a l l e d “ e l e c t r o n i c b r i c k s ” . T h e

b o a r d i s b a s e d o n a n A t m e g a 3 2 8 m i c r o c o n t r o l l e r

t h a t c o m e s p r e l o a d e d w i t h t h e f i r m w a r e r e q u i r e d

t o c o m m u n i c a t e w i t h t h e R a s p b e r r y P i . I t a l s o

p o s s i b l e t o f l a s h t h e o n b o a r d m i c r o c o n t r o l l e r w i t h

y o u r o w n f i r m w a r e f o r a p r o j e c t . T h e G r o v e P i

c o m m u n i c a t e s w i t h t h e R a s p b e r r y P i u s i n g t h e

I

2

C i n t e r f a c e . S i n c e t h e G r o v e P i i s s t a c k a b l e , i t

i s p o s s i b l e t o m a k e u s e o f o t h e r G P I O p i n s , a s

w e l l a s o t h e r d e v i c e s t h a t a r e c o n n e c t e d t o t h e

R a s p b e r r y P i I

2

C b u s .

Getting Started

T h e G r o v e P i l i b r a r y c a n b e d o w n l o a d e d f r o m :

h t t p s : / / g i t h u b . c o m / D e x t e r I n d / G r o v e P i

T h e d e p e n d e n c i e s f o r t h e G r o v e P i c a n b e

i n s t a l l e d u s i n g a s c r i p t a v a i l a b l e i n t h e ‘ S c r i p t ’

d i r e c t o r y o f t h e G r o v e P i l i b r a r y a n d t h e l i b r a r y

c o u l d b e u s e d u p o n r e b o o t .

T h i s a r t i c l e d i s c u s s e s s o m e s i m p l e P y t h o n 3

e x a m p l e s t h a t c a n b e u s e d w i t h t h e G r o v e P i .

T h e e x a m p l e c o d e d i s c u s s e d i s a v a i l a b l e a t :

h t t p s : / / g i t h u b . c o m / y a m a n o o r s a i / G r o v e P i _ T e s t

Page 9: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 9/489

Blinking LED example

T o t e s t t h a t t h e G r o v e P i i s w o r k i n g c o r r e c t l y , a

s i m p l e L E D e x a m p l e c a n b e u s e d . T h e G r o v e P i

i s c o n t r o l l e d f r o m t h e R a s p b e r r y P i u s i n g P y t h o n

a n d t h e G r o v e P i m o d u l e . T o c a u s e t h e L E D o n

t h e G r o v e P i t o b l i n k :

import time

import grovepi

grovepi.pinMode(7,"OUTPUT")

time.sleep(1)

while True:

try:

grovepi.digitalWrite(7,1)

time.sleep(1)

grovepi.digitalWrite(7,0)

time.sleep(1)

except IOError:

print("Error")

A t t h e t o p o f t h e p r o g r a m , t h e G r o v e P i m o d u l e i s

i m p o r t e d . T h e n p i n 7 i s c o n f i g u r e d a s a n o u t p u t

p i n .

grovepi.pinMode(7,"OUTPUT")

W i t h i n t h e l o o p , t h e digitalWrite

f u n c t i o n i s

u s e d t o t u r n t h e L E D o n a n d o f f . T h e p r o g r a m

w a i t s f o r o n e s e c o n d a f t e r t u r n i n g t h e L E D o n

a n d o n e s e c o n d a f t e r t u r n i n g t h e L E D o f f .

T h e G r o v e P i f u n c t i o n n a m i n g s c h e m e i s s i m i l a r

t o t h e A r d u i n o l i b r a r i e s a n d W i r i n g P i .

Data-logger application

T h e G r o v e P i c a n b e u s e d t o m e a s u r e t h e a i r

q u a l i t y a n d l o g t h e a s s o c i a t e d d a t a . T h i s i s

a c h i e v e d u s i n g a n a i r q u a l i t y s e n s o r m o d u l e a n d

a r e a l t i m e c l o c k ( R T C ) m o d u l e ,

tk = Tk()

# Add a canvas area ready for drawing onSensorButton = Button(tk,text="Update Air

Quality Value",command= Analog)

SensorButton.pack()

#Add an exit button

btn = Button(tk, text="Exit",

command=terminate)

btn.pack()

Label1 = Label(tk)

Label1.pack()

Label1.configure(text=("Sensor Value = %d")%

grovepi.analogRead(0))

Label2 = Label(tk)

Label2.pack()

I n t h i s e x a m p l e , t h e G r o v e P i i s u s e d t o r e a d t h e

c u r r e n t t i m e f r o m t h e R T C m o d u l e a n d d i s p l a y i t

i n a d e s k t o p w i n d o w . T h e s e n s o r v a l u e a n d

a s s o c i a t e d r e a d o u t t i m e i s w r i t t e n i n t o a t e x t f i l e .

T h i s p r o g r a m m a k e s u s e o f t h e T k i n t e r g r a p h i c a l

u s e r i n t e r f a c e ( G U I ) t o o l k i t , t o i m p l e m e n t a

s i m p l e u s e r i n t e r f a c e . A b a s i c g u i d e t o u s i n g

T k i n t e r f r o m P y t h o n i s g i v e n a t :

h t t p : / / p i h w . w o r d p r e s s . c o m / l e s s o n s / r g b - l e d -

l e s s o n - 5 - c r e a t i n g - a - g r a p h i c a l - u s e r - i n t e r f a c e /

T h e T k i n t e r o b j e c t i s i n i t i a l i s e d a n d t w o b u t t o n

w i d g e t s a r e a d d e d . O n e b u t t o n w i d g e t c a l l e d

SensorButton i s u s e d t o r e c o r d t h e a n a l o g

v a l u e f r o m t h e a i r q u a l i t y s e n s o r a l o n g w i t h t h e

t i m e r e a d f r o m t h e R T C m o d u l e . A c a l l b a c k

Page 10: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 10/4810

f u n c t i o n c a l l e d Analog

i s r e g i s t e r e d t o r e a c t t o

SensorButtonc h a n g e s .

def Analog():

global Label1

try:

Value = (int)(grovepi.analogRead(0))DisplayTime = grovepi.rtc_getTime()

Label1.configure(text=("Sensor value =

%d")% Value)

text_file = open("Output.txt", "a")

text_file.write("%02d:%02d:%02d Sensor

value:%d\n" % (DisplayTime[1],

DisplayTime[2],DisplayTime[3],Value))

text_file.close()

except IOError:

pass

T h e o t h e r b u t t o n c a l l e d Exit

t e r m i n a t e s t h e

a p p l i c a t i o n a n d d e s t r o y s t h e w i n d o w .

def terminate():

global tk

tk.destroy()

W e u s e t h e Label

w i d g e t t o u p d a t e t h e t i m e

u s i n g t h e R T C m o d u l e .

def Display_Time():

global Label2,DisplayTime

try:

DisplayTime = grovepi.rtc_getTime()

Label2.configure(text=("%02d:%02d:%02d")

%(DisplayTime[1],DisplayTime[2],

DisplayTime[3]))

tk.after(1000,Display_Time)

except Exception,e:

print 'Exception was thrown', str(e) print "Error"

tk.after(1000,Display_Time)

T h e f u n c t i o n c a l l rtc_getTime()

r e t u r n s a n 8

b y t e a r r a y , c o n t a i n i n g t h e d a t e a n d t i m e

i n f o r m a t i o n . T h i s i s s e t a s t h e t e x t i n t h e Label

w i d g e t . T h e t e x t i s u p d a t e d e v e r y s e c o n d , u s i n g

t h e t k w i d g e t s m e t h o d after

.

tk.after(1000,Display_Time)

P u t t i n g a l l t h e c o d e t o g e t h e r , t h e G U I l o o k s

s o m e t h i n g l i k e t h i s :

T h e a p p l i c a t i o n l o g s d a t a a s f o l l o w s :

06:09:56 Sensor value: 260

Conclusion

T h e G r o v e P i i s a b o a r d t h a t a l l o w s G r o v e P i

s e n s o r s t o b e e a s i l y i n t e r f a c e w i t h t h e R a s p b e r r y

P i . T h e G r o v e P i b o a r d c o s t s 2 4 U S D . T h e r e

a r e d i f f e r e n t g r o v e m o d u l e s , a v a i l a b l e f r o m

p r i c e s a s l o w a s 5 U S D ( a r e l a y m o d u l e ) t o

5 0 U S D ( a n E M G d e t e c t o r u s e d f o r d e t e c t i o n o f

s i g n a l s f r o m s k e l e t a l m u s c l e s ) .

M o r e e x a m p l e s f o r t h e G r o v e P i a r e a v a i l a b l e a t :

h t t p : / / w w w . d e x t e r i n d u s t r i e s . c o m / G r o v e P i / p r o j e c t

s - f o r - t h e - r a s p b e r r y - p i /

Page 11: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 11/48

Page 12: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 12/482

S K I L L L E V E L : I N T E R M E D I A T E

W . H . B e l l

M a g P i W r i t e r

L E G O N X T I N T E R F A C E

P l u g i n L E G O ® s e n s o r s a n d m o t o r s

B r i c k P i S c r a t c h i n t e r f a c e w i t h

R p i S c r a t c h I O - p a r t 3

I n t r o d u c t i o n

T h e B r i c k P i p r o v i d e s a n i n t e r f a c e b e t w e e n L E G O ®

M I N D S T O R M S ® m o t o r s a n d s e n s o r s a n d t h e

R a s p b e r r y P i . T h e b o a r d h a s f i v e s e n s o r p o r t s a n d

f o u r m o t o r p o r t s . T h e R a s p b e r r y P i c o m m u n i c a t e s

w i t h t h e B r i c k P i o v e r t h e s e r i a l p o r t ( U A R T ) , w h i c h i s

a v a i l a b l e v i a p i n s 8 a n d 1 0 o f t h e 2 6 p i n h e a d e r o n

t h e R a s p b e r r y P i :

h t t p : / / e l i n u x . o r g / R P i _ L o w - l e v e l _ p e r i p h e r a l s

T h e B r i c k P i i s a b l e t o r e a d b o t h t h e s e n s o r s a n d t h e

m o t o r e n c o d e r v a l u e s , p r o v i d i n g a s i m p l e i n t e r f a c e t o

a r a n g e o f M I N D S T O R M S ® b a s e d p r o j e c t s . M o r e

d e t a i l s o f t h e B r i c k P i a r e g i v e n i n I s s u e s 1 7 a n d 1 8 o f

T h e M a g P i .

A l o c a l s c h o o l d e c i d e d t o b u y s e v e r a l B r i c k P i b o a r d s ,

f o r t h e i r e n g i n e e r i n g c o u r s e . A f t e r a f e w t e s t s , i t w a s

c l e a r t h a t a n e w S c r a t c h d r i v e r w a s n e e d e d t o a l l o w

t h e B r i c k P i t o b e u s e d w i t h i n t h e c o u r s e m a t e r i a l . T o

a l l o w o t h e r R a s p b e r r y P i G P I O c o n n e c t i o n s t o b e

u s e d a n d p r o v i d e e a s y c o n f i g u r a t i o n o f t h e B r i c k P i ,

R p i S c r a t c h I O w a s c h o s e n a s t h e b a s i s o f t h e S c r a t c h

i n t e r f a c e . M o r e i n f o r m a t i o n o n R p i S c r a t c h I O c a n b e

f o u n d i n I s s u e s 2 0 a n d 2 2 o f T h e M a g P i a n d a t :

h t t p s : / / p y p i . p y t h o n . o r g / p y p i / R p i S c r a t c h I O /

I n s t a l l a t i o n & c o n f i g u r a t i o n

T h e s e i n s t a l l a t i o n i n s t r u c t i o n s s t a r t f r o m t h e b a s i c

R a s p b i a n i m a g e t h a t c a n b e d o w n l o a d e d f r o m t h e

R a s p b e r r y P i w e b s i t e :

h t t p : / / w w w . r a s p b e r r y p i . o r g / d o w n l o a d s /

T h e B r i c k P i P y t h o n a n d S c r a t c h i n t e r f a c e s a r e

a v a i l a b l e a s a P y t h o n m o d u l e . T o i n s t a l l b o t h

i n t e r f a c e s , t o g e t h e r w i t h t h e i r d e p e n d e n c i e s , o p e n a

t e r m i n a l w i n d o w a n d t y p e :

s u d o a p t - g e t i n s t a l l - y p y t h o n - s e r i a l \

p y t h o n - s e t u p t o o l s p y t h o n - d e v

e a s y _ i n s t a l l p i p

p i p i n s t a l l B r i c k P i

T o u s e t h e s e r i a l p o r t t o c o m m u n i c a t e w i t h t h e

B r i c k P i , i t h a s t o b e a v a i l a b l e a s a n i n p u t / o u t p u t ( I / O )

c o n n e c t i o n . T o a l l o w a c o n n e c t i o n u s i n g t h e s e r i a l

b u s , t h e R a s p b i a n c o n f i g u r a t i o n n e e d s t o b e c h a n g e d

s l i g h t l y . T y p e :

s u d o - s

T h e n u s e t h e n a n o e d i t o r t o e d i t

/ b o o t / c o n f i g . t x t :

n a n o / b o o t / c o n f i g . t x t

Page 13: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 13/481 3

G o t h e e n d o f t h e f i l e a n d a d d :

i n i t _ u a r t _ c l o c k = 3 2 0 0 0 0 0 0

T h e n s a v e t h i s f i l e a n d o p e n / e t c / i n i t t a b . F i n d

T 0 : 2 3

a n d p u t a # c h a r a c t e r i n f r o n t , t o c o m m e n t i t o u t .

T h e n s a v e i t . N e x t , o p e n / b o o t / c m d l i n e . t x t a n d

r e m o v e

c o n s o l e = t t y A M A 0 , 1 1 5 2 0 0 k g d b o c = t t y A M A 0 , 1 1 5 2 0 0

a n d s a v e t h e f i l e . N o w r e b o o t t h e R a s p b e r r y P i :

r e b o o t

B u i l d i n g t h e t a n k

F o l l o w t h e i n s t r u c t i o n s g i v e n a t ,

h t t p : / / w w w . d e x t e r i n d u s t r i e s . c o m / B r i c k P i / p r o j e c t s /

t a n k /

t o b u i l d a t r a c k e d v e h i c l e . T h e n p u t t h e B r i c k P i o n

t o p a n d c o n n e c t t h e r i g h t m o t o r t o B r i c k P i p o r t M A a n d

t h e l e f t m o t o r t o B r i c k P i p o r t M B

. F i n a l l y , a d d a

f o r w a r d - f a c i n g u l t r a s o n i c s e n s o r a n d c o n n e c t i t t o

B r i c k P i s e n s o r p o r t S 1 . T h e B r i c k P i p o r t l a b e l l i n g i s

g i v e n i n T h e M a g P i I s s u e 1 8 a r t i c l e o n t h e B r i c k P i .

H u m a n c o n t r o l l e d t a n k

T h e B r i c k P i h a s t o b e a p p r o p r i a t e l y c o n f i g u r e d f o r

e a c h d i f f e r e n t L E G O ® s e n s o r . C r e a t e a n e w f i l e

c a l l e d t a n k . c f g t h a t c o n t a i n s :

N o w o p e n S c r a t c h a n d e n a b l e r e m o t e s e n s o r

c o n n e c t i o n s , b y s e l e c t i n g t h e " S e n s i n g " p a l e t t e a n d

r i g h t c l i c k i n g o n " s e n s o r v a l u e " a t t h e b o t t o m o f t h e

t o o l p a l e t t e . ( F o r t h e c u r r e n t R a s p b i a n v e r s i o n o f

S c r a t c h , t h e r e m o t e s e n s o r c o n n e c t i o n s n e e d t o b e

d i s a b l e d a n d t h e n r e - e n a b l e d . ) N o w s t a r t

R p i S c r a t c h I O b y t y p i n g :

R p i S c r a t c h I O t a n k . c f g

T h i s c r e a t e s n e w S c r a t c h s e n s o r s w i t h n a m e s w i t h i n

t h e r a n g e s L E G O : 0 - L E G O : 3 , L E G O : 1 0 - L E G O : 1 3

a n d L E G O : 2 0 - L E G O : 2 3 , w h e r e L E G O : 0 - L E G O : 3

a r e t h e s e n s o r p o r t s S 1 - S 4 , L E G O : 1 0 - L E G O : 1 3

c o r r e s p o n d t o t h e v a l u e s w r i t t e n t o t h e f o u r m o t o r

p o r t s M A - M B a n d L E G O : 2 0 - L E G O : 2 3 a r e t h e m o t o r

e n c o d e r s f o r M A - M B . T h e s e n s o r ( S 1 - S 4 ) a n d t h e

m o t o r e n c o d e r ( M A - M B ) v a l u e s c a n b e u p d a t e d i n

S c r a t c h b y b r o a d c a s t i n g r e a d c o m m a n d s . F o r

e x a m p l e ,

L E G O : r e a d : 0

t r a n s f e r s t h e c u r r e n t S 1 v a l u e i n t o t h e S c r a t c h s e n s o r

L E G O : 0 . T h e m o t o r s p e e d c a n b e c h a n g e d b y

b r o a d c a s t i n g a w r i t e c o m m a n d :

L E G O : w r i t e : 1 0 , 2 5 5

w h e r e t h i s s e t s t h e m o t o r c o n n e c t e d t o M A t o r u n

f o r w a r d s a t f u l l s p e e d . T o s t o p t h e m o t o r a t t a c h e d t o

M A , u s e S c r a t c h t o b r o a d c a s t :

L E G O : w r i t e : 1 0 , 0

I f a s e n s o r c h a n n e l i s u s e d t h a t h a s n o t b e e n

[ D e v i c e T y p e s ]

L E G O = i m p o r t B r i c k P i ; f r o m B r i c k P i . B r i c k P i S c r a t c h i m p o r t B r i c k P i S c r a t c h ; B r i c k P i S c r a t c h ( )

[ D e v i c e C o n n e c t i o n s ]

L E G O = U A R T 0

[ B r i c k P i ]

S 1 = U L T R A S O N I C _ C O N T

M A =

M B =

Page 14: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 14/484

c o n f i g u r e d i n t h e c o n f i g u r a t i o n f i l e , t h e n a w a r n i n g i s

r e p o r t e d . T o u p d a t e t h e s e n s o r s t h a t a r e a c t i v e i n

S c r a t c h , e x i t R p i S c r a t c h I O b y t y p i n g C T R L - C , e d i t

t h e R p i S c r a t c h I O c o n f i g u r a t i o n f i l e a n d t h e n r e s t a r t

R p i S c r a t c h I O a s b e f o r e .

T h e B r i c k P i S c r a t c h i n t e r f a c e c a n b e e a s i l y u s e d t o

c o n t r o l t h e p o s i t i o n o f t h e t a n k , u s i n g t h e k e y b o a r d .

I n t h i s e x a m p l e , a s i m p l e t o p v i e w o f a t a n k w a s u s e d

a s t h e m a i n s p r i t e .

T h e n t w o s p r i t e s w e r e m a d e , o n e f o r e a c h t r a c k o f

t h e v e h i c l e . T h e m a i n s p r i t e w a s t h e n l i n k e d t o t h e

u l t r a s o n i c s e n s o r a n d e a c h t r a c k w a s l i n k e d t o a

m o t o r . T h e m a i n s p r i t e p r o g r a m ,

s e n d s a r e a d c o m m a n d t o r e a d t h e u l t r a s o n i c

s e n s o r s , w a i t s f o r h a l f a s e c o n d a n d t h e n p r i n t s t h e

v a l u e o f t h e s e n s o r . T h e v a l u e o f t h e s e n s o r i s a l s o

u s e d t o c h a n g e t h e c o s t u m e o f t h e m a i n s p r i t e , t o

i n d i c a t e i f t h e r e i s a n o b j e c t c l o s e b y .

T h e p r o g r a m f o r t h e l e f t m o t o r ,

s e t s t h e m o t o r t o r u n f o r w a r d w h e n t h e a

k e y i s h e l d

d o w n . W h e n t h e k e y i s r e l e a s e d , t h e m o t o r s t o p s . T o

m a k e t h e m o t o r g o b a c k w a r d s , t h e z k e y s h o u l d b e

h e l d d o w n . W h e n t h e m o t o r i s r u n n i n g f o r w a r d s o r

b a c k w a r d s , t h e t r a c k s p r i t e c o l o u r c h a n g e s t o s h o w

t h a t t h e m o t o r i s r u n n i n g . T h e r i g h t t r a c k p r o g r a m i s

t h e s a m e , b u t w i t h t h e d a n d x k e y s a n d t h e m o t o r

c h a n n e l 1 0 .

D a t a a c q u i s i t i o n & l i m i t s

T o u n d e r s t a n d t h e l i m i t s o f t h e S c r a t c h d r i v e r , i t i s

h e l p f u l t o k n o w w h a t i s h a p p e n i n g w i t h i n t h e d r i v e r

a n d t h e B r i c k P i i t s e l f . T h e B r i c k P i S c r a t c h d i v e r i s

w r i t t e n i n P y t h o n a n d c o m m u n i c a t e s b e t w e e n

S c r a t c h a n d t h e B r i c k P i . I f t h e S c r a t c h d r i v e r s e n t t h e

B r i c k P i a s i n g l e m o t o r c o n t r o l l e r c o m m a n d , t h e

B r i c k P i w o u l d r u n t h e a s s o c i a t e d m o t o r f o r a b o u t o n e

s e c o n d a n d t h e n s t o p . T h i s d e s i g n c h o i c e w a s m a k e

i n t h e B r i c k i t s e l f t o p r e v e n t r u n - a w a y r o b o t s . T o

m a k e S c r a t c h p r o g r a m s m o r e e f f i c i e n t a n d s i m p l e r ,

t h e S c r a t c h d r i v e r s t a r t s a d a t a a c q u i s i t i o n l o o p w i t h

t h e B r i c k P i t h a t r u n s e v e r y t e n t h o f a s e c o n d . T h i s

l o o p s e n d s t h e c u r r e n t m o t o r v a l u e s t o t h e B r i c k P i

a n d r e t r i e v e s t h e c u r r e n t s e n s o r v a l u e s f o r t h e

Page 15: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 15/48

Page 16: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 16/48

E a c h t i m e t h e s e n s o r s a r e a u t o m a t i c a l l y u p d a t e d , a

L E G O : t r i g m e s s a g e i s s e n t t o S c r a t c h . T h e

L E G O : t r i g m e s s a g e i s u s e d t o r u n t h e t h r e e

s e c t i o n s t h a t f o l l o w , w h e r e t h e f i r s t s e c t i o n s t o r e s t h e

s e n s o r v a l u e i n t h e U S v a r i a b l e a n d t h e o t h e r t w o

s e c t i o n s c o n t r o l t h e t w o m o t o r s . W h e n t h e L E G O : 1 0

o r L E G O : 1 1 v a l u e s a r e u p d a t e d , t h e y a r e

a u t o m a t i c a l l y s e n t t o t h e B r i c k P i c h a n n e l s M A a n d M B

r e s p e c t i v e l y .

T h e l a s t s e c t i o n i s o n l y r u n w h e n t h e q k e y i s p r e s s e d

o n t h e k e y b o a r d , w h i c h s t o p s t h e a u t o m a t i c u p d a t e s .

T h e p r o g r a m t h e n w a i t s , i n c a s e o n e m o t o r c o m m a n d

i s s t i l l b e i n g p r o c e s s e d . T h e n t h e c o s t u m e i s s e t

b a c k t o n o r m a l a n d b o t h m o t o r s a r e s t o p p e d .

T r y p o i n t i n g t h e r o b o t a t a s m o o t h w a l l a n d c l i c k i n g

o n t h e g r e e n f l a g . W h e n t h e w a l l i s f a r a w a y , t h e

m o t o r s w i l l r u n a t h i g h s p e e d . T h e n t h e r o b o t w i l l

s l o w d o w n a n d f i n a l l y s t o p .

L E G O ® i s a t r a d e m a r k o f t h e L E G O G r o u p o f c o m p a n i e s

w h i c h d o e s n o t s p o n s o r , a u t h o r i z e o r e n d o r s e t h i s s i t e

Page 17: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 17/48

Page 18: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 18/488

T h e M a g P i h a s n o w r e a c h e d i t s t w o y e a r a n n i v e r s a r y . W i t h t h e

s u p p o r t o f m a n y c o n t r i b u t o r s f r o m a c r o s s t h e g l o b e w e h a v e s e e n a

w e a l t h o f i n t e r e s t i n g a r t i c l e s o n o u r f a v o u r i t e t i n y c o m p u t e r . T o

c e l e b r a t e o u r s e c o n d y e a r w e h a v e a v e r y b i g b i r t h d a y c o m p e t i t i o n

f o r y o u a l l t o e n t e r , a n d a l l y o u h a v e t o d o i s a n s w e r t h e f i r s t f i v e

m u l t i p l e c h o i c e q u e s t i o n s b e l o w . T h e l a s t t w o q u e s t i o n s a r e

o p t i o n a l , b u t w e w o u l d l o v e t o h e a r y o u r t h o u g h t s .

E n t e r a t w w w . t h e m a g p i . c o m / b i r t h d a y . A l l c o r r e c t e n t r i e s r e c e i v e d

b y J u n e 1 s t t h i s y e a r w i l l b e p u t i n t o t h e p r i z e d r a w , w i t h t h e f i r s t 1 4

d r a w n w i n n i n g a p r i z e i n t h e o r d e r l i s t e d . W i n n e r s w i l l b e n o t i f i e d b y

e m a i l a n d t h e w i n n i n g n a m e s w i l l b e p o s t e d o n T h e M a g P i w e b s i t e .

A l l a n s w e r s a r e t o b e f o u n d i n T h e M a g P i f r o m i s s u e 1 2 o n w a r d s .

F u l l t e r m s a r e a v a i l a b l e a t : w w w . t h e m a g p i . c o m / b i r t h d a y

T h e M a g P i ' s S e c o n d B i r t h d a y C o m p e t i t i o n

O v e r £ 2 , 0 0 0 w o r t h o f p r i z e s t o b e w o n !

W h i c h g a m e d i d w e b r i n g t o P y t h o n w r i t t e n b y T i m H a r t n e l l ?

a ) T h e D u k e o f D r a g o n f e a r

b ) S t r o n g h o l d o f t h e D w a r v e n L o r d s

c ) T h e B a n n o c h b u r n L e g a c y

Q u e s t i o n s

Q 1

Q 2

Q 3

Q 4

Q 5

Q 6

Q 7

H o w m a n y d e g r e e s n o r t h o f t h e e q u a t o r i s t h e i s l a n d o f C u r a c a o ?

a ) 1 2 d e g r e e s

b ) 1 4 d e g r e e s

c ) 1 8 d e g r e e s

W h i c h p i r a t e s y m b o l f e a t u r e s o n t h e c h e s t o f t h e B r i c k P i L E G O ® m a n i n T h e M a g P i ?

a ) F l i n t l o c k p i s t o l s

b ) C r o s s e d c u t l a s s e s

c ) S k u l l a n d c r o s s b o n e s

W h o w a s m e n t i o n e d a s h a v i n g f a m o u s l y f l i c k e d p a i n t o f f t h e e n d o f a p a i n t b r u s h o n t o c a n v a s ?

a ) J a c k s o n P o l l o c k

b ) A n d y W a r h o l

c ) L e o n a r d o d a V i n c i

W h o i s A c o r n ' s E l i t e c o m m a n d e r ?

a ) C o m m a n d e r D e v e r e a u x

b ) C o m m a n d e r B l u e h a i r

c ) C o m m a n d e r J a m e s o n

[ O p t i o n a l ] W h a t a r t i c l e s w o u l d y o u l i k e t o s e e i n T h e M a g P i o v e r t h e n e x t y e a r ?

[ O p i o n a l ] W h a t d o y o u u s e y o u r R a s p b e r r y P i ( s ) f o r ?

Page 19: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 19/481 9

S p o n s o r s

T h e M a g P i V o l u m e s 1 & 2 , I O P i 3 2 , A D C P i , R a s P i O B r e a k o u t P r o , B a r e C o n d u c t i v e H o u s e K i t , W o l f s o n A u d i o

C a r d , B r i c k P i S t a r t e r K i t , G r o v e P i , O u t d o o r P i C E a n d w e a t h e r s h i e l d , 1 y e a r d o m a i n n a m e a n d h o s t i n g f o r y o u r

R a s p b e r r y P i P r o j e c t , L a i k a E x p l o r e r I n v e n t o r K i t , R a s P i C o n n e c t v o u c h e r , $ 1 0 0 O p e n E l e c t r o n s v o u c h e r ,

P a n a v i s e 2 0 1 , 3 1 2 & 3 7 1 , U P i S A d v a n c e d w i t h c a s e a n d P i C o o l F a n , L E D B o r g , P i c o B o r g , X L o B o r g , T r i B o r g ,

R a s p b e r r y P i C o o k b o o k f o r P y t h o n P r o g r a m m e r s & h a r d w a r e k i t , A d a f r u i t P i T F T & P i B o w T F T c a s e , P i S u p p l y

S w i t c h , R T K M o t o r c o n t r o l l e r b o a r d , S w e e t b o x w i t h h e a t s i n k s S c o r P i & C A M l o t , R a s p b e r r y P i P r o j e c t s b o o k ,

A d v e n t u r e s i n R a s p b e r r y P i b o o k , F L I R C & r e m o t e , R a s p b e r r y P i M u g C o a s t e r .

F o r t h e c o m p l e t e l i s t v i s i t w w w . t h e m a g p i . c o m / b i r t h d a y

1 s t

2 n d

3 r d

4 t h

P r i z e s

I O P i 3 2 , A D C P i , P i T F T , R a s P i O B r e a k o u t B o a r d , B a r e C o n d u c t i v e H o u s e K i t , W o l f s o n A u d i o C a r d , P i U P S , 1

y e a r d o m a i n n a m e a n d h o s t i n g f o r y o u r R a s p b e r r y P i p r o j e c t , K a n o c o m p u t e r k i t ( e t a J u l y ) , R a s P i C o n n e c t

v o u c h e r , $ 7 5 O p e n E l e c t r o n s v o u c h e r , L E D B o r g , P i c o B o r g , P i S u p p l y S w i t c h , P l u s b e r r y P i C a s e ( w h e n

a v a i l a b l e ) , S w e e t b o x w i t h h e a t s i n k s & S c o r P i , R a s p b e r r y P i P r o j e c t s f o r D u m m i e s b o o k , W i F i a d a p t o r , P i b r e l l a .

S e r i a l P i R S 2 3 2 , R a s P i O P o r t R e f e r e n c e B o a r d , B a r e C o n d u c t i v e C a r d K i t , W o l f s o n A u d i o C a r d , P i U P S ,

R a s P i C o n n e c t v o u c h e r , $ 7 5 O p e n E l e c t r o n s v o u c h e r , L E D B o r g , P i C r u s t , P i S u p p l y S w i t c h , P l u s b e r r y P i C a s e

( w h e n a v a i l a b l e ) , S c o r P i & C A M l o t , R a s p b e r r y P i f o r D u m m i e s b o o k , T i n y b r e a d b o a r d k i t .

S e r i a l P i R S 2 3 2 , R a s P i O P o r t R e f e r e n c e B o a r d , B a r e C o n d u c t i v e C a r d K i t , P i R a s P i C o n n e c t v o u c h e r , $ 5 0 O p e n

E l e c t r o n s v o u c h e r , U P i S A d v a n c e d w i t h c a s e & P i C o o l F a n , P i C r u s t , P i S u p p l y S w i t c h , S h o r t C r u s t C a s e , B r i g h t

P i , P l u s b e r r y P i C a s e ( w h e n a v a i l a b l e ) , S w e e t b o x w i t h h e a t s i n k s , L e a r n i n g P y t h o n w i t h R a s p b e r r y P i b o o k .

Page 20: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 20/480

S K I L L L E V E L : I N T E R M E D I A T E

W . H . B e l l

M a g P i W r i t e r

I n t e r f a c i n g M i n e c r a f t w i t h

P i F a c e D i g i t a l

M i n e c r a f t i s a v e r y p o p u l a r g a m e t h a t h a s m o r e

r e c e n t l y b e e n u s e d a s a t e a c h i n g t o o l , t o

e n c o u r a g e p r o g r a m m i n g . T h e R a s p b e r r y P i

v e r s i o n o f M i n e c r a f t i s a v a i l a b l e f o r f r e e . T h e

g a m e i s p a c k a g e d w i t h a P y t h o n a p p l i c a t i o n

p r o g r a m m e r i n t e r f a c e ( A P I ) , w h i c h a l l o w s

i n t e r a c t i o n s w i t h p l a y e r s a n d b l o c k s . A n

i n t r o d u c t i o n t o t h i s A P I w a s g i v e n i n I s s u e 1 1 o f

T h e M a g P i . T h e r e a r e a l s o a d d i t i o n a l t e a c h i n g

r e s o u r c e s , s u c h a s C r a i g R i c h a r d s o n ’ s " P y t h o n

P r o g r a m m i n g f o r R a s p b e r r y P i " b o o k .

T h e i n t e r a c t i o n w i t h M i n e c r a f t v i a t h e P y t h o n A P I

i s n o t l i m i t e d t o j u s t s o f t w a r e . I n p u t / o u t p u t ( I / O )

d e v i c e s t h a t a r e c o n n e c t e d t o a R a s p b e r r y P i

c a n r e a c t t o , o r p r o d u c e e v e n t s i n M i n e c r a f t .

T h i s o p e n s u p a r a n g e o f p o s s i b i l i t i e s . F o r

e x a m p l e , t h e g a r a g e d o o r c o u l d o p e n w h e n t h e

M i n e c r a f t p l a y e r g o e s i n t o t h e g a r a g e o r t h e

d o o r b e l l c o u l d b e c o n n e c t e d t o a M i n e c r a f t c h a t

m e s s a g e , e t c . S i n c e t h e P y t h o n A P I s e n d s

c o m m a n d s t o t h e M i n e c r a f t s e r v e r p r o c e s s o v e r

a n e t w o r k , t h e R a s p b e r r y P i t h a t i s i n t e r a c t i n g

w i t h t h e M i n e c r a f t s e s s i o n c o u l d b e i n t h e g a r d e n

m o n i t o r i n g t h e w e a t h e r .

I n t h i s a r t i c l e , t h e M i n e c r a f t A P I i s u s e d w i t h t h e

P i F a c e D i g i t a l e x p a n s i o n b o a r d t o c r e a t e s o m e

t r a p s a r o u n d t h e s e l e c t e d p l a y e r .

P i F a c e D i g i t a l

T h e P i F a c e D i g i t a l e x p a n s i o n b o a r d p r o v i d e s a

s a f e w a y t o c o n n e c t d i g i t a l d e v i c e s t o t h e

R a s p b e r r y P i , v i a b u f f e r e d s c r e w t e r m i n a l s .

T h e b o a r d i n c l u d e s t w o r e l a y s t h a t a r e s u i t a b l e

f o r l o w v o l t a g e a p p l i c a t i o n s , f o u r s w i t c h e s , e i g h t

d i g i t a l i n p u t s , e i g h t o p e n c o l l e c t o r o u t p u t s a n d

e i g h t L E D i n d i c a t o r s . T h e R a s p b e r r y P i

c o m m u n i c a t e s w i t h t h e P i F a c e v i a t h e S P I b u s

o n t h e 2 6 p i n h e a d e r . T h e r e a r e P y t h o n a n d

S c r a t c h i n t e r f a c e s t h a t a r e p a c k a g e d f o r e a s y

i n s t a l l a t i o n u s i n g t h e D e b i a n p a c k a g e m a n a g e r .

M o r e i n f o r m a t i o n o n t h e P i F a c e c a n b e f o u n d a t :

h t t p : / / w w w . p i f a c e . o r g . u k / p r o d u c t s / p i f a c e _ d i g i t a l /

Page 21: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 21/482 1

P i F a c e P y t h o n i n t e r f a c e

S t a r t i n g f r o m a r e c e n t R a s p b i a n i m a g e , m a k e

s u r e t h a t t h e R a s p b i a n i n s t a l l a t i o n i s u p t o d a t e :

s u d o a p t - g e t u p d a t e

s u d o a p t - g e t u p g r a d e - y

T h e n s t a r t t h e r a s p i - c o n f i g ,

s u d o r a s p i - c o n f i g

S e l e c t t h e " A d v a n c e d O p t i o n s " a n d c h o o s e

" S P I " . T h e n s e t t h e v a l u e t o " Y e s " , s e l e c t " O k "

a n d " F i n i s h " . ( T h e P y t h o n l i b r a r y p y t h o n -

p i f a c e d i g i t a l i o i s a l r e a d y i n s t a l l e d i n t h e l a t e s t

R a s p b i a n i m a g e c o n f i g u r a t i o n . ) P i F a c e e x a m p l e

P y t h o n p r o g r a m s c a n b e f o u n d i n :

/ u s r / s h a r e / d o c / p y t h o n - p i f a c e d i g i t a l i o / e x a m p l e s /

I n s t a l l i n g M i n e c r a f t

I f M i n e c r a f t i s n o t a l r e a d y i n s t a l l e d , t h e n t y p e :

c d $ H O M E

w g e t h t t p s : / / s 3 . a m a z o n a w s . c o m / a s s e t s . m i n e c r a f t

. n e t / p i / m i n e c r a f t - p i - 0 . 1 . 1 . t a r . g z

t a r z x v f m i n e c r a f t - p i - 0 . 1 . 1 . t a r . g z

R a t h e r t h a n c o p y i n g t h e A P I f i l e s , t h e d i r e c t o r y

w h e r e t h e P y t h o n A P I i s c a n b e a d d e d t o t h e

P Y T H O N P A T H v a r i a b l e . U s e n a n o t o e d i t t h e

. b a s h r c f i l e :

n a n o ~ / . b a s h r c

G o t o t h e e n d o f t h e f i l e a n d a d d :

# F o r M i n e c r a f t

M C P I _ P Y = $ H O M E / m c p i / a p i / p y t h o n

i f [ [ - z $ P Y T H O N P A T H ] ] ; t h e n

e x p o r t P Y T H O N P A T H = $ M C P I _ P Y

e l i f [ [ $ P Y T H O N P A T H ! = * " $ M C P I _ P Y " * ] ] ; t h e n

e x p o r t P Y T H O N P A T H = " $ P Y T H O N P A T H : $ M C P I _ P Y "

f i

u n s e t M C P I _ P Y

T h e n s a v e t h e f i l e .

M i n e c r a f t t r a p s

T h e r e a r e m a n y a c t i o n s t h a t c o u l d b e t r i g g e r e d

b y h a r d w a r e i n p u t c h a n g e s . I n t h i s a r t i c l e , s o m e

d r a m a t i c e v e n t s t h a t a r e c e n t r e d o n a g i v e n

p l a y e r a r e u s e d .

C r e a t e a n e w f i l e c a l l e d M c T r a p s . p y i n t h e

p r e s e n t w o r k i n g d i r e c t o r y , a d d t h e P y t h o n g i v e n

a t t h e b o t t o m o f t h i s p a g e a n d s a v e t h e f i l e . T h e n

o p e n a s e c o n d f i l e c a l l e d m c C o n t r o l . p y a n d a d d

t h e P y t h o n f o u n d o n t h e n e x t p a g e .

i m p o r t m c p i

f r o m m c p i . b l o c k i m p o r t *

i m p o r t t i m e

d e f s a n d T r a p ( m c ) :

p o s = m c . p l a y e r . g e t T i l e P o s ( )

m c . s e t B l o c k s ( p o s . x - 1 0 , p o s . y + 1 5 , p o s . z - 1 0 , p o s . x + 1 0 , p o s . y + 1 8 , p o s . z + 1 0 , S A N D )

m c . p o s t T o C h a t ( " W e l c o m e t o t h e b e a c h ! " )

d e f v o l c a n o T r a p ( m c ) :

p o s = m c . p l a y e r . g e t T i l e P o s ( )

m c . p o s t T o C h a t ( " W a r n i n g . . v o l c a n o ! " )

t i m e . s l e e p ( 1 )

m c . s e t B l o c k s ( p o s . x , p o s . y - 5 0 , p o s . z , p o s . x , p o s . y - 1 , p o s . z , L A V A )

t i m e . s l e e p ( 1 )

m c . s e t B l o c k s ( p o s . x - 2 , p o s . y , p o s . z - 2 , p o s . x + 2 , p o s . y + 2 , p o s . z + 2 , L A V A )

m c . p o s t T o C h a t ( " A b i t t o o h o t ! " )

d e f h o l e T r a p ( m c ) :

p o s = m c . p l a y e r . g e t T i l e P o s ( )

m c . p o s t T o C h a t ( " W a t c h y o u r f e e t ! " )

t i m e . s l e e p ( 1 )

m c . s e t B l o c k s ( p o s . x - 2 , p o s . y - 4 0 , p o s . z - 2 , p o s . x + 2 , p o s . y , p o s . z + 2 , A I R )

Page 22: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 22/482

# ! / u s r / b i n / e n v p y t h o n

i m p o r t m c p i

f r o m m c p i . m i n e c r a f t i m p o r t M i n e c r a f t

i m p o r t p i f a c e d i g i t a l i o

f r o m M c T r a p s i m p o r t *

i m p o r t s y s , t i m e

c l a s s M c C o n t r o l :

d e f _ _ i n i t _ _ ( s e l f , i p s ) :

s e l f . i p s = [ ]

s e l f . i p s + = i p s

# O p e n c o n n e c t i o n s w i t h t h e M i n e c r a f t s e s s i o n s

s e l f . c o n n e c t i o n s =

f o r i p i n s e l f . i p s :

t r y :

# s e l f . c o n n e c t i o n s [ i p ] = M i n e c r a f t . c r e a t e ( i p )

s e l f . c o n n e c t i o n s [ i p ] = N o n e

e x c e p t :

p r i n t ( " E R R O R : c a n n o t c o n n e c t t o M i n e c r a f t o n % s " % i p )

s y s . e x i t ( 1 )

# S t o r e t h e n u m b e r o f c o n n e c t i o n s a n d i n i t i a l i s e t h e c u r r e n t c o n n e c t i o n t o b e t h e f i r s t o n e

s e l f . c o n n e c t i o n N u m b e r = 0

s e l f . n u m b e r O f C o n n e c t i o n = l e n ( i p s )

# S e t u p a n i n p u t e v e n t l i s t e n e r , o n e p e r s w i t c h o n t h e P i F a c e

p i f a c e d i g i t a l = p i f a c e d i g i t a l i o . P i F a c e D i g i t a l ( )

s e l f . l i s t e n e r = p i f a c e d i g i t a l i o . I n p u t E v e n t L i s t e n e r ( c h i p = p i f a c e d i g i t a l )

f o r i i n r a n g e ( 4 ) :

s e l f . l i s t e n e r . r e g i s t e r ( i , p i f a c e d i g i t a l i o . I O D I R _ O N , s e l f . s w i t c h P r e s s e d )

d e f l i s t e n ( s e l f ) :

# S t a r t l i s t e n i n g t o t h e P i F a c e

s e l f . l i s t e n e r . a c t i v a t e ( )

p r i n t ( " > > L i s t e n i n g t o P i F a c e " )

d e f s h u t d o w n ( s e l f ) :

# S t o p l i s t e n i n g t o t h e P i F a c e

s e l f . l i s t e n e r . d e a c t i v a t e ( )

p r i n t ( " > > L i s t e n e r s s h u t d o w n " )

d e f n e x t C o n n e c t i o n ( s e l f ) :

# C h a n g e t o t h e c o n n e c t i o n a s s o c i a t e d w i t h t h e n e x t I P i n t h e l i s t .

s e l f . c o n n e c t i o n N u m b e r = s e l f . c o n n e c t i o n N u m b e r + 1

i f s e l f . c o n n e c t i o n N u m b e r > = s e l f . n u m b e r O f C o n n e c t i o n :

s e l f . c o n n e c t i o n N u m b e r = 0

p r i n t ( " > > U s i n g c o n n e c t i o n t o % s " % s e l f . i p s [ s e l f . c o n n e c t i o n N u m b e r ] )

d e f g e t C o n n e c t i o n ( s e l f ) :

# R e t u r n t h e c o n n e c t i o n a s s o c i a t e d w i t h t h e s e l e c t e d I P

i f n o t s e l f . i p s [ s e l f . c o n n e c t i o n N u m b e r ] i n s e l f . c o n n e c t i o n s . k e y s ( ) :

r a i s e E x c e p t i o n ( " E r r o r : n o c o n n e c t i o n t o % s " % s e l f . i p s [ s e l f . c o n n e c t i o n N u m b e r ] )

r e t u r n s e l f . c o n n e c t i o n s [ s e l f . i p s [ s e l f . c o n n e c t i o n N u m b e r ] ]

d e f s w i t c h P r e s s e d ( s e l f , e v e n t ) :

# H a n d l e s w i t c h p r e s s e v e n t s :

# ( 0 ) - I f t h e f i r s t s w i t c h h a s b e e n p r e s s e d , c h a n g e t o t h e n e x t I P

i f e v e n t . p i n _ n u m = = 0 :

s e l f . n e x t C o n n e c t i o n ( )

r e t u r n N o n e

# G e t t h e c u r r e n t M i n e c r a f t c o n n e c t i o n

m c = s e l f . g e t C o n n e c t i o n ( )

Page 23: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 23/482 3

S e t t h e m c C o n t r o l . p y f i l e a s e x e c u t a b l e ,

c h m o d 7 5 5 m c C o n t r o l . p y

T h e n u s e a n e w t e r m i n a l w i n d o w t o s t a r t

M i n e c r a f t , u s i n g t h e l o c a l R a s p b e r r y P i :

c d m c p i

. / m i n e c r a f t - p i

O n c e M i n e c r a f t i s r u n n i n g a n d a w o r l d h a s b e e n

c r e a t e d . C l i c k o n t h e t a b k e y , t o b r e a k t h e

w i n d o w f o c u s . T h e n s e l e c t t h e o r i g i n a l t e r m i n a l

w i n d o w a n d t y p e :

. / m c C o n t r o l . p y

B y d e f a u l t , t h e p r o g r a m u s e s t h e l o c a l h o s t . T o

a c c e s s o n e o r m o r e r e m o t e M i n e c r a f t g a m e s ,

t y p e :

. / m c C o n t r o l . p y 1 9 2 . 1 6 8 . 1 . 2 0 1 9 2 . 1 6 8 . 1 . 2 1

e t c . , w h e r e t h e I P a d d r e s s e s a r e t h e I P

a d d r e s s e s o f t h e R a s p b e r r y P i s r u n n i n g

M i n e c r a f t . T h e I P a d d r e s s o n a R a s p b e r r y P i

c a n b e f o u n d b y t y p i n g

i f c o n f i g

i n a t e r m i n a l w i n d o w .

O n c e t h e m c C o n t r o l . p y p r o g r a m i s r u n n i n g ,

p r e s s i n g s w i t c h 0 o n t h e P i F a c e w i l l c a u s e t h e

p r o g r a m t o c h a n g e t h e c o n n e c t i o n u s e d f o r t h e

t r i c k s . T h i s i s u s e f u l , w h e n m o r e t h a n o n e

M i n e c r a f t s e s s i o n i s i n t h e c o n n e c t i o n d i c t i o n a r y .

B u t t o n s 1 , 2 a n d 3 , r u n t h e s a n d T r a p ,

v o l c a n o T r a p a n d h o l e T r a p f u n c t i o n s

r e s p e c t i v e l y .

T o p r e v e n t a l o t o f C P U b e i n g u s e d , t h e

M c C o n t r o l c l a s s c r e a t e s a I n p u t E v e n t L i s t e n e r

o b j e c t . T h i s l i s t e n e r i s t h e n u s e d t o a s s o c i a t e t h e

s w i t c h P r e s s e d f u n c t i o n w i t h o n e o f t h e f o u r

s w i t c h e s b e i n g p r e s s e d . O n c e t h e

l i s t e n e r . a c t i v a t e ( ) f u n c t i o n h a s b e e n c a l l e d , t h e

p r o g r a m c o n t i n u e s t o l i s t e n f o r P i F a c e s w i t c h

c h a n g e s u n t i l i t i s c l o s e d w i t h C T R L - C .

# ( 1 - 3 ) - U s e t h e s w i t c h n u m b e r t o d e c i d e w h i c h t r a p t o r u n

i f e v e n t . p i n _ n u m = = 1 :

p r i n t ( " > > S a n d t r a p " )

s a n d T r a p ( m c )

e l i f e v e n t . p i n _ n u m = = 2 :

p r i n t ( " > > V o l c a n o t r a p " )

v o l c a n o T r a p ( m c )

e l i f e v e n t . p i n _ n u m = = 3 :

p r i n t ( " > > H o l e t r a p " )

h o l e T r a p ( m c )

e l s e :

r a i s e E x c e p t i o n ( " E R R O R : p i n n u m b e r i s o u t o f r a n g e . " )

i f _ _ n a m e _ _ = = " _ _ m a i n _ _ " :

# I f n o c o m m a n d l i n e o p t i o n s a r e p r o v i d e , a s s u m e t h a t t h e l o c a l h o s t i s r u n n i n g M i n e c r a f t

i p s = [ ]

i f l e n ( s y s . a r g v ) = = 1 :

i p s + = [ " l o c a l h o s t " ]

e l s e :

i p s + = s y s . a r g v [ 1 : ]

# S t a r t t h e M i n e C r a f t c o n n e c t i o n s a n d P i F a c e l i s t e n e r s .

# T h e l i s t e n e r s a r e s h u t d o w n w e n t t h e p r o g r a m e x i t s .

m c C o n t r o l = M c C o n t r o l ( i p s )

t r y :

m c C o n t r o l . l i s t e n ( )

w h i l e ( 1 ) :

t i m e . s l e e p ( 1 0 0 0 )

e x c e p t K e y b o a r d I n t e r r u p t :

m c C o n t r o l . s h u t d o w n ( )

Page 24: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 24/48

Page 25: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 25/48

Page 26: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 26/480 6

S K I L L L E V E L : B E G I N N E R

J a c o b M a r s h

M o d M y P i

P H Y S I C A L C O M P U T I N G

B r o u g h t t o y o u b y M o d M y P i

G P I O S e n s i n g : U s i n g 1 - W i r e

t e m p e r a t u r e s e n s o r s - P a r t 2

1 - W i r e s e n s o r s

I n p r e v i o u s t u t o r i a l s w e ’ v e o u t l i n e d t h e

i n t e g r a t i o n o f s i m p l e s e n s o r s a n d s w i t c h e s w i t h

t h e R a s p b e r r y P i . T h e s e c o m p o n e n t s h a v e h a d a

s i m p l e o n / o f f o r h i g h / l o w o u t p u t , w h i c h i s s e n s e d

b y t h e R a s p b e r r y P i . O u r P I R m o v e m e n t s e n s o r

t u t o r i a l i n I s s u e 2 1 , f o r e x a m p l e , s i m p l y s a y s

“ Y e s , I ’ v e d e t e c t e d m o v e m e n t ” .

S o , w h a t h a p p e n s w h e n w e c o n n e c t a m o r e

a d v a n c e d s e n s o r a n d w a n t t o r e a d m o r e

c o m p l e x d a t a ? I n t h i s t u t o r i a l w e w i l l c o n n e c t a 1 -

W i r e d i g i t a l t h e r m o m e t e r s e n s o r a n d p r o g r a m m e

o u r R a s p b e r r y P i t o r e a d t h e o u t p u t o f t h e

t e m p e r a t u r e i t s e n s e s !

I n 1 - W i r e s e n s o r s a l l d a t a i s s e n t d o w n o n e w i r e ,

w h i c h m a k e s i t g r e a t f o r m i c r o c o n t r o l l e r s a n d

c o m p u t e r s , s u c h a s t h e R a s p b e r r y P i , a s i t o n l y

r e q u i r e s o n e G P I O p i n f o r s e n s i n g . I n a d d i t i o n t o

t h i s , m o s t 1 - W i r e s e n s o r s w i l l c o m e w i t h a u n i q u e

s e r i a l c o d e ( m o r e o n t h i s l a t e r ) w h i c h m e a n s y o u

c a n c o n n e c t m u l t i p l e u n i t s w i t h o u t t h e m

i n t e r f e r i n g w i t h e a c h o t h e r .

T h e s e n s o r w e ’ r e g o i n g t o u s e i n t h i s t u t o r i a l i s

t h e M a x i m D S 1 8 B 2 0 + P r o g r a m m a b l e R e s o l u t i o n

1 - W i r e D i g i t a l T h e r m o m e t e r .

T h e D S 1 8 B 2 0 + h a s a

s i m i l a r l a y o u t t o t r a n s i s t o r s ,

c a l l e d t h e T O - 9 2 p a c k a g e ,

w i t h t h r e e p i n s : G N D , D a t a

( D Q ) a n d 3 . 3 V p o w e r l i n e

( V

D D

) . Y o u a l s o n e e d s o m e

j u m p e r w i r e s , a b r e a d b o a r d

a n d a 4 . 7 k Ω ( o r 1 0 k Ω )

r e s i s t o r .

T h e r e s i s t o r i n t h i s s e t u p i s u s e d a s a ' p u l l - u p ' f o r

t h e d a t a - l i n e , a n d s h o u l d b e c o n n e c t e d b e t w e e n

t h e D Q a n d V

D D

l i n e . I t e n s u r e s t h a t t h e 1 - W i r e

d a t a l i n e i s a t a d e f i n e d l o g i c l e v e l a n d l i m i t s

i n t e r f e r e n c e f r o m e l e c t r i c a l n o i s e i f o u r p i n w a s

l e f t f l o a t i n g . W e a r e a l s o g o i n g t o u s e G P I O 4

[ P i n 7 ] a s t h e d r i v e r p i n f o r s e n s i n g t h e

t h e r m o m e t e r o u t p u t . T h i s i s t h e d e d i c a t e d p i n f o r

1 - W i r e G P I O s e n s i n g .

H o o k i n g i t u p

1 . C o n n e c t G P I O G N D [ P i n 6 ] o n t h e R a s p b e r r y

P i t o t h e n e g a t i v e r a i l o n t h e b r e a d b o a r d .

2 . C o n n e c t G P I O 3 . 3 V [ P i n 1 ] o n t h e R a s p b e r r y

P i t o t h e p o s i t i v e r a i l o n t h e b r e a d b o a r d .

3 . P l u g t h e D S 1 8 B 2 0 + i n t o y o u r b r e a d b o a r d ,

Page 27: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 27/482 7

e n s u r i n g t h a t a l l t h r e e p i n s a r e i n d i f f e r e n t r o w s .

F a m i l i a r i s e y o u r s e l f w i t h t h e p i n l a y o u t , a s i t i s

q u i t e e a s y t o h o o k i t u p b a c k w a r d s !

4 . C o n n e c t D S 1 8 B 2 0 + G N D [ P i n 1 ] t o t h e

n e g a t i v e r a i l o f t h e b r e a d b o a r d .

5 . C o n n e c t D S 1 8 B 2 0 + V

D D

[ P i n 3 ] t o t h e p o s i t i v e

r a i l o f t h e b r e a d b o a r d .

6 . P l a c e y o u r 4 . 7 k Ω r e s i s t o r b e t w e e n D S 1 8 B 2 0 +

D Q [ P i n 2 ] a n d a f r e e r o w o n y o u r b r e a d b o a r d .

7 . C o n n e c t t h a t f r e e e n d o f t h e 4 . 7 k Ω r e s i s t o r t o

t h e p o s i t i v e r a i l o f t h e b r e a d b o a r d .

8 . F i n a l l y , c o n n e c t D S 1 8 B 2 0 + D Q [ P i n 2 ] t o

G P I O 4 [ P i n 7 ] w i t h a j u m p e r w i r e .

T h a t ’ s i t ! W e a r e n o w r e a d y f o r s o m e

p r o g r a m m i n g !

P r o g r a m m i n g

W i t h a l i t t l e s e t u p , t h e D S 1 8 B 2 0 + c a n b e r e a d

d i r e c t l y f r o m t h e c o m m a n d l i n e w i t h o u t t h e n e e d

f o r a n y P y t h o n p r o g r a m m i n g . H o w e v e r , t h i s

r e q u i r e s u s t o i n p u t a c o m m a n d e v e r y t i m e w e

w a n t t o k n o w t h e t e m p e r a t u r e r e a d i n g . I n o r d e r

t o i n t r o d u c e s o m e c o n c e p t s f o r 1 - W i r e

i n t e r f a c i n g , w e w i l l a c c e s s i t v i a t h e c o m m a n d

l i n e f i r s t a n d t h e n w e w i l l w r i t e a P y t h o n p r o g r a m

w h i c h w i l l r e a d t h e t e m p e r a t u r e a u t o m a t i c a l l y a t

s e t t i m e i n t e r v a l s .

T h e R a s p b e r r y P i c o m e s e q u i p p e d w i t h a r a n g e

o f d r i v e r s f o r i n t e r f a c i n g . H o w e v e r , i t ’ s n o t

f e a s i b l e t o l o a d e v e r y d r i v e r w h e n t h e s y s t e m

b o o t s , a s i t i n c r e a s e s t h e b o o t t i m e s i g n i f i c a n t l y

a n d u s e s a c o n s i d e r a b l e a m o u n t o f s y s t e m

r e s o u r c e s f o r r e d u n d a n t p r o c e s s e s . T h e s e

d r i v e r s a r e t h e r e f o r e s t o r e d a s l o a d a b l e m o d u l e s

a n d t h e c o m m a n d m o d p r o b e

i s e m p l o y e d t o b o o t

t h e m i n t o t h e L i n u x k e r n e l w h e n t h e y ’ r e r e q u i r e d .

T h e f o l l o w i n g t w o c o m m a n d s l o a d t h e 1 - W i r e

a n d t h e r m o m e t e r d r i v e r s o n G P I O 4 . A t t h e

c o m m a n d l i n e e n t e r :

s u d o m o d p r o b e w 1 - g p i o

s u d o m o d p r o b e w 1 - t h e r m

W e t h e n n e e d t o c h a n g e d i r e c t o r y t o o u r 1 - W i r e

d e v i c e f o l d e r a n d l i s t t h e d e v i c e s i n o r d e r t o

e n s u r e t h a t o u r t h e r m o m e t e r h a s l o a d e d

c o r r e c t l y . E n t e r :

c d / s y s / b u s / w 1 / d e v i c e s

l s

I n t h e d e v i c e l i s t , y o u r s e n s o r s h o u l d b e l i s t e d a s

a s e r i e s o f n u m b e r s a n d l e t t e r s . I n m y c a s e , t h e

d e v i c e i s r e g i s t e r e d a s 2 8 - 0 0 0 0 0 5 e 2 f d c 3 . Y o u

t h e n n e e d t o a c c e s s t h e s e n s o r w i t h t h e c d

c o m m a n d , r e p l a c i n g t h e s e r i a l n u m b e r w i t h t h a t

f r o m y o u r o w n s e n s o r . E n t e r :

c d 2 8 - 0 0 0 0 0 5 e 2 f d c 3

T h e s e n s o r p e r i o d i c a l l y w r i t e s t o t h e w 1 _ s l a v e

f i l e . W e c a n u s e t h e c a t

c o m m a n d t o r e a d i t :

Page 28: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 28/488

c a t w 1 _ s l a v e

T h i s y i e l d s t h e f o l l o w i n g t w o l i n e s o f t e x t , w i t h t h e

o u t p u t t

s h o w i n g t h e t e m p e r a t u r e i n m i l l i -

d e g r e e s C e l s i u s . D i v i d e t h i s n u m b e r b y 1 0 0 0 t o

g e t t h e t e m p e r a t u r e i n d e g r e e s , e . g . t h e

t e m p e r a t u r e r e a d i n g w e ’ v e r e c e i v e d i s 2 3 . 1 2 5

d e g r e e s C e l s i u s .

7 2 0 1 4 b 4 6 7 f f f 0 e 1 0 5 7 : c r c = 5 7 Y E S

7 2 0 1 4 b 4 6 7 f f f 0 e 1 0 5 7 t = 2 3 1 2 5

I n t e r m s o f r e a d i n g f r o m t h e s e n s o r , t h i s i s a l l

t h a t ’ s r e q u i r e d f r o m t h e c o m m a n d l i n e . T r y

h o l d i n g o n t o t h e t h e r m o m e t e r f o r a f e w s e c o n d s

a n d t h e n t a k e a n o t h e r r e a d i n g . S p o t t h e

i n c r e a s e ? W i t h t h e s e c o m m a n d s i n m i n d , w e c a n

n o w w r i t e a P y t h o n p r o g r a m t o o u t p u t o u r

t e m p e r a t u r e d a t a a u t o m a t i c a l l y .

P y t h o n p r o g r a m

O u r f i r s t s t e p i s t o i m p o r t t h e r e q u i r e d m o d u l e s .

T h e o s m o d u l e a l l o w s u s t o e n a b l e o u r 1 - W i r e

d r i v e r s a n d i n t e r f a c e w i t h t h e s e n s o r . T h e t i m e

m o d u l e a l l o w s t h e R a s p b e r r y P i t o d e f i n e t i m e ,

a n d e n a b l e s t h e u s e o f t i m e p e r i o d s i n o u r c o d e .

i m p o r t o s

i m p o r t t i m e

W e t h e n n e e d t o l o a d o u r d r i v e r s :

o s . s y s t e m ( ' m o d p r o b e w 1 - g p i o ' )

o s . s y s t e m ( ' m o d p r o b e w 1 - t h e r m ' )

T h e n e x t s t e p i s t o d e f i n e t h e s e n s o r ’ s o u t p u t f i l e

( t h e w 1 _ s l a v e

f i l e ) a s d e f i n e d a b o v e . R e m e m b e r

t o u t i l i s e y o u r o w n t e m p e r a t u r e s e n s o r ’ s s e r i a l

c o d e !

t e m p _ s e n s o r = ' / s y s / b u s / w 1 / d e v i c e s / 2 8 - 0 0 0 0 0 5 e 2

f d c 3 / w 1 _ s l a v e '

W e t h e n n e e d t o d e f i n e a v a r i a b l e f o r o u r r a w

t e m p e r a t u r e v a l u e , t e m p _ r a w

; t h e t w o l i n e s

o u t p u t b y t h e s e n s o r , a s d e m o n s t r a t e d b y t h e

c o m m a n d l i n e e x a m p l e . W e c o u l d s i m p l y p r i n t

t h i s s t a t e m e n t n o w , h o w e v e r w e a r e g o i n g t o

p r o c e s s i t i n t o s o m e t h i n g m o r e u s a b l e . T o d o t h i s

w e o p e n , r e a d , r e c o r d a n d t h e n c l o s e t h e

t e m p _ s e n s o r f i l e . W e u s e t h e

r e t u r n f u n c t i o n

h e r e , i n o r d e r t o r e c a l l t h i s d a t a a t a l a t e r s t a g e i n

o u r c o d e .

d e f t e m p _ r a w ( ) :

f = o p e n ( t e m p _ s e n s o r , ' r ' )

l i n e s = f . r e a d l i n e s ( )

f . c l o s e ( )

r e t u r n l i n e s

F i r s t , w e c h e c k o u r v a r i a b l e f r o m t h e p r e v i o u s

f u n c t i o n f o r a n y e r r o r s . I f y o u s t u d y o u r o r i g i n a l

o u t p u t , a s s h o w n i n t h e c o m m a n d l i n e e x a m p l e ,

w e g e t t w o l i n e s o f o u t p u t c o d e . T h e f i r s t l i n e w a s

" 7 2 0 1 4 b 4 6 7 f f f 0 e 1 0 5 7 : c r c = 5 7 Y E S " . W e

s t r i p t h i s l i n e , e x c e p t f o r t h e l a s t t h r e e c h a r a c t e r s ,

a n d c h e c k f o r t h e “ Y E S ” s i g n a l , w h i c h i n d i c a t e s

a s u c c e s s f u l t e m p e r a t u r e r e a d i n g f r o m t h e

s e n s o r . I n P y t h o n , n o t - e q u a l i s d e f i n e d a s “ ! = ” ,

s o h e r e w e a r e s a y i n g t h a t w h i l e t h e r e a d i n g

d o e s n o t e q u a l " Y E S " , s l e e p f o r 0 . 2 s a n d r e p e a t .

d e f r e a d _ t e m p ( ) :

l i n e s = t e m p _ r a w ( )

w h i l e l i n e s [ 0 ] . s t r i p ( ) [ - 3 : ] ! = ' Y E S ' :

t i m e . s l e e p ( 0 . 2 )

l i n e s = t e m p _ r a w ( )

O n c e a Y E S s i g n a l h a s b e e n r e c e i v e d , w e

p r o c e e d t o o u r s e c o n d l i n e o f o u t p u t c o d e . I n o u r

e x a m p l e t h i s w a s " 7 2 0 1 4 b 4 6 7 f f f 0 e 1 0 5 7

t = 2 3 1 2 5 " . W e f i n d o u r t e m p e r a t u r e o u t p u t “ t = ” ,

c h e c k i t f o r e r r o r s a n d s t r i p t h e o u t p u t o f t h e “ t = ”

p h r a s e t o l e a v e j u s t t h e t e m p e r a t u r e d a t a . F i n a l l y

w e r u n t w o c a l c u l a t i o n s t o g i v e u s t h e

t e m p e r a t u r e i n C e l s i u s a n d F a h r e n h e i t .

t e m p _ o u t p u t = l i n e s [ 1 ] . f i n d ( ' t = ' )

i f t e m p _ o u t p u t ! = - 1 :

t e m p _ s t r i n g = l i n e s [ 1 ] . s t r i p ( ) [ t e m p _ o u t p u t

+ 2 : ]

t e m p _ c = f l o a t ( t e m p _ s t r i n g ) / 1 0 0 0 . 0

t e m p _ f = t e m p _ c * 9 . 0 / 5 . 0 + 3 2 . 0

r e t u r n t e m p _ c , t e m p _ f

Page 29: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 29/482 9

F i n a l l y , w e l o o p o u r p r o c e s s a n d t e l l i t t o o u t p u t

o u r t e m p e r a t u r e d a t a e v e r y 1 s e c o n d .

w h i l e T r u e :

p r i n t ( r e a d _ t e m p ( ) )

t i m e . s l e e p ( 1 )

T h a t ’ s o u r c o d e ! A s c r e e n s h o t o f t h e c o m p l e t e

p r o g r a m i s s h o w n b e l o w . S a v e y o u r p r o g r a m a n d

r u n i t t o d i s p l a y t h e t e m p e r a t u r e o u t p u t , a s

s h o w n o n t h e r i g h t .

M u l t i p l e s e n s o r s

D S 1 8 B 2 0 + s e n s o r s c a n b e c o n n e c t e d i n p a r a l l e l

a n d a c c e s s e d u s i n g t h e i r u n i q u e s e r i a l n u m b e r .

O u r P y t h o n e x a m p l e c a n b e e d i t e d t o a c c e s s a n d

r e a d f r o m m u l t i p l e s e n s o r s !

A s a l w a y s , t h e D S 1 8 B 2 0 + s e n s o r a n d a l l

c o m p o n e n t s a r e a v a i l a b l e s e p a r a t e l y o r a s p a r t

o f o u r w o r k s h o p k i t f r o m t h e M o d M y P i w e b s i t e

h t t p : / / w w w . m o d m y p i . c o m .

T h i s a r t i c l e i s

s p o n s o r e d b y

M o d M y P i

A l l b r e a k o u t b o a r d s a n d a c c e s s o r i e s u s e d i n t h i s

t u t o r i a l a r e a v a i l a b l e f o r w o r l d w i d e s h i p p i n g f r o m

t h e M o d M y P i w e b s h o p a t w w w . m o d m y p i . c o m

Page 30: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 30/480

S K I L L L E V E L : A D V A N C E D

W . H . B e l l

M a g P i W r i t e r

5 - C l a s s e s

W e l c o m e b a c k t o t h e C + + C a c h e . T h e s u b j e c t o f t h i s m o n t h ' s a r t i c l e i s t h e i n t r o d u c t i o n o f C + + c l a s s e s .

B e f o r e c o n t i n u i n g , i t m a y b e h e l p f u l t o r e a d p r e v i o u s C a r t i c l e s i n I s s u e s 3 , 4 , 5 , 6 , 9 , 1 3 a n d 1 7 , a n d

p r e v i o u s C + + a r t i c l e s i n I s s u e s 7 , 8 , 1 0 a n d 1 8 .

O b j e c t - o r i e n t a t e d p r o g r a m m i n g

W h e n p r o g r a m s b e c o m e v e r y l a r g e a n d t h e r e a r e m a n y d a t a s t r u c t u r e s a s s o c i a t e d w i t h m a n y d i f f e r e n t p i e c e s o f

a p r o g r a m , t h e n i t c a n b e h e l p f u l t o a s s o c i a t e p a r t i c u l a r f u n c t i o n s w i t h p a r t i c u l a r d a t a s t r u c t u r e s . I n o b j e c t -

o r i e n t a t e d p r o g r a m m i n g , t h e b a s i c b u i l d i n g b l o c k o f a p r o g r a m i s a n o b j e c t . A n o b j e c t c a n i n c l u d e f u n c t i o n s a n d

d a t a m e m b e r s . O b j e c t - o r i e n t a t e d p r o g r a m m i n g r e p r e s e n t s a d i f f e r e n t s t y l e o f p r o g r a m m i n g , w h e r e c o n c e p t s a r e

g r o u p e d t o g e t h e r t o m a t c h p u r p o s e s . F o r e x a m p l e , a c a r o b j e c t c o u l d c o n t a i n s o m e v a r i a b l e s s u c h a s t h e

n u m b e r o f s e a t s o r t h e p e t r o l l e f t i n t h e t a n k . T h e c a r o b j e c t c o u l d a l s o c o n t a i n a G P S f u n c t i o n t h a t w o u l d r e t u r n

t h e p o s i t i o n o f t h e c a r i n s p a c e .

E a c h o b j e c t i s i n s t a n t i a t e d i n a s i m i l a r m a n n e r a s a s i m p l e v a r i a b l e . I n s t e a d o f a s i m p l e t y p e , a n o b j e c t i s

i n s t a n t i a t e d w i t h a c l a s s . I n t h e c a s e o f a s i m p l e v a r i a b l e ,

i n t i ;

t h e t y p e i s i n t a n d t h e v a r i a b l e i s i . A n o b j e c t c a n b e i n s t a n t i a t e d u s i n g t h e S q u a r e c l a s s i n a s i m i l a r w a y ,

S q u a r e s ;

J u s t a s t w o i n t v a r i a b l e s d o n o t s h a r e t h e s a m e m e m o r y l o c a t i o n b y d e f a u l t , t w o o b j e c t s a l s o d o n o t s h a r e t h e

s a m e m e m o r y l o c a t i o n b y d e f a u l t . T h i s m e a n s t h a t i f a v a l u e i n s i d e a n o b j e c t i s c h a n g e d , i t w i l l n o t n o r m a l l y

a f f e c t t h e v a l u e s w i t h i n a n o t h e r o b j e c t o f t h e s a m e c l a s s . W h e n a n o b j e c t i s i n s t a n t i a t e d , t h e a s s o c i a t e d

c o n s t r u c t o r f u n c t i o n d e s c r i b e d i n t h e c l a s s d e c l a r a t i o n i s c a l l e d . T h e c o n s t r u c t o r f u n c t i o n i s t y p i c a l l y u s e d t o

i n i t i a l i s e d a t a m e m b e r s t h a t b e l o n g t o t h e c l a s s .

Page 31: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 31/483 1

A f i r s t C + + c l a s s

O p e n a t e r m i n a l w i n d o w . T h e n u s e a t e x t e d i t o r , s u c h a s n a n o o r e m a c s , t o c r e a t e a f i l e c a l l e d s q u a r e . h . C o p y

t h e c o d e b e l o w i n t o s q u a r e . h a n d s a v e t h e f i l e .

T h e s q u a r e . h h e a d e r f i l e c o n t a i n s t h e c l a s s d e c l a r a t i o n , w h e r e n a m e o f t h e c l a s s i s S q u a r e . T h e S q u a r e

c l a s s c o n t a i n s t w o c o n s t r u c t o r f u n c t i o n s , t w o o t h e r m e m b e r f u n c t i o n s , a n d t w o d a t a m e m b e r s . T h e n a m e o f t h e

c o n s t r u c t o r f u n c t i o n m u s t b e t h e s a m e a s t h e n a m e o f t h e c l a s s . T h e p r o g r a m u s e s t h e f i r s t c o n s t r u c t o r i f n o

a r g u m e n t s a r e g i v e n a n d t h e s e c o n d c o n s t r u c t o r i f t w o v a l u e s a r e p r o v i d e d . I f t h e o b j e c t i n s t a n t i a t i o n d o e s n o t

m a t c h e i t h e r o f t h e s e c o n s t r u c t o r s , t h e n t h e c o m p i l e r w i l l r e p o r t a n e r r o r .

H e a d e d f i l e s s u p p o r t l i m i t e d f u n c t i o n a l i t y . F o r e x a m p l e , i t i s p o s s i b l e t o r e t u r n a v a l u e f r o m f u n c t i o n t h a t i s

d e c l a r e d i n a h e a d e r f i l e . I n t h i s c a s e , t h e c o l o u r ( ) f u n c t i o n i s d e c l a r e d a n d i m p l e m e n t e d i n t h e h e a d e r f i l e . I n

c o n t r a s t , t h e a r e a ( ) f u n c t i o n i s d e c l a r e d i n t h e h e a d e r f i l e , b u t i s n o t i m p l e m e n t e d i n t h e h e a d e r f i l e . I n t h i s

e x a m p l e , t h e i m p l e m e n t a t i o n o f t h e c o n s t r u c t o r s a n d t h e a r e a ( ) f u n c t i o n i s g i v e n i n a . c p p s o u r c e f i l e .

W i t h i n a c l a s s d e c l a r a t i o n , f u n c t i o n s a n d d a t a m e m b e r s c a n b e p u b l i c , p r o t e c t e d o r p r i v a t e . p u b l i c

m e m b e r s c a n b e a c c e s s e d f r o m o u t s i d e t h e c l a s s , p r o t e c t e d

m e m b e r s c a n o n l y b e a c c e s s e d b y o b j e c t s o f t h e

s a m e c l a s s o r o f a d e r i v e d c l a s s a n d p r i v a t e m e m b e r s c a n o n l y b e a c c e s s e d b y o b j e c t s o f t h e s a m e c l a s s .

( T h e u s e f u l n e s s o f p r o t e c t e d m e m b e r s b e c o m e s c l e a r o n c e i n h e r i t a n c e h a s b e e n i n t r o d u c e d . ) I n t h e c a s e o f

S q u a r e , a l l o f t h e f u n c t i o n s a r e p u b l i c a n d b o t h d a t a m e m b e r s a r e p r i v a t e .

T h e i m p l e m e n t a t i o n o f a c l a s s i s g i v e n i n a . c p p ( o r . c c o r . c x x o r . C ) f i l e . C r e a t e a f i l e c a l l e d S q u a r e . c p p .

T h e n a d d t h e c o d e f o u n d a t t h e t o p o f t h e n e x t p a g e a n d s a v e t h e f i l e .

S i n c e t h e S q u a r e . c p p f i l e i n c l u d e s t h e i m p l e m e n t a t i o n o f f u n c t i o n s t h a t w e r e d e f i n e d i n t h e c l a s s d e c l a r a t i o n , i t

h a s t o i n c l u d e t h e c l a s s d e c l a r a t i o n i t s e l f . T h i s i s i n c l u d e d b y i n c l u d i n g t h e h e a d e r f i l e S q u a r e . h . T h e c m a t h

h e a d e r f i l e i s n e e d e d t o u s e t h e p o w f u n c t i o n , w h i c h i s u s e d t o s q u a r e t h e s i d e v a l u e .

B e l o w t h e i n c l u d e s t a t e m e n t s , t h e S q u a r e . c p p f i l e c o n t a i n s t h e i m p l e m e n t a t i o n o f t h e t h r e e f u n c t i o n s t h a t w e r e

d e f i n e d i n t h e h e a d e r f i l e b u t w e r e n o t i m p l e m e n t e d i n t h e h e a d e r f i l e . E a c h o f t h e f u n c t i o n n a m e s i s p r e f i x e d b y

t h e c l a s s n a m e a n d t w o c o l o n s . T h e c o n s t r u c t o r f u n c t i o n s d o n o t h a v e a d e c l a r e d r e t u r n t y p e , s i n c e t h e y r e t u r n

# i f n d e f S Q U A R E _ H

# d e f i n e S Q U A R E _ H

c l a s s S q u a r e

p u b l i c :

S q u a r e ( v o i d ) ; / / D e f a u l t c o n s t r u c t o r

S q u a r e ( d o u b l e s i d e , c h a r c o l o u r ) ; / / C o n s t r u c t o r w i t h a r g u m e n t s

d o u b l e a r e a ( ) ; / / R e t u r n t h e a r e a o f a s q u a r e

d o u b l e c o l o u r ( ) r e t u r n m _ c o l o u r ; / / T h e c o l o u r c h a r a c t e r

p r i v a t e :

d o u b l e m _ s i d e ; / / T h e l e n g t h o f a s i d e

c h a r m _ c o l o u r ; / / T h e c o l o u r c h a r a c t e r

;

# e n d i f

Page 32: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 32/482

a n o b j e c t o f t h e g i v e n c l a s s . F o r o t h e r f u n c t i o n s , t h e r e t u r n t y p e m u s t b e g i v e n t o t h e l e f t o f t h e c l a s s n a m e . I n

t h i s e x a m p l e , t h e t y p e d o u b l e i s g i v e n b e f o r e S q u a r e : : a r e a ( ) . I n t h e c o n s t r u c t o r s , v a l u e s c a n b e a s s i g n e d

t o d a t a m e m b e r s u s i n g t h e p a r e n t h e s e s n o t a t i o n g i v e n o r w i t h t h e a s s i g n m e n t o p e r a t o r ( = ) . T h e d a t a m e m b e r s

a c t a s g l o b a l v a r i a b l e s w i t h i n t h e f u n c t i o n s o f t h e c l a s s . S i n c e t h e y a r e b o t h p r i v a t e d a t a m e m b e r s , t h e y a r e

c o m m o n l y p r e f i x e d w i t h m _ . T h i s i s n o t a r e q u i r e m e n t , b u t a c o n v e n t i o n t h a t m a k e s r e a d i n g C + + c o d e a l i t t l e

s i m p l e r .

T h e l a s t p a r t o f t h i s i n t r o d u c t i o n i s t h e u s e o f t h e c l a s s S q u a r e . C r e a t e a n e w f i l e c a l l e d m a i n . c p p . T h e n a d d

t h e c o d e b e l o w a n d s a v e t h e f i l e .

T o u s e t h e c l a s s , t h e m a i n . c p p f i l e i n c l u d e s t h e h e a d e r f i l e S q u a r e . h . T w o o b j e c t s a r e i n s t a n t i a t e d , w h e r e t h e

f i r s t i n s t a n t i a t i o n c a l l s t h e d e f a u l t c o n s t r u c t o r a n d t h e s e c o n d i n s t a n t i a t i o n c a l l s t h e s e c o n d c o n s t r u c t o r . T h e n t h e

t w o f u n c t i o n s a r e a ( ) a n d c o l o u r ( ) a r e c a l l e d a n d t h e v a l u e s o f t h e a r e a a n d t h e c o l o u r ( i n t e g e r v a l u e ) i s

p r i n t e d o n t h e s c r e e n . W h e n c a l l i n g a f u n c t i o n o f a g i v e n o b j e c t , t h e f u n c t i o n n a m e i s p r e f i x e d w i t h t h e o b j e c t

n a m e . I n t h i s e x a m p l e , t h e o b j e c t s a r e c r e a t e d o n t h e s t a c k a n d g o o u t o f s c o p e i n t h e s a m e w a y a s a s i m p l e

v a r i a b l e . T h i s m e a n s t h a t t h e t w o o b j e c t s a r e d e s t r o y e d w h e n t h e m a i n ( ) f u n c t i o n f i n i s h e s .

R a t h e r t h a n t y p e g + + s e v e r a l t i m e s , a M a k e f i l e c a n b e u s e d t o c o m p i l e t h e t w o . c p p f i l e s a n d p r o d u c e a n

e x e c u t a b l e . C r e a t e a n e w f i l e c a l l e d M a k e f i l e , a d d t h e c o d e a t t h e t o p o f t h e n e x t p a g e a n d s a v e t h e f i l e . T h e

i n d e n t e d l i n e s s h o u l d b e i n d e n t e d b y a s i n g l e t a b , r a t h e r t h a n s p a c e s . ( M o r e i n f o r m a t i o n o n M a k e f i l e s i s p r o v i d e d

# i n c l u d e " S q u a r e . h "

# i n c l u d e < c m a t h >

S q u a r e : : S q u a r e ( v o i d ) :

m _ s i d e ( 0 . ) ,

m _ c o l o u r ( ' 0 ' )

S q u a r e : : S q u a r e ( d o u b l e s i d e , c h a r c o l o u r ) :

m _ s i d e ( s i d e ) ,

m _ c o l o u r ( c o l o u r )

d o u b l e S q u a r e : : a r e a ( )

r e t u r n s t d : : p o w ( m _ s i d e , 2 ) ;

# i n c l u d e < i o s t r e a m >

# i n c l u d e " S q u a r e . h "

u s i n g n a m e s p a c e s t d ;

i n t m a i n ( )

S q u a r e s ; / / U s i n g t h e d e f a u l t c o n s t r u c t o r

S q u a r e s 2 ( 3 . 0 , ' b ' ) ; / / U s i n g t h e s e c o n d c o n s t r u c t o r

c o u t < < " s . a r e a ( ) = " < < s . a r e a ( ) < < " , s . c o l o u r ( ) = " < < s . c o l o u r ( ) < < e n d l ;

c o u t < < " s 2 . a r e a ( ) = " < < s 2 . a r e a ( ) < < " , s 2 . c o l o u r ( ) = " < < s 2 . c o l o u r ( ) < < e n d l ;

r e t u r n 0 ;

Page 33: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 33/483 3

i n I s s u e 7 o f T h e M a g P i . ) T h e M a k e f i l e s h o u l d b e i n t h e s a m e d i r e c t o r y a s t h e S q u a r e . h , S q u a r e . c p p a n d

m a i n . c p p f i l e s . T h e n t y p e

m a k e

t o c o m p i l e t h e c o d e a n d b u i l d t h e e x e c u t a b l e . O n c e t h e e x e c u t a b l e h a s b e e n b u i l d , r u n t h e p r o g r a m :

. / s q u a r e

O b j e c t s o n t h e h e a p

W h e n a n o b j e c t i s n e e d e d w i t h i n s e v e r a l d i f f e r e n t f u n c t i o n s c a l l s , i t m i g h t b e h e l p f u l t o c r e a t e i t o n t h e h e a p

i n s t e a d . T h e d i f f e r e n c e b e t w e e n c r e a t i n g a n o b j e c t o n t h e s t a c k a n d t h e h e a p i s t h a t o b j e c t s o n t h e s t a c k a r e

a u t o m a t i c a l l y c l e a n e d u p w h e n t h e y g o o u t o f s c o p e , w h e r e a s o b j e c t s o n t h e h e a p s t a y i n m e m o r y . T o c l e a n u p

a n o b j e c t o n t h e h e a p , i t h a s t o b e e x p l i c i t l y d e l e t e d . A m o d i f i e d v e r s i o n o f m a i n . c p p i s g i v e n b e l o w , w h e r e t h e

o b j e c t s a r e c r e a t e d o n t h e h e a p i n s t e a d . T h e s y n t a x s - > c o l o u r ( ) i s s h o r t h a n d f o r ( * s ) . c o l o u r ( ) .

C C = g + +

T A R G E T = s q u a r e

O B J E C T S = m a i n . o S q u a r e . o

$ ( T A R G E T ) : $ ( O B J E C T S )

@ e c h o " * * L i n k i n g E x e c u t a b l e "

$ ( C C ) $ ( O B J E C T S ) - o $ ( T A R G E T )

c l e a n :

@ r m - f * . o * ~

v e r y c l e a n : c l e a n

@ r m - f $ ( T A R G E T )

% . o : % . c p p

@ e c h o " * * C o m p i l i n g C + + S o u r c e "

$ ( C C ) - c $ ( I N C F L A G S ) $ <

# i n c l u d e < i o s t r e a m >

# i n c l u d e " S q u a r e . h "

u s i n g n a m e s p a c e s t d ;

i n t m a i n ( )

S q u a r e * s = n e w S q u a r e ( ) ; / / U s i n g t h e d e f a u l t c o n s t r u c t o r

S q u a r e * s 2 = n e w S q u a r e ( 3 . 0 , ' b ' ) ; / / U s i n g t h e s e c o n d c o n s t r u c t o r

c o u t < < " s - > a r e a ( ) = " < < s - > a r e a ( ) < < " , s - > c o l o u r ( ) = " < < s - > c o l o u r ( ) < < e n d l ;

c o u t < < " s 2 - > a r e a ( ) = " < < s 2 - > a r e a ( ) < < " , s 2 - > c o l o u r ( ) = " < < s 2 - > c o l o u r ( ) < < e n d l ;

d e l e t e s ;

d e l e t e s 2 ;

r e t u r n 0 ;

Page 34: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 34/484

S K I L L L E V E L : I N T E R M E D I A T E

G i a n l u c a S e r r a

G u e s t W r i t e r

C H A T R O O M

T u r n y o u r R a s p b e r r y P i i n t o a n

X M P P C h a t S e r v e r

T h e s t o r y D u e t o i t s l o w c o s t a n d p o w e r c o n s u m p t i o n ,

t h e R a s p b e r r y P i i s p r o b a b l y t h e b e s t d e v i c e t o b u i l d

a h o m e s e r v e r f r o m . A h o m e s e r v e r i s a b l e t o d o a l o t

o f u s e f u l t h i n g s , i n c l u d i n g r u n n i n g a s a m e d i a c e n t e r ,

h o m e r e p o s i t o r y , p o w e r o r t e m p e r a t u r e m o n i t o r i n g

a n d m u c h m o r e .

I n m y o p i n i o n , o n e o f t h e m o r e i n t e r e s t i n g u s e s o f t h e

R a s p b e r r y P i i s a s a n X M P P s e r v e r . X M P P i s a

s t a n d a r d p r o t o c o l f o r I n s t a n t M e s s a g i n g , u s e d b y

G T a l k f o r e x a m p l e , t h a t p r o v i d e s i n f r a s t r u c t u r e f o r a n

X M P P s e r v e r t o s e n d m e s s a g e s a n d t w o o r m o r e

X M P P c l i e n t s t o e x c h a n g e m e s s a g e s t o e a c h o t h e r .

I w a s s e a r c h i n g f o r t h e b e s t X M P P s e r v e r t o p u t o n a

R a s p b e r r y P i w h e n I c a m e a c r o s s P r o s o d y

( h t t p s : / / p r o s o d y . i m ) . P r o s o d y i s a l i g h t w e i g h t X M P P

s e r v e r w r i t t e n i n L U A ( a f a s t p r o g r a m m i n g l a n g u a g e

b a s e d o n C a n d u s e d f o r g a m e p r o g r a m m i n g ) w i t h a l l

o f t h e b a s i c c a p a b i l i t i e s o f a n X M P P s e r v e r , p l u s

s o m e e x t r a - m o d u l e s y o u c a n a d d a s y o u w i s h , s u c h

a s a n a d m i n c o n s o l e , s e c u r e c o m m u n i c a t i o n , e t c .

W i t h a X M P P s e r v e r i n s t a l l e d o n y o u r R a s p b e r r y P i

y o u c a n c r e a t e a c h a t s e r v e r t o b e u s e d o n y o u r

i n t r a n e t W i - F i n e t w o r k ( b e t w e e n y o u a n d y o u r

r e l a t i v e s , f o r e x a m p l e ) o r a r e a l I n t e r n e t c h a t s e r v e r

( l i k e G T a l k o r W h a t s A p p ) e x p o s e d t h r o u g h t h e

I n t e r n e t a n d u n d e r y o u r c o n t r o l .

I n t h i s a r t i c l e w e ' l l d e s c r i b e h o w t o c r e a t e a n I n t e r n e t

c h a t s e r v e r w i t h p r i v a t e a n d r e s t r i c t e d a c c e s s .

S e t u p P r o s o d y

P r o s o d y h a s a n o t h e r b i g a d v a n t a g e : i t i s i n t h e

R a s p b i a n r e p o s i t o r y . Y o u c a n i n s t a l l i t s i m p l y b y

t y p i n g :

$ sudo apt-get update$ sudo apt-get install prosody

O n c e y o u r d o w n l o a d a n d i n s t a l l a t i o n a r e c o m p l e t e d ,

y o u n e e d t o d o t w o t h i n g s :

1 ) C r e a t e a v a l i d c o n f i g u r a t i o n f i l e f o r P r o s o d y

2 ) C r e a t e u s e r s t h a t y o u w a n t t o b e i n y o u r c h a t l i s t

O b v i o u s l y , i f y o u w a n t t o e x p o s e y o u r c h a t s e r v i c e

t h r o u g h t h e I n t e r n e t , y o u w i l l n e e d t o s e t u p p o r t -

f o r w a r d i n g o n y o u r r o u t e r a n d p o i n t t o t h e s e l e c t e d

p o r t f o r t h e P r o s o d y s e r v e r . W e ' l l c o m e t o t h a t i n a

m i n u t e .

Y o u c a n l o c a t e t h e c o n f i g f i l e i n :

/etc/prosody/prosody.cfg.lua

C r e a t e a b a c k u p o f t h i s f i l e i f i t a l r e a d y e x i s t s . T h e n ,

r e p l a c e t h e c o n t e n t s w i t h t h e f o l l o w i n g . Y o u c a n a l s o

f i n d a n e x a m p l e a t :

h t t p s : / / p r o s o d y . i m / d o c / e x a m p l e _ c o n f i g

Page 35: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 35/483 5

L e t ' s r u n t h r o u g h t h i s f i l e a n d s e e w h a t i t d o e s . T h e

" modules_enabled" s e c t i o n d e f i n e s w h i c h m o d u l e s

P r o s o d y l o a d s o n s t a r t u p . S o m e a r e r e q u i r e d , o t h e r s

a r e o p t i o n a l . F e e l f r e e t o s e l e c t t h e m o d u l e s y o u l i k e .

T h e " posix" m o d u l e f o r e x a m p l e i s n e c e s s a r y f o r t h e

U N I X s y s t e m ( s o , a l s o f o r R a s p b e r r y P i ) , w h i l e t h e

" register" m o d u l e i s u s e f u l o n l y i f y o u w a n t t o a l l o w

e x t e r n a l u s e r s t o r e g i s t e r w i t h y o u r c h a t s e r v i c e ( s e e

b e l o w ) .

T h e n e x t p a r a m e t e r i s " allow_registration" .

T h i s a l l o w s e x t e r n a l r e g i s t r a t i o n . I h a v e s e t t h i s t o

false b e c a u s e I w a n t e d t o c r e a t e m y c h a t s e r v i c e

u s e r s m a n u a l l y a s I w a n t t o c r e a t e a p r i v a t e a n d

modules_enabled =

-- Generally required

"roster"; -- Allow users to have a roster. Recommended ;)

"saslauth"; -- Authentication for clients and servers.

"tls"; -- Add support for secure TLS on c2s/s2s connections

"dialback"; -- s2s dialback support

"disco"; -- Service discovery-- Not essential, but recommended

"private"; -- Private XML storage (for room bookmarks, etc.)

"vcard"; -- Allow users to set vCards

-- Nice to have

"legacyauth"; -- Legacy authentication. Only used by some old clients and bots.

"version"; -- Replies to server version requests

"uptime"; -- Report how long server has been running

"time"; -- Let others know the time here on this server

"ping"; -- Replies to XMPP pings with pongs

"register"; -- Allow users to register on this server using a client and change passwords

-- Other specific functionality

"posix"; -- POSIX functionality, sends server to background, enables syslog, etc.

--"console"; -- telnet to port 5582 (needs console_enabled = true)

--"bosh"; -- Enable BOSH clients, aka "Jabber over HTTP"

--"httpserver"; -- Serve static files from a directory over HTTP

;

-- Disable account creation by default, for security

-- For more information see http://prosody.im/doc/creating_accounts

allow_registration = false;

-- Debian:

-- send the server to the backgrounddaemonize = true;

-- Create a pidfile for nohup launch

pidfile = "/var/run/prosody/prosody.pid";

-- Logs info and higher to /var/log

log =

-- Log files (change 'infor' to 'debug' for debug logs):

info = "/var/log/prosody/prosody.log";

error = "/var/log/prosody/prosody.err";

-- Syslog:

levels = "error" ; to = "syslog"; ;

-- Your personal domain

VirtualHost "mydomain";

c2s_ports = 5222;

Page 36: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 36/486

h i d d e n s e r v i c e . O n l y a n a d m i n i s t r a t i v e u s e r l o g g e d

o n t o t h e R a s p b e r r y P i a n d w i t h p r i v i l e g e s t o r u n

P r o s o d y c o m m a n d s c a n c r e a t e a n d d e l e t e u s e r s .

T h e " daemonize" p a r a m e t e r i s u s e f u l , i n c o m b i n a t i o n

w i t h " pidfile" i f y o u w a n t t o r u n P r o s o d y w i t h o u t

c r e a t i n g a n o h u p s c r i p t ( i . e . : t o k e e p P r o s o d y r u n n i n g

e v e n i f y o u l o g o u t o f t h e R a s p b e r r y P i ) . F u r t h e r m o r e ,

y o u c a n m a n a g e t h e P r o s o d y s e r v i c e w i t h t h e i n i t . d

c o m m a n d s ( s t a r t , r e s t a r t , s t o p , e t c . ) F o r e x a m p l e :

$ sudo /etc/init.d/prosody restart

B y a d d i n g t h e " log" d i r e c t i v e w e c a n d e t e c t a n y e r r o r

o r e v e n t u a l i n t r u s i o n i n y o u r c h a t s e r v e r . Y o u c a n

s p e c i f y a n y p a t h y o u l i k e , b u t I s u g g e s t y o u k e e p t o

t h e d e f a u l t /var/log f i l e s .

T h e l a s t , " VirtualHost" , e n a b l e s t h e a d m i n i s t r a t o r

t o c r e a t e v a r i o u s h o s t s e a c h w i t h o n e o r m o r e u s e r s .

A V i r t u a l H o s t i s e f f e c t i v e l y a s i n g l e c h a t r o o m o r c h a t

s e r v i c e , i n d e p e n d e n t f r o m a n y o t h e r , w h i c h c a n c a n

c o n t a i n o n e o r m o r e u s e r s . F o r m y p u r p o s e , I c r e a t e d

a s i n g l e d o m a i n n a m e d " mydomain" .

U n d e r e a c h V i r t u a l H o s t y o u c a n d e f i n e z e r o o r m o r e

d i r e c t i v e s t h a t c a n o v e r r i d e t h e m a s t e r o n e s : y o u c a n

s p e c i f y a s p e c i f i c l o g d i r e c t i v e , o r a l l o w e x t e r n a l

r e g i s t r a t i o n b y o v e r r i d i n g t h e p a r a m e t e r

" allow_registration" f o r e x a m p l e .

I n m y f i l e I e x p l i c i t l y p u t t h e " c2s_ports" d i r e c t i v e

w i t h t h e d e f a u l t v a l u e o f 5 2 2 2 i n o r d e r t o a l l o w m e t o

e a s i l y c h a n g e i t a s w e w i l l s e e i n t h e n e x t s e c t i o n .

T h a t d i r e c t i v e s p e c i f i e s a c o m m a - s e p a r a t e d a r r a y o f

p o r t n u m b e r s o n w h i c h t h e P r o s o d y s e r v e r w i l l l i s t e n .

C r e a t e u s e r s

P r o s o d y c o m e s w i t h a n u t i l i t y c a l l e d prosodyctlt h a t a l l o w s y o u t o c r e a t e u s e r s a n d m a n a g e t h e

s e r v i c e . C r e a t e y o u r u s e r s w i t h t h e f o l l o w i n g , e n t e r i n g

a p a s s w o r d f o r t h e u s e r i n e a c h c a s e w h e n p r o m p t e d :

$ sudo prosodyctl adduser user1@mydomain$ sudo prosodyctl adduser user2@mydomain

B y e n t e r i n g t h e a b o v e , w e h a v e c r e a t e d t w o u s e r s

b o u n d t o t h e ' m y d o m a i n ' d o m a i n .

N o w , r e s t a r t ( o r start) t h e s e r v e r :

$ prosodyctl restart

C o n g r a t u l a t i o n s ! Y o u r c h a t s e r v i c e s h o u l d n o w b e u p

a n d r u n n i n g .

T h e c l i e n t s i d e

N o w , t o t e s t i t . L e t ' s i n s t a l l s o m e X M P P c l i e n t s o n

y o u r p r e f e r r e d d e v i c e s . I h a v e a n A n d r o i d p h o n e , o n

w h i c h I i n s t a l l e d X a b b e r ( f r o m P l a y S t o r e ) a n d a

M a c B o o k w i t h A d i u m .

B o t h X a b b e r a n d A d i u m h a v e a s i m p l e w i z a r d t o

c o n f i g u r e y o u r a c c o u n t . W i t h X a b b e r , y o u c r e a t e a

n e w a c c o u n t w i t h u s e r n a m e u s e r 1 @ m y d o m a i n a n d

t h e p a s s w o r d y o u s p e c i f i e d .

O t h e r i m p o r t a n t p a r a m e t e r s y o u n e e d t o m o d i f y a r e

t h e h o s t a n d p o r t u n d e r S e t t i n g s > X M P P a c c o u n t s

p a n e l . B y d e f a u l t , P r o s o d y r u n s o n p o r t 5 2 2 2 ( t h e

s t a n d a r d X M P P p o r t ) , w h i l e f o r t h e h o s t , s i m p l y t y p e

y o u r R a s p b e r r y P i ' s c u r r e n t I P a d d r e s s o n y o u r

n e t w o r k .

D o t h e s a m e t h i n g s w i t h A d i u m o r w i t h a n o t h e r

A n d r o i d d e v i c e w i t h X a b b e r ( o r w i t h C r o s s T a l k f o r

i P h o n e ) . O n c e y o u r d e v i c e s a r e c o n n e c t e d t o t h e

c h a t s e r v i c e , a d d e a c h o t h e r b y a d d i n g a n e w

c o n t a c t . T h e p r o c e d u r e i s t h e s a m e f o r a l l I M

s e r v i c e s : u s e r 1 a s k s t o a d d u s e r 2 a n d u s e r 2 h a s t o

c o n f i r m t h e f r i e n d s h i p .

Y o u s h o u l d b e a b l e t o s e n d a n d r e c e i v e m e s s a g e s

f r o m u s e r 1 a n d u s e r 2 , t h r o u g h y o u r X M P P c l i e n t s

a n d v i a y o u r R a s p b e r r y P i .

E x p o s e t h e s e r v i c e t o t h e I n t e r n e t

O p t i o n a l l y y o u c a n p u b l i s h y o u r s e r v i c e o n t o t h e

I n t e r n e t . F i r s t , c h o o s e a p o r t ( o r a s e t o f p o r t s ) t h a t

y o u c a n f o r w a r d b e h i n d y o u r r o u t e r ( d e f a u l t : 5 2 2 2 ) ,

n o t i n g t h a t s o m e I n t e r n e t p r o v i d e r s r e s t r i c t a c c e s s t o

s o m e p o r t s . F o r e x a m p l e I s e t t h e d i r e c t i v e l i k e t h i s :

c2s_ports = 9999

a n d p o r t - f o r w a r d e d 9 9 9 9 t o m y l o c a l R a s p b e r r y P i ' s

I P a d d r e s s . I n t h i s w a y , a l l r e q u e s t e s r e c e i v e d

t h r o u g h p o r t 9 9 9 9 f r o m t h e I n t e r n e t w i l l b e d i r e c t e d t o

m y R a s p b e r r y P i , w h i c h P r o s o d y w i l l c a t c h a s i t i s

l i s t e n i n g o n p o r t 9 9 9 9 .

T o a c c e s s t h e c h a t s e r v i c e a c r o s s t h e I n t e r n e t f r o m

o u t s i d e y o u r L A N c h a n g e t h e h o s t a n d p o r t f o r

X a b b e r : c h a n g e t h e I P a d d r e s s t o y o u r r o u t e r ' s p u b l i c

Page 37: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 37/483 1

f a c i n g I P a d d r e s s a n d p o r t t o 9 9 9 9 . Y o u s h o u l d n o w

b e a b l e t o e x c h a n g e m e s s a g e s o u t s i d e y o u r l o c a l

n e t w o r k . T r y i t !

S e c u r i t y

O n c e y o u h a v e e x p o s e d y o u r s e r v i c e y o u a r e

p r o b a b l y a s k i n g y o u r s e l f : h o w c a n I p r o t e c t m y

c h a t r o o m s w i t h a s e c u r e c h a n n e l ? P r o s o d y a l l o w s

y o u t o s e t u p S S L / T L S c o n f i g u r a t i o n t o e n c r y p t

c o m m u n i c a t i o n s a n d t o e n s u r e t h a t t h e s e r v e r y o u a r e

t a l k i n g w i t h i s r e a l l y y o u r s e r v e r ( t h e R a s p b e r r y P i ) .

T o a d d a n a d v a n c e d s e c u r i t y c o n f i g u r a t i o n y o u f i r s t

n e e d t o c r e a t e a c e r t i f i c a t e , w h i c h i s a k i n d o f b u c k e d

i n w h i c h i s s t o r e d t h e s e r v e r ' s i d e n t i t y . W i t h a t r u s t e d

c e r t i f i c a t e s u c c e s s f u l l y v e r i f i e d a c l i e n t c a n c h e c k i f

t h e s e r v e r i s r e a l l y w h o i t c l a i m s t o b e . T o c r e a t e a

c e r t i f i c a t e a n d a v a l i d S S L / T L S c o n f i g u r a t i o n I

s u g g e s t t h e s e l i n k s :

h t t p s : / / p r o s o d y . i m / d o c / c e r t i f i c a t e s

h t t p s : / / p r o s o d y . i m / d o c / a d v a n c e d _ s s l _ c o n f i g

R e f e r e n c e s

P r o s o d y w e b s i t e : h t t p s : / / p r o s o d y . i m

X a b b e r c l i e n t : h t t p : / / w w w . x a b b e r . o r g

A d i u m c l i e n t : h t t p s : / / a d i u m . i m

T h e e x c e l l e n t P i d g i n c h a t c l i e n t i s a v a i l a b l e d i r e c t l y

f r o m t h e R a s p b i a n r e s p o s i t o r y f o r u s e o n t h e

R a s p b e r r y P i :

$ sudo apt-get install pidgin

A f t e r i n s t a l l a t i o n y o u w i l l f i n d P i d g i n u n d e r L X D E ' s

M a i n M e n u / I n t e r n e t / P i d g i n I n s t a n t M e s s e n g e r .

W i t h i n P i d g i n , t h e m e n u o p t i o n A c c o u n t s / M a n a g e

A c c o u n t s l e t s y o u a d d o n e o f y o u r p r e - d e f i n e d

P r o s o d y u s e r s .

Page 38: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 38/488

S K I L L L E V E L : B E G I N N E R

B e r n h a r d S u t e r

G u e s t W r i t e r

L I N U X C O M M A N D S

b a s h , w h i c h & h i s t o r y

T a l e s f r o m t h e L i n u x t o o l s h e d :

D o n ’ t b a s h t h e s h e l l !

B a s h

T h e f i r s t L i n u x c o m m a n d - l i n e c o m m a n d w h i c h a u s e r

e n c o u n t e r s m i g h t t y p i c a l l y b e b a s h , t h e d e f a u l t L i n u x

c o m m a n d - l i n e i n t e r p r e t e r i t s e l f .

F o r m a n y n e w c o m e r s , t h e L i n u x c o m m a n d - l i n e

i n t e r f a c e c a n f e e l d a u n t i n g l y c o m p l e x a n d o f t e n

s l i g h t l y i n c o n s i s t e n t . O n e o f t h e r e a s o n s f o r t h i s

f e e l i n g m i g h t b e t h a t t h e r e r e a l l y i s n ’ t s u c h a t h i n g a s

a s i n g l e m o n o l i t h i c L i n u x c o m m a n d - l i n e i n t e r f a c e .

T h e i n t e r f a c e w h i c h w e s e e i s t h e r e s u l t o f a r e l a t i v e l y

b a s i c c o m m a n d - l i n e i n t e r p r e t e r s h e l l i n c o m b i n a t i o n

w i t h a v a s t a n d e v e r g r o w i n g c o l l e c t i o n o f c o m m a n d s ,

w h i c h a r e e a c h s t a n d - a l o n e e x e c u t a b l e p r o g r a m s

i n s t a l l e d s o m e w h e r e o n t h e f i l e s y s t e m .

A s h e l l i s j u s t l i k e a n y o f t h e s e c o m m a n d s , e x c e p t

t h a t o n e o f i t s m a i n p u r p o s e s i s t o i n t e r a c t w i t h t h e

u s e r a n d t o e x e c u t e o t h e r c o m m a n d s . T h e r e a r e

s e v e r a l p o p u l a r s h e l l s a v a i l a b l e - b e s i d e s t h e o r i g i n a l

s h , f o r e x a m p l e a l s o k s h , c s h o r t c s h - b u t o n L i n u x

t o d a y , b a s h h a s l a r g e l y b e c o m e t h e s t a n d a r d .

b a s h i s n a m e d s o m e w h a t t o n g u e i n c h e e k “ B o u r n e

a g a i n s h e l l ” , a f t e r t h e s h o r B o u r n e s h e l l , w h i c h w a s

w r i t t e n b y S t e v e n B o u r n e , a B r i t i s h c o m p u t e r s c i e n t i s t

w o r k i n g a t B e l l L a b s o n t h e e a r l y U n i x s y s t e m .

L i k e m o s t s h e l l s , b a s h c a n b e u s e d b o t h i n a n

i n t e r a c t i v e a s w e l l a s i n a b a t c h m o d e . I n b a t c h m o d e ,

b a s h b e c o m e s a p r o g r a m m i n g l a n g u a g e i n t e r p r e t e r

i n i t s o w n r i g h t . H o w t o u s e b a s h a s a s c r i p t i n g

l a n g u a g e i s d e s c r i b e d i n m o r e d e t a i l i n t h e “ B a s h

G a f f e r T a p e ” s e r i e s s t a r t i n g i n I s s u e 1 0 o f T h e

M a g P i .

I n i n t e r a c t i v e m o d e , b a s h a l l o w s t h e u s e r t o e d i t t h e

c u r r e n t c o m m a n d l i n e b u f f e r u s i n g t h e l e f t a n d r i g h t

a r r o w k e y s , d e l e t e e t c . a n d t o g g l e t h r o u g h t h e h i s t o r y

o f r e c e n t c o m m a n d s u s i n g t h e u p a n d d o w n a r r o w

k e y s . I t a l s o s u p p o r t s m a n y m o r e c r y p t i c b u t p o w e r f u l

< C t r l > - k e y s e q u e n c e s f o r l i n e e d i t i n g a n d n a v i g a t i o n .

H o w e v e r t h e m a i n p u r p o s e o f a n i n t e r a c t i v e s h e l l

s e s s i o n i s u s u a l l y t o r u n s o m e o t h e r p r o g r a m s o n

b e h a l f o f t h e u s e r .

C o m m a n d e x e c u t i o n

W h e n t h e u s e r p r e s s e s < E n t e r > , t h e s h e l l p r o c e s s e s

t h e s t a t e o f t h e c o m m a n d l i n e e d i t i n g b u f f e r , b r e a k s

t h e i n p u t s t r i n g i n t o t h e a p p r o p r i a t e p i e c e s , m a y b e

p e r f o r m s s o m e v a r i a b l e s u b s t i t u t i o n s , a n d e x e c u t e s

s o m e b u i l t - i n c o m m a n d s o r s e t s u p o n e o r m o r e

e x t e r n a l c o m m a n d s t o b e e x e c u t e d .

F o r e x a m p l e , i n o r d e r t o e x e c u t e ,

Page 39: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 39/483 9

p i @ r a s p b e r r y p i ~ $ l s - l $ S H E L L

- r w x r - x r - x 1 r o o t r o o t 8 1 3 9 9 2 J a n 1 0 2 0 1 3

/ b i n / b a s h

b a s h f i r s t e x p a n d s t h e v a r i a b l e “ S H E L L ” t o i t s

d e f i n i t i o n S H E L L = / b i n / b a s h , b e c a u s e $ < v a r n a m e >

t r i g g e r s v a r i a b l e s u b s t i t u t i o n . T h e n i t l o c a t e s t h e

e x e c u t a b l e w h i c h c o r r e s p o n d s t o t h e c o m m a n d l s

a n d e x e c u t e s i t w i t h t h e t w o a r g u m e n t s - l a n d

/ b i n / b a s h .

W h i c h

A n e x e c u t a b l e i s l o c a t e d b y s e a r c h i n g i n o r d e r

t h r o u g h t h e l i s t o f d i r e c t o r i e s i n t h e P A T H

e n v i r o n m e n t v a r i a b l e , w h i c h t y p i c a l l y i n a d e f a u l t

i n s t a l l a t i o n o f R a s p b i a n i s s e t t o :

P A T H = / u s r / l o c a l / s b i n : / u s r / l o c a l / b i n : / u s r / s b i n : / u s r / b i n : /

s b i n : / b i n : / u s r / l o c a l / g a m e s : / u s r / g a m e s

W e c a n l o o k a t w h e r e b a s h w o u l d f i n d a p a r t i c u l a r

c o m m a n d b y u s i n g t h e w h i c h c o m m a n d :

p i @ r a s p b e r r y p i ~ $ w h i c h l s

/ b i n / l s

p i @ r a s p b e r r y p i ~ $ w h i c h w h i c h

/ u s r / b i n / w h i c h

E a c h e x e c u t a b l e p r o g r a m i n t h e s e a r c h p a t h i n f a c t

a p p e a r s t o b e a b a s h c o m m a n d . M a n y l i k e l s , b a s h

i t s e l f , o r o t h e r s d i s c u s s e d i n t h i s s e r i e s , a r e p a r t o f

t h e s t a n d a r d L i n u x d i s t r i b u t i o n . B u t e a c h u s e r c a n

a l s o w r i t e t h e i r o w n p r o g r a m s a n d r u n t h e m f r o m t h e

s h e l l l i k e a n y s t a n d a r d t o o l . S o m e c o m m a n d s l i k e c d

o r h i s t o r y a r e d i r e c t l y e x e c u t e d b y t h e s h e l l i t s e l f

a n d a r e c a l l e d " b u i l t i n " c o m m a n d s ( e n t e r m a n

b u i l t i n s f o r a c o m p l e t e l i s t ) .

I / O r e d i r e c t i o n & p i p e l i n i n g

O n e o f t h e m o s t p o w e r f u l a s p e c t s o f b a s h i s

i n p u t / o u t p u t r e d i r e c t i o n . E a c h p r o g r a m w h i c h i s r u n

f r o m t h e s h e l l i s l a u n c h e d w i t h t h r e e p s e u d o - f i l e s

o p e n a t s t a r t u p : s t a n d a r d i n p u t , s t a n d a r d o u t p u t a n d

s t a n d a r d e r r o r ( o u t p u t ) . B y d e f a u l t t h e y a r e c o n n e c t e d

t o t h e t e r m i n a l w h e r e t h e s h e l l i s r u n n i n g . T h e r e a r e

t w o m a j o r w a y s t o c h a n g e t h a t d e f a u l t a s s o c i a t i o n :

I / O r e d i r e c t i o n a n d p i p e l i n i n g .

R e d i r e c t i o n

I / O r e d i r e c t i o n i n v o l v e s t h e u s e o f o p e r a t o r s t o

c o n n e c t o n e o f t h e t h r e e I / O s t r e a m s t o a n a m e d f i l e

i n s t e a d o f t h e i n t e r a c t i v e t e r m i n a l :

< : r e d i r e c t i n p u t

> : r e d i r e c t o u t p u t

2 > : r e d i r e c t e r r o r o u t p u t

> > : r e d i r e c t o u t p u t a n d a p p e n d i n s t e a d o f o v e r w r i t e

p i @ r a s p b e r r y p i ~ $ g r e p r a s p b e r r y < i n p u t . t x t

> o u t p u t . t x t 2 > e r r o r . t x t

I n t h e a b o v e e x a m p l e , t h e g r e p s t r i n g m a t c h i n g t o o l

i s r e a d i n g l i n e s o f t e x t f r o m t h e f i l e “ i n p u t . t x t ” , i n s t e a d

o f t h e c o n s o l e , a n d w r i t e s t h o s e w h i c h c o n t a i n t h e

s u b - s t r i n g “ r a s p b e r r y ” i n t o t h e f i l e “ o u t p u t . t x t ” ,

i n s t e a d o f p r i n t i n g t h e m t o t h e c o n s o l e . I f t h e r e a r e

a n y e r r o r s , t h e y a r e w r i t t e n t o t h e f i l e “ e r r o r . t x t ” .

P i p e l i n i n g

P i p e l i n i n g r e q u i r e s t h e s h e l l t o r u n m u l t i p l e p r o g r a m s

i n p a r a l l e l a n d u s e t h e |

p i p e o p e r a t o r t o d i r e c t l y f e e d

t h e o u t p u t f r o m o n e c o m m a n d i n t o t h e i n p u t o f t h e

n e x t c o m m a n d , a s i f t h e y w e r e c o n n e c t e d b y a d a t a

p i p e .

B e c a u s e o f a s h a r e d d e s i g n p h i l o s o p h y , m o s t

c o m m o n L i n u x c o m m a n d s a r e v e r y s i m p l e a n d

t y p i c a l l y o n l y h a v e o n e p u r p o s e . . . a n d m o s t o f t h e m

o p e r a t e o n s i m p l e u n f o r m a t t e d t e x t . U s i n g t h e p i p e

o p e r a t o r , t h e s h e l l a l l o w s t h e m t o b e c o m b i n e d i n

i n g e n i o u s w a y s t o a c h i e v e m u c h m o r e c o m p l e x

f u n c t i o n a l i t y .

T o p u t i t i n t o t h e w o r d s o f D o u g l a s M c I l r o y , t h e

i n v e n t o r o f t h e U n i x p i p e c o n c e p t :

“ T h i s i s t h e U n i x p h i l o s o p h y : W r i t e p r o g r a m s t h a t d o

o n e t h i n g a n d d o i t w e l l . W r i t e p r o g r a m s t o w o r k

t o g e t h e r . W r i t e p r o g r a m s t o h a n d l e t e x t s t r e a m s ,

b e c a u s e t h a t i s a u n i v e r s a l i n t e r f a c e . ”

Page 40: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 40/48

Page 41: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 41/484 1

9 6 5 c a t / e t c / p a s s w d | c u t - d : - f 7 | s o r t |

u n i q - c | s o r t - n r

9 6 6 l e s s / e t c / p a s s w d

9 6 7 h i s t o r y

p i @ r a s p b e r r y p i ~ $

U s i n g t h e u p a r r o w k e y , w e c a n t h e f l i p b a c k t h r o u g h

t h e h i s t o r y u n t i l w e g e t t o t h e c o m m a n d w e w a n t t o

r e - r u n , o r e d i t t h e l i n e f i r s t b e f o r e r u n n i n g .

I f w e a r e l o o k i n g f o r s o m e t h i n g m o r e s p e c i f i c , w e c a n

a l s o f i l t e r t h e o u t p u t o f h i s t o r y , e . g . w i t h t h e g r e p

t o o l :

p i @ r a s p b e r r y p i ~ $ h i s t o r y | g r e p g r e p

9 0 9 g r e p r a s p b e r r y < i n p u t . t x t > o u t p u t . t x t 2 >

e r r o r . t x t

9 1 1 c a t | g r e p - v m o u s e | s o r t | u n i q - c

9 6 9 h i s t o r y | g r e p g r e p

p i @ r a s p b e r r y p i ~ $

I n a g r a p h i c a l t e r m i n a l , w e c a n u s e t h e m o u s e t o

c o p y / p a s t e a n y c o m m a n d f r o m t h e h i s t o r y o u t p u t t o

t h e c o m m a n d l i n e , o r o t h e r w i s e r e c a l l a l i n e b y i t s

n u m b e r b y p r e f i x i n g i t w i t h a n e x c l a i m a t i o n m a r k ! :

p i @ r a s p b e r r y p i ~ $ ! 9 0 9

g r e p r a s p b e r r y < i n p u t . t x t > o u t p u t . t x t 2 >

e r r o r . t x t

- b a s h : i n p u t . t x t : N o s u c h f i l e o r d i r e c t o r y

p i @ r a s p b e r r y p i ~ $

W e c a n a l s o p r e s s < C t r l > - R t o s t a r t a s e a r c h b a c k

t h r o u g h t h e h i s t o r y f o r t h e f i r s t c o m m a n d w h i c h

c o n t a i n s w h a t e v e r w e a r e n o w t y p i n g :

p i @ r a s p b e r r y p i ~ $

( r e v e r s e - i - s e a r c h ) ` f u n ' : e c h o “ b a s h i s a l o t

o f f u n … ”

A u t o - c o m p l e t e

A n o t h e r c o n v e n i e n t s h o r t h a n d i s c o n t e x t u a l a u t o -

c o m p l e t i o n . D e p e n d i n g o n t h e p o s i t i o n i n t h e

c o m m a n d - l i n e , p r e s s i n g t h e < T a b > k e y o n c e m a y

c o m p l e t e t h e c u r r e n t w o r d b e h i n d t h e c u r s o r , i f t h e r e

i s a s i n g l e m a t c h i n g c o m p l e t i o n . P r e s s i n g t h e < T a b >

k e y t w i c e p r i n t s a l l t h e p o s s i b l e s u g g e s t i o n s . F o r

e x a m p l e , t y p i n g b a < T a b > < T a b > r e s u l t s i n :

p i @ r a s p b e r r y p i ~ $ b a

b a d b l o c k s b a s e 6 4 b a s e n a m e b a s h b a s h b u g

p i @ r a s p b e r r y p i ~ $ b a

A u t o - c o m p l e t i o n i s a n i c e w a y t o a v o i d t y p o s a n d

s a v e o n s o m e t y p i n g f o r c o m m a n d n a m e s o r c o m p l e x

f i l e s y s t e m p a t h s f o r e x a m p l e . F o r s o m e c o m m o n

c o m m a n d s , e v e n t h e o p t i o n s c a n b e a u t o - c o m p l e t e d .

F o r e x a m p l e , t y p i n g l s - - < T a b > < T a b > r e s u l t s i n :

p i @ r a s p b e r r y p i ~ $ l s - -

- - a l l

- - a l m o s t - a l l

. . .

- - v e r s i o n

- - w i d t h =

p i @ r a s p b e r r y p i ~ $ l s - -

S i m p l i c i t y

Y o u c a n a l s o c u s t o m i z e t h e b e h a v i o u r a n d

a p p e a r a n c e o f y o u r l o g i n s h e l l s e s s i o n b y a d d i n g

c o n f i g u r a t i o n c o m m a n d s t o s o m e o f t h e d e f a u l t f i l e s

w h i c h b a s h l o a d s a n d e x e c u t e s o n s t a r t u p , e . g .

r e a d i n g f r o m ~ / . b a s h r c .

P a r t o f t h e e l e g a n t s i m p l i c i t y o f t h e U n i x c o m m a n d

m o d e l i s t h a t t h e c o m m a n d l i n e i n t e r p r e t e r i s n o t h i n g

b u t a c o m m a n d i t s e l f , w h i c h h a p p e n s t o e x e c u t e

o t h e r c o m m a n d s . I t h a s n o s p e c i a l p r i v i l e g e s a n d

a n y b o d y c o u l d w r i t e o n e o f t h e i r o w n . H o w e v e r , a w e l l

d e s i g n e d s h e l l l i k e b a s h s h o u l d p l a y t o t h e s t r e n g t h o f

t h e U n i x p h i l o s o p h y w h e r e e a c h t o o l s h o u l d b e

s i m p l e , d o o n e t h i n g w e l l a n d b e a b l e t o a c t a s a f i l t e r

i n a m u l t i - s t a g e p i p e l i n e o f c o m m a n d s t o i m p l e m e n t

c o m p l e x f u n c t i o n s a s n e e d e d .

M o d e r n s h e l l s l i k e b a s h h a v e a l o t o f n i c e u s a b i l i t y

f e a t u r e s w h i c h m a k e i n t e r a c t i n g w i t h a L i n u x t e r m i n a l

s e s s i o n a l o t l e s s d a u n t i n g a n d k n o w i n g s o m e o f t h e

t r i c k s a n d s h o r t h a n d s c a n s a v e a l o t o f t i m e .

B u t b a s h i s o n l y a s u s e f u l a s t h e c o m m a n d s i t c a n

e x e c u t e . W h a t a r e s o m e o f y o u r f a v o r i t e o r m o s t

p u z z l i n g L i n u x c o m m a n d s ? S e n d y o u r c o m m e n t s t o

e d i t o r @ t h e m a g p i . c o m a n d m a y b e w e c a n c o v e r t h e m

i n a l a t e r e p i s o d e .

Page 42: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 42/48

M a k e r F a i r e B a y A r e a 2 0 1 4

W h e n : S a t u r d a y 1 7 t h t o S u n d a y 1 8 t h M a y 2 0 1 4 , 1 0 . 0 0 a m t o 6 . 0 0 p m ( P D T )

W h e r e : S a n M a t e o C o u n t y E v e n t C e n t e r , 1 3 4 6 S a r a t o g a D r , S a n M a t e o , C A 9 4 4 0 3 , U S A

C e l e b r a t e M A K E m a g a z i n e ' s 1 7 t h M a k e r F a i r e , s h o w c a s i n g c r e a t i v i t y i n t h e a r e a s o f s c i e n c e a n d

t e c h n o l o g y , e n g i n e e r i n g , f o o d , a n d a r t s a n d c r a f t s . h t t p : / / w w w . e v e n t b r i t e . c o m / e / 9 0 9 8 3 0 2 2 6 7

N o r t h S t a f f o r d s h i r e R a s p b e r r y J a m

W h e n : M o n d a y 1 2 t h M a y 2 0 1 4 , 6 . 0 0 p m t o 9 . 0 0 p m

W h e r e : N e w c a s t l e u n d e r L y m e C o l l e g e , K n u t t o n L a n e , N e w c a s t l e - u n d e r - L y m e , S T 5 2 G B , U K

A n e v e n i n g t o a l l o w R a s p b e r r y P i u s e r s a n o p p o r t u n i t y t o n e t w o r k , l e a r n a n d s h o w o f f p r o j e c t s .

h t t p s : / / n o r t h s t a f f s r j a m 1 . e v e n t b r i t e . c o . u k

U s i n g R a s p b e r r y P i G P I O t o t e a c h c o m p u t i n g

W h e n : W e d n e s d a y 2 1 s t M a y 2 0 1 4 , 1 . 3 0 p m t o 5 . 3 0 p m

W h e r e : E a s t B u i l d i n g 0 . 1 0 , T h e U n i v e r s i t y o f B a t h , C l a v e r t o n D o w n , B a t h , B A 2 7 A Y , U K

A h a l f d a y c o u r s e o n g e t t i n g t h e m o s t f r o m R a s p b e r r y P i s i n t h e s c h o o l e n v i r o n m e n t .

h t t p : / / w w w . e v e n t b r i t e . c o . u k / e / 1 1 1 6 7 8 8 2 4 3 7

W a n t t o k e e p u p t o d a t e w i t h a l l t h i n g s R a s p b e r r y P i i n y o u r a r e a ?

T h e n t h i s s e c t i o n o f T h e M a g P i i s f o r y o u ! W e a i m t o l i s t R a s p b e r r y J a m e v e n t s i n y o u r a r e a , p r o v i d i n g

y o u w i t h a R a s p b e r r y P i c a l e n d a r f o r t h e m o n t h a h e a d .

A r e y o u i n c h a r g e o f r u n n i n g a R a s p b e r r y P i e v e n t ? W a n t t o p u b l i c i s e i t ?

E m a i l u s a t : e d i t o r @ t h e m a g p i . c o m

F o c u s o n E d u c a t i o n @ C a m b r i d g e R a s p b e r r y J a m

W h e n : S a t u r d a y 1 0 t h M a y 2 0 1 4 , 1 2 . 0 0 p m t o 6 . 0 0 p m

W h e r e : I n s t i t u t e o f A s t r o n o m y , M a d i n g l e y R d , C B 3 0 H A , U K

F o r t e a c h e r s , c o - o r d i n a t o r s a n d o t h e r e d u c a t o r s : t a l k s a n d p r e s e n t a t i o n s c e n t r i n g o n t h e n e w

C o m p u t i n g c u r r i c u l u m . h t t p : / / w w w . e v e n t b r i t e . c o . u k / e / 1 0 8 6 7 0 7 2 7 0 7

P i C y m r u - C a r d i f f R a s p b e r r y J a m

W h e n : S a t u r d a y 1 7 t h M a y 2 0 1 4 , 1 . 3 0 p m t o 5 . 3 0 p m

W h e r e : H o w a r d i a n C e n t r e , H a m m o n d W a y , C a r d i f f , C F 2 3 9 N B , U K

T h e s e c o n d e v e n t o r g a n i s e d b y P i C y m r u : t a l k s , d e m o s a n d s h o w a n d t e l l . T h e M a g P i w i l l b e t h e r e -

c o m e a l o n g a n d s a y h e l l o . h t t p s : / / w w w . e v e n t b r i t e . c o . u k / e / 1 1 0 3 9 1 7 5 4 7 1

2

Page 43: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 43/48

Page 44: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 44/48

Page 45: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 45/484 5

Y o u w i l l t h e n b e a b l e t o o p e n S o n i c P i b y c l i c k i n g

o n t h e m a i n m e n u , a n d l o o k i n g w i t h i n E d u c a t i o n

- > S o n i c P i .

W h a t ' s N e w ?

S o m e o f y o u m a y h a v e a l r e a d y h a d a p l a y

a r o u n d w i t h S o n i c P i . H o p e f u l l y y o u a l l h a d f u n

m a k i n g b e e p s o f d i f f e r e n t p i t c h e s . Y o u c a n t a k e

a l l t h e m u s i c c o d i n g s k i l l s y o u ' v e l e a r n e d w i t h

v e r s i o n 1 a n d a p p l y i t t o v e r s i o n 2 . F o r t h o s e o f

y o u t h a t h a v e y e t t o o p e n S o n i c P i , n o w i s

d e f i n i t e l y t h e t i m e t o g i v e i t a t r y . Y o u ' l l b e

a m a z e d w i t h w h a t y o u c a n d o w i t h i t . H e r e ' s a

q u i c k l i s t o f t h e m a j o r n e w f e a t u r e s :

* S h i p s w i t h o v e r 2 0 s y n t h s o u n d s

* A b i l i t y t o p l a y a n y w a v o r a i f f s a m p l e f i l e

* S h i p s w i t h o v e r 7 0 s a m p l e s

* E x t r e m e l y a c c u r a t e t i m i n g

* S u p p o r t f o r o v e r 1 0 s t u d i o e f f e c t s : r e v e r b , e c h o ,

d i s t o r t i o n , e t c .

* S u p p o r t f o r L i v e C o d i n g : c h a n g i n g t h e c o d e

w h i l e i t r u n s

L e t ' s h a v e a l o o k a t a l l o f t h e s e f e a t u r e s .

P l a y i n g a d r u m l o o p

L e t ' s c o d e u p a s i m p l e d r u m l o o p . W e c a n u s e

t h e a m e n

b r e a k t o g e t u s s t a r t e d . I n t h e m a i n

c o d e e d i t o r w i n d o w o f S o n i c P i , t y p e t h e

f o l l o w i n g a n d t h e n h i t t h e R u n b u t t o n :

s a m p l e : l o o p _ a m e n

B o o m ! I n s t a n t d r u m s ! G o o n , p r e s s i t a f e w

t i m e s . H a v e f u n . I ' l l s t i l l b e h e r e w h e n y o u ' v e

f i n i s h e d .

B u t t h a t ' s n o t a l l . W e c a n m e s s a r o u n d w i t h t h e

s a m p l e . T r y t h i s :

s a m p l e : l o o p _ a m e n , r a t e : 0 . 5

O o o h , h a l f s p e e d . G o o n , t r y c h a n g i n g t h e r a t e .

T r y l o w e r a n d h i g h e r n u m b e r s . W h a t h a p p e n s i f

y o u u s e a n e g a t i v e n u m b e r ?

W h a t i f w e w a n t e d t o p l a y t h e l o o p a f e w t i m e s

o v e r ? O n e w a y t o d o t h i s i s t o c a l l s a m p l e a f e w

t i m e s w i t h s o m e s l e e p s i n b e t w e e n :

s a m p l e : l o o p _ a m e n

s l e e p 1 . 7 5 3

s a m p l e : l o o p _ a m e n

s l e e p 1 . 7 5 3

s a m p l e : l o o p _ a m e n

H o w e v e r , t h i s c o u l d g e t a b i t a n n o y i n g i f y o u

w a n t e d t o r e p e a t i t 1 0 t i m e s . S o w e h a v e a n i c e

w a y o f s a y i n g t h a t w i t h c o d e :

1 0 . t i m e s d o

s a m p l e : l o o p _ a m e n

s l e e p 1 . 7 5 3

e n d

O f c o u r s e , w e c a n c h a n g e t h e 1 0 t o w h a t e v e r

n u m b e r w e w a n t . G o o n , t r y i t ! W h a t i f w e w a n t t o

l o o p f o r e v e r ? W e s i m p l y s a y l o o p

i n s t e a d o f

1 0 . t i m e s . A l s o , I ' m s u r e y o u ' r e a s k i n g w h a t t h e

m a g i c 1 . 7 5 3 r e p r e s e n t s a n d h o w I g o t i t . W e l l , i t

i s t h e l e n g t h o f t h e s a m p l e i n s e c o n d s a n d I g o t i t

b e c a u s e I a s k e d S o n i c P i :

p u t s s a m p l e _ d u r a t i o n : l o o p _ a m e n

A n d i t t o l d m e 1 . 7 5 3 3 1 0 6 5 7 5 9 6 3 7 2 - I j u s t

s h o r t e n d e d i t t o 1 . 7 5 3 t o m a k e i t e a s i e r f o r y o u t o

t y p e i n . N o w , t h e c o o l t h i n g i s , w e c a n c o m b i n e

t h i s c o d e a n d a d d a v a r i a b l e f o r f u n :

s a m p l e _ t o _ l o o p = : l o o p _ a m e n

s a m p l e _ r a t e = 0 . 5

l o o p d o

s a m p l e s a m p l e _ t o _ l o o p , r a t e : s a m p l e _ r a t e

s l e e p s a m p l e _ d u r a t i o n s a m p l e _ t o _ l o o p , r a t e :

s a m p l e _ r a t e

e n d

N o w , y o u c a n c h a n g e t h e : l o o p _ a m e n

t o a n y o f

t h e o t h e r l o o p s a m p l e s ( u s e t h e a u t o - c o m p l e t e t o

d i s c o v e r t h e m ) . C h a n g e t h e r a t e t o o . H a v e f u n !

F o r a c o m p l e t e l i s t o f a v a i l a b l e s a m p l e s c l i c k t h e

H e l p b u t t o n .

Page 46: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 46/486

A d d i n g E f f e c t s

O n e o f t h e m o s t e x c i t i n g f e a t u r e s i n v e r s i o n 2 . 0

o f S o n i c P i i s t h e s u p p o r t f o r s t u d i o e f f e c t s s u c h

a s r e v e r b , e c h o a n d d i s t o r t i o n . T h e s e a r e r e a l l y

e a s y t o u s e . F o r e x a m p l e t a k e t h e f o l l o w i n g

s a m p l e t r i g g e r c o d e :

s a m p l e : g u i t _ e _ f i f t h s

T o a d d s o m e r e v e r b t o t h i s , w e s i m p l y n e e d t o

w r a p i t w i t h a w i t h _ f x

b l o c k :

w i t h _ f x : r e v e r b d o

s a m p l e : g u i t _ e _ f i f t h s

e n d

T o a d d s o m e d i s t o r t i o n , w e c a n a d d m o r e f x :

w i t h _ f x : r e v e r b d o

w i t h _ f x : d i s o r t i o n d o

s a m p l e : g u i t _ e _ f i f t h s

e n d

e n d

J u s t l i k e s y n t h s a n d s a m p l e s , F X a l s o s u p p o r t s

p a r a m e t e r s , s o y o u c a n t i n k e r w i t h t h e i r s e t t i n g s :

w i t h _ f x : r e v e r b , m i x : 0 . 8 d o

w i t h _ f x : d i s t o r t i o n , d i s t o r t : 0 . 8 d o

s a m p l e : g u i t _ e _ f i f t h s

e n d

e n d

O f c o u r s e , y o u c a n w r a p F X b l o c k s a r o u n d a n y

c o d e . F o r e x a m p l e h e r e ' s h o w y o u ' d c o m b i n e t h e

: i x i _ t e c h n o F X a n d o u r d r u m l o o p :

w i t h _ f x : i x i _ t e c h n o d o

l o o p d o

s a m p l e : l o o p _ a m e n

s l e e p s a m p l e _ d u r a t i o n : l o o p _ a m e n

e n d

e n d

F o r a c o m p l e t e l i s t o f F X a n d t h e i r p a r a m e t e r s

c l i c k t h e H e l p b u t t o n .

L i v e C o d i n g a S y n t h L o o p

N o w w e ' v e m a s t e r e d t h e b a s i c s o f t r i g g e r i n g

s a m p l e s , s l e e p i n g a n d l o o p i n g , l e t ' s d o t h e s a m e

w i t h s o m e s y n t h s a n d t h e n j u m p h e a d f i r s t i n t o

l i v e c o d i n g t e r r i t o r y :

l o o p d o

u s e _ s y n t h : t b 3 0 3

p l a y 3 0 , a t t a c k : 0 , r e l e a s e : 0 . 3

s l e e p 0 . 5

e n d

S o , w h a t d o t h e n u m b e r s m e a n i n t h i s e x a m p l e ?

W e l l , y o u c o u l d s t o p i t p l a y i n g , c h a n g e a n u m b e r ,

t h e n s t a r t i t a n d s e e i f y o u c a n h e a r t h e

d i f f e r e n c e . H o w e v e r a l l t h a t s t o p p i n g a n d s t a r t i n g

g e t s q u i t e a n n o y i n g . L e t ' s m a k e i t p o s s i b l e t o l i v e

c o d e s o y o u c a n i n s t a n t l y h e a r c h a n g e s . W e

n e e d t o p u t o u r c o d e i n t o a n a m e d f u n c t i o n w h i c h

w e l o o p :

d e f i n e : p l a y _ m y _ s y n t h d o

u s e _ s y n t h : t b 3 0 3

p l a y 3 0 , a t t a c k : 0 , r e l e a s e : 0 . 3

s l e e p 0 . 5

e n d

l o o p d o

p l a y _ m y _ s y n t h

e n d

N o w w h e n y o u r u n t h i s i t w i l l s o u n d e x a c t l y t h e

s a m e a s t h e s i m p l e r l o o p a b o v e . H o w e v e r , n o w

w e h a v e g i v e n o u r c o d e a n a m e ( i n t h i s c a s e ,

p l a y _ m y _ s y n t h ) w e c a n c h a n g e t h e d e f i n i t i o n o f

o u r c o d e w h i l e t h i n g s r u n . F o l l o w t h e s e s t e p s :

1 . W r i t e t h e c o d e a b o v e ( b o t h t h e d e f i n e a n d l o o p

b l o c k s )

2 . P r e s s t h e R u n b u t t o n

3 . C o m m e n t o u t t h e l o o p b l o c k ( b y a d d i n g #

a t

t h e b e g i n n i n g o f e a c h l i n e )

4 . C h a n g e t h e d e f i n i t i o n o f y o u r f u n c t i o n

5 . P r e s s t h e R u n b u t t o n a g a i n

6 . K e e p c h a n g i n g t h e d e f i n i t i o n a n d p r e s s i n g

R u n !

7 . P r e s s S t o p

F o r e x a m p l e , s t a r t w i t h t h e c o d e a b o v e . H i t R u n .

C o m m e n t o u t t h e l o o p

b l o c k t h e n c h a n g e t h e

p l a y c o m m a n d t o s o m e t h i n g d i f f e r e n t . Y o u r

c o d e s h o u l d l o o k s i m i l a r t o t h i s :

Page 47: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 47/484 7

d e f i n e : p l a y _ m y _ s y n t h d o

u s e _ s y n t h : t b 3 0 3

p l a y 4 5 , a t t a c k : 0 , r e l e a s e : 0 . 3 , c u t o f f :

7 0

s l e e p 0 . 5

e n d

# l o o p d o

# p l a y _ m y _ s y n t h

# e n d

T h e n h i t t h e R u n b u t t o n a g a i n . Y o u s h o u l d h e a r

t h e n o t e g o h i g h e r . T r y c h a n g i n g t h e a t t a c k ,

r e l e a s e a n d c u t o f f p a r a m e t e r s . L i s t e n t o t h e

e f f e c t t h e y h a v e . N o t i c e t h a t a t t a c k a n d r e l e a s e

c h a n g e t h e l e n g t h o f t h e n o t e a n d t h a t c u t o f f

c h a n g e s t h e ' b r i g h t n e s s ' o f t h e s o u n d . T r y

c h a n g i n g t h e s y n t h t o o - f u n v a l u e s a r e

: p r o p h e t ,

: d s a w a n d

: s u p e r s a w . P r e s s t h e

H e l p b u t t o n f o r a f u l l l i s t .

T h e r e a r e l o t s o f o t h e r t h i n g s w e c a n d o n o w , b u t

u n f o r t u n a t e l y I ' m r u n n i n g o u t o f s p a c e i n t h i s

a r t i c l e s o I ' l l j u s t t h r o w a c o u p l e o f i d e a s a t y o u .

F i r s t , w e c a n t r y s o m e r a n d o m i s a t i o n . A r e a l l y f u n

f u n c t i o n i s r r a n d

. I t w i l l r e t u r n a r a n d o m v a l u e

b e t w e e n t w o v a l u e s . W e c a n u s e t h i s t o m a k e

t h e c u f f o f f b o u n c e a r o u n d f o r a r e a l l y c o o l e f f e c t .

I n s t e a d o f p a s s i n g a n u m b e r l i k e 7 0 t o t h e c u t o f f

v a l u e , t r y r r a n d ( 4 0 , 1 2 0 )

. A l s o , i n s t e a d o f o n l y

p l a y i n g n o t e 4 5 , l e t ' s c h o o s e a v a l u e f r o m a l i s t o f

n u m b e r s . T r y c h a n g i n g 4 5 t o c h o r d ( : a 3 ,

: m i n o r ) . c h o o s e . Y o u r p l a y l i n e s h o u l d l o o k l i k e

t h i s :

p l a y c h o r d ( : a 2 , : m i n o r ) . c h o o s e , a t t a c k : 0 ,

r e l e a s e : 0 . 3 , c u t o f f : r r a n d ( 4 0 , 1 2 0 )

N o w y o u c a n s t a r t e x p e r i m e n t i n g w i t h d i f f e r e n t

c h o r d s a n d r a n g e v a l u e s f o r c u t o f f . Y o u c a n d o

s o m e t h i n g s i m i l a r w i t h t h e p a n v a l u e t o o :

p l a y c h o r d ( : a 2 , : m i n o r ) . c h o o s e , a t t a c k : 0 ,

r e l e a s e : 0 . 3 , c u t o f f : r r a n d ( 4 0 , 1 2 0 ) , p a n :

r r a n d ( - 1 , 1 )

N o w t h r o w s o m e F X i n , m e s s a r o u n d a n d j u s t

h a v e f u n ! H e r e a r e s o m e i n t e r e s t i n g s t a r t i n g

p o i n t s f o r y o u t o p l a y w i t h . C h a n g e t h e c o d e ,

m a s h i t u p , t a k e i t i n a n e w d i r e c t i o n a n d p e r f o r m

f o r y o u r f r i e n d s !

# H a u n t e d B e l l s

l o o p d o

s a m p l e : p e r c _ b e l l , r a t e : ( r r a n d 0 . 1 2 5 , 1 . 5 )

s l e e p r r a n d ( 0 . 5 , 2 )

e n d

# F M N o i s e

u s e _ s y n t h : f m

l o o p d o

p = p l a y c h o r d ( : E b 3 , : m i n o r ) . c h o o s e -

[ 0 , 1 2 , - 1 2 ] . c h o o s e , d i v i s o r : 0 . 0 1 , d i v _ s l i d e :

r r a n d ( 1 , 1 0 0 ) , d e p t h : r r a n d ( 0 . 1 , 2 ) , a t t a c k :

0 . 0 1 , r e l e a s e : r r a n d ( 0 . 1 , 5 ) , a m p : 0 . 5

p . c o n t r o l d i v i s o r : r r a n d ( 0 . 0 0 1 , 5 0 )

s l e e p [ 0 . 5 , 1 , 2 ] . c h o o s e

e n d

# D r i v i n g P u l s e

d e f i n e : d r u m s d o

s a m p l e : d r u m _ h e a v y _ k i c k , r a t e : 0 . 7 5

s l e e p 0 . 5

s a m p l e : d r u m _ h e a v y _ k i c k

s l e e p 0 . 5

e n d

d e f i n e : s y n t h s d o

u s e _ s y n t h : m o d _ p u l s e

u s e _ s y n t h _ d e f a u l t s a m p : 1 , m o d _ r a n g e : 1 5 ,

a t t a c k : 0 . 0 3 , r e l e a s e : 0 . 6 , c u t o f f : 8 0 ,

p u l s e _ w i d t h : 0 . 2 , m o d _ r a t e : 4

p l a y 3 0

s l e e p 0 . 2 5

p l a y 3 8

s l e e p 0 . 2 5

e n d

i n _ t h r e a d ( n a m e : : t 1 ) l o o p d r u m s

i n _ t h r e a d ( n a m e : : t 2 ) l o o p s y n t h s

# t r o n b i k e

l o o p d o

w i t h _ s y n t h : d s a w d o

w i t h _ f x ( : s l i c e r , f r e q : [ 4 , 8 ] . c h o o s e ) d o

w i t h _ f x ( : r e v e r b , r o o m : 0 . 5 , m i x : 0 . 3 ) d o

n 1 = c h o r d ( [ : b 1 , : b 2 , : e 1 , : e 2 , : b 3 ,

: e 3 ] . c h o o s e , : m i n o r ) . c h o o s e

n 2 = c h o r d ( [ : b 1 , : b 2 , : e 1 , : e 2 , : b 3 ,

: e 3 ] . c h o o s e , : m i n o r ) . c h o o s e

p = p l a y n 1 , a m p : 2 , r e l e a s e : 8 ,

n o t e _ s l i d e : 4 , c u t o f f : 3 0 , c u t o f f _ s l i d e : 4 ,

d e t u n e : r r a n d ( 0 , 0 . 2 )

p . c o n t r o l n o t e : n 2 , c u t o f f : r r a n d ( 8 0 ,

1 2 0 )

e n d

e n d

e n d

s l e e p 8

e n d

Page 48: The Magpi-Issue 23

8/10/2019 The Magpi-Issue 23

http://slidepdf.com/reader/full/the-magpi-issue-23 48/48

T h e M a g P i i s a t r a d e m a r k o f T h e M a g P i L t d . R a s p b e r r y P i i s a t r a d e m a r k o f t h e R a s p b e r r y P i F o u n d a t i o n . T h e M a g P i m a g a z i n e i s

c o l l a b o r a t i v e l y p r o d u c e d b y a n i n d e p e n d e n t g r o u p o f R a s p b e r r y P i o w n e r s , a n d i s n o t a f f i l i a t e d i n a n y w a y w i t h t h e R a s p b e r r y P i

Feedback & Question TimeA t T h e M a g P i , w e l o v e t o h e a r

y o u r f e e d b a c k - w h e t h e r i t i s b y

e m a i l , F a c e b o o k , T w i t t e r o r f a c e

t o f a c e a t o n e t h e m a n y

R a s p b e r r y P i r e l a t e d e v e n t s t h a t

a r e h a p p e n i n g g l o b a l l y . R e c e n t l y ,

o u r v e r y o w n C o l i n D e a d y w a s a t

t h e D i g i m a k e r s A t - B r i s t o l e v e n t

a n d h e r e i s s o m e o f t h e f e e d b a c k

h e r e c e i v e d f r o m p e o p l e v i s i t i n g

T h e M a g P i s t a n d :

E v e r y m o n t h t h e m a g a z i n e i s

r e l e a s e d s o m e o f t h e k i d s a t m y

s c h o o l p r i n t a n d p h o t o c o p y i t s o

t h e y e a c h h a v e t h e i r o w n c o p y .

K e e p u p t h e g o o d w o r k , w e l o o k

f o r w a r d t o e v e r y i s s u e .

F r o m a t e a c h e r

I t ’ s g r e a t t o s e e V o l u m e 2 o f T h e

M a g P i i n p r i n t . I t ’ s l o o k i n g e v e n

b e t t e r t h a n V o l u m e 1 .

A n o n

T h e M a g P i m a g p i e . . . i t l o o k s m o r e

l i k e a n e a g l e

A n o n

D o y o u c o v e r M i n e c r a f t ?

T e e n 1

D o y o u c o v e r M i n e c r a f t ?

T e e n 2

D o y o u c o v e r M i n e c r a f t ?

T e e n . . . n

A s y o u c a n s e e t h e r e w a s a

c o m m o n t h r e a d t o a n u m b e r o f

t h e s e q u e s t i o n s - a n d t h e a n s w e r

t o t h o s e i s Y E S ! ( I s s u e 1 1 c o v e r s

M i n e c r a f t a n d i s a v a i l a b l e o n l i n e

a n d i n p r i n t . )

S o t h e n e x t t i m e y o u ' r e a t a n

e v e n t a n d y o u s e e T h e M a g P i

s t a n d , c o m e o v e r a n d s a y h e l l o !

I r e c e n t l y d i s c o v e r e d t h e

R a s p b e r r y P i w h i l e l o o k i n g f o r a n

F M t r a n s m i t t e r I c o u l d u s e a t w o r k

a f t e r g e t t i n g t i r e d o f l i s t e n i n g t o

t h e s a m e s o n g s I ' v e b e e n

l i s t e n i n g t o f o r t h e l a s t 3 5 y e a r s . I

f o u n d a n e a s y s o l u t i o n u s i n g t h e

R a s b e r r y P i . I a l s o d i s c o v e r e d

y o u r f a n t a s t i c m a g a z i n e , w h i c h

b r o u g h t b a c k g r e a t m e m o r i e s o f

t y p i n g i n c o d e f r o m m a g a z i n e s

i n t o m y V i c 2 0 . I h a v e a l w a y s

r e g r e t t e d s e l l i n g t h a t c o m p u t e r ,

a n d t h e R a s p b e r r y P i i s a g r e a t

w a y t o b r i n g b a c k t h o s e d a y s .

I l o o k f o r w a r d t o g e t t i n g t o k n o w

m y l i t t l e c o m p u t e r b e t t e r t h r o u g h

y o u r m a g a z i n e . I h a v e a n e x c i t i n g

p r o j e c t i n m i n d , a n d c a n ' t w a i t t o

g e t e x p e r i e n c e d e n o u g h s o I c a n

i m p l e m e n t a n d s h a r e i t !

T h a n k y o u ,

L y n n W i l l i s

S a l t L a k e C i t y , U t a h

I f y o u a r e i n t e r e s t e d i n w r i t i n g f o r

T h e M a g P i , w o u l d l i k e t o r e q u e s t

a n a r t i c l e o r w o u l d l i k e t o j o i n t h e

t e a m i n v o l v e d i n t h e p r o d u c t i o n o f

t h e m a g a z i n e , p l e a s e g e t i n t o u c h

b y e m a i l i n g t h e e d i t o r a t :

e d i t o r @ t h e m a g p i . c o m