Skip to content

Instantly share code, notes, and snippets.

@VTSTech
Last active December 19, 2024 21:27
Show Gist options
  • Save VTSTech/465ef06994cfea462094c4ae6de3870d to your computer and use it in GitHub Desktop.
Save VTSTech/465ef06994cfea462094c4ae6de3870d to your computer and use it in GitHub Desktop.
Raspberry Pi RPi Temperature Monitor by VTSTech
<html>
<!-- If you get /dev/vcio error then 'sudo usermod -aG video www-data' -->
<?php
// Handle theme selection and refresh rate from GET parameters
$isDarkMode = isset($_GET['dark_mode']) ? ($_GET['dark_mode'] === 'on') : true; // Default to dark mode
$refreshRate = isset($_GET['refresh_rate']) && is_numeric($_GET['refresh_rate']) ? (int)$_GET['refresh_rate'] : 10; // Default to 10 seconds
// Define styles for light and dark themes
if ($isDarkMode) {
$bgColor = '000000';
$textColor = 'ffffff';
$linkColor = '00ff00';
} else {
$bgColor = 'ffffff';
$textColor = '000000';
$linkColor = '0000ff';
}
// Get Raspberry Pi model using grep
$modelOutput = shell_exec("grep Model /proc/cpuinfo");
$piModel = trim(str_replace("Model :", "", $modelOutput));
// Map detailed model names to simplified $rpi values
if (strpos($piModel, 'Pi 5') !== false) {
$rpi = 'RPi5';
} elseif (strpos($piModel, 'Pi 4') !== false) {
$rpi = 'RPi4';
} elseif (strpos($piModel, 'Pi 3') !== false) {
$rpi = 'RPi3';
} elseif (strpos($piModel, 'Pi 2') !== false) {
$rpi = 'RPi2';
} elseif (strpos($piModel, 'Pi Zero W') !== false) {
$rpi = 'RPi0W';
} elseif (strpos($piModel, 'Pi Zero') !== false) {
$rpi = 'RPi0';
} elseif (strpos($piModel, 'Pi') !== false) {
$rpi = 'RPi';
} else {
$rpi = 'Unknown Pi';
}
?>
<head>
<meta http-equiv="refresh" content="<?php echo $refreshRate; ?>">
</head>
<body bgcolor="<?php echo $bgColor; ?>" text="<?php echo $textColor; ?>" link="<?php echo $linkColor; ?>">
<b><br>Raspberry Pi RPi Temperature Monitor</b>
<br>Written by <b><a href='https://www.vts-tech.org/' style="color:<?php echo $linkColor; ?>">VTSTech</a>
(<a href='https://github.com/VTSTech' style="color:<?php echo $linkColor; ?>">GitHub</a></b>)
<!-- Settings form -->
<form method="get" style="margin-bottom: 10px;">
<!-- Refresh rate input -->
<label for="refresh_rate">Refresh:</label>
<input type="number" name="refresh_rate" id="refresh_rate"
value="<?php echo $refreshRate; ?>" min="1" max="999"
onchange="this.form.submit()">
<!-- Dark mode toggle -->
<label for="dark_mode">
<input type="checkbox" name="dark_mode" id="dark_mode"
<?php echo $isDarkMode ? 'checked' : ''; ?>
onchange="this.form.submit()"> Dark Mode
</label>
</form>
<?php
echo '<font color="'.$bgColor.'"><pre>';
$output = shell_exec("vcgencmd measure_temp"); // Retrieve temperature
$temp = substr($output, 5);
$output = system("vcgencmd measure_clock arm"); // Retrieve CPU speed
$speed = substr($output, 13);
$speed = round($speed / 1000000000, 1);
echo '</pre></font><font color="'.$textColor.'">';
echo '<title>' . $rpi . ' ' . $temp . ' ' . $speed . 'GHz</title>';
echo $piModel . '<br>Temp: ' . $temp . '<br>Speed: ' . $speed . 'GHz';
$uptimeContents = file_get_contents('/proc/uptime');
if ($uptimeContents !== false) {
// Extract the uptime in seconds (first value in /proc/uptime)
$uptimeSeconds = (float)explode(' ', $uptimeContents)[0];
// Convert uptime into days, hours, minutes, and seconds
$days = floor($uptimeSeconds / 86400);
$hours = floor(($uptimeSeconds % 86400) / 3600);
$minutes = floor(($uptimeSeconds % 3600) / 60);
$seconds = $uptimeSeconds % 60;
// Display the uptime
printf("<br>Uptime: %d days, %02d:%02d:%02d\n", $days, $hours, $minutes, $seconds);
} else {
echo "Unable to read uptime information.";
}
//$output = system("neofetch --stdout --disable shell cpu");
?>
</body>
</html>
@VTSTech
Copy link
Author

VTSTech commented Dec 18, 2024

image

@VTSTech
Copy link
Author

VTSTech commented Dec 18, 2024

image

@VTSTech
Copy link
Author

VTSTech commented Dec 18, 2024

image

@VTSTech
Copy link
Author

VTSTech commented Dec 18, 2024

image

@VTSTech
Copy link
Author

VTSTech commented Dec 19, 2024

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment