I connected an Arduino Nano via USB to my Raspberry PI3. The Arduino is programmed to send (via serial) 16 bytes of information every 500 milliseconds. In this RPi3 I have Python, Apache2 and PHP5 installed on my Raspbian Jesse, so I wrote the following code and save in var/www/html/ler.py :

import serial
comunica = serial.Serial('dev/ttyUSB0', 9600)
print comunica.read(16)

And then when I type in the command line:

python ler.py

I have the perfect response from the script: it shows me 2.502.502.505.00 that is the analogRead from four potentiometers in the Arduino.

The trouble starts when I try to do the same thing via browser. I did create a page named index.php and add the following code in it:

system('ls -l');
system('python ler.py');

When I access the page, it return the content of the directory (ls -l) but doesn't return the values from the command "python ler.py"

In the command line works fine, but not in PHP.

I think there is something to do with the permissions, Apache runs over the www-data user and this scripts needs to access the dev ttyUSB0.

Does someone experience a similar situation?

  • is the phython script executable? execute permissions should for the apache user ( or php) – Joe Platano Jul 19 '16 at 17:38
  • I set the python script executable with: sudo chmod +x ler.py but doesn't work anyway. But I think that is the crucial point of the question: What permissions do I need to set? – Diego Dyan Jul 19 '16 at 23:55
  • set ownership the script to the www-data group chgrp www-data ler.py and the permissions to read + execute for the group – Joe Platano Jul 20 '16 at 0:12
  • Thanks, but the www-data user execute any other simple python scripts. The problem was accessing the device. Thanks a lot. – Diego Dyan Jul 20 '16 at 0:15

Turns out the the answer is to give the correct privilege to device itself.

I set total permissions for any group/user throug the command:

sudo chmod 777 /dev/ttyUSB0

To left no doubt, I set the ler.py script executable too:

sudo chmod +x ler.py

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.