My goal is to write a string through the serial of my Raspberry Pi which will then be sent towards an Arduino and I'll read it through the Serial Monitor.

I got my Python script working and it works perfectly when I execute it on the Raspberry Pi's terminal. As soon as I run it through the website, it does not work.

To test it out, I made the PHP code run the script as soon as the site was launched. This is what the code looks like:


    exec('/usr/bin/python /var/www/UART3.py test');


The test at the end of the exec() sends that string through the serial communication. I can copy and paste that code into the terminal and it works perfectly.

Things I have done:

  • Tested the code individually (works)
  • Gave rights to www-data by doing chmod +x UART3.py

Any ideas?

  • Can you execute anything from the exec function?
    – user67191
    Jul 9, 2017 at 22:27
  • What is the output of ls -l UART3.py?
    – user67191
    Jul 9, 2017 at 22:33
  • @yambo1 the ls -l UART3.py reads -rwxr-xr-x. And yes I normally can execute things with exec function. Just now i redid my entire code in C, compiled it, and ran that through through the website using the same exec function and it works perfect. Now I'm curious as to why it worked when I did it with the C compiled file as opposed to the python file.
    – Rayaarito
    Jul 9, 2017 at 22:37
  • What is the output of echo exec('/usr/bin/python /var/www/UART3.py test'); in your php file
    – user67191
    Jul 9, 2017 at 22:48
  • @yamboy1 it doesn't output anything. The php is the in the header of my HTML file. If i put echo exec( 'chmod +x' ) it doesn't change anything or show anything on the website. Am i supposed to type that into a specific program? Just to be clear, I'm doing all of this headless on my Raspberry Pi Rasbian Jessie and then checking the results out via website and Arduino serial monitor
    – Rayaarito
    Jul 9, 2017 at 22:50

1 Answer 1


The answer was found thanks to @yamboy1 and finding this link: How to set a permission to call a Python script from PHP?

Basically, I had to give the correct privilege to the device itself. Instead of doing sudo chmod 777 /dev/ttyUSB0 like @Diego Dyan did, i did sudo chmod 777 /dev/ttyAMA0

So for people in the future, things to check:

  • Does your program work individually? Compile it and run it by itself to check.

  • Does your file have the correct privilege? In my case, the python code. If not, do sudo chmod +x file.py

  • Does your device have the correct privilege? If not, do sudo chmod 777 /dev/ttyAMA0
  • Nice summary. However, it doesn't explain why your c code worked but the python version didn't. How did the c code access the device without the permissions set correctly? Jul 10, 2017 at 11:17
  • 1
    @DaddytheRunner , that I couldn't figure out. If anyone would like to spread the knowledge, please do.
    – Rayaarito
    Jul 10, 2017 at 14:57

Your Answer

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

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