1. Introduction



This tutorial explains how to use the Ethernet library. The Ethernet library uses the uIP library made by Adam Dunkels. All credit for this library goes to him. For this tutorial basic HTML knowledge is required. The library and examples can be downloaded here. A PDF explaining all possibilities of the uIP library is included to.
For this tutorial it is useful to open the example and look at the original code. This will give a better understanding.

2. Required tools


When changes are made to the HTML code a script has to be executed. To execute this script a Perl compiler has to be installed. A good free one is Strawberry Perl that can be downloaded here: Strawberry perl.
 
After installing, Perl scripts can be executed by double-clicking on them.

3. Starting the ethernet web server


To begin with, a couple includes are needed:

#include "LPC17xx.h"
#include "lpc_types.h"
#include "uIP/ethernet.h"
#include "GPIO.h"
#include "adc.h"

The GPIO and ADC includes are only needed when you want to change IO pins or read out the ADC or IO pins from the website.
In the main void the command EthernetInit(); is used to setup the Ethernet web server. In the while loop the command EthernetHandle(); has to be called. EthernetHandle checks if there is new data and handles the Ethernet data. The full code is:
int main(void)
{
             EthernetInit();
             while(1)
             {
                    EthernetHandle();
             }
}
The default IP address of the Simplecortex is 192.168.1.5. To change this open the ethernet.h file in the uIP folder. In the Ethernet.h the following code can be found:

#define MYIP_1      192
#define MYIP_2      168
#define MYIP_3      1
#define MYIP_4      5

By changing these parameters the IP address can be changed. For more info about changing the IP-address check the getting started.
 
4. Changing the website


By default the Simplecortex shows the index.shtml. The file is located in:
uIP --> apps --> webserver -->httpd-fs folder. This file is fully adjustable but is has as limitation that the website van be bigger than 512Kbyte. The uIP library also takes code space so it is recommended to keep the whole website including pictures under 400Kbyte. At the moment it is not possible to save the website on a MicroSD card but that is planned for in the future.
In the folder uIP -->apps -->webserver a Perl script can be found called makefsdata.pl. After updating or changing the website this script has to be executed else the website will not be updated in the Simplecortex! After running this script the workspace in CoIDE has to be refreshed. To do this right click on the project name and press “Refresh”
 

The index page has to be named index.shtml instead of index.html. This is because it is only possible to access IO pins, ADC pins and other hardware trough a shtml file.
 
5. Communication between the website and the Simplecortex

 
5.1 The httpd-cgi
The httpd-cgi.c file is where the code is placed to communicate between html and the Simplecortex. With making some changes in this code it is possible add a function to the web page. This code can be found in uIP --> apps --> webserver.
These are a few examples of a function:
  • Change a value of a variable.
  • Read a port or ADC value.
  • Change a port status.


 Warning! Please don’t remove the existing code. Else the webserver will not work properly.
 
5.2 How it works
When the browser is processing a web page it has triggers for a few combinations of symbols. The symbol combination to access the CGI is %! Behind the %! is always a keyword. For example %! port_change. If there is a combination of these symbols in a webpage, the webserver will checkup the httpd-cgi.c file and check there is a command with the same keyword available.
 
To sum it all up; to add a new function to a webpage like reading out an ADC pin and displaying it the following things has to be done:
  • Make a new HTTPD_CGI_CALL.
  • Edit the httpd_cgi_call *calls[] variable.
  • Add the function to execute the code.
  • Add the function to display the result on the webpage.

5.3 Create new CGI call
HTTPD_CGI_CALL(variablename, "websitename", webfunction)
This declaration needed so that the Simplecortex can execute some code when a %! is found in a webpage.
variablename is the name of the variable for the new function.
websitename is the keyword on the website after %!
webfunction is the name of the function which will be executed after the keyword is found.
A correct command would be:
HTTPD_CGI_CALL(num1, "port-number1", port_number1);

When the text %! port-number1 is found on a website the Simplecortex executes the function port_nummer1.

5.4 Save the new function in memory
A variable containing all HTTPD_CGI_CALL variables is needed, by default it looks like this:

static const struct httpd_cgi_call *calls[] = {&port, &toggle, &num1, &num2, &adc1, NULL };
When a new HTTPD_CGI_CALL is made the variable has to be added. For example when a new HTTPD_CGI_CALL is made that looks like this:

HTTPD_CGI_CALL(adc2, "read-adc2", Read_ADC2);

adc2 has to be added to the variable. After adding adc2 it would look like this:
static const struct httpd_cgi_call *calls[] = {&port, &toggle, &num1, &num2, &adc1, &adc2, NULL };

5.5 Execute code
Now comes hard part, the actual code that has to be executed. This code is to generate the text on the webpage.
        The first part looks like this:
       
        static PT_THREAD(webfunction(struct httpd_state *s, char *ptr))
                  {
                  PSOCK_BEGIN(&s->sout);
                  PSOCK_GENERATOR_SEND(&s->sout, generate_webfunction, s);
                  PSOCK_END(&s->sout);
                  }

It looks quite complicated but the only things that need to be changed are the names.
Webfunction has to be changed in the keyword used in the HTTPD_CGI_CALL. generate_webfunction: webfunction has to be replaced with the keyword. For example: generate_port_number1
 
5.6 Replace keyword with normal text on web page
With this piece of code it is possible to replace the keyword with normal text on the main page. The text can be anything; in this way it is possible to put results on the main page. It is also possible to implement any c code you want.
The example code below replaces the word after %! on the actual webpage with "Hello world!".
 
static unsigned short
generate_webfunction(void *arg)
{
       return snprintf((char *)uip_appdata, UIP_APPDATA_SIZE,
       "%s ", ("Hello World!");
}
 
generate_webfunction: webfunction has to be replaced with the keyword. For example: generate_port_number1
 
To read out an GPIO pin a working piece of code would be like this:
It reads out GPIO pin P1.27 and displays "On" or "Off" accordingly on the website. It is also possible to send a number. To do this change "%s " with "%16u\ " In the httpd-cgi.c file an example is included that reads out the ADC port and displays that value. Please read the pointer documentation for more information about file formats.
 
        static unsigned short
        generate_webfunction(void *arg)
        {
              unsigned int port_number2;                             // Variable for readout
              GPIOSetDir(1, 27, 0);                                       // Make GPIO pin an input
              GPIOSetPull(1, 27, 1);                                     // Enable the internal pull-up resistor
              port_number2 = !GPIOGetValue(1, 27);              // Read the GPIO pin
              return snprintf((char *)uip_appdata, UIP_APPDATA_SIZE,
              "%s ", (port_number2 ? "On":"Off"));                  // If GPIO pin is 1 send "On" else send "Off"
          }
 
5.7 CGI example code
As an example, let’s design a new function that reads GPIO port 1.26 and displays it on the website. To begin a new HTTPD_CGI_CALL command:
 
HTTPD_CGI_CALL(button1, "port-button1", port_button1);

Now add button1 to the *calls[] variable:

static const struct httpd_cgi_call *calls[] = {&port, &toggle, &num1, &num2, &adc1, button1, NULL };

The function to execute the code should look like this:

 static unsigned short
 generate_port_button1 (void *arg)
 {
          unsigned int port_but1;                                     // Variable for readout
          GPIOSetDir(1, 26, 0);                                         // Make GPIO pin an input
          GPIOSetPull(1, 26, 1);                                       // Enable the internal pull-up resistor
          port_but1 = !GPIOGetValue(1, 26);                      // Read the GPIO pin
          return snprintf((char *)uip_appdata, UIP_APPDATA_SIZE,
          "%s ", (port_but1 ? "Pressed":"Not pressed"));     // If GPIO pin is 1 send "Pressed" else send
 
  }                                                                               // "Not pressed"
 
  And as last, the code to display the result on the webpage:
 
  static PT_THREAD(port_button1(struct httpd_state *s, char *ptr))
  {
          PSOCK_BEGIN(&s->sout);
          PSOCK_GENERATOR_SEND(&s->sout, generate_port_button1, s);
          PSOCK_END(&s->sout);
   }


6. The main page
 
Some small adjustments need to be made to a webpage before it works. The webpage has to be saved as a .shtml page instead of a .html page, else the Simplecortex is unable to display data on it. Some basic HTML knowledge is required for the next part. If you are unfamiliar with HTML a good beginners tutorial can be found here.
 
A simple webpage that displays the button status for the function made in 5.1 would look like this:
 
<html>
<body>
<p>Button status: %! port-button1 </p>
 </body>
 </html>
 
The Simplecortex will replace %! port-button1 with Pressed or Not pressed according if it is pressed or not.
The example contains a function to read out each of the buttons, ADC3 and toggle a button.
It is possible to automatically refresh the html page once every couple of seconds. To do this, add the following code to your webpage between <html> and <body>:
<meta http-equiv="refresh" content="10">
Instead of 10 enter the delay in seconds before it refreshes.

7. Troubleshooting

 
1. Please check if both led’s on the Ethernet connector are shining or blinking. If they don’t shine than the problem is probably the hardware.
2. Please check if the IP from the PC is chanced automatically. If this is the case follow the next steps:
There are some network adjustments needed.
Open: Start > Control Panel > Network and Internet > Network adapters
 
  • Right-click on the LAN network adapter.
  • Click on Properties
  • Select Internet protocol version 4 (TCP/IPv4)
  • Click on Properties
  • Select Use the following IP-address
 
Use the following parameter:
IP-address                 = 192.168.1.2
Network mask            = 255.255.255.0
Default gateway          = 192.168.1.1
 
Please check the Getting Started for more information.

 

Copyright 2011. Joomla 1.7 templates - Joomla template maker. The Simplecortex is developed by BRC-Electronics