Local version for working on Nucleo F401 and Ethernet shield

Dependencies:   WIZnet_Library mbed

Fork of Embedded_web_wiznet by IPN ESIME ZACATENCO

main.cpp

Committer:
highroads
Date:
2014-12-28
Revision:
6:09f34aa14399
Parent:
5:8f5448521cd8
Child:
7:e5c9b43f5754

File content as of revision 6:09f34aa14399:

//w5500
#include "mbed.h"
#include "WIZnetInterface.h"

#define ECHO_SERVER_PORT   80


WIZnetInterface eth(SPI_MOSI, SPI_MISO, SPI_SCK,SPI_CS,PB_4); // spi, cs, reset
Serial pc(SERIAL_TX,SERIAL_RX);
// theres a conflict with LED1 on the Nucleo board it uses the same pin as SPI_SCK!
//DigitalOut led(LED1);
// This is the chip select for the sd card which shares the SPI bus on the Arduino shield.
DigitalOut SD_CS(PB_5);


void f_ethernet_init(void);



const char * IP_Addr    = "192.168.1.210";
const char * IP_Subnet  = "255.255.255.0";
const char * IP_Gateway = "192.168.1.1";
char data[8];
int length;

int ret,dummy;
int lv=1;
bool status;


char a[]={"HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n"};
char b[]={"<html>\r\n<body>\r\n"};
char c[]={"<body bgcolor=\"green\">"};
char c1[]={"<body bgcolor=\"000ff0\"></body>\r\n"};
char d[]={"<title>Nucleo F401</title>\r\n"};
char e[]={"<center><h2>Website using Wiznet</h2>\r\n"};
char f[]={"<br /><hr></center>\r\n"};
//char g[]={"<FONT FACE=\"Arial\" SIZE=\"8\" COLOR=\"FF0000\">"};
//char w[]={"<meta http-equiv=\"refresh\" content=\"10; url=/\">\r\n"};
char h[]={"<center><div STYLE=\"position:absolute;bgcolor:black; top:100px; left:200px;width:54px\">"};

//char r[]={"HTTP/1.1 200 OK\r\n"};
//char s[]={"Content-Type: text/html\r\n"};
//char t[]={"Connection: close\r\n"};
//char u[]={"Refresh: 5\r\n\r\n"};
//char v[]={"<!DOCTYOE HTML>\r\n"};
//char y[]={"<html>\r\n"};


char paq_en[64];


int main()
{
// force the chip select for the SD card high to avoid collisions. We're not using the sd card for this program    
    SD_CS=1;
    f_ethernet_init();
    TCPSocketServer server;
    TCPSocketConnection client;
    server.bind(ECHO_SERVER_PORT);
    server.listen();
    pc.printf("\nWaiting for connection to client...\n");
   
    
    while(lv)
    {
        lv=server.accept(client);
        if(!lv)
        {
           pc.printf("Client connected:%s\n", client.get_address());
          
        }
        else
        {
           wait(10);
        }
    }   
           client.receive(data,sizeof(data));
           pc.printf("%c",data);

           client.send(a,sizeof(a));
           client.send(b,sizeof(b));
           client.send(c,sizeof(c));
           client.send(d,sizeof(d));
           client.send(e,sizeof(e)); 
           client.send(f,sizeof(f)); 
        
            
    while(1)       
  {         
    while(client.is_connected())
    { 
       dummy++;
      
       
       client.send(h,sizeof(h));
       sprintf(paq_en,"<p style=\"color:white;font-size:200px\">%d</p></div></center>\r\n",dummy);
       client.send(paq_en,sizeof(paq_en)); 
       wait(1); 
        client.send(h,sizeof(h));
       sprintf(paq_en,"<p style=\"color:green;font-size:200px\">%d</p></div></center>\r\n",dummy);
       client.send(paq_en,sizeof(paq_en));  
                        
    }    
     server.bind(ECHO_SERVER_PORT);
     server.listen();
     server.accept(client);
    
          
           client.send(a,sizeof(a));
           client.send(b,sizeof(b));
           client.send(c,sizeof(c));
           client.send(d,sizeof(d));
           client.send(e,sizeof(e));
           client.send(f,sizeof(f)); 
     client.send(paq_en,sizeof(paq_en));             
   }   
    
   
}   

void f_ethernet_init()
{
    uint8_t mac[]={0x90,0xa2,0xDa,0x0d,0x42,0xe0};
    // mbed_mac_address((char *)mac); 
    pc.printf("\tStarting Ethernet Server ...\n\r");
    wait(1.0);
    ret = eth.init(mac);
    if(!ret)
    {
        pc.printf("Initialized, MAC= %s\n\r",eth.getMACAddress());
    }    
    else
    {
        pc.printf("Communication Failure  ... Restart devices ...\n\r");    
    }
    pc.printf("Connected");
    wait(0.5);
    pc.printf(".");
    wait(0.5);
    pc.printf(".\n\r");
    wait(0.5);
    ret = eth.connect();
    if(!ret)
    {
        pc.printf("Connection Established!\n\n\r");
        wait(1);
        pc.printf("IP=%s\n\rMASK=%s\n\rGW=%s\n\r",eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
    }    
    else
    {
        pc.printf("Communication Failure  ... Restart devices ...\n\r"); 
    }
}