简体   繁体   中英

How to get a float from python to JavaScript without cgi

I am running an apache server from a Raspberry Pi and have a python script that returns sensor input by printing it. This properly prints it to the console. Currently I have a php script that gets this output and shuts off a light if the sensor is reading is high, before printing it again. This also works when run from the console. The last part is javascript that is supposed to get the output from the php. It does that using ajax, which runs the "success" function, but gets "0" from the php script.

My php script:

<?php

exec ("python temp.py", $temp);
if((float)$temp[0]>28)
{
exec("gpio read 0",$state);
if($state[0]=="1")
{
include('gpio.php');
echo("Overheated: ".$temp[0]);
}
}
else
{
echo($temp[0]);
}
?>

My js:

$.ajax(
    {
        type: "GET",
        url: "temp.php",
        dataType: "text",
        success: function(msg)
        {
            alert("asd"+typeof msg);
        document.getElementById('text').innerHTML = msg;
            return msg;
        },
    error: function(jqXHR, textStatus, errorThrown){
                alert(jqXHR.responseText);
            }

});

Any suggestions are greatly appreciated. Thank you.

Right. It's probably user permission issues, because you get the output running script under your user from terminal. And browser, which is running it under server's user, returns an empty string.

Check www-data (or whatever) permissions when running script from your server. Set error reporting and run:

ini_set('display_errors', true);
error_reporting(E_ALL);

echo exec('whoami');

If it prints out user who is not sudoer, set correct permissions to allow your .py script execution by that user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM