2

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; 
>?
Ilko
  • 1,288
  • 1
  • 14
  • 19
Mike123
  • 21
  • 1
  • Following further investigation, the issue isn't related to the USB-relay-board itself. I'm able to switch GPIO & USB relay using 1 PHP-script. The issue is (most likely) access-rights. Executing a PHP-script via php -f testusb.php switches both types of relays (returned info: user:PI and current user:root), whereas execution of PHP via browser still switches only GPIO relays, not USB-relays (returned info: user:www-data and current user:root). Grateful if anyone could give me an idea how to execute this PHP script with the correct user ('pi' instead of 'www-data') ? TIA. – Mike123 Feb 14 '19 at 14:27

0 Answers0