1

I have Apache server running on my pi. I send a httprequest from my Java app to the server to turn the motor on and off by executing a pyhton script. PHP code on the server:

<?php
 if(isset($_POST["on"])){
 echo "turning on";
 exec("python /home/pi/motor/on.py /dev/null/ 2>/dev/null/ &")
}

if(isset($_POST["off"])){
 echo "turning off";
 exec("python /home/pi/motor/off.py")
}
?>

I think my problem is when turning the motor on, my application freezes as the exec call has not returned yet (because my python code is in a loop and can only exit when interrupt arrives). I want to be able to turn the motor on and off, but I can only turn it on. Any solutions for this problem or another way of achieving what I want to do?

1
exec("python /home/pi/motor/on.py /dev/null/ 2>/dev/null/ &")

There's a > missing. To redirect the standard output, it must read

exec("python /home/pi/motor/on.py >/dev/null/ 2>/dev/null/ &")

If you don't redirect both the standard output and standard error channels, php's exec() waits for completion, regardless of an &.

  • hmm this worked anyway. Thnaks. – Casper2210 Jan 30 '18 at 16:04
  • It's the same for you "off" script. You have to redirect standard output and standard error. – Janka Jan 30 '18 at 16:14
1

If you migrate your web server from php to flask, you can give the motor command natively. Make routes as @app.route('/motor-on') and ('/motor-off'), with their respective python commands, then trigger the motor control with ajax request on button click (java script). TIP: Use gpiozero library, it is super easy and effective.

  • The thing is it has to be a Java app controlling the motor. Can flask work with java? – Casper2210 Jan 30 '18 at 16:02
  • [speculative] yes i guess. – Abbas Kagdi Jan 31 '18 at 4:43
  • Assuming that you are using Java to read a web server response, then it is recommendable to simplify your setup to either Python or PHP. Partially, that is a matter of preference and skill. However, the Raspberry community is producing a lot of Python libraries which makes PHP a bit more difficult choice. To state in another way: Java sees web pages as text, so as long as your server produces text in a way understood by your Java app, the technology behind the curtain does not matter. – ferrix Feb 20 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.