I have a simple Python (sockets.py) module which operates some Energenie sockets. See below. Note the print statements are simply there as a basic 'breakpoint' function so I can see how far the module runs.
When run from terminal, or from the Thonny editor it executes just fine.
When called from a simple PHP script it simply stops at line 'import energenie as e' and Apache reports an error (also below).
The PHP script (also below) runs without errors and appears to call the sockets.py module just fine, and 'import time' also fine.
The owner/group of both the PHP script and the Python module are root:root. The files are located in var/www/html folder.
sockets.py is as follows:
print('Hello, world! This proves that the call to Python is correct !!')
import time
print('This proves sockets is running past import time')
import energenie as e
print('This proves sockets is running past import energenie')
e.switch_on()
time.sleep(2)
e.switch_off()
time.sleep(2)
e.switch_on(1)
time.sleep(2)
e.switch_off(1)
e.switch_off(2)
print('This proves sockets is running')
index.php is as follows:
<html>
<head>
<title>Python test</title>
</head>
<body>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post"><input name="on" type="submit" value="On"></form>
</body>
<?php
ini_set('DISPLAY_ERRORS',1);
if (isset($_POST['on'])){
echo shell_exec("python /var/www/html/sockets.py");
}
?>
</html>
The Apache Error is as follows:
Traceback (most recent call last):
File "/var/www/html/sockets.py", line 1, in <module>
import energenie as e
File "/usr/local/lib/python2.7/dist-packages/energenie/__init__.py", line 2, in <module>
from .energenie import switch_on, switch_off
File "/usr/local/lib/python2.7/dist-packages/energenie/energenie.py", line 16, in <module>
GPIO.setup(BIT1, GPIO.OUT)
RuntimeError: No access to /dev/mem. Try running as root!
I am a newbie at Raspberry Pi and Python, and would really appreciate some help please.
All the best
Colin