I'm a beginner currently working on a project using a raspberrypi3 with a LAMP stack, and an arduino with some sensors for recording telemetry.
I'm stuck right now, as my Python script to import data into MySQL can connect to /dev/ttyACM0 when triggered within the shell, and works perfectly. When triggered system(), popen(), or exec() in PHP it fails to connect to the serial port.
I know that the script is running because in the PHP output, it shows the message with failed to connect to /dev/ttyAMC0
This is my first time ever using PHP, so it might be something really dumb.
Currently I've referenced these sites for my current problem:
Calling Python in PHP
Execute PHP function with onClick
PHP - Command Line
Here is the code from my index.php:
@charset "utf-8";
body {
 background-color: rgba(233,233,233,1.00);
 font-family: Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif;
 font-size: x-large;
}
h1 {
 color: rgba(233,233,233,1.00);
 background-color: rgba(55,55,55,1.00);
 padding-top: 5px;
 padding-right: 5px;
 padding-bottom: 5px;
 padding-left: 5px;
}
.button {
 display: inline-block;
 padding-top: 30px;
 padding-right: 30px;
 padding-bottom: 30px;
 padding-left: 30px;
 background-color: rgba(55,55,55,1.00);
 color: rgba(233,233,233,1.00);
 text-align: center;
 margin-top: 10px;
 margin-right: 10px;
 margin-bottom: 10px;
 margin-left: 10px;
}
<!doctype html>
<?php
function start_recording(){
 system("./getdata.py");
}
function stop_recording(){
 popen("pkill python");
}
if (isset($_GET['start'])) {
 start_recording();
}
if (isset($_GET['stop'])) {
 stop_recording();
}
?>
<html>
 <head>
  <meta charset="utf-8">
  <title>Telemetry Computer</title>
  <link href="/assets/stylesheet.css" rel="stylesheet" type="text/css">
 </head>
 <body>
  <h1>Telemetry Box OS v1.0.0</h1>
  <h4>by Jake Johnson</h4>
   <a href="?start=true">
    <div class="button">Start Recording Data</div>
   </a><br>
   <a href="?stop=true">
    <div class="button">Kill Recording</div>
   </a><br>
   <a href="/data">
    <div class="button">View Data</div>
   </a><br>
   <a href="/manage">
    <div class="button">Data Managment</div>
   </a>
   </body>
</html>
and here is my python:
#!/usr/bin/python
import serial 
import MySQLdb
#establish connection to MySQL. You'll have to change this for your database.
dbConn = MySQLdb.connect(host="localhost", user="ayylmao", passwd="ayylmao", db="telemetry") or die ("could not connect to database")
#open a cursor to the database
device = '/dev/ttyACM0' 
try:
  print "Trying...",device 
  arduino = serial.Serial(device, 9600) 
except: 
  print "Failed to connect on",device     
while True:
  cursor = dbConn.cursor()
  try:
    print("---------  Inserting Data ---------") 
    data = arduino.readline()  #read the data from the arduino
    pieces = data.split("\t")  #split the data by the tab
   #insert the data into the Database
    try:
      cursor.execute("INSERT INTO telemetryData (runTime,tempF,rpm,xforce,yforce,zforce) VALUES (%s,%s,%s,%s,%s,%s)", (pieces[0],pieces[1],pieces[2],pieces[3],pieces[4],pieces[5]))
      dbConn.commit() #commit the insert
      cursor.close()  #close the cursor
    except MySQLdb.IntegrityError:
      print "failed to insert data"
    finally:
      cursor.close()  #close just incase it failed
  except:
    print "Failed to get data from Arduino!"