I have a dual H bridge connected to my Pi for controlling 2 brushed DC motors with.

I intend to have a web interface to control the speed of the motors. I'm thinking of a HTML page submitting AJAX requests to a PHP file. I'm a web developer so I've already installed Nginx and PHP on the Pi.

My problem is that I want the motors running most of the time, the AJAX requests are to change the motors speed and direction. This doesn't seem suited to a PHP file as it's designed for the HTTP request-response cycle, a single PHP script is not designed to execute indefinitely waiting for input.

What's the best approach to solve this? Will I need to have a Python script running in the background generating PWM and have some sort of communication between the python script and the PHP file? Is this possible?

  • Why do you need the php? Why not just let the python handle the web requests?
    – Bex
    Commented Jan 7, 2016 at 14:54
  • 1
    @Bex Are python scripts run via apache to respond to web requests persistent? In what form? Are you asking the OP to learn to use a framework? If they are not persistent (e.g., cgi), then what difference does it make? Doesn't that amount to saying, "Why don't you use my favorite technology X instead?". Should we all come on and chime in with that?
    – goldilocks
    Commented Jan 7, 2016 at 15:02
  • @goldilocks Fair point, but do cool off a bit. What I meant with my comment was that if python is used to control the motors, then the PHP layer between apache and the controlling python script seems awkward.
    – Bex
    Commented Jan 7, 2016 at 16:11
  • Sorry for being snippy, I wanted to skip a few steps ;) I understand what you are saying, but the interface here is going to be so simple any awkwardness is imaginary. IPC is language agnostic as is networking generally. If you want to serialize (data) objects, there are protocols like json, which both php and python will deal with easily and transparently. The only advantage is if you want to use the same code (classes etc.) on both sides, but that is probably not the case here -- in which case the only reason to use the same language on both sides is personal preference.
    – goldilocks
    Commented Jan 7, 2016 at 16:27
  • 1
    Sorry for not noticing you actually wrote nginx here, not apache, although I think similar logic applies. If you haven't started yet you might as well learn server side python, I doubt it is any harder than PHP. So I've come round to supporting @Bex in the end ;)
    – goldilocks
    Commented Jan 7, 2016 at 17:37

1 Answer 1


Will I need to have a Python script

You do not need python, you just need something with a module that wraps one or more of the following C libraries:

  • pigpio
  • wiringPi
  • libbcm2835

All of which provide PWM for at least the hardware driven pin(s). Python for has wrappers for all three. PHP has one for wiringPi and may have ones for the others. However, while the underlying libraries are I think reasonably well maintained, sometimes the wrappers are half-complete and/or abandoned. Since python is probably the most widely used method, it is probably the safest bet other than C.

a single PHP script is not designed to execute indefinitely waiting for input

Not necessarily -- PHP scripts can be executed in the same manner as python -- but in an apache context you are correct, they are not persistent.

have some sort of communication

This is called "inter process communication" (IPC). There are all kinds of ways of doing this. Probably the most common and versatile is via sockets. You would create a persistent local server process and send it requests from the web server script. This is a form of daemon.

I know that pigpio already has such a daemon, so the easiest thing to do would probably be to investigate that; it is likely capable of meeting your needs since there is an explicit reference to PWM on that page. It also has a pipe interface (aka. a fifo, for "first in first out") in addition to a server socket, which will be easier if you do not know how to use the latter.

  • 1
    There is a pigpio CGI example using Python at abyz.me.uk/rpi/pigpio/examples.html#Python_pigpio_cgi_py The example is for Apache2 but any other web server (e.g. lighttpd) would do just as well.
    – joan
    Commented Jan 7, 2016 at 14:56
  • Am reading through your links now, am I right in thinking by "pipe interface" this is the same thing as linux command line pipes like "cat /dev/null > /home/pi/file"? I thought pipes could only send data to a program when it starts executing, not after the program has already been running some time?
    – Ralph
    Commented Jan 7, 2016 at 16:53
  • 1
    Technically, > is redirection to a file and | is piping to another process. However, daemon processes commonly use named fifo pipes, which are file nodes, in which case you would use > (i.e. redirect to a pipe). For pigpiod, that file node (beware pipes are not files in a normative, static sequence of stored bytes sense!) is /dev/pigpiod as documented here. There's also a description of a command line client (pigs) which can be used to communicate with the daemon via its socket (meaning, you don't have to think about the socket).
    – goldilocks
    Commented Jan 7, 2016 at 17:11

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.