FTPmicro Tutorial - Example
Let's analyze an example for the monitoring in real time from the temperature, using the sensor on the FTPMicro board, and the switch of two LED, through a HTML page and a CGI one.
In the example is used AHAH (Asynchronous HTTP And HTML), that it permits to send requests HTTP in asynchronous way, or rather where the page is reloaded.
The CGI file that use (status.cgi) is very simple; is constituted from a html code to realize a multiplication table, inside are wrote the LED condition and the system temperature.
<table class="status_table"> <tr><th colspan="2">Temperature</th></tr> <tr><td>System</td><td>%00°C</td></tr> <tr><th colspan="2">LED Status</th></tr> <tr><td>LED 1</td><td>%01</td></tr> <tr><td>LED 2</td><td>%02</td></tr> </table>
Like you see, the variables used are:
- 00: the temperature;
- 01: the first LED
- 02: the second LED
So this page will be required periodically to know the system condition.
Now let's analyze the main page (index.htm), which will contain two buttons to modify the LADs condition, and it will include, thanks to AHAH, the CGI page.
<html>
<head>
<title>FTPMicro: Temperature Example</title>
<script type="text/javascript" src="/ahah.js"></script>
<script type="text/javascript">
function refresh() {
if (lo) return true; // interrupts the charging if there is already another charging in progress (because of the buttons)
noloadAHAH('/Status.cgi','tempdiv','GET'); // requests the status.cgi page and it charges in the div "tempdiv"
}
window.setInterval("refresh()",2000);
</script>
<link rel="stylesheet" type="text/css" media="all" href="/style.css" />
</head>
<body>
<h1>FTPMicro</h1>
<div class="bar">
The world smallest WebServer and FtpClient with DHCP and UDP features
</div>
<div id="tempdiv" class="left">
Loading...
</div>
<div class="left">
<form>
<table class="status_table">
<tr><th>LED Toggle</th></tr>
<tr><td><input type="submit" value="LED 1" onclick="javascript: noloadAHAH('/Status.cgi?t=1','tempdiv','GET'); return false;"></td></tr>
<tr><td><input type="submit" value="LED 2" onclick="javascript: noloadAHAH('/Status.cgi?t=2','tempdiv','GET'); return false;"></td></tr>
</table>
</form>
</div>
</body>
</html>
Like you see, the page is quite simple; the thing that must result a little bit cryptic is the using of AHAH.
First of all, in the page header, there is a Javascript script, that through the function setInterval, permits to visualize the condition to the regulars intervals recalling the CGI page. This last one will be charge inside the div which has the id "tempdiv".
The other one relevant section, is that one which concerns the buttons. These recur to the AHAH too to send the commands without reload the page.
In this example, is used the "t" parameter and the value corresponds to the LED that this one acts, so /Status.cgi?t=1 will be the command to send to the first LED, and /Status.cgi?t=2 for the second. After the command execution will be return the requested page (status.cgi) that will be reload always inside its div.
In the example is also used a file css, but this serve to define the page style.

At this point let's analyze the methods implementation ProcessIO, HTTPExecCmd and HTTPGetVar; here is the first:
static char Temperature[8];
static void ProcessIO(void)
{
signed long temp;
// Start A/D conversion
ADCON0bits.GO = 1;
// Wait until A/D conversion is done
while(ADCON0bits.GO);
temp = (long)(*(WORD*)(&ADRESL)) * 322 - 50000; // approximative conversion...
itoa(temp/1000, Temperature);
}This method is invoked cliking, "as soon as possible", or rather when the stack doesn't perform its functions. In this example, makes the exit tension reading of the temperature sensor, and the next conversion in the string.
#define VAR_TEMPERATURE (0x00) #define VAR_LED1 (0x01) #define VAR_LED2 (0x02) #define CMD_LED1 (0x1) #define CMD_LED2 (0x2)
These define are useful to identify the CGI variables and the commands.
void HTTPExecCmd(BYTE** argv, BYTE argc)
{
if (argv[1][0] == 't') {
switch (argv[2][0] - '0') {
case CMD_LED1 :
LED1_IO = !LED1_IO;
break;
case CMD_LED2 :
LED2_IO = !LED2_IO;
break;
}
}
}Like we saw, the possibles commands that we send are the "status.cgi?t=x" type where x represents the led to apply the command. With this command, when it will be invoked the HTTPExecCmd method, the argument argv, will contain to the first place the string "status.cgi" which will remained unchanged; the second element will be "t" (and we have to be sure that it is like this); the third will contain the relative number to the LED but the ASCII character underform, that's why we must convert it all, subtracting the character "0".
WORD HTTPGetVar(BYTE var, WORD ref, BYTE* val)
{
switch (var) {
case VAR_LED1 :
*val = LED1_IO ? '1' : '0';
break;
case VAR_LED2 :
*val = LED2_IO ? '1' : '0';
break;
case VAR_TEMPERATURE:
*val = Temperature[(BYTE)ref];
if(Temperature[(BYTE)ref] == '\0')
return HTTP_END_OF_VAR;
else if(Temperature[(BYTE)++ref] == '\0' )
return HTTP_END_OF_VAR;
return ref;
default : break;
}
return HTTP_END_OF_VAR;
}At last this method HTTPGetVar return the LED current condition, or rather the string Temperature; In the first case is enough to assign a *val the correspond character to the (0 o 1) led condition, while in the second case, speaking about a string, it's necessary to return one byte at a time making the reference to the value of ref.
In the code it makes reference to the definitions LEDx_IO, that are found in the Compiler.h file:
#define LED1_IO (LATAbits.LATA0) #define LED1_TRIS (TRISAbits.TRISA0) #define LED2_IO (LATAbits.LATA1) #define LED2_TRIS (TRISAbits.TRISA1)
In this case are used the LEDs tied to the Ethernet module, because are already presented in the card, but modifying these definitions can employ two generics pins.
Sources
The FTPMicro software, is available to here.
The sources include all the file necessary to realize the example proposed in the article "Example".
Schematic

Go here to view the Schematic
PINOut
PINOut
| Attachment | Size |
|---|---|
| FTPMicro_1v01_0.rar | 256.31 KB |
- Emanuele's blog
- 3630 reads






Post new comment