root/mcbuilder/trunk/resources/examples/Communication/LedWebServer/LedWebServer.c

Revision 758, 3.6 KB (checked in by liamstask, 6 months ago)

- add LED Web Server example

Line 
1/*
2  LED Web Server - MakingThings, 2008
3 
4  Control the application board LEDs through a web server interface.
5  The default IP address of the Make Controller is 192.168.0.200, so once
6  you've uploaded the project and plugged the board into your network,
7  type 192.168.0.200 into your browser and control the LEDs via the
8  checkboxes on the screen.
9*/
10#include "config.h"
11#include "appled.h"
12#include "webserver.h"
13
14int LedHandler( char* requestType, char* address, char* requestBuffer,
15                      int requestMaxSize, void* socket, char* buffer, int len );
16
17void Run( ) // this gets called as soon as we boot up.
18{
19  Network_SetActive(true); // turn on the network system
20  WebServer_SetActive(true); // fire up our webserver
21  WebServer_Route("/", LedHandler); // specify that we want to handle all requests with LedHandler
22}
23
24/*
25  This is our handler.  It gets called by the webserver when an incoming request matches
26  the route we supplied in WebServer_Route() above.
27  We're going to write out a simple HTML form with some checkboxes, and if
28  any of the checkboxes are selected, light up the corresponding LED.
29*/
30int LedHandler( char* requestType, char* address, char* requestBuffer,
31                      int requestMaxSize, void* socket, char* buffer, int len )
32{
33  (void)address; // unused parameter
34  char temp[100];
35
36  if( !WebServer_WriteResponseOkHTML( socket ) )
37    return 0;
38
39  if( !WebServer_WriteHeader( true, socket, buffer, len ) )
40    return 0;
41
42  if( !WebServer_WriteBodyStart( 0, socket, buffer, len ) )
43    return 0;
44 
45  int formElements = 0;
46  HtmlForm form;
47  form.count = 0;
48 
49  // extract the form data, depending on how the form was submitted - either GET or POST
50  if ( strncmp( requestType, "GET", 3 ) == 0 )
51  {
52    char *p = strchr( requestBuffer, '?' );
53    if( p != NULL ) // if we didn't find a ?, then there were no form elements
54      formElements = WebServer_ParseFormElements( p+1, &form );
55  }
56  else if ( strncmp( requestType, "POST", 4 ) == 0 )
57  {
58    // make sure we're pointing at the POST data and if it looks good, process it
59    if( WebServer_GetPostData( socket, requestBuffer, requestMaxSize ) )
60      formElements = WebServer_ParseFormElements( requestBuffer, &form ); // grab the data out of the form
61  }
62 
63  // write out a form with checkboxes for each LED.
64  strcat( buffer, "<h1>Welcome!  Have some fun with the LEDs, please.</h1>" );
65  strcat( buffer, "<form method=\"POST\">" );
66  strcat( buffer, "App LED 0: <input type=\"checkbox\" name=\"appled0\"><br>" );
67  strcat( buffer, "App LED 1: <input type=\"checkbox\" name=\"appled1\"><br>" );
68  strcat( buffer, "App LED 2: <input type=\"checkbox\" name=\"appled2\"><br>" );
69  strcat( buffer, "App LED 3: <input type=\"checkbox\" name=\"appled3\"><br>" );
70  strcat( buffer, "<p></p>" );
71  strcat( buffer, "<input type=\"submit\" value=\"Submit\">" );
72  strcat( buffer, "</form>" );
73 
74  // now deal with any form elements we may have parsed from above
75  int i, j;
76  for( j = 0; j < 4; j++ )
77  {
78    int value = 0;
79    snprintf( temp, 100, "appled%d", j ); // for each LED, see if we got a form element with its name
80    for( i = 0; i < formElements; i++ )
81    {
82      if( strcmp( temp, form.elements[i].key ) == 0 ) // check the name of the key
83        value = 1; // if the checkbox was checked, light up the LED
84    }
85    AppLed_SetState( j, value );
86  }
87 
88  // Write out the dynamically generated page.
89  if( !SocketWrite( socket, buffer, strlen( buffer ) ) )
90    return 0 ;
91 
92  WebServer_WriteBodyEnd( socket );
93  return 1;
94}
95
Note: See TracBrowser for help on using the browser.