This solution is the “heavy way”, since with php you have to load all the page each time you want to refresh the measured value displayed.
copy index.php and get_sense.py in /var/www/html
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>RPI Apache-php Sensor App </title>
</head>
<body>
<hr> <!---------------------------------------------------------------->
<h2>Control GPIO4 LED</h2>
<form method="get" action="index.php">
<input type="submit" style = "font-size: 14 pt" value="OFF" name="off">
<input type="submit" style = "font-size: 14 pt" value="ON" name="on">
</form>
<?php
shell_exec('gpio -g mode 4 out');
if(isset($_GET['off']))
{
echo "LED is off";
shell_exec('echo LED OFF');
shell_exec('gpio -g write 4 0');
}
else if(isset($_GET['on']))
{
echo "LED is on";
shell_exec('gpio -g write 4 1');
}
?>
<hr> <!---------------------------------------------------------------->
<h2>Get Measured data </h2>
<?php
if ($_GET){
$temp = shell_exec('sudo python3 /var/www/html/get_sense.py');
}
?>
<form NAME = "sense" Method = "GET">
<button name = "ask"> ask </button> <br/> <br/>
<div id='log' > Temperature: <?php echo $temp; ?> °C</div>
</form>
<hr>
</body>
</html>
#! /usr/bin/python3
import sys
from sense_hat import SenseHat
sense = SenseHat()
def main():
t = sense.get_temperature();
t = round(t,2)
print(t)
if __name__=="__main__":
main()