Sunday, May 12, 2013

Web Interface for an ethernet based IR Interface

In the past few blogs, we looked at implementing an IR remote control interface on an ethernet SBC. We also looked at how records can be stored on the EEPROM on the SBC. Now we look at the web interface required to use the SBC as an IR remote control.

Implementing a web interface

A good starting point for a web interface is the sample program that comes with the Microchip TCP/IP Stack itself. The Olimex SBC has a slightly modified version with fewer peripherals and a much larger logo. The opening page shows the state of the LED, the analog value for and onboard potentiometer and the temperature detected by the temperature sensor. While the web page itself is relatively elaborate, the actual web requests to get the changing information is very basic. It uses a web technique called Ajax.

Using Ajax and SPA

Most of the web page is static and is served up as a normal file. The changing information is gathered by making smaller requests to the PIC SBC. The requests are very simple and the response is very basic and compact. The web page processes the response and updates the web page.

This makes the code on the PIC that handles the web requests relatively easy to write. A few parameters are passed in the URL. The PIC uses a library function to extract the value of the parameters. The code to extract values of parameters and to output the response have been looked at in a previous blog.

In addition to Ajax, another useful technique to use is the Singe Page Application (SPA) approach to web design. This loads all the web page and javascript you need in one go.

Combining Ajax and SPA into your PIC front-end has several advantages. Any data fetched from the PIC is retained and does not have to be requeried. Instead of moving from page to page and retrieving them from the PIC, the entire application is largely held in the browser. Unlike many SPA, fragments of HTML are not downloaded.

As the browser environment, even on a smartphone is much more powerful than a piddly little PIC, much of the logic will be shifted into javascript.

Ajax Interface for IR

The ajax requests are basic with the browser proving most of the complex logic. Parameters to be passed as part of the request are passed in the URL. All EEPROM requests are handled by the mem.cgi request and all IR requests are handled by the ir.cgi request. All requests have a basic function code that is passed as the parameter f. The response if a simple OK or if more data is involved, one or more lines of text delimited by vertical bars(|). A summary of the requests is shown below.

cgi file f parameter Description Input Output
mem.cgi clr Erase all records None OK
mem.cgi gta Get all records None <type>|<val1>|<val2>|...
...
mem-cgi set Save a new record t=<rec type>
r=<val1>|<val2>|...
OK
ir.cgi acq Acquire raw IR data None OK|D25|Mxx|Sxx|...
ir.cgi snd Send an IR code si=<CS Id>
dt=<data>
OK

The mem.cgi routines have already been documented in a previous blog. The two new ir.cgi requests are documented below.

Acquire raw IR data
ir.cgi?f=acq
    
  Response:
    OK|D25|Mxx|Sxx|...
         or
    Err|Timeout

When the command is received, the PIC goes into IR Receive mode. It measures any incoming signal. The pulse waveform is stored as a series of duration of the high and low parts of the pulse train. Once no data is received for a few seconds or the internal buffer is full, the receive is terminated and the contents of the buffer are sent as a response. The elements of the response are separated by a vertical bar(|). The first element is an OK. The next is the duration unit. This is set to 25 as the current design measures all signals in 25us units. The remaining elements are a series of mark or space values. The first character is an M or S indicating a mark or space respectively. The remaining is a decimal number indicating the duration of the mark or space in multiple of 25us. If no signal is received for a few seconds, an error is returned.

Send an IR code
ir.cgi?f=snd&si=<CS Id>&dt=<data>
    
  Response:
    OK

The parameters passed in the URL is the index of the Coding Scheme to be used and the data to be sent as a series of hex digits. The Coding Scheme index is the sequence of the Coding Scheme in the EEPROM starting from 0. When the command is received, the PIC generates the IR signal based on the coding scheme and the data.

These handful of web requests should be sufficient for the IR web application I have in mind.


No comments:

Post a Comment