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]

smottt
3,33011 gold badges41 silver badges47 bronze badges
asked Feb 28, 2015 at 10:36
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()
answered Feb 28, 2015 at 11:51
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

answered Feb 28, 2015 at 10:52

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

answered Mar 3, 2015 at 1:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.