Once the ATMega32L circuit was operational it was time to write some Perl code so that it could be accessed in University Linux. The first priority was to have a way to view the analog input channels from a browser. The best way to do this is to write a server in Perl so that any application running anywhere on the Black Tower can call up data coming from the ATMega32L.
I adapted the Perl server from the temperature sensor. I simply changed the port number (to avoid conflicts) then added some code to request the counts from each analog input channel via a call to the serial port. Perl treats any device just like a file, so it was a matter of opening /dev/ttyS0. Perl is a bit fussy about reading and writing to the same file, so the eventual open statement looked like this:
open (SPOut, "+>/dev/ttyS0");
I wrote a "0" to the serial port, and then read in the reply from the ATMega32L in counts, multiplied this by 5 and divided by 1024 to get the Vdc reading for analog input channel 0. Since there are 8 channels, I put this into a loop. Turned out that the serial port had to be opened and closed to get each separate reading. This was not as elegant as I had hoped, but it seemed to work OK.
The next problem was how to pass the values back to the client. I decided a single long string would be easiest, and individual readings could be parsed out from that. For this to work, however, each channel reading had to have the same number of digits. A reading of 4 volts, for example, would have to be 4.000 and not 4.0 or something else. I decided that three digits to the right of the decimal would be sufficient since the 10-bit A/D converter was reading only to 5 mV.
The solution to this was to take each reading and pad it by concatenating three 0's to it, then extracting a substring consisting of the first five characters. This seemed to work in every case and produced consistent 5 character readings for all channels.
All 8 readings, now five characters each, were concatenated together, separated by a space. This was passed back to the client by the Perl server. The client code parsed out each reading from the large string, and created a webpage displaying all the readings in Vdc with a channel label. A button on the webpage calls the server and generates a new page of readings. When the page is first viewed the readings are blank, but pressing the "Get Readings" button fills the page with current values.
The update goes fairly fast considering that the webpage has to enable a Perl client that calls a server that has to open and close a 2400 baud serial port 8 times. It has proven reliable in a few days of continuous testing; serial ports can be prone to locking up. Everything is situated on the Gamma machine in the Black Tower which is the newest and fastest PC. The ATMega32L controller page now is accessible from the Black Tower homepage, as shown in the screen shot.
The first application for the new interface was to connect a photocell in series with a resistor and the +5 V supply to form a light level sensing circuit. When it is dark, the photocell circuit puts out about 4.9 V, and when in bright light, about 1.5 V. This signal was connected to channel 1 of the ATMega32L analog inputs. I put the work light directly over the photocell and used the X10 control page to turn the light on or off and to vary the brightness. The photocell signal gives a positive indication of a change in light level, so now I can confirm the state of the X10 light switch remotely using the ATMega32L web page.
The next step is to add digital input and output controls. But to do this, I will have to figure out how have the Perl client pass a value (desired switch state) to the server calling the ATMega32L. Taking over the world is never easy...