Simple HTTP server example with W5500 (WIZnet chipset) .access the mbed via any browser, mbed echo back http request header.

Dependencies:   WIZnet_Library mbed

Fork of HTTPServer_echoback_WIZ550io by W5500-Ethernet-Interface Makers

Files at this revision

API Documentation at this revision

Comitter:
sjallouli
Date:
Wed Dec 30 23:26:05 2015 +0000
Parent:
20:f0c3af075949
Commit message:
Port to NUCLEO-STM32F401

Changed in this revision

W5500Interface.lib Show diff for this revision Revisions of this file
WIZnet_Library.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r f0c3af075949 -r 8f790ebb9f3c W5500Interface.lib
--- a/W5500Interface.lib	Tue Sep 23 23:41:25 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/teams/EthernetInterfaceW5500-makers/code/W5500Interface/#af0ed4fbca02
diff -r f0c3af075949 -r 8f790ebb9f3c WIZnet_Library.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WIZnet_Library.lib	Wed Dec 30 23:26:05 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/teams/WIZnet/code/WIZnet_Library/#cb8808b47e69
diff -r f0c3af075949 -r 8f790ebb9f3c main.cpp
--- a/main.cpp	Tue Sep 23 23:41:25 2014 +0000
+++ b/main.cpp	Wed Dec 30 23:26:05 2015 +0000
@@ -1,157 +1,134 @@
 #include "mbed.h"
-#include "EthernetInterface.h"
-//#include "EthernetInterfaceW5500.h"
+#include "WIZnetInterface.h"
 #include <stdio.h>
 #include <string.h>
 
-//#define PORT   80
-#define PORT   888
-#define _DHCP
-
-// move to main().
-//EthernetInterface eth;
+#define USE_DHCP    1
 
-// move to main().
-//TCPSocketServer svr;
+const char * IP_Addr    = "192.168.1.2";
+const char * IP_Subnet  = "255.255.255.0";
+const char * IP_Gateway = "192.168.1.1";
+unsigned char MAC_Addr[6] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xD0 };
+    
+Serial pc(SERIAL_TX, SERIAL_RX);
+
+SPI spi(SPI_MOSI, SPI_MISO, SPI_SCK); // mosi, miso, sclk
+    
+#define PORT   80
+
 bool serverIsListened = false;
-
-// move to main().
-//TCPSocketConnection client;
 bool clientIsConnected = false;
-DigitalOut led1(LED1); //server listning status
-DigitalOut led2(LED2); //socket connecting status
-
-Ticker ledTick;
-
-void ledTickfunc()
-{
-    if(serverIsListened)  {
-        led1 = !led1;
-    } else {
-        led1 = false;
-    }
-}
 
 int main (void)
 {
-#if defined(TARGET_LPC1114)
-    SPI spi(dp2, dp1, dp6); // mosi, miso, sclk
-    EthernetInterface eth(&spi, dp25, dp26); // spi, cs, reset
+  spi.frequency(1000000);
+  WIZnetInterface ethernet(&spi,D10, D3);
 
-#elif defined(TARGET_LPC1768)
-    SPI spi(p11, p12, p13); // mosi, miso, sclk
-    EthernetInterface eth(&spi, p14, p15); // spi, cs, reset
+  pc.printf("Ethernet Init\r\n");
+#if USE_DHCP
+  int ret = ethernet.init(MAC_Addr);
+#else
+  int ret = ethernet.init(MAC_Addr,IP_Addr,IP_Subnet,IP_Gateway);
+#endif
 
-#elif defined(TARGET_LPC11U68)
-    SPI spi(P0_9, P0_8, P1_29); // mosi, miso, sclk
-    EthernetInterface eth(&spi, P0_2, P1_28);//, nRESET(p9); // reset pin is dummy, don't affect any pin of WIZ550io
-    spi.format(8,0); // 8bit, mode 0
-    spi.frequency(7000000); // 7MHz
-    wait(1); // 1 second for stable state
-#elif defined (TARGET_NUCLEO_F030R8)
-    SPI spi(SPI_MOSI, SPI_MISO, SPI_SCK); // mosi, miso, sclk
-    EthernetInterface eth(&spi, PB_6, PA_9);//, nRESET(p9); // reset pin is dummy, don't affect any pin of WIZ550io
-    spi.format(8,0); // 8bit, mode 0
-    spi.frequency(7000000); // 7MHz
-    wait(1); // 1 second for stable state
-
-#endif
+  if (!ret) 
+  {
+    pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress());
+    ret = ethernet.connect();
+    
+    if (!ret) 
+    {
+      pc.printf("IP: %s, MASK: %s, GW: %s\r\n", ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway());
+    }
+    else
+    {
+      pc.printf("Error ethernet.connect() - ret = %d\r\n", ret);
+      exit(0);
+    }
+  }
+  else
+  {
+    pc.printf("Error ethernet.init() - ret = %d\r\n", ret);
+    exit(0);
+  }
 
     // late binding 
     TCPSocketServer svr;
     TCPSocketConnection client;
-
-    ledTick.attach(&ledTickfunc,0.5);
-
-    /*
-        //setup ethernet interface
-        eth.init(); //Use DHCP
-        eth.connect();
-        printf("IP Address is %s\n\r", eth.getIPAddress());
-    */
-
-    printf("Board is on\r\n");
     
-#ifdef _DHCP 
-// didn't test yet in DHCP => TEST OK...
-
-    printf("eth.init()\r\n");
-    eth.init(); //Use DHCP
-    printf("eth.connect()\r\n");
-    eth.connect();
-    //printf("IP Address is %s\n\r", eth.getIPAddress());
-    printf("Initialized, MAC: %s\n", eth.getMACAddress());
-    printf("Connected, IP: %s, MASK: %s, GW: %s\n",
-           eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
-
-#else
-    // as your env. change to real IP address and so on.
-    int ret = eth.init("222.98.173.212", "255.255.255.192", "222.98.173.254");
-    if (!ret) {
-        printf("Initialized, MAC: %s\n", eth.getMACAddress());
-        printf("Connected, IP: %s, MASK: %s, GW: %s\n",
-               eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
-    } else {
-        printf("Error eth.init() - ret = %d\n", ret);
+    //setup tcp socket
+    if(svr.bind(PORT)< 0) 
+    {
+        pc.printf("tcp server bind failed.\n\r");
         return -1;
-    }
-#endif
-
-    //setup tcp socket
-    if(svr.bind(PORT)< 0) {
-        printf("tcp server bind failed.\n\r");
-        return -1;
-    } else {
-        printf("tcp server bind successed.\n\r");
+    } 
+    else
+     {
+        pc.printf("tcp server bind successed.\n\r");
         serverIsListened = true;
     }
 
-    if(svr.listen(1) < 0) {
-        printf("tcp server listen failed.\n\r");
+    if(svr.listen(1) < 0)
+     {
+        pc.printf("tcp server listen failed.\n\r");
         return -1;
-    } else {
-        printf("tcp server is listening...\n\r");
+    }
+    else
+    {
+        pc.printf("tcp server is listening...\n\r");
     }
 
     //listening for http GET request
-    while (serverIsListened) {
-        //blocking mode(never timeout)
-        if(svr.accept(client)<0) {
-            printf("failed to accept connection.\n\r");
-        } else {
-            printf("connection success!\n\rIP: %s\n\r",client.get_address());
-            clientIsConnected = true;
-            led2 = true;
+  while (serverIsListened) 
+  {
+    //blocking mode(never timeout)
+    if(svr.accept(client)<0) 
+    {
+      pc.printf("failed to accept connection.\n\r");
+    }
+    else 
+    {
+      pc.printf("connection success!\n\rIP: %s\n\r",client.get_address());
+      clientIsConnected = true;
 
-            while(clientIsConnected) {
-                char buffer[1024] = {};
-                switch(client.receive(buffer, 1023)) {
-                    case 0:
-                        printf("recieved buffer is empty.\n\r");
-                        clientIsConnected = false;
-                        break;
-                    case -1:
-                        printf("failed to read data from client.\n\r");
-                        clientIsConnected = false;
-                        break;
-                    default:
-                        printf("Recieved Data: %d\n\r\n\r%.*s\n\r",strlen(buffer),strlen(buffer),buffer);
-                        if(buffer[0] == 'G' && buffer[1] == 'E' && buffer[2] == 'T' ) {
-                            printf("GET request incomming.\n\r");
-                            //setup http response header & data
-                            char echoHeader[256] = {};
-                            sprintf(echoHeader,"HTTP/1.1 200 OK\n\rContent-Length: %d\n\rContent-Type: text\n\rConnection: Close\n\r\n\r",strlen(buffer));
-                            client.send(echoHeader,strlen(echoHeader));
-                            client.send(buffer,strlen(buffer));
-                            clientIsConnected = false;
-                            printf("echo back done.\n\r");
-                        }
-                        break;
-                }
+      while(clientIsConnected) 
+      {
+        char buffer[1024] = {};
+                
+        switch(client.receive(buffer, 1023)) 
+        {
+          case 0:
+            pc.printf("recieved buffer is empty.\n\r");
+            clientIsConnected = false;
+            break;
+                        
+          case -1:
+            pc.printf("failed to read data from client.\n\r");
+            clientIsConnected = false;
+            break;
+                        
+          default:
+            pc.printf("Recieved Data: %d\n\r\n\r%.*s\n\r",strlen(buffer),strlen(buffer),buffer);
+                        
+            if(buffer[0] == 'G' && buffer[1] == 'E' && buffer[2] == 'T' ) 
+            {
+              pc.printf("GET request incomming.\n\r");
+              //setup http response header & data
+              char echoHeader[256] = {};
+              sprintf(echoHeader,"HTTP/1.1 200 OK\n\rContent-Length: %d\n\rContent-Type: text\n\rConnection: Close\n\r\n\r",strlen(buffer));
+              client.send(echoHeader,strlen(echoHeader));
+              client.send(buffer,strlen(buffer));
+              clientIsConnected = false;
+              pc.printf("echo back done.\n\r");
             }
-            printf("close connection.\n\rtcp server is listening...\n\r");
-            client.close();
-            led2 = false;
+            
+            break;
         }
+      }
+            
+      pc.printf("close connection.\n\rtcp server is listening...\n\r");
+            
+      client.close();
     }
+  }
 }