UscCmd in Linux Apache2 Websocket or PHP and shell scripts

I am attempting to control servos attached to a USB connected Micro Maestro 12 with a Javascript / PHP webpage served by an Apache2 web server running under Debian Linux on a Raspberry Pi B+. I researched communication with the Maestro using the Apache2 Websocket (readme attached), installed the Websocket code and got it to initialize with the Apache2 server. Further development requires a message handler for UscCmd using the Websocket facility that would be called when the Apache2 server initializes. The handler is specified in the Apache.conf file and a UscCmd handler might be implemented with the following statements.

websocket_module   /usr/lib/apache2/modules/
<IfModule mod_websocket.c>
  <Location /whatever-location>
  SetHandler websocket-handler
  WebSocketHandler libexec/apache2/ UscCmd_init
  MaxMessageSize 16384

Having not written raw code since my Fortran 4, Assembler and Cobol days (I stuck to the scripting languages), I am at a loss as to how to accomplish such a feat. I have looked at the Micro Maestro SDK and it seems to me that all the pieces are there to put a robust Apache2 message handler together for UscCmd and the Micro Maestro. So perhaps one of you gurus would take on the challenge that could benefit all of us Micro Maestro users wanting a simple webpage interface.

Barring a Websocket interface, I am able to command the Micro Maestro servos from the Debian command line with UscCmd and have written some .sh shell scripts that pass variables for scripts that initialize the servos and set target and speed settings. Now I am attempting to execute the scripts from a PHP script called by a Javascript webpage, but I’m not having much luck, the .sh scripts work fine from the CLI, but not from the page. Perhaps I don’t have the .sh shell scripts in the correct library location.

Below are some samples. Lots of code in lots of places, a Websocket interface would be so much cleaner.

The .sh shell script:

# This is a comment!
# Sends a Set Target command to a Pololu Maestro servo controller
# DEVICE is the Device number
# CHANNEL is the channel number
# TARGET is the target in units of quarter microseconds.
# The Maestro must be configured to be in USB Dual Port mode.
echo attempting to run UscCmd 
/home/pi/maestro_linux/UscCmd --device $1 --servo $2,$3
# This is a comment, too!

The PHP script that is supposed to run the shell script and pass variables:

<title>Shell Script Test</title>
<body><h1>Does the PHP work?</h1>
<p>This is a test web page for manipulating the Micro Maestro with PHP.</p>
shell_exec("./cgi-bin/ 00096224 2 6312")

As I said, the .sh scripts work fine from the CLI, but not from the web/PHP page. Perhaps I don’t have the .sh shell scripts in the correct library location or I am using the wrong PHP command.

THANKS in advance.
Apache2 Websocket Readme.txt (10.2 KB)


It is possible PHP cannot find the directory of your scripts. You could try writing a directory listing in PHP using diropen() and readdir() and open it via web, so you can verify that Apache can view your scripts. If they exist, then it might be the Apache’s php.ini file disabling the use of shell_exec(). You can enable the function by opening the php.ini file and removing shell_exec from the disable_function list (if it is listed) and try restarting the web server.

You might try looking at the Apache logs to see if there is an error message.

By the way, I added [code] tags in your last post to make it easier to read; please use this method to post code in the future.

- Amanda