Real world control from a Web page

In "Acquiring data from the physical world" we examined how to use a microcontroller to turn physical events (such as reading a switch) into data and send that data to a Linux box via the serial line. With this capability you can do things like counting people passing through a gate or recording how the temperature changes over time in a factory process. You can then send the data to a Web page. In this article, we'll do it in reverse -- from a Web form, instruct a microcontroller to perform some actions.

You can create a Web form on an Apache/Linux server, process the data with a PHP script, then push the control instructions down to the microcontroller over the serial line. The microcontroller will run code that interprets the data from the serial line and instruct the I/O pins to perform some action. Basic applications might be to turn on lights, relays, or motors from a remote browser screen.

Build the Web form and PHP back end

The HTML code below presents a bare-bones Web form. It prints a label and then supplies a little fill-in box for data. The "Submit" button is the standard way of processing the form. I arbitrarily chose "2" to turn on the LED (attached to the microcontroller prototype board). Any other character turns the LED off.

To test this scheme, I used a $60 ZX-24 microcontroller from Elba Corporation. The "Stamp-like" micro is more than double the speed of the Netmedia BX-24 I used for earlier tests, has more space for data, and is pin-compatible with BX-24. The sample microcontroller source code included with this article compiles and runs, without modification, on either the BX-24 or the ZX-24 module.

As with the Netmedia device, the ZX-24 uses a compiler that runs under Windows 98. I was unsuccessful at getting the Zbasic compiler working with Wine on my Athlon 64/SUSE laptop, so I had to again use a spare 200MHz Pentium desktop machine for programming. This ancient machine is more than fast enough to run the ZX-24 or BX-24 compiler.

I used a 2x6-inch white plastic prototype breadboard to secure the ZX-24, the other support parts (voltage regulator, resistor, and a switch), and the connection points for the external wires of the serial cables. I used the same pair of serial cables and the 5.5-volt, 950 ma. power supply from the previous project. (See diagram.)

As this was a simple educational project, all I wanted to do was turn on an LED. You could easily expand the circuits to turn on relays, motors, and lights.

I used the Zbasic editor/compiler to create a sample program that reads data from the serial port. The main Do loop first checks for data in the buffer, then uses an If/Else statement to determine if a "2" (binary 50) has been received. If it has, the pin (16 in this case) is turned on, lighting up the LED. Any other character turns the pin off, as the microcontroller cycles through its main loop.

Go exercise some control

This procedure represents a basic template that you can use to create your own Web-enabled system. My intention was to show how you could take off-the-shelf parts and build something quickly without a lot of fuss.

For more robust applications, you'll want to consider adding security features to the HTML/PHP code. Using a sensor on the microcontroller and sending an acknowledgment back to the Web page will verify that the action you wanted actually took place.

The Apache/PHP/Linux and microcontroller combination is a natural for test sets, data logging, and remote Web-enabled control systems. You could even use your new Wi-Fi and browser-enabled cell phone as an microcontroller user interface. Put your imagination to work and go control something.

Rob Reilly is a consultant, trend spotter, and writer. He can advise you on your data acquisition projects, along with, portable computing and presentation technology integration.