1

I have a raspberry with 2 php pages in /var/www, one is led1off.php the other is led1on.php. I also have 2 Python scripts in cgi-bin led1off.py & led1on.py

led1off.py

#!/usr/bin/env python 
Import serial
ser0 = serial.Serial('/dev/ttyACM0'), 9600)
ser0.write('2')

led1on.py

#!/usr/bin/env python 
Import serial
ser0 = serial.Serial('/dev/ttyACM0'), 9600)
ser0.write('1')

--

led1on.php

<?php
exec('sudo -u www-data python /usr/lib/cgi-bin/led1on.py')
?>

led1off.php

<?php
exec('sudo -u www-data python /usr/lib/cgi-bin/led1off.py')
?>

What should happen in theory is when I load http://192.168.0.2/led1on.php The php script should run its code in the terminal so that it executes led1on.py. Then led1on.py should send "1" to the arduino which turns on an led. Similar thing goes for led1off.php.

The thing is I am able to type

sudo -u www-data python /usr/lib/cgi-bin/led1on.py

In the terminal, and when I do it, the led on the arduino turns on. So the code on the arduino is correct, there is comunication between the 2, and the Python code is correct. The problem is that it doesn't work when I load the php from a browser. Am I doing something wrong? Do I need to give special permissions to www-data to send serial data?

4
  • have you tried serial from CLI ? to see you are entering the correct details Commented Jan 30, 2016 at 12:15
  • have you tried making the connection without php scripts from ur rasp pi? Commented Jan 30, 2016 at 12:24
  • I have tried typing: "sudo -u www-data python /usr/lib/cgi-bin/led1on.py" in the terminal and it works. But it doesn't work if I load it from the php Commented Jan 30, 2016 at 12:29
  • so you have typed that command from your rasp? you have made a serial connection from your PI to your ADRino? Commented Jan 30, 2016 at 12:34

1 Answer 1

1

To run command as Super user www-data should be in /etc/sudoers - could you check if it is there?

Similar question was asked here: sudo in php exec()

As we found during discussion, this code will run correctly:

<?php
$command = escapeshellcmd('sudo /usr/lib/cgi-bin/led1on.py');
$output = shell_exec($command);
echo $output;
?>

Hope this helps.

Sign up to request clarification or add additional context in comments.

7 Comments

I already added this line in sudoers: www-data ALL=(ALL) NOPASSWD:ALL
Are you logged in as www-data? Could you use whoami command to confirm this? Also, maybe you need to set up also group permissions as in this thread: askubuntu.com/questions/192050/…
If i type "whoami" in the terminal it sais I am "pi". If I ask the php page to print it, it sais I am "www-data"
Maybe you don't need to provide -u www-data in the command when PHP already uses the www-data account? As pi it's fine in the terminal, but in PHP maybe it's not needed?
Please set up also a group for www-data and NOPASSWD:ALL for the group in /etc/sudoers
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.