I'm having a problem with a project. I need to do a detection system which uses python to communicate with electronic devices. I'm creating a detection system. The problem is that I want to detect and then send to a php file which serves as my user interface.
Python:
if led is on, send on to php,
if led is off, send off to php,
PHP:
display [value receive from python]
-
1How are these working together? A PHP application is typically very short lived and cannot really be "talked to" from a longer running process like Python. Perhaps you just want to store the value somewhere and read it from PHP when needed?deceze– deceze ♦2015年02月28日 12:14:36 +00:00Commented Feb 28, 2015 at 12:14
-
that might be true..maybe if i can save the value somewhere and retrieve it whenever i want that will be great..but i'm totally lost in php..can i do that with simple coding..?syafiqul haziq– syafiqul haziq2015年02月28日 14:34:04 +00:00Commented Feb 28, 2015 at 14:34
-
1Simple: a database. Maybe MySQL, maybe Redis, maybe just memcached, maybe just a plain file. All these things are easy to write to from Python and read from in PHP.deceze– deceze ♦2015年02月28日 15:09:12 +00:00Commented Feb 28, 2015 at 15:09
3 Answers 3
If you want to call php script directly:
php code:
<?php
$state = $argv[1];
echo $state;
?>
python code:
from subprocess import *
#ledstate='on'
p = Popen(['/usr/bin/php','<php file name>',ledstate],stdout=PIPE)
print p.stdout.read()
If you want to call via server:
php code:
<?php
$state = $_GET["led"];
echo $state;
?>
python code:
import urllib2
#ledstate='on'
req = urllib2.Request(url='http://example.com/<php file name>?led=%s' % ledstate )
f = urllib2.urlopen(req)
print f.read()
Comments
You can use subprocess.check_call, to call the php command passing the variable from python:
from subprocess import check_call
check_call(["list","of","php" ,"commands","off/on"])
If you want to store the output use check_output
Comments
now i already got a way to retrieve data form python using php..
<?php
$result = exec("sudo python /home/pi/detect_led.py");
$resultData = json_decode($result);
print $resultData;
?>
but the problem is..the data is slow..i mean..when there's changes i have to refresh 2 time for it to change its value..so is there any where i can change to make it correct..?