Patryk Patlas / Mbed 2 deprecated SiSK_HTTP

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of HTTP_SD_Server_K64F by FRDM-K64F Code Share

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 
00004 #include <stdio.h>
00005 #include <string.h>
00006 
00007 #define HTTPD_SERVER_PORT   80
00008 #define HTTPD_MAX_REQ_LENGTH   1023
00009 #define HTTPD_MAX_HDR_LENGTH   2056
00010 
00011 #define LED_R 26
00012 #define LED_G 22
00013 #define LED_B 21
00014 
00015 
00016 Serial uart(USBTX, USBRX);
00017 
00018 
00019 EthernetInterface eth;
00020 TCPSocketServer server;
00021 TCPSocketConnection client;
00022 
00023 char buffer[HTTPD_MAX_REQ_LENGTH+1];
00024 char httpHeader[HTTPD_MAX_HDR_LENGTH+1];
00025 
00026 char *uristr;
00027 char *eou;
00028 char *qrystr;
00029 
00030 
00031 int rdCnt;
00032 
00033 char *first = "/first";
00034 char *sec = "/second";
00035 char *led = "/led";
00036 char *ledr = "/led/r";
00037 char *ledg = "/led/g";
00038 char *ledb = "/led/b";
00039 
00040 DigitalOut ledR(LED1,1);
00041 DigitalOut ledG(LED2,1);
00042 DigitalOut ledB(LED3,1);
00043 
00044 char led_stat = 0;
00045 void show_page(char* uri)
00046 {
00047     uart.printf("Trying to open requested uri\r\n");
00048     uart.printf("%s\r\n",uri);
00049     char *lstchr_ptr = strrchr(uri, NULL) -1; //function try to find char which is non ascii (recently set to 0 so no ascii)
00050     if(!strcmp(uri, first)){
00051         sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
00052         client.send(httpHeader,strlen(httpHeader));
00053         sprintf(httpHeader,"<html><head><title>First_one</title></head><body><h1>First page has been opened</h1></body></html>");
00054         client.send(httpHeader,strlen(httpHeader));
00055     }
00056     else if(!strcmp(uri, sec)){
00057         sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
00058         client.send(httpHeader,strlen(httpHeader));
00059         sprintf(httpHeader,"<html><head><title>Seond_one</title></head><body><h1>Second page has been opened</h1></body></html>");
00060         client.send(httpHeader,strlen(httpHeader));
00061     }
00062     else if(!strcmp(uri, led)){
00063         sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
00064         client.send(httpHeader,strlen(httpHeader));
00065         sprintf(httpHeader,"<html><head><title>LED</title></head><body> \n"
00066         "<button id='rb' onclick='hget(\"r\")'>RED</button><br/> \n"
00067         "<button id='gb' onclick='hget(\"g\")'>GREEN</button><br/> \n"
00068         "<button id='bb' onclick='hget(\"b\")'>BLUE</button><br/> \n"
00069         "<div id='ramka'>NAPIS</div><br/> \n"
00070         "</h1> \n"
00071         "</body>\n"
00072         "<script type='text/javascript'>\n"
00073         "function hget(val){ \n"
00074         " var x = new XMLHttpRequest(); \n"
00075         " x.onreadystatechange = function() { \n"
00076         " if (x.readyState == 4 && x.status == 200){ \n"
00077         " if(x.responseText=='r') document.body.style.backgroundColor='red';\n"
00078         " else if (x.responseText=='b') document.body.style.backgroundColor='blue';\n"
00079         " else if (x.responseText=='g') document.body.style.backgroundColor='green';\n"
00080         " else document.body.style.backgroundColor='white';}}\n"
00081         " x.open('GET', '/led/'+val, true);\n"
00082         " x.send(null);}\n"
00083         " </script>\n"
00084     "</html>");
00085         client.send(httpHeader,strlen(httpHeader));
00086     }
00087     else if(!strcmp(uri, ledr)){
00088         led_stat ^= 1;
00089         ledR.write(!(led_stat&1));
00090         if(led_stat & 1){
00091             client.send("r",1); //+zapalic led
00092         } else {
00093             client.send("w",1);
00094         }
00095         //uart.printf("RED\n");
00096     }
00097     else if(!strcmp(uri, ledg)){
00098         led_stat ^= 2;
00099         ledG.write(led_stat&2);
00100         if(!(led_stat & 2)){
00101             client.send("g",1); //+zapalic led
00102         } else {
00103             client.send("w",1);
00104         }
00105         //uart.printf("GREEN\n");
00106     }
00107     else if(!strcmp(uri, ledb)){
00108         led_stat ^= 4;
00109         ledB.write(!(led_stat&4));
00110         if(led_stat & 4){
00111             client.send("b",1); //+zapalic led
00112         } else {
00113             client.send("w",1);
00114         }
00115         //uart.printf("BLUE\n");
00116     }
00117     else{
00118         sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
00119         client.send(httpHeader,strlen(httpHeader));
00120         sprintf(httpHeader,"<html><head><title>ERROR</title></head><body><h1>THERE IS NO SUCH PAGE</h1></body></html>");
00121         client.send(httpHeader,strlen(httpHeader));
00122     }
00123 }
00124 
00125 int main (void)
00126 {
00127     //ledR.write(1);
00128     //ledB.write(1);
00129 //    Serial Interface eth;
00130     uart.baud(115200);
00131     uart.printf("Initializing\r\n");
00132 
00133 //    EthernetInterface eth;
00134     uart.printf("Initializing Ethernet ...\r\n");
00135     if(!eth.init("192.168.5.10", "255.255.255.0", "192.168.5.50")){//Init interface with static IP
00136         uart.printf("Ethernet interface configured properly\r\n");
00137     }
00138     uart.printf("Setting interface UP ...\r\n");
00139     eth.connect();
00140     uart.printf("IP: %s\r\n", eth.getIPAddress());
00141     uart.printf("Mask: %s\r\n", eth.getNetworkMask());
00142 
00143 //    TCPSocketServer server;
00144     uart.printf("Starting TCP server at port: %d\r\n", HTTPD_SERVER_PORT);
00145     server.bind(HTTPD_SERVER_PORT);
00146     server.listen();
00147     uart.printf("Server starts listening at port: %d\r\n", HTTPD_SERVER_PORT);
00148 
00149     while (true) {
00150         uart.printf("\nWaiting for new connection...\r\n");
00151         server.accept(client);
00152         client.set_blocking(false, 1500); // Timeout after (1.5)s
00153 
00154         uart.printf("Client with IP %s has connected with server.\r\n", client.get_address());
00155         while (true) {
00156             int n = client.receive(buffer, sizeof(buffer));
00157             if (n <= 0) break;
00158             uart.printf("Recieved Data: %d\r\n\r\n%.*s\r\n",n,n,buffer);
00159             if (n >= 1024) {
00160                 sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
00161                 client.send(httpHeader,strlen(httpHeader));
00162                 client.send(buffer,n);
00163                 break;
00164             } else {
00165                 buffer[n]=0;
00166             }
00167             if (!strncmp(buffer, "GET ", 4)) {
00168                 uristr = buffer + 4;
00169                 eou = strstr(uristr, " ");
00170                 if (eou == NULL) {
00171                     sprintf(httpHeader,"HTTP/1.1 400 Bad Request \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
00172                     client.send(httpHeader,strlen(httpHeader));
00173                     client.send(buffer,n);
00174                 } else {
00175                     *eou = 0;
00176                     show_page(uristr);
00177                 }
00178             }
00179         }
00180 
00181         client.close();
00182     }
00183 }