RaspBerry PiA little faq/guide for how to setup Raspberry Pi with rrdtool and crontab, generating some graphs, building an rrdtool database and some more.
This is one of my earlier tests so please bare with me…
Prereqs: Apache2, php5

 

Step 1:
To configure i2c follow the guide: http://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c
First steps are to disconnect the power from the Raspberry Pi and connect the correct pins between BMP085 and the Raspberry Pi.
3.3V to 3v0
GND = GND
0 SDA = SDA
0 SCL = SCL

Raspberry Pi connected to BMP085 via GPIO

When finished and the Pi is hooked up do a sudo nano /etc/modules and add these two lines: to the end of the file:

{code class="xml;"}i2c-bcm2708 
i2c-dev{/code}

Install python and i2c-tools with:

{code class="xml;"}sudo apt-get install python-smbus
sudo apt-get install i2c-tools{/code}

Edit raspi-blacklist.conf:

{code class="xml;"}$sudo nano /etc/modprobe.d/raspi-blacklist.conf{/code}

Locate the following lines and comment them out by putting a # in front:

{code class="xml;"}
#blacklist spi-bcm2708
#blacklist i2c-bcm2708
{/code}

Do a ”sudo i2cdetect -y 1” (0 if you have a 256mb pi).
This will search /dev/i2c-0 or /dev/i2c-1 for all address, and if an Adafruit BMP085 Breakout is properly connected it should show up as 0x77

Download the code:

{code class="xml;"}$ git clone https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git
$ cd Adafruit-Raspberry-Pi-Python-Code
$ cd Adafruit_BMP085
$ sudo ./Adafruit_BMP085_example.py{/code}

Step 2: Install rrdtool
$ sudo apt-get install rrdtool

Step 3: Install database
Create a filestructure in your homefolder:

{code class="xml;"}$ mkdir rrd
$ cd rrd/
$ mkdir db scripts tools
$ cd db{/code}

Create the database script:

{code class="xml;"}$ nano bmp085.sh{/code}
{code class="xml;"}
rrdtool create bmp085.rrd \
--start N --step 60 \
DS:temp:GAUGE:600:00:95 \
DS:hygro:GAUGE:600:0:10000 \
DS:altitud:GAUGE:600:0:20000 \
RRA:MIN:0.5:1:1000 \
RRA:MAX:0.5:1:1000 \
RRA:AVERAGE:0.5:1:1000
{/code}

Step 3.2 Install cpu database

{code class="xml;"}$ nano cpu.sh{/code}
{code class="xml;"}
rrdtool create cpu.rrd \
-start N --step 60 \
DS:temp:GAUGE:600:00:95 \
DS:frekvens:GAUGE:600:0:10000 \
RRA:MIN:0.5:1:1000 \
RRA:MAX:0.5:1:1000 \
RRA:AVERAGE:0.5:1:1000 {/code}
{code class="xml;"}$ chmod +x cpu.sh
$ chmod +x bmp085.sh
$ ./cpu.sh
$ ./bmp085.sh{/code}

Step 4: Create the update script

{code class="xml;"}$ cd ..
$ cd scripts
$ nano update.sh{/code}
{code class="xml;"}
# Update script #
# CODED BY SNIGLOM & XIANZE #
# rrdtool update <file.rrd> timestamp:val1[:val2:...] #
db=/home/pi/rrd/db/bmp085.rrd
adafruit=`/home/pi/rrd/tools/adafruit_BMP085.py |tr '.' ' ' | awk '{printf "%s:", $2}' | head -c -1`
rrdtool update $db N:$adafruit

cpudb=/home/pi/rrd/db/cpu.rrd
temp=`cat /sys/class/thermal/thermal_zone0/temp | head -c 2`
frekvens=`cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq | head -c 3`
rrdtool update $cpudb N:$temp:$frekvens{/code}

Step 5: Generate some graphs!

{code class="xml;"}$ mkdir /var/www/graphs
$ nano generate.sh
{/code}
{code class="xml;"}
#!/bin/bash
start=60m
end=N
output=/var/www/graphs
width=600
height=100

rrdtool graph $output/temp.png -s N-$start -a PNG -E \
--title="Temperatur" --vertical-label "RasPi-Temp" \
--width $width --height $height \
DEF:temp=/home/pi/rrd/db/bmp085.rrd:temp:AVERAGE \
LINE1:temp#ff0000:"Temperature" \
GPRINT:temp:LAST:"Temp\: %1.0lf°C"

rrdtool graph $output/tempweek.png -s N-1w -a PNG -E \
--title="Temperatur 1 week" --vertical-label "RasPi-Temp" \
--width $width --height $height \
DEF:temp=/home/pi/rrd/db/kajan2.rrd:temp:AVERAGE \
LINE1:temp#ff0000:"Temperatur" \
GPRINT:temp:LAST:"Temp\: %1.0lf°C"

rrdtool graph $output/hygro.png -l 800 -s N-$start -a PNG -E \
--title="Hygrometer" --vertical-label "RaSpi-Hygro" \
--width $width --height $height \
DEF:hygro=/home/pi/rrd/db/bmp085.rrd:hygro:AVERAGE \
LINE1:hygro#0000ff:"Hygrometer" \
GPRINT:hygro:LAST:"Tryck\: %1.0lf %sPa"

rrdtool graph $output/altitude.png -s N-$start -a PNG -E \
--title="Höjd" --vertical-label "RasPi-Alt" \
--width $width --height $height \
DEF:altitud=/home/pi/rrd/db/bmp085.rrd:altitude:AVERAGE \
LINE1:altitude#ff0000:"Altitude" \
GPRINT:altitude:LAST:"Alt\: %1.0lfm"

rrdtool graph $output/cpu.png -s N-$start -a PNG -E \
--title="CPU Temperature" --vertical-label "RasPi-Alt" \
--width $width --height $height \
DEF:temp=/home/pi/rrd/db/cpu.rrd:temp:AVERAGE \
LINE1:temp#ff0000:"Temperatur" \
GPRINT:temp:LAST:"Temp\: %1.0lf°C"
{/code}

Step 6: Create a cronjob!

{code class="xml;"}$ sudo crontab -e{/code}

Add these two lines in the bottom of the file:

{code class="xml;"}
* * * * * /home/pi/rrd/scripts/uppdate.sh
* * * * * /home/pi/rrd/scripts/generate.sh
{/code}

Step 7: Fetch the graphs to your production webserver!
Your graphs are now autogenerated every minute and the files and db are updated.
If your raspberry pi is your public webserver, you can stop read here.. Otherwise please continue reading =).

{code class="xml;"}Webserver$ mkdir /var/www/website/graphs
Webserver$ nano /var/www/website/getgraphs.sh{/code}
{code class="xml;"}
#!/bin/bash

# Shell script with purpose of: #
# get rrd graphs from raspberry pi #
# and put them in correct folder #
# By xianze #


wget -P /var/www/website/graphs/ -N http://raspberryip/graphs/altitude.png
wget -P /var/www/website/graphs/ -N http://raspberryip/graphs/cpu.png
wget -P /var/www/website/graphs/ -N http://raspberryip/graphs/hygro.png
wget -P /var/www/website/graphs/ -N http://raspberryip/graphs/temp.png
wget -P /var/www/website/graphs/ -N http://raspberryip/graphs/tempvecka.png

{code class="xml;"}Webserver$ sudo crontab -e {/code}

Add this line to the bottom of the crontab file:

{code class="xml;"}
* * * * * /var/www/webserver/graphs/getgraphs.sh{/code}

You are now finished, click this link for a live view and please keep in mind, this is my first try of something like this and i've learned alot. Please comment or e-mail me for questions.

Kategorier: Blogginlägg

Kommentera

E-postadressen publiceras inte. Obligatoriska fält är märkta *