Simple server for AGH accademic purpose

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of HTTP_SD_Server_K64F by FRDM-K64F Code Share

Revision:
9:aebb88e6e653
Parent:
8:a27748c1fc33
--- a/main.cpp	Thu Nov 17 16:35:33 2016 +0000
+++ b/main.cpp	Thu Dec 01 23:09:37 2016 +0000
@@ -6,7 +6,11 @@
 
 #define HTTPD_SERVER_PORT   80
 #define HTTPD_MAX_REQ_LENGTH   1023
-#define HTTPD_MAX_HDR_LENGTH   255
+#define HTTPD_MAX_HDR_LENGTH   2056
+
+#define LED_R 26
+#define LED_G 22
+#define LED_B 21
 
 
 Serial uart(USBTX, USBRX);
@@ -28,22 +32,88 @@
 
 char *first = "/first";
 char *sec = "/second";
+char *led = "/led";
+char *ledr = "/led/r";
+char *ledg = "/led/g";
+char *ledb = "/led/b";
+
+DigitalOut ledR(LED1,1);
+DigitalOut ledG(LED2,1);
+DigitalOut ledB(LED3,1);
+
+char led_stat = 0;
 void show_page(char* uri)
 {
-    uart.printf("Trying to open requested uri\n");
+    uart.printf("Trying to open requested uri\r\n");
+    uart.printf("%s\r\n",uri);
     char *lstchr_ptr = strrchr(uri, NULL) -1; //function try to find char which is non ascii (recently set to 0 so no ascii)
-    if(!memcmp(uri, first, 6)){
+    if(!strcmp(uri, first)){
         sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
         client.send(httpHeader,strlen(httpHeader));
         sprintf(httpHeader,"<html><head><title>First_one</title></head><body><h1>First page has been opened</h1></body></html>");
         client.send(httpHeader,strlen(httpHeader));
     }
-    else if(!memcmp(uri, sec, 6)){
+    else if(!strcmp(uri, sec)){
         sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
         client.send(httpHeader,strlen(httpHeader));
         sprintf(httpHeader,"<html><head><title>Seond_one</title></head><body><h1>Second page has been opened</h1></body></html>");
         client.send(httpHeader,strlen(httpHeader));
     }
+    else if(!strcmp(uri, led)){
+        sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
+        client.send(httpHeader,strlen(httpHeader));
+        sprintf(httpHeader,"<html><head><title>LED</title></head><body> \n"
+        "<button id='rb' onclick='hget(\"r\")'>RED</button><br/> \n"
+        "<button id='gb' onclick='hget(\"g\")'>GREEN</button><br/> \n"
+        "<button id='bb' onclick='hget(\"b\")'>BLUE</button><br/> \n"
+        "<div id='ramka'>NAPIS</div><br/> \n"
+        "</h1> \n"
+        "</body>\n"
+        "<script type='text/javascript'>\n"
+        "function hget(val){ \n"
+        " var x = new XMLHttpRequest(); \n"
+        " x.onreadystatechange = function() { \n"
+        " if (x.readyState == 4 && x.status == 200){ \n"
+        " if(x.responseText=='r') document.body.style.backgroundColor='red';\n"
+        " else if (x.responseText=='b') document.body.style.backgroundColor='blue';\n"
+        " else if (x.responseText=='g') document.body.style.backgroundColor='green';\n"
+        " else document.body.style.backgroundColor='white';}}\n"
+        " x.open('GET', '/led/'+val, true);\n"
+        " x.send(null);}\n"
+        " </script>\n"
+    "</html>");
+        client.send(httpHeader,strlen(httpHeader));
+    }
+    else if(!strcmp(uri, ledr)){
+        led_stat ^= 1;
+        ledR.write(!(led_stat&1));
+        if(led_stat & 1){
+            client.send("r",1); //+zapalic led
+        } else {
+            client.send("w",1);
+        }
+        //uart.printf("RED\n");
+    }
+    else if(!strcmp(uri, ledg)){
+        led_stat ^= 2;
+        ledG.write(led_stat&2);
+        if(!(led_stat & 2)){
+            client.send("g",1); //+zapalic led
+        } else {
+            client.send("w",1);
+        }
+        //uart.printf("GREEN\n");
+    }
+    else if(!strcmp(uri, ledb)){
+        led_stat ^= 4;
+        ledB.write(!(led_stat&4));
+        if(led_stat & 4){
+            client.send("b",1); //+zapalic led
+        } else {
+            client.send("w",1);
+        }
+        //uart.printf("BLUE\n");
+    }
     else{
         sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
         client.send(httpHeader,strlen(httpHeader));
@@ -54,25 +124,27 @@
 
 int main (void)
 {
+    //ledR.write(1);
+    //ledB.write(1);
 //    Serial Interface eth;
     uart.baud(115200);
-    uart.printf("Initializing\n");
+    uart.printf("Initializing\r\n");
 
 //    EthernetInterface eth;
-    uart.printf("Initializing Ethernet ...\n");
+    uart.printf("Initializing Ethernet ...\r\n");
     if(!eth.init("192.168.5.10", "255.255.255.0", "192.168.5.50")){//Init interface with static IP
-        uart.printf("Ethernet interface configured properly\n");
+        uart.printf("Ethernet interface configured properly\r\n");
     }
-    uart.printf("Setting interface UP ...\n");
+    uart.printf("Setting interface UP ...\r\n");
     eth.connect();
-    uart.printf("IP: %s\n", eth.getIPAddress());
-    uart.printf("IP: %s\n", eth.getNetworkMask());
+    uart.printf("IP: %s\r\n", eth.getIPAddress());
+    uart.printf("Mask: %s\r\n", eth.getNetworkMask());
 
 //    TCPSocketServer server;
-    uart.printf("Starting TCP server at port: %d", HTTPD_SERVER_PORT);
+    uart.printf("Starting TCP server at port: %d\r\n", HTTPD_SERVER_PORT);
     server.bind(HTTPD_SERVER_PORT);
     server.listen();
-    uart.printf("Server starts listening at port: %d\n", HTTPD_SERVER_PORT);
+    uart.printf("Server starts listening at port: %d\r\n", HTTPD_SERVER_PORT);
 
     while (true) {
         uart.printf("\nWaiting for new connection...\r\n");