This is a working HTTP server with I2C, GPIO and PWM commands accessible through URLs. It also includes the ability to serve files from an SD card.

Dependencies:   EthernetInterfaceMuri SDFileSystem mbed-rtos mbed

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 #include "SDFileSystem.h"
00004 #include "muri.h"
00005 #include <stdio.h>
00006 #include <string.h>
00007 
00008 #define HTTPD_SERVER_PORT   80
00009 #define HTTPD_MAX_REQ_LENGTH   1023
00010 #define HTTPD_MAX_HDR_LENGTH   255
00011 #define HTTPD_MAX_FNAME_LENGTH   127
00012 #define MURI_MAX_RESP_LENGTH  127
00013 #define I2C_BUFFER_SIZE 31
00014 
00015 SDFileSystem sd(p5, p6, p7, p8, "sd"); //
00016 // I2C i2c(p28, p27);
00017 EthernetInterface eth;
00018 TCPSocketServer server;
00019 TCPSocketConnection client;
00020 
00021 char buffer[HTTPD_MAX_REQ_LENGTH+1];
00022 char httpHeader[HTTPD_MAX_HDR_LENGTH+1];
00023 char filename[HTTPD_MAX_FNAME_LENGTH+1];
00024 char obuf[MURI_MAX_RESP_LENGTH+1];
00025 char data[I2C_BUFFER_SIZE];
00026 char *uristr;
00027 char *eou;
00028 char *qrystr;
00029 
00030 FILE *fp;
00031 int rdCnt;
00032 
00033 void get_file(char* uri)
00034 {
00035     printf("get_file %s\n", uri);
00036     char *lstchr = strrchr(uri, NULL) -1;
00037     if ('/' == *lstchr) {
00038         printf("Open directory /sd%s\n", uri);
00039         *lstchr = 0;
00040         sprintf(filename, "/sd%s", uri);
00041         DIR *d = opendir(filename);
00042         if (d != NULL) {
00043             sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
00044             client.send(httpHeader,strlen(httpHeader));
00045             sprintf(httpHeader,"<html><head><title>Directory Listing</title></head><body><h1>%s</h1><ul>", uri);
00046             client.send(httpHeader,strlen(httpHeader));
00047             struct dirent *p;
00048             while((p = readdir(d)) != NULL) {
00049                 printf("%s\n", p->d_name);
00050                 sprintf(httpHeader,"<li>%s</li>", p->d_name);
00051                 client.send(httpHeader,strlen(httpHeader));
00052             }
00053         }
00054         closedir(d);
00055         printf("Directory closed\n");
00056         sprintf(httpHeader,"</ul></body></html>");
00057         client.send(httpHeader,strlen(httpHeader));
00058     } else {
00059         sprintf(filename, "/sd%s", uri);
00060         fp = fopen(filename, "r");
00061         if (fp == NULL) {
00062             sprintf(httpHeader,"HTTP/1.1 404 Not Found \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
00063             client.send(httpHeader,strlen(httpHeader));
00064             client.send(uri,strlen(uri));
00065         } else {
00066             sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
00067             client.send(httpHeader,strlen(httpHeader));
00068             while ((rdCnt = fread(buffer, sizeof( char ), 1024, fp)) == 1024) {
00069                 client.send(buffer, rdCnt);
00070             }
00071             client.send(buffer, rdCnt);
00072             fclose(fp);
00073         }
00074     }
00075 }
00076 
00077 void get_cgi(char* uri)
00078 {
00079     char *result;
00080     muri(uri, data, obuf);
00081     if (!strncmp(obuf, "200 ", 4)) {
00082         result = obuf +4;
00083         sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
00084         client.send(httpHeader,strlen(httpHeader));
00085         client.send(result,strlen(result));
00086         printf("result:  %s", result);
00087     } else {
00088         sprintf(httpHeader,"HTTP/1.1 ");
00089         client.send(httpHeader,strlen(httpHeader));
00090         client.send(obuf,strlen(obuf));
00091         sprintf(httpHeader,"\r\n");
00092         client.send(httpHeader,strlen(httpHeader)); 
00093     }
00094     
00095 }
00096 
00097 
00098 int main (void)
00099 {
00100 //    EthernetInterface eth;
00101     eth.init(); //Use DHCP
00102     eth.connect();
00103     printf("IP Address is %s\n", eth.getIPAddress());
00104 
00105 //    TCPSocketServer server;
00106     server.bind(HTTPD_SERVER_PORT);
00107     server.listen();
00108     
00109     init_dio(); //initialize pmd digital IO
00110 
00111     while (true) {
00112         printf("\nWait for new connection...\r\n");
00113         server.accept(client);
00114         client.set_blocking(false, 1500); // Timeout after (1.5)s
00115 
00116         printf("Connection from: %s\r\n", client.get_address());
00117         while (true) {
00118             int n = client.receive(buffer, sizeof(buffer));
00119             if (n <= 0) break;
00120             printf("Recieved Data: %d\r\n\r\n%.*s\r\n",n,n,buffer);
00121             if (n >= 1024) {
00122                 sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
00123                 client.send(httpHeader,strlen(httpHeader));
00124                 client.send(buffer,n);
00125                 break;
00126             } else {
00127                 buffer[n]=0;
00128             }
00129             if (!strncmp(buffer, "GET ", 4)) {
00130                 uristr = buffer + 4;
00131                 eou = strstr(uristr, " ");
00132                 if (eou == NULL) {
00133                     sprintf(httpHeader,"HTTP/1.1 400 Bad Request \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
00134                     client.send(httpHeader,strlen(httpHeader));
00135                     client.send(buffer,n);
00136                 } else {
00137                     *eou = 0;
00138                     if (!strncmp(uristr, "/muri/", 6)) {
00139                         get_cgi(uristr+6);
00140                     } else {
00141                         get_file(uristr);
00142                     }
00143                 }
00144             }
00145         }
00146 
00147         client.close();
00148     }
00149 }