Free Wed Editor CoffeeFreeHTML15.0

Dependencies:   WIZnetInterface mbed

Fork of WIZ_Proto_Makers_4th_Lab1 by Ricky Kwon

Revision:
0:722e7ff3a065
Child:
1:a4e5ec9f59cc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Feb 03 03:04:46 2016 +0000
@@ -0,0 +1,140 @@
+#include "mbed.h"
+#include "EthernetInterface.h"
+
+#define WEB_SERVER_PORT   80
+char send_dat[1024]={0,};
+uint16_t calcu_len(void);
+
+DigitalOut myled(LED1);
+
+int main (void) 
+{
+    printf("Wait a second...\r\n");
+    uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02}; 
+    const char ip_addr[] = "192.168.0.222"; 
+    const char mask_addr[] = "255.255.255.0"; 
+    const char gateway_addr[] = "192.168.0.1";
+    
+    EthernetInterface eth;
+    //eth.init(mac_addr); //Use DHCP
+    eth.init(mac_addr, ip_addr, mask_addr, gateway_addr); //Use Static
+    eth.connect();
+    printf("Server IP Address is %s\r\n", eth.getIPAddress());
+    
+    TCPSocketServer server;
+    server.bind(WEB_SERVER_PORT);
+    server.listen();
+
+    while (true) 
+    {
+        printf("Wait for new connection...\r\n");
+        TCPSocketConnection client;
+        server.accept(client);
+        client.set_blocking(false, 15000); // Timeout after (1.5)s
+        
+        printf("Connection from: %s\r\n", client.get_address());
+        char buffer[256];
+        char dat[128];
+        char *dat_temp;
+        
+        memset(dat, 0, sizeof(dat));
+        memset(send_dat, 0, sizeof(send_dat));
+        memset(buffer, 0, sizeof(buffer));
+        
+        while (true) {
+            
+            wait(0.5);
+            int n = client.receive_all(buffer, sizeof(buffer));
+            if (n <= 0) break;
+            //buffer[n] = '\0';
+            
+            printf("rev_dat : %s\r\n", buffer);
+    
+                       
+            if((buffer[0]=='G')&&(buffer[1]=='E')&&(buffer[2]=='T')&&(buffer[3]==' '))// GET_Request
+            { 
+                if((buffer[13]=='?'))
+                {
+                    if((buffer[18]=='O')&&(buffer[19]=='F'))
+                    {
+                        printf("---LED OFF---\r\n");
+                        myled = 1;
+                        dat_temp = "HTTP/1.1 200 OK\r\n";                           strcat(dat, dat_temp);
+                        dat_temp = "Content-Type: text/html\r\n";                   strcat(dat, dat_temp);
+                        dat_temp = "Connection: close\r\n";                         strcat(dat, dat_temp);
+  
+                        sprintf(dat, "%sContent-Length: %d\r\n", dat, calcu_len());     strcat(dat, dat_temp);
+                    
+                        client.send(dat, strlen(dat));
+                        printf("send_dat : %s\r\n", dat);
+                    
+                        client.send(send_dat, sizeof(send_dat));   
+                        printf("send_dat : %s\r\n", send_dat);
+                        
+                        memset(dat, 0, sizeof(dat));
+                        memset(send_dat, 0, sizeof(send_dat));
+                        memset(buffer, 0, sizeof(buffer));
+                    }
+                    else
+                    {
+                        printf("---LED ON---\r\n");
+                        myled = 0;
+                        dat_temp = "HTTP/1.1 200 OK\r\n";                           strcat(dat, dat_temp);
+                        dat_temp = "Content-Type: text/html\r\n";                   strcat(dat, dat_temp);
+                        dat_temp = "Connection: close\r\n";                         strcat(dat, dat_temp);
+
+                        sprintf(dat, "%sContent-Length: %d\r\n", dat, calcu_len());     strcat(dat, dat_temp);
+                    
+                        client.send(dat, strlen(dat));
+                        printf("send_dat : %s\r\n", dat);
+                    
+                        client.send(send_dat, sizeof(send_dat));   
+                        printf("send_dat : %s\r\n", send_dat);
+                        
+                        memset(dat, 0, sizeof(dat));
+                        memset(send_dat, 0, sizeof(send_dat));
+                        memset(buffer, 0, sizeof(buffer));
+                    }                    
+                }
+
+                else
+                {
+                    dat_temp = "HTTP/1.1 200 OK\r\n";                           strcat(dat, dat_temp);
+                    dat_temp = "Content-Type: text/html\r\n";                   strcat(dat, dat_temp);
+                    dat_temp = "Connection: close\r\n";                         strcat(dat, dat_temp);
+                    //len = calcu_len();
+                    sprintf(dat, "%sContent-Length: %d\r\n", dat, calcu_len());     strcat(dat, dat_temp);
+                
+                    client.send(dat, strlen(dat));
+                    printf("send_dat : %s\r\n", dat);
+                
+                    client.send(send_dat, sizeof(send_dat));   
+                    printf("send_dat : %s\r\n", send_dat);
+                    
+                    memset(dat, 0, sizeof(dat));
+                    memset(send_dat, 0, sizeof(send_dat));
+                    memset(buffer, 0, sizeof(buffer));
+                }
+            }
+            
+        }
+        
+        client.close();
+    }
+    
+}
+uint16_t calcu_len(void)
+{
+    char * dat_temp;
+    
+    dat_temp = "<html>\r\n"\
+                "<body>\r\n"\
+                "<a href='led.html?LED=ON'>LED ON</a>\r\n"\
+                "<br>\r\n"\
+                "<a href='led.html?LED=OFF'>LED OFF</a>\r\n"\
+                "<body>\r\n"\                                                                           
+                "<html>\r\n";
+    strcat(send_dat, dat_temp);
+    return strlen(send_dat);
+}
+ 
\ No newline at end of file