如何在覆盆子pi上用PHP打印?

时间:2016-04-08 14:03:27

标签: php raspberry-pi

我的Raspberry Pi上有PHP问题。这是有问题的一行:

$voltage = exec("/opt/vc/bin/vcgencmd measure_volts | tail -n 1| grep -oP 'volt=\s*\K\d...'");

这是在Raspberry Pi上运行的PHP脚本,包含上面的行:

<?php

define(LANGUAGE, "english");


$temp = shell_exec('cat /sys/class/thermal/thermal_zone*/temp');
$temp = round($temp / 1000, 1);

$clock = shell_exec('cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq');
$clock = round($clock / 1000);

$voltage = array;
$voltage = exec("/opt/vc/bin/vcgencmd measure_volts | tail -n 1| grep -oP 'volt=\s*\K\d...'", $output);
$voltage = $output;
$cpuusage = 100 - shell_exec("vmstat | tail -1 | awk '{print $15}'");

$uptimedata = shell_exec('uptime');
$uptime = explode(' up ', $uptimedata);
$uptime = explode(',', $uptime[1]);
$uptime = $uptime[0].', '.$uptime[1];

include 'localization/'.LANGUAGE.'.lang.php';

?>

这是前端HTML / PHP页面:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>asdasdsdfagwerwmrovimwtvp</title>
        <link rel="stylesheet" href="stylesheets/main.css">
        <link rel="stylesheet" href="stylesheets/button.css">
        <script src="javascript/raphael.2.1.0.min.js"></script>
        <script src="javascript/justgage.1.0.1.min.js"></script>

        <script>
            function checkAction(action){
                if (confirm('<?php echo TXT_CONFIRM; ?> ' + action + '?'))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

            window.onload = doLoad;

            function doLoad()
            {
            setTimeout( "refresh()", 30*1000 );
            }

            function refresh()
            {
            window.location.reload( false );
            }
        </script>
    </head>

    <body>
        <div id="container">
                <img id="logo" src="images/raspberry.png">
                <div id="title">asdfasdfasdf</div>

                <?php if(isset($uptime)){ ?>
                    <div id="uptime"><b><?php echo TXT_RUNTIME; ?></b>&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $uptime; ?> <span STYLE="font-size: 8px;">(hh:mm)</span></div>
                <?php } ?>

                <?php if(isset($temp) && is_numeric($temp)){ ?>
                    <div id="tempgauge"></div>
                    <script>
                        var t = new JustGage({
                            id: "tempgauge",
                            value: <?php echo $temp; ?>,
                            min: 0,
                            max: 100,
                            title: "<?php echo TEMPERATURE; ?>",
                            label: "°C"
                        });
                    </script>
                <?php } ?>

                <?php if(isset($voltage) && is_numeric($voltage)){ ?>
                    <div id="voltgauge"></div>
                    <script>
                        var v = new JustGage({
                            id: "voltgauge",
                            value: <?php echo $voltage; ?>,
                            min: 0.1,
                            max: 3.4,
                            title: "<?php echo TXT_VOLTAGE; ?>",
                            label: "V"
                        });
                    </script>
                <?php } ?>

                <?php if(isset($cpuusage) && is_numeric($cpuusage)){ ?>
                    <div id="cpugauge"></div>
                    <script>
                        var u = new JustGage({
                            id: "cpugauge",
                            value: <?php echo $cpuusage; ?>,
                            min: 0,
                            max: 100,
                            title: "<?php echo TXT_USAGE; ?>",
                            label: "%"
                        });
                    </script>
                <?php } ?>

                <?php if(isset($clock) && is_numeric($clock)){ ?>
                    <div id="clockgauge"></div>
                    <script>
                        var c = new JustGage({
                            id: "clockgauge",
                            value: <?php echo $clock; ?>,
                            min: 0,
                            max: 1000,
                            title: "<?php echo TXT_CLOCK; ?>",
                            label: "MHz"
                        });
                    </script>
                <?php } ?>

                <div id="controls">
                    <a class="button_orange" href="modules/shutdown.php?action=0" onclick="return checkAction('<?php echo TXT_RESTART_1; ?>');"><?php echo TXT_RESTART_2; ?></a><br/>
                    <a class="button_red" href="modules/shutdown.php?action=1" onclick="return checkAction('<?php echo TXT_SHUTDOWN_1; ?>');"><?php echo TXT_SHUTDOWN_2; ?></a>
                </div>
        </div>
    </body>
</html>

2 个答案:

答案 0 :(得分:0)

exec只会返回最后一行,要获得完整的结果,必须提供第二个参数

试试这个

$output = array;
string exec ( "/opt/vc/bin/vcgencmd measure_volts | tail -n 1| grep -oP 'volt=\s*\K\d...'", $output);
print_r ($output);

答案 1 :(得分:0)

这是一个页面:

<?php
ob_start();

define(LANGUAGE, "english");


$temp = shell_exec('cat /sys/class/thermal/thermal_zone*/temp');
$temp = round($temp / 1000, 1);

$clock = shell_exec('cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq');
$clock = round($clock / 1000);

$voltage = array;
$voltage = exec("/opt/vc/bin/vcgencmd measure_volts | tail -n 1| grep -oP 'volt=\s*\K\d...'", $output);
$voltage = $output;
$cpuusage = 100 - shell_exec("vmstat | tail -1 | awk '{print $15}'");

$uptimedata = shell_exec('uptime');
$uptime = explode(' up ', $uptimedata);
$uptime = explode(',', $uptime[1]);
$uptime = $uptime[0].', '.$uptime[1];

include 'localization/'.LANGUAGE.'.lang.php';

?>