├── README.md
├── cleanDB.php
├── config.php
├── cpu.php
├── db.php
├── funs.php
├── index.php
├── install.sh
├── measuretemp.php
├── style.css
└── temp.php
/README.md:
--------------------------------------------------------------------------------
1 | Raspberry-Pi-Heartbeat
2 | ======================
3 |
4 | ##Status webpage for Raspberry Pi##
5 |
6 | 
7 |
8 | ###Prerequisites###
9 | You will have to check a few things for this page to show up correctly. You need to have:
10 |
11 | * lsb-release (for non Raspbian distributions) - so the automated installer script can find your distribution name.
12 | * Webserver (apache \ lighttpd \ ..) with PHP
13 |
14 |
15 | #####Optional (./install.sh will install them for you):
16 | * Sqlite for PHP `$ sudo apt-get install php5-sqlite`
17 | * PHP Safe Mode = off
18 | * vstat installed `$ sudo apt-get install vnstat`
19 |
20 | First 3 are more or less general and common. VNSTAT is additional app which collects traffic information. You might fiddle around with it's settings after the installation if you want. Main requirement is that it does give an output with this command `$ vnstat --dumpdb`.
21 |
22 | ###Contents of the package & Installation###
23 |
24 | As for now: 8-9 files
25 |
26 | * config.php - Where you can configure all the settings (folder path, verbosity and future options)
27 | * index.php - Home page which shows the graphs and info.
28 | * style.css - Styles for the fashion!
29 | * rpiTemp.db - SQLite db file. With some test data.
30 | * measuretemp.php - php script for cron job.
31 | * cleanDB.php - script to clean the DB from sampling data and forbid future accidental deletions using a lock file.
32 | * db.lck - lock file used by cleanDB.php to flag the clean operation as done and forbid future database cleanups. If you do want to reset the database, then delete db.lck first.
33 | * funs.php - extras for Ajax and live measurements
34 |
35 | **To set it up:**
36 |
37 | 1. Download a zip from [this page] (http://geekbeard.github.io/Raspberry-Pi-Heartbeat/) and unzip to a desired web folder (i.e. /var/www/rpih/)
38 | 2. Open config.php and update `$config["root_dir"]` the with directory you put it in. For example, if you put it into /var/www/rpih/: `$config["root_dir"]="/var/www/rpih/"`
39 | 3. Check and install if needed the prerequisites (see above) OR
40 | 4. Run `$ sudo ./install.sh` - this will try to install all missing prerequisites, initialize the database with the first temperature sample and update the crontab with the periodical temperature sampling. Default is a sample every 5 minutes. This can later be changed in `$ sudo crontab -e`
41 |
42 | You can also manually add the cron job to your system/download the
43 | packages/clean the database. `$ sudo crontab -e` and add a line at the end of the file:
44 | `*/5 * * * * php /path/measuretemp.php` - /path/ is the place where you've
45 | unziped the package! This will run measuretemp.php every 5 minutes. If you want
46 | it to happen less often, change 5 to a higher number of minutes (*/10 for every
47 | 10 minutes, */35 for every 35 minutes and so on).
48 |
49 | The database can be cleaned using cleanDB.php, which will also produce a lock
50 | file to avoid future accidental deletions. Delete the lock file (`db.lck`) first if you
51 | really want to clean the database.
52 |
53 | **You are done!**
54 |
55 | You've unzipped the package, created a scheduled task for Raspbian to run the php script every N minutes and you have cleaned the database from test data and the script created a fail safe flag (file: `db.lck`) which will prevent anyone from cleaning db again!
56 |
57 | Access the webpage with the web browser and start collecting statistics!:)
58 |
59 | And of course come back for updated versions!
60 |
61 | ###Authors and Contributors###
62 | First version by: @beard
63 |
64 | ###Support or Contact###
65 | If you have any questions \ suggestions \ feedbacks: geekbearddev@gmail.com - we hope you send us a note!:)
66 |
67 | Please feel free to fork and improve!
68 |
--------------------------------------------------------------------------------
/cleanDB.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | ";
24 | DB::exec("CREATE TABLE 'rpi_temp' ('id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 'temp' REAL, 'time' TEXT, 'cpu' REAL,'rtemp' REAL,'hum' REAL)");
25 |
26 | if($verbose)
27 | echo "DB created!
";
28 |
29 | Stats::measureTemp();
30 |
31 | if($verbose)
32 | echo "First values inserted! Good luck:)
";
33 |
34 | file_put_contents($filename, $s);
35 |
36 | }
37 | ?>
38 |
39 |
--------------------------------------------------------------------------------
/config.php:
--------------------------------------------------------------------------------
1 |
17 |
--------------------------------------------------------------------------------
/cpu.php:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/db.php:
--------------------------------------------------------------------------------
1 |
2 | query($query);
20 | }
21 | public static function exec($query) {
22 | DB::init();
23 | return DB::$db->exec($query);
24 | }
25 | }
26 |
27 | ?>
--------------------------------------------------------------------------------
/funs.php:
--------------------------------------------------------------------------------
1 | fetchArray()) {
11 | $id = $row['id'];
12 | $cpu=$row['cpu'];
13 | $dtime=substr($row['time'], -8, 5);
14 | $cpu=$row['cpu'];
15 |
16 | $buf .= "['$dtime',$cpu],";
17 |
18 | }
19 | return $buf ;
20 | }
21 |
22 | public static function cpuLoad() {
23 | //CPU Usage
24 | $output1 = null;
25 | $output2 = null;
26 | //First sample
27 | exec("cat /proc/stat", $output1);
28 | //Sleep before second sample
29 | sleep(1);
30 | //Second sample
31 | exec("cat /proc/stat", $output2);
32 | $cpuload = 0;
33 | for ($i=0; $i < 1; $i++)
34 | {
35 | //First row
36 | $cpu_stat_1 = explode(" ", $output1[$i+1]);
37 | $cpu_stat_2 = explode(" ", $output2[$i+1]);
38 | //Init arrays
39 | $info1 = array("user"=>$cpu_stat_1[1], "nice"=>$cpu_stat_1[2], "system"=>$cpu_stat_1[3], "idle"=>$cpu_stat_1[4]);
40 | $info2 = array("user"=>$cpu_stat_2[1], "nice"=>$cpu_stat_2[2], "system"=>$cpu_stat_2[3], "idle"=>$cpu_stat_2[4]);
41 | $idlesum = $info2["idle"] - $info1["idle"] + $info2["system"] - $info1["system"];
42 | $sum1 = array_sum($info1);
43 | $sum2 = array_sum($info2);
44 | //Calculate the cpu usage as a percent
45 | $load = (1 - ($idlesum / ($sum2 - $sum1))) * 100;
46 | $cpuload += $load;
47 | }
48 | $cpuload = round($cpuload, 1); //One decimal place
49 | return $cpuload ;
50 | }
51 |
52 | public static function temperatureURL() {
53 | include "config.php" ;
54 | return "\"".$config["temperatureURL"]."\"" ;
55 | }
56 |
57 | public static function cpuURL() {
58 | include "config.php" ;
59 | return "\"".$config["cpuURL"]."\"" ;
60 | }
61 |
62 | public static function temperature() {
63 | $temp = round(exec("cat /sys/class/thermal/thermal_zone0/temp ") / 1000, 1) ;
64 | return $temp ;
65 | }
66 |
67 | public static function temperatures() {
68 | $buf = "";
69 | $results = DB::query("SELECT * FROM rpi_temp ORDER BY id DESC LIMIT 0,288");
70 | while ($row = $results->fetchArray()) {
71 | $id = $row['id'];
72 | $temp=$row['temp'];
73 | $rtemp=$row['rtemp'];
74 | $dtime=substr($row['time'], -8, 5);
75 | $cpu=$row['cpu'];
76 |
77 | $buf .= "['$dtime',$temp],"; //fix here!!!
78 |
79 | }
80 |
81 | return $buf ;
82 | }
83 |
84 | public static function measureTemp() {
85 | $temp=round(exec("cat /sys/class/thermal/thermal_zone0/temp ") / 1000, 1);
86 | $dtime=date("Y-m-d H:i:s", strtotime ("+0 hour"));
87 |
88 | //dht stuff
89 | /* $fh = fopen('dht11.v','r');
90 | while ($line = fgets($fh)) {
91 | $dhtout = $line;
92 | }
93 | fclose($fh);exs
94 | //$dhtout = exec("cat /var/www/temp/dht11.v ");
95 | $dhtvalues = explode(";",$dhtout);
96 | $rtemp = $dhtvalues[1];
97 | $hum = $dhtvalues[0];
98 |
99 | */
100 | //end dht stuff
101 |
102 |
103 |
104 | //CPU Usage
105 | $output1 = null;
106 | $output2 = null;
107 | //First sample
108 | exec("cat /proc/stat", $output1);
109 | //Sleep before second sample
110 | sleep(1);
111 | //Second sample
112 | exec("cat /proc/stat", $output2);
113 | $cpuload = 0;
114 | for ($i=0; $i < 1; $i++)
115 | {
116 | //First row
117 | $cpu_stat_1 = explode(" ", $output1[$i+1]);
118 | $cpu_stat_2 = explode(" ", $output2[$i+1]);
119 | //Init arrays
120 | $info1 = array("user"=>$cpu_stat_1[1], "nice"=>$cpu_stat_1[2], "system"=>$cpu_stat_1[3], "idle"=>$cpu_stat_1[4]);
121 | $info2 = array("user"=>$cpu_stat_2[1], "nice"=>$cpu_stat_2[2], "system"=>$cpu_stat_2[3], "idle"=>$cpu_stat_2[4]);
122 | $idlesum = $info2["idle"] - $info1["idle"] + $info2["system"] - $info1["system"];
123 | $sum1 = array_sum($info1);
124 | $sum2 = array_sum($info2);
125 | //Calculate the cpu usage as a percent
126 | $load = (1 - ($idlesum / ($sum2 - $sum1))) * 100;
127 | $cpuload += $load;
128 | }
129 | $cpuload = round($cpuload, 1); //One decimal place
130 | $rtemp = 0;
131 | $hum = 0;
132 |
133 | $q="INSERT INTO rpi_temp(temp,time,cpu,rtemp,hum) VALUES('$temp','$dtime','$cpuload','$rtemp','$hum')";
134 | DB::exec($q);
135 | }
136 |
137 |
138 | public static function network() {
139 | $network = [];
140 | //traffic
141 | $traffic=null;
142 | exec("vnstat --dumpdb", $traffic);
143 | $network['alltrxL']=explode(";",$traffic[6]);
144 | $network['allttxL']=explode(";",$traffic[7]);
145 |
146 | $network['alltrx'] = $network['alltrxL'][1];
147 | $network['allttx'] = $network['allttxL'][1];
148 | //$alltrxtx = $alltrx+$allttx;
149 |
150 | $network['day0'] = explode(";",$traffic[13]);
151 | $network['day0rx'] = $network['day0'][3];
152 | $network['day0tx'] = $network['day0'][4];
153 |
154 | $network['day1'] = explode(";",$traffic[14]);
155 | $network['day1rx'] = $network['day1'][3];
156 | $network['day1tx'] = $network['day1'][4];
157 |
158 | $network['day2'] = explode(";",$traffic[15]);
159 | $network['day2rx'] = $network['day2'][3];
160 | $network['day2tx'] = $network['day2'][4];
161 |
162 | $network['mon0'] = explode(";",$traffic[43]);
163 | $network['mon0rx'] = $network['mon0'][3];
164 | $network['mon0tx'] = $network['mon0'][4];
165 |
166 | $network['mon1'] = explode(";",$traffic[44]);
167 | $network['mon1rx'] = $network['mon1'][3];
168 | $network['mon1tx'] = $network['mon1'][4];
169 |
170 | //var_dump($network);
171 | return $network ;
172 | }
173 | }
174 |
175 |
176 | ?>
177 |
--------------------------------------------------------------------------------
/index.php:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |