Table of Contents

Ethernet

Necessary knowledge: [HW] Controller module

The following part of library contains HomeLab Ethernet functions.

Constants

Functions

Brings Ethernet controller into sleep mode (low energy consumption).

Ethernet controller initial setup with MAC and IP addresses.

  Parameters:
  * //*mac// - MAC address array.
  * //*ip// - IP address array.

Polls network until it receives packet addressed to it and containing data.

  Parameters:
  * //maxlen// - maximum allowed data buffer size.
  * //*buffer// - array where to save received data for further operations.
  * If the packet has arrived, returns the length of the received packet in bytes and in opposite case returns 0.

Analyzes if received packet contains info about URL. Will respond to ping if needed.

  Parameters:
  * //*buf// - data array to analyze.
  * //plen// - length of received packet in bytes.
  * Tagastab URL-i esimese tähemärgi aadressi. Kui URL on tühi, siis tagastab 0.
  * Returns the address of the first char in URL. If URL is empty, it will return 0.

Loads HTML-code for displaying webpage from controller program memory into TCP send buffer.

  Parameters:
  * //*buf// - data arrya to send using TCP.
  * //pos// - data end address in buffer which must be maintained if data is loaded in queue. The first address is 0.
  * //*progmem_s// - string name that is saved into program memory and from where data is loaded into send array.
  * Retruns end address of data array which can be input as a parameter for loading next HTML-code.

Dislays webpage according to preloaded HTML-code.

  Parameters:
  * //*buf// - Data array where HTLM is preloaded.
  * //dplen// - End address of data in buffer to be displayed as HTML-code.

 

Example

#include <string.h>
#include <homelab/module/ethernet.h>
#include <homelab/pin.h>
 
//	Function for compiling webpage
uint16_t ethernet_load_webpage(uint8_t on_off);
 
// MAC and IP must be unique in network,
// same addresses in two devices are not allowed.
// Random MAC address (usually no need to change it)
static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24};
 
// IP according to LAN (you need to choose the last number)
static uint8_t myip[4] = {192,168,1,100};
 
//length of data array
static uint16_t gPlen; 
 
//Data buffer size
#define BUFFER_SIZE 600
static uint8_t buf[BUFFER_SIZE+1];
 
//LED PB7 variable
pin debug_led = PIN(B, 7);
 
 
int main(void)
{
 
	uint16_t pktlen;
	char *str;  
 
	// LED PB7 is output
	pin_setup_output(debug_led);
 
	// Etherneti initialization with given addresses
	ethernet_init(mymac, myip);			
 
        while(1)
	{
	// Receive packets until it receives addressed to him with data
    	pktlen=ethernet_get_packet(BUFFER_SIZE, buf);
 
        // Performs packet preconditioning and answer to "ping" packet. 
        // Returns packet URL.
        gPlen=ethernet_analyse_packet(buf,pktlen);
 
	// If URL contains info, start analyzing it
        if (gPlen!=0)
	{               
            // Load URL address part into "str". 
            // The first 4 digits are IP address numbers.
            str = (char *)&(buf[gPlen+4]);
 
			// Find string "/1" from URL              
			if (strncmp("/1",str,2)==0)
			{
	                        // Load webpage
				gPlen = ethernet_load_webpage(0);
 
				// LED on
				pin_clear(debug_led);		
 
	                 }
			// At next, find string "/0" from URL
			else if (strncmp("/0",str,2)==0)
			{
	                        // Load webpage
				gPlen = ethernet_load_webpage(1);
 
				// LED on
				pin_set(debug_led);
 
	                 }
			// In other cases load webpage according to LED condition
	                else 
			{	           
	                   gPlen=ethernet_load_webpage(pin_get_value(debug_led));
	                }                                    
 
			// Display preloaded webpage                
                        ethernet_print_webpage (buf,gPlen);    			            
		}
    }
    return (0);
}
 
 
// Webpage will be loaded by writing data into TCP send buffer
uint16_t ethernet_load_webpage(uint8_t on_off)
{
    uint16_t plen=0; 
 
	// Load HTML-code into buffer to send it
	// Those large strings are saved into program memory using PSTR macro,
	// to save SRAM
 
	// Load webpage header
 
	plen=ethernet_load_data(buf,0,PSTR
	("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n"));
 
	plen=ethernet_load_data(buf,plen,PSTR("<center><p>Homelab PB7 LED: "));
 
    // if LED is off, display it
    if (on_off)
    {
      plen=ethernet_load_data(buf,plen,PSTR("<font color=\"#00FF00\"> OFF</font>"));
    }
    // if LED is on
    else
    {
      plen=ethernet_load_data(buf,plen,PSTR("<font color=\"#FF0000\"> ON</font>"));
    }
    // Lae "Refresh" nupp
    plen=ethernet_load_data(buf,plen,PSTR
    (" <small><a href=\".\">[refresh status]</a></small></p>\n<p><a href=\"."));
 
    // Load the button for LED condition change.
    if (on_off)
    {
        plen=ethernet_load_data(buf,plen,PSTR("/1\">Switch on</a><p>"));
 
    }
    else
    {
         plen=ethernet_load_data(buf,plen,PSTR("/0\">Switch off</a><p>"));
 
    } 
 
    // Return the end address of data in buffer
    return(plen);
}