I'm trying to switch a relay, connected via USB to RPi, using a PHP command. I'm using an additional USB-8-relay-board, next to all available GPIO-pins on the RPi.
I'm able to switch both (all 8) USB-relays on the board, together with (all 28) GPIO-connected-relays on GPIO-relay-boards, via only 1 Python script.
The relevant Python instructions in test.py
-file are:
os.system("gpio write 25 1")
os.system("usbrelay HW554_1=1")
This works all fine when called directly via python3 test.py
However, when I use a PHP script (to address the action via a website), using the PHP instruction:
$command = escapeshellcmd('/usr/custom/test.py');
$output = shell_exec($command);
echo $ output;
the results is the (...I think) complete execution of the PHP-script, where on the RPi, via Python, only the instruction os.system("gpio write 25 1")
is effecively executed (relay is switching), whilst the instruction os.system("usbrelay HW554_1=1")
seems to be executed (since extra time consumption is noticed, when using time.sleep()
commands before and after the os.system()-instruction)
, but this doesn't result actually in a switch of the USB-relay. I've also tried to use a bash-shell-file test.sh
(with execution-rights) instead, which wasn't the solution.
How to address a USB-relay via PHP & Python & RPi, whilst it is working directly via Python & RPi?
#!/usr/bin/env python
import os
import time
os.system("gpio write 25 1")
time.sleep(1)
os.system("gpio write 25 0")
time.sleep(1)
os.system("usbrelay HW554_1=1")
time.sleep(1)
os.system("usbrelay HW554_1=0")
<?php
$command = escapeshellcmd('/usr/custom/test.py');
$output = shell_exec($command);
echo $output;
>?