Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 513
dragoneye000 Avatar asked Jan 22 '26 19:01

dragoneye000


1 Answers

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.

like image 131
Damaged Organic Avatar answered Jan 24 '26 07:01

Damaged Organic