You are on page 1of 4

12/16/13

Arduino + Raspberry Pi + Sending data to the web | Ben Kenney Online


My Rsum LinkedIn Profile My Publications theWatt Podcast Ben on Twitter bits Contact

BEN KENNEY

Arduino + Raspberry Pi + Sending data to the web


Submitted by benk on Sat, 07/07/2012 - 10:46 Ever since I needed a simple thermocouple data logger at work and found that they cost at least $600, it's been a little bit of a hobby of mine to get small, cheap computers to do useful things. After all, they used the equivalent of about 3 Commodore 64s to fly to the moon, and these things have about the same computing power as a modern day calculator, so why is it so hard/expensive to set up something with similar computing power to simply record data? The alternative to a $600 thermocouple data logger? A microcontroller like the Arduino (http://www.arduino.cc/) (~$25) connected to a Raspberry Pi (http://en.wikipedia.org/wiki/Raspberry_Pi) (~$40) running a database and webserver. That means I can charge $535 for brain power and still be competitive ;). The Arduino+Raspberry Pi also happens to be an infinitely more powerful system than a thermocouple data logger. You can basically plug in any sensor that you want, program any control scheme that you want and now, communicate with anything that you want by sending data to the internet.
Note: For the specific case outlined in these instructions, you don't actually need an Arduino since you can hook up directly to the GPIO pins on the Raspberry Pi, but this requires some connector cables that I don't have and adding an Arduino is useful for other projects as well.

The Raspberry Pi + Arduino looks like this:

Each component easily fits into the palm of my hand. So, what's going on here?

The Arduino is plugged into the Raspberry Pi via the USB port. The Raspberry Pi communicates with the Arduino (via the USB port) using serial communication programmed using Python (requiring the module pySerial). The Python program logs the data from the Arduino and saves the data to a file which can then be read by a webserver running on the Raspberry Pi. Here's how to set everything up: (Most of this is just directing you to other people's instructions)

www.benk.ca/node/10

1/4

12/16/13
What you'll need:

Arduino + Raspberry Pi + Sending data to the web | Ben Kenney Online

1. An Arduino Uno (or some other Arduino compatible variant) 2. A Raspberry Pi with SD Micro card and a 5V/~1A micro USB power supply and (perhaps 1) a powered USB hub 3. An internet connection 4. For the simple example of a temperature and light intensity logger controlled by the Arduino: i) a 10K thermistor ii) a 10K resistor iii) 10K photocell iv) a LED v) ~240ohm resistor vi) a breadboard. All of these components can be found in any "getting started with Arduino kit".
1

The Raspberry Pi is still a relatively new device and there seems to be a couple of uncertainties about how much power can be delivered via the USB ports, what peripherals it can handle and how many. Some USB hubs don't seem to work, some do. If your Raspberry Pi isn't working as you expect, then it could be a power issue.

Step 1. Install Debian linux onto the SD Micro card for the Raspberry Pi: http://elinux.org/RPi_Easy_SD_Card_Setup (http://elinux.org/RPi_Easy_SD_C ard_Setup) Step 2. Make sure you can boot into your Raspberry Pi system (default username/password: pi/raspberry) Step 3. Change default password:
p i @ r a s p b e r r y p i : ~ $p a s s w d[ e n t e r ][ t y p ey o u rn e wp a s s w o r d ]

Step 4. Configure your Raspberry Pi by installing: 1. SSH: http://fusionstrike.com/2012/setting-ssh-ftp-raspberry-pi-debian


(http://fusionstrike.com/2012/setting-ssh-ftp-raspberry-pi-debian)

2. Apache web server: http://fusionstrike.com/2012/installing-apache2-raspberry-pi-debian


(http://fusionstrike.com/2012/installing-apache2-raspberry-pi-debian)

3. PHP: http://fusionstrike.com/2012/setting-php-raspberry-pi-debian (http://fusionstrike.com/2012/setting-phpraspberry-pi-debian)

4. MySQL: http://fusionstrike.com/2012/setting-mysql-raspberry-pi-debian
(http://fusionstrike.com/2012/setting-mysql-raspberry-pi-debian)

5. Gnuplot (for making graphs):


p i @ r a s p b e r r y p i : ~ $s u d oa p t g e ti n s t a l lg n u p l o t

6. python-serial module (for using python to communicate with the Arduino):


p i @ r a s p b e r r y p i : ~ $s u d oa p t g e ti n s t a l lp y t h o n s e r i a l

Step 5. If you want, reclaim space on your SD card: https://projects.drogon.net/raspberry-pi/initialsetup1/ ( https://projects.drogon.net/raspberry-pi/initial-setup1/) Step 6. Upload a program to your Arduino using your normal everyday PC. To help you out, I've made a simple program that logs temperature and light intensity data from a thermistor and a photocell (it records data when it senses that the temperature changes by more than 0.4o C). Just copy/paste into the Arduino IDE and upload to your Arduino device:
/ * B yB e nK e n n e y-J u l y2 0 1 2 U s ea sy o uw i l l ,m i l e a g em a yv a r y ,d o n ' ts h o r tc i r c u i ty o u re l e c t r o n i c s , Iw o n ' tb eh e l dr e s p o n s i b l ef o ra n y t h i n gy o ud o ,e t c . . . * / # i n c l u d e< m a t h . h > c o n s ti n tl e d R e d D i g i t a l=9 ; c o n s ti n tt h e r m i s t o r A n a l o g I n=0 ; c o n s ti n tL M 3 4 A n a l o g I n=1 ; c o n s ti n tp h o t o c e l l A n a l o g I n=2 ; f l o a tt e m p _ t h e r m=0 . 0 ; f l o a tt e m p _ L M 3 4=0 . 0 ; f l o a tl i g h t _ p h o t o c e l l=0 . 0 ; f l o a tt e m p _ t h e r m _ o l d=0 . 0 ; f l o a tt e m p _ L M 3 4 _ o l d=0 . 0 ;

d o u b l eT h e r m i s t e r ( i n tR a w A D C ){ / / c o n v e r tv o l t a g ei n t oat e m p e r a t u r e d o u b l eT e m p ; T e m p=l o g ( ( ( 1 0 2 4 0 0 0 0 / R a w A D C )-1 0 0 0 0 ) ) ; T e m p=1/( 0 . 0 0 1 1 2 9 1 4 8+( 0 . 0 0 0 2 3 4 1 2 5+( 0 . 0 0 0 0 0 0 0 8 7 6 7 4 1*T e m p*T e m p) ) *T e m p) ; T e m p=T e m p-2 7 3 . 1 5 ; / /C o n v e r tK e l v i nt oC e l c i u s r e t u r nT e m p ; }

www.benk.ca/node/10

2/4

12/16/13
v o i ds e t u p ( ){ S e r i a l . b e g i n ( 9 6 0 0 ) ; p i n M o d e ( l e d R e d D i g i t a l , O U T P U T ) ; }

Arduino + Raspberry Pi + Sending data to the web | Ben Kenney Online

v o i dl o o p ( ){ t e m p _ t h e r m=T h e r m i s t e r ( a n a l o g R e a d ( t h e r m i s t o r A n a l o g I n ) ) ;/ /t h e r m i s t o rt e m p t e m p _ L M 3 4=( a n a l o g R e a d ( L M 3 4 A n a l o g I n ) * 5 . 0 * 1 0 0 . 0 ) / 1 0 2 4 . 0 ;/ /L M 3 4t e m p( F ) t e m p _ L M 3 4=( t e m p _ L M 3 4 3 2 . 0 ) * 5 . 0 / 9 . 0 ;/ /L M 3 4t e m p( C ) l i g h t _ p h o t o c e l l=a n a l o g R e a d ( p h o t o c e l l A n a l o g I n ) ;/ /p h o t o c e l lv a l u e / /r e c o r dd a t ao n l yi ft h e r m i s t o rt e m p e r a t u r ec h a n g e sb ym o r et h a n0 . 4d e gC i f(a b s ( t e m p _ t h e r m t e m p _ t h e r m _ o l d )>0 . 4){ S e r i a l . p r i n t ( l i g h t _ p h o t o c e l l ) ; S e r i a l . p r i n t ( " , " ) ; S e r i a l . p r i n t ( t e m p _ L M 3 4 ) ; S e r i a l . p r i n t ( " , " ) ; S e r i a l . p r i n t l n ( t e m p _ t h e r m ) ; d i g i t a l W r i t e ( l e d R e d D i g i t a l , H I G H ) ;/ / b l i n kL E Di fw em a k eam e a s u r e m e n t d e l a y ( 5 0 0 ) ; } d i g i t a l W r i t e ( l e d R e d D i g i t a l , L O W ) ; t e m p _ t h e r m _ o l d=t e m p _ t h e r m ; / / t e m p _ L M 3 4 _ o l d=t e m p _ L M 3 4 ; d e l a y ( 1 0 0 0 * 3 ) ;/ /w a i t3s e c o n d sa n ds t a r tl o o po v e ra g a i n }

Step 7. Hook up the thermistor/photocell to your breadboard and Arduino:

Step 8. Hook up the Arduino to the Raspberry Pi via USB Step 9. On the Raspberry Pi, make a python program that uses the serial module to read data coming out of the Arduino. The program should then write this data to disk. Something like this should work (name it something like dataLogger.py):
# ! / u s r / b i n / p y t h o n ' ' ' b yB e nK e n n e y-J u l y2 0 1 2 T h i sp r o g r a mr e a d sd a t ac o m i n gf r o mt h es e r i a lp o r ta n ds a v e st h a td a t at oat e x tf i l e .I te x p e c t sd a t ai nt h ef o r m a t : " p h o t o c e l l _ r e a d i n g , t h e r m i s t o r _ r e a d i n g " I ta s s u m e st h a tt h eA r d u i n os h o w su pi n/ d e v / t t y A C M 0o nt h eR a s p b e r r yP iw h i c hs h o u l dh a p p e ni fy o u ' r eu s i n gD e b i a n . ' ' '

i m p o r ts e r i a l f r o mt i m ei m p o r ts t r f t i m e f r o md a t e t i m ei m p o r td a t e t i m e ,t i m e s e r=s e r i a l . S e r i a l ( ' / d e v / t t y A C M 0 ' , 9 6 0 0 ) s t a r t T i m e=d a t e t i m e . n o w ( ) t r y :

www.benk.ca/node/10

3/4

12/16/13

Arduino + Raspberry Pi + Sending data to the web | Ben Kenney Online

w h i l e1 : l i n e = s e r . r e a d l i n e ( ) . r s t r i p ( ) l i g h t , t e m p 2 = l i n e . s p l i t ( " , " ) n o w=d a t e t i m e . n o w ( ) e l a p s e d T i m e=n o w s t a r t T i m e e l a p s e d S e c o n d s=( e l a p s e d T i m e . m i c r o s e c o n d s + ( e l a p s e d T i m e . d a y s * 2 4 * 3 6 0 0 + e l a p s e d T i m e . s e c o n d s ) * 1 0 * * 6 ) / 1 0 * * 6 p r i n t ( " % s , % s , % s , % s " % ( n o w . s t r f t i m e ( " % Y % m % d% H : % M : % S " ) , e l a p s e d S e c o n d s , l i g h t , t e m p 2 ) ) f = o p e n ( ' t e m p L o g . d a t ' , ' a ' ) p r i n t> > f , ( " % s , % s , % s , % s " % ( n o w . s t r f t i m e ( " % Y % m % d% H : % M : % S " ) , e l a p s e d S e c o n d s , l i g h t , t e m p 2 ) ) f . c l o s e ( ) e x c e p tK e y b o a r d I n t e r r u p t : p r i n t" \ n d o n e "

This program saves the data being collected by the Arduino into a file called tempLog.dat. Step 10. Run the python program to log data (note: I suggest you run this program within screen so that you can do other things with your Raspberry Pi while it's collecting data):
p i @ r a s p b e r r y p i : ~ $s u d oa p t g e ti n s t a l ls c r e e n p i @ r a s p b e r r y p i : ~ $s c r e e nSc o l l e c t D a t admp y t h o nd a t a L o g g e r . p y

Step 11. Use Gnuplot (which was installed in step 4.5) to make a graph of the data. To do this, make a small gnuplot script (call it something like plotData.plt):
r e s e t s e td a t a f i l es e p a r a t o r" , " s e tt e r mp n g s e tx d a t at i m e s e tt i m e f m t" % Y % m % d% H : % M : % S " s e tf o r m a tx" % b% d% H : % M " s e tx t i c sr o t a t eb y4 5 s e tx l a b e l" " s e ty l a b e l" T e m p e r a t u r e( C ) " s e ty 2 l a b e l" L i g h tI n t e n s i t y( % ) " s e ty t i c sn o m i r r o r s e ty 2 t i c s s e to u t p u t" d a t a . p n g " s e tk e yl e f t p l o t" t e m p L o g . d a t "u s i n g1 : 4t i t l e" T h e r m i s t o r "w i t hl i n e sa x e sx 1 y 1 ,\ " t e m p L o g . d a t "u s i n g1 : ( $ 3 / 1 0 2 4 * 1 0 0 . 0 )t i t l e" P h o t o c e l l "w i t hl i n e sa x e sx 1 y 2

and make the graph using:


p i @ r a s p b e r r y p i : ~ $g n u p l o tp l o t D a t a . p l t

This will make the file "data.png". Step 12. Move the file data.png into your web server directory, which defaults to /var/www/
p i @ r a s p b e r r y p i : ~ $c pd a t a . p n g/ v a r / w w w

This can be set up in a shell script and run automatically using cron. The end Finally, the end result: temperature and light coming through my living room window and showing up on the internet via my Raspberry Pi web server:

It does get hot in direct sunlight in Ottawa. After typing all of this out, it seems like a lot of work, but if you know your way around linux, it's not too bad.

Log in Powered by Drupal

www.benk.ca/node/10

4/4

You might also like