0

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!"
5
  • The real question here is why you are mixing python and php.
    – e4c5
    Mar 14, 2017 at 4:08
  • @e4c5, pure ignorance. Mar 14, 2017 at 4:17
  • Try to run the script with supper user
    – Arun
    Mar 14, 2017 at 4:17
  • please stick to python if you are not familiar with php. Python has it's built in webserver which is adequate for this sort of thing. If you want more there is Flask,webapp2 and django. There isn't a need to complicate matters by mixing php and python
    – e4c5
    Mar 14, 2017 at 4:20
  • @e4c5 Thanks for the suggestions, didn't know those where available. Will try tomorrow. Mar 14, 2017 at 4:29

1 Answer 1

0

Short answer:

The problem is likely in access rights to dev node. Web server is operating under a different user ID with a very restricted rights, and it is so for a very good reason. Also, you can not just chmod 777 /dev/ttyACM0 because every time you restart the system it will be reset (seriously, don't do it because exec bit on a device node is potentially unsafe). Instead, you need to write a udev rule, which will probably take some time to figure out how.

Long answer:

Linux has a magic component, udev, which mounts device nodes from sysfs, something accessible from kernel, into devfs, the userspace file system accessible to you as /dev. I.e. udev will decide whether to mount a device into devfs and what access rights to assign

To make these decisions, udev is provided with a set of rules (aka udev rules) written in configuration files located in /lib/udev/rules.d/ (might vary by distribution). Here is a list of rules that affect serial devices on Ubuntu:

/lib/udev/rules.d/50-udev-default.rules
/lib/udev/rules.d/60-persistent-serial.rules
/lib/udev/rules.d/77-mm-usb-serial-adapters-greylist.rules

Rules are processed in the increasing order, so the 50 one will have precedence. You can also add custom rules under /etc/udev/rules.d/, probably starting it with something 90+ will be a good idea.

Finally, the answer: create a file named (e.g.) /etc/udev/rules.d/90-my-serial-dev-accessfix. It should contain something like this:

ACTION=="remove", GOTO="accessfix_end"

SUBSYSTEM=="tty", KERNEL=="ttyACM[0-9]*", GROUP="tty", MODE="0666"

LABEL="accessfix_end"

I have no means to test this right now, so there is likely to be an error or two in this config. However, it definitely worth reading about udev rules to fix it.

3
  • Never suggest chmod 777 to newcomers, they have no idea of the impact.
    – Klaus D.
    Mar 14, 2017 at 5:09
  • @KlausD. Added an extra note
    – Marat
    Mar 14, 2017 at 5:10
  • @Marat Thanks for this response. I'm at the point now where I understand just enough to get myself into trouble with a lot of stuff, but I think this is the response I was looking for. I'll get into it tomorrow as I just finished for the night. Mar 15, 2017 at 4:20

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.