DNS Web Client for WIZnet Academu

Dependencies:   WIZnetInterface mbed

main.cpp

Committer:
joon874
Date:
2016-04-04
Revision:
0:02f6cd2ee811

File content as of revision 0:02f6cd2ee811:

#include "mbed.h"
#include "EthernetInterface.h"

DigitalOut myled1(LED1,1);
DigitalOut myled2(LED2,1);

#define ECHO_SERVER_PORT    80      // HTTP default Port 80
char ServerName[] = "naver.com";
char http_cmd[] = "GET / HTTP/1.0\r\n\r\n";

#define BUFFER_SIZE  2048
char buffer[BUFFER_SIZE];


// Intitialize the Ethernet Client Library
EthernetInterface eth;    


int main() {
   
    printf("-- Welcome WIZwiki-W7500 Platform -- \r\n");
    
    myled1 = 0;
    
    // Enter a MAC Address for your Platform
    uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x03, 0x04, 0x02}; 
    
    // Initializing MAX Address
    eth.init(mac_addr);
    
    do{
        printf("   Link - Wait... \r\n");
        wait(1);
    }while(!eth.ethernet_link());
    printf("-- Ethetnet PHY Link - Done -- \r\n");
    
    if (eth.connect() < 0 )
        printf("-- EThernet Connect - Fail -- \r\n");
    else
    {
        printf("-- Assigned Network Information -- \r\n");
        printf("   IP   : %s\r\n\r\n", eth.getIPAddress()); 
        printf("   MASK : %s\r\n\r\n", eth.getNetworkMask());
        printf("   GW   : %s\r\n\r\n", eth.getGateway());
    }
    
    // Initialize the TCP Socket Connection
    TCPSocketConnection sock;
    
    if(sock.connect(ServerName, ECHO_SERVER_PORT) < 0)
        printf("-- Connect - Fail -- \r\n");
    else
    {
        printf("-- Connect - Connected -- \r\n");
        wait(3);
        while(sock.is_connected() == false)
        {
            printf("  .");
        }
        sock.send_all(http_cmd, sizeof(http_cmd));
    }
    
    while(true) {
    
        int n = sock.receive_all(buffer, BUFFER_SIZE);
        
        if(n < 0)
            break;
        else
        {
            for(int i=0; i<n; i++) 
            printf("%c", buffer[i]);
        }
        
        if(sock.is_connected() == false){
            sock.close();
            
            myled1 = 1;
        
            while(1) {
                myled2 = 1;
                wait(0.5);
                myled2 = 0;
                wait(0.5);
            }
        }
    
    }
    
}