Eric David Blog

Making PHP control the world (via interfacing with LCD screens, sensors, motors, etc.)

Yup, it can be done. PHP can be interfaced to monitor what cycle your washing machine is in and display it on your web site. PHP can be made to display RSS data on a small LCD display mounted in a drive bay. PHP can change the background color of your web site to represent the brightness level of the lights in your room or the temperature inside your computer case. PHP can web-enable your air conditioner, allowing you to turn it on from work before leaving the office.

What it takes is knowledge of the PHP Direct IO functions (PECL), programming a microcontroller, building an RS232 interface and setting up the desired electronic elements (LCD, relay, etc.) Before you keep reading, let me warn you that this is not a detailed how-to but rather a road map for your inspiration. Projects of this nature may not be for the faint-of-heart, but the successful implementation will leave you gleeful.

First, order a Maxim-IC MAX232 chip and the handful of capacitors that it requires along with a serial plug connector. The MAX232 will be needed for any project that connects a computer's serial port to the 5-volt lines found in most electronics. Your motherboard hasn't had a serial port since Y2K you say? In that case you will also need a USB-to-RS232 adapter.

Next, you will need a microcontroller, chip-programmer and a power source. My choice of microcontroller family is the Microchip PIC series and programmed with the In-Circuit Debugger 2 (ICD2), but you could just as well use any programmable microcontroller. You can use the five-volt rail of a computer power supply as the power source, but I prefer to just build my own from a "wall-wart" AC-to-DC converter (any wall-wart outputting between 6 and 12 volts DC will do,) 7805 voltage regulator and two smoothing caps, thus leaving me substantially more desk space.

You will also need the interfacing hardware; there are many sources online for surplus sensors, LCD screens and relays. Those are the base components for the projects mentioned earlier.

Before discussing interfacing your washing machine, I must first mention that most large electrical machines contain powerful capacitors. Capacitors are like rechargeable batteries except that they build a charge and release the charge extremely rapidly, measured in milliseconds. Additionally, an array of capacitors, each of which may be smaller than a AAA-battery, can contain a charge powerful enough to kill a person and maintain that charge for days after the device has been unplugged. Additionally, shorting a large capacitor can make it explode. If you do not know how to safely discharge them then stick to the battery powered projects.

The easiest way to sense the cycle of a washing machine is to simply unscrew the facade over the control panel and attach a transistor to each of the LEDs that display the status and then connect the transistor to the microcontroller. If the washing machine uses small incandescent lights instead of LEDs then you would be wise to use either an optointerrupter or relay between the lights and your circuit, isolating any hazardous voltages. Now, program your microcontroller to output a serial representation of the light statuses and output the serial signal to the MAX232 which connects to the serial port of your computer. Next, set up PHP to read the serial info with Direct IO and display it on your web site.

For RSS feeds you can do something similar, but in reverse. Set up a cron job (or Windows Scheduler event) to make a periodic hit to your PHP page that pulls the RSS feeds in. That page then converts RSS headlines to serial text and sends them to your serial port, through the MAX232 and then into the microcontroller. The microcontroller then saves the data to its RAM and updates an LCD display (with parallel controller) that you have attached to the microcontroller. Parallel LCD controllers will require six lines of your microcontroller; technically, you could buy an LCD with serial controller and skip the microcontroller altogether, connecting directly to the MAX232. While a serial LCD would be less effort, the price for one is typically $30 while a surplus parallel LCD is just $4 and PIC microcontrollers start at under $2. The nice thing about mounting the LCD into a drive bay is that the whole circuit can then be powered by the computers power supply.

While working inside your computer case, you can mount a thermistor inside the case and a photodiode peeking out to measure case temperatures and room light levels, respectively. Most motherboards usually have built-in temperature sensors, but using your own thermistor allow you to place the sensor wherever you want in the case. Wire the two devices to a microcontroller that has analog-input lines and an A/D converter. Use the A/D converter in the microcontroller to send a digital signal to the MAX232 which can then provide PHP a numeric reference to the light-level and case-temperature. Calibration will be necessary to convert the eight-bit numeric value from the thermistor into accurate temperatures and display that data on your web site for remote monitoring of both the computer temperature and if anyone turned on the light in the room. If you want to be really creative/geeky you can directly use the bit value of the serial data to generate a base-16 HEX-code color value to use for the background color of your web site.

Finally, a high-wattage relay can easily convert a high/low 5v DC signal from the microcontroller to turn on or off an air conditioner as long as the unit has mechanical controls. Note, to avoid a fire you must know the wattage your AC requires and buy a relay rated for at least the same value. Alternatively, if your AC has a digital control panel and remote control then you can use an IR-emitter and just turn it on and off by its own remote control interface, but that can be somewhat more tricky as every remote device has its own remote codes.

Once you get PHP interacting with the physical world you can do all sorts of neat things. Some food for though, imagine the power of having a PHP-controlled database connected to a solenoid that can lock and unlock a deadbolt in a low-security entrance based on the username and password keyed into a keyboard mounted at the entrance. A motion sensor on the inside can unlock the door automatically when people want to exit and a reed switch can sense if the door is closed and alert you by email if someone has left the door open.

While we are thinking, just sit back and awe at how much geek-value you could generate with a PHP-powered robot...