0

This is probably a Linux question, but concerns specific Pi code. I am using the DHT11 class from http://abyz.me.uk/rpi/pigpio/download.html and have written a program which uses this.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import, division, print_function, unicode_literals

import time
import pigpio
from dht11 import  DHT11

pi = pigpio.pi()
sensor = DHT11(pi, 4)
d = sensor.next()
print("temperature: {}°C".format(d['temperature']))
print("humidity: {}%".format(d['humidity']))
sensor.close()

The pigpiod daemon runs on boot. The code is installed in /usr/local/bin/

-rwsr-sr-x 1 root staff 9284 2018-05-13 16:34 dht*
-rwxr-xr-x 1 root staff  440 2018-11-08 10:13 dht11_once.py*
-rw-r--r-- 1 root staff 4674 2018-11-08 10:13 dht11.py

The program can be run by any user (even unprivileged users) but I cannot seem to get it to work from php using the following:-

<?php $cpu_temp = (float) file_get_contents('/sys/class/thermal/thermal_zone0/temp'); ?>
<?php $hostname = shell_exec('/bin/hostname'); ?>
<?php $hostIP = shell_exec('/bin/hostname -I'); ?>
<?php $temperature = shell_exec('/usr/local/bin/dht11_once.py'); ?>
<html><head><title>RPi PHP Test</title></head>
 <body>
 <h1>Hello from the Raspberry Pi - <?php echo $hostname ?></h1>
 <div>Your IP address is: <?php echo $_SERVER['REMOTE_ADDR']; ?></div>
 <div>Pi IP address is: <?php echo $hostIP ?></div>
 <div><?php echo $temperature ?></div>
 <div><h3>The CPU temperature is: <?php echo $cpu_temp/1000 ?>°C</h3></div>
 </body>
</html>

This is probably a permission problem, but what would be needed eludes me. Is it even possible to get a python program to run GPIO access code this way? (The dht program is compiled c code, which does run, but has other issues.)

Update The code above does run and does call '/usr/local/bin/dht11_once.py' but does not appear to call the code that interfaces with the GPIO.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.