5

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]

3
  • 1
    How 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? Commented 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..? Commented Feb 28, 2015 at 14:34
  • 1
    Simple: 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. Commented Feb 28, 2015 at 15:09

3 Answers 3

4

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()
Sign up to request clarification or add additional context in comments.

Comments

0

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

0

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..?

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.