1

I started a project that will allow me to use a raspi with a relay to measure and control the temperature of a Maple Evaporator using a fan to force air via 12v fan to increase the combustion of a wood-burning stove, thus increasing the temperature and allowing the wood boiler to stay at a specified temperature.

So I am trying to create an HTML web-form that will accept a number (any whole number 0-1000) and insert it into a PHP web file that will then insert it into an exec() function within the PHP file. So far I have been successful on creating an Html web form and using variable name ($HOLD_TEMPERATURE) to be inserted into the exec() function, but the python script that the exec() is running will not accept the variable name, only real number values such as 1, or 66, or 450, or 1000, and so on... If I open an Xterm session and insert the real number into the script, the program runs without any issues. Also, if I substitute a real value into the PHP file the script will also run without issues. I added some debugging values in the exec() function to help explain what is happening, as shown below.

So is there an option for me to enter a real number in the web form and it automatically propagates into the python script without a $variable needed?

Below is the Html code, PHP, and python script. Thanks!

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Temperature of Maple Evaporator </title>
</head>
<link href="stylesheets/common.css" type="text/css" rel="stylesheet">

<body>

<h1>Temperature of Flue</h1>

<form method="post" action="web-hold-temperature.php">

Temperature: <br />
<input type="number" name="HOLD_TEMPERATURE" size="35" />
<br />


<input type="submit" value="Submit Temperature (F)" />
<br />
</form>
</body>
</html>
<?php
$HOLD_TEMPERATURE = $_POST['HOLD_TEMPERATURE'];


if(empty($HOLD_TEMPERATURE )) {
echo "<h2>You must fill in temperature field</h2>\n" ;
die ("Click Back to start again.");
}
echo "<h2>You Entered the following information:</h2>";
echo "<b>Temperature:</b><br><br>";
echo $HOLD_TEMPERATURE;
?>

<?php
exec('bash -c "sudo ./tempctl/temperature_hold \$HOLD_TEMPERATURE 2>&1"', $output);
print_r($output);
?>

Here is the python script that is executed within the PHP file.

#!/usr/bin/python

#robogaia.com
#temperature controller
# the temperatures are in Fahrenheit
# temperature_hysteresis sets how low the temperature
#goes until the heater starts

import SMBus
import time
import datetime
from subprocess import call
import sys

bus = smbus.SMBus(1)

#I2C address

address = 0x4d
#this will tell how low will go until the heater starts again below the set point
temperature_hysteresis = 5
isHeating = True




filterArray = [0 , 0, 0 , 0 , 0, 0, 0, 0] #holds the filter values


def filter(input_value):
    filterArray.append(input_value);
    filterArray.pop(0);
    result = (filterArray[0] + filterArray[1]+ filterArray[2]+ filterArray[3]+ filterArray[4]+ filter
Array[5]+ filterArray[6]+ filterArray[7])/8 ;
    stringFilterArray = [str(a) for a in filterArray];
    #print(", ".join(stringFilterArray));
    return result;
def get_fahrenheit_val():
    data = bus.read_i2c_block_data(address, 1,2)
    val = (data[0] << 8) + data[1]
    return val/5.00*9.00/5.00+32.00
def get_celsius_val():
    data = bus.read_i2c_block_data(address, 1,2)
    val = (data[0] << 8) + data[1]
    return val/5.00


def set_cold():
    print "cooling"
    call(["temp_relay_on", "cold"])
    call(["temp_relay_off", "hot"])

def set_hot():
    print "heating"
    call(["temp_relay_on", "hot"])
    call(["temp_relay_off", "cold"])

def set_close():
    print "hold"
    call(["temp_relay_off", "cold"])
    call(["temp_relay_off", "hot"])



def main(argv):

    if len (sys.argv) < 2 :
            print "Usage: temperature_hold  [temperature] "
            sys.exit (1)
    set_point=sys.argv[1]
    try:
            set_point=float(sys.argv[1])
    except ValueError:
            print "the argument is not a number"
            sys.exit (1)
    print "set point =" , set_point
    print "temperature hysteresis =" , temperature_hysteresis
def main(argv):

    if len (sys.argv) < 2 :
            print "Usage: temperature_hold  [temperature] "
            sys.exit (1)
    set_point=sys.argv[1]
    try:
            set_point=float(sys.argv[1])
    except ValueError:
            print "the argument is not a number"
            sys.exit (1)
    print "set point =" , set_point
    print "temperature hysteresis =" , temperature_hysteresis

    #main loop
    while 1 == 1:
            #get the temperature form sensor
            temperature_raw =get_fahrenheit_val()
            temperature = filter(temperature_raw);

            #uncomment this for celsius
            #temperature = get_celsius_val()
            print temperature

            #verify if we need to cool or to heat
            if temperature > set_point :
                    set_cold()
                    isHeating = False
            elif temperature  <= (set_point- temperature_hysteresis):
                    set_hot()
                    isHeating = True
            elif isHeating == True and temperature  <= set_point:
                    set_hot()
            else:
                    print "hold"

            time.sleep(5.0)

 if __name__ == "__main__":
       main(sys.argv[1:])

[Here is the resultant log message from the exec() function is run. ][1]
Mujahid Bhoraniya
  • 1,518
  • 10
  • 22
  • this is probably a permission problem on this part of the code `exec('bash -c "sudo ./tempctl/temperature_hold \$HOLD_TEMPERATURE 2>&1"', $output);` – Pedro Lobito Feb 26 '20 at 05:14
  • @Pedro Lobito Not sure if the permissions are the cause, if I substitute the value of 70 (for example) in the equation --exec('bash -c "sudo ./tempctl/temperature_hold \70 2>&1"', $output); the relay picks up @ <70 and drops out at 70 and above. So the code works just the $HOLD_TEMPERATURE is not being substituted correctly. – Icefishingmn Feb 27 '20 at 02:32
  • Afaik, the user which the webserver runs under (`www`/`apache`?), shouldn't be able to `sudo`, please double check that. You may have to change the webserver user, but this weakens your security. You can also read: https://stackoverflow.com/a/8146982/797495. Please notice that when you run a php script from the console (php myscript.php), the permissions which it runs under, are different from the ones running on the webserver (http://.../myscript.php) – Pedro Lobito Feb 27 '20 at 04:10
  • @Pedro Lobito The user is www-data, since this originally was a LEMP install, so NGINX is the webserver. I do have www-data with administrative power, but since this is just a devel server with no i-net connectivity, and I just want the program to work before I secure it. The program will only work if a numeric value is put into the code in place of the $HOLD_TEMPERATURE, then the relay operates as it should. If the $HOLD_TEMPERATURE variable is used in the code, the relay will not operate, and the logfile will show a syntax error on the command. Confusing? – Icefishingmn Mar 01 '20 at 15:30
  • ***the logfile will show a syntax error on the command.*** - add it to your question. – Pedro Lobito Mar 01 '20 at 15:31

0 Answers0