Release V0.1
Dependencies: SDFileSystem WIZnetInterface mbed
Fork of HTTP_SDCard_File_Server_WIZwiki-W7500P by
Revision 0:48575e6a55f9, committed 2016-07-15
- Comitter:
- jh_ndm
- Date:
- Fri Jul 15 08:01:43 2016 +0000
- Commit message:
- DFRobot httpsever
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SDFileSystem.lib Fri Jul 15 08:01:43 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/SDFileSystem/#7b35d1709458
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WIZnetInterface.lib Fri Jul 15 08:01:43 2016 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/teams/WIZnet/code/WIZnetInterface/#c91884bd2713
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Fri Jul 15 08:01:43 2016 +0000
@@ -0,0 +1,173 @@
+#include "mbed.h"
+#include "EthernetInterface.h"
+#include "SDFileSystem.h"
+#include <stdio.h>
+#include <string.h>
+
+
+#define MAC "\x00\x08\xDC\x11\x34\x78"
+#define IP "192.168.1.210"
+#define MASK "255.255.255.0"
+#define GATEWAY "192.168.1.1"
+
+#define HTTPD_SERVER_PORT 80
+#define HTTPD_MAX_REQ_LENGTH 1023
+#define HTTPD_MAX_HDR_LENGTH 255
+#define HTTPD_MAX_FNAME_LENGTH 127
+#define HTTPD_MAX_DNAME_LENGTH 127
+
+Serial uart(PA_13,PA_14);
+
+//SDFileSystem sd(p5, p6, p7, p8, "sd"); // LPC1768 MBD2PMD
+//SDFileSystem sd(P0_18, P0_17, P0_15, P0_16, "sd"); // Seeeduino Arch Pro SPI2SD
+//SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // K64F
+SDFileSystem sd(PB_3, PB_2, PB_1, PB_0, "sd"); // WIZwiki-W7500
+
+EthernetInterface eth;
+TCPSocketServer server;
+TCPSocketConnection client;
+
+char buffer[HTTPD_MAX_REQ_LENGTH+1];
+char httpHeader[HTTPD_MAX_HDR_LENGTH+1];
+char fileName[HTTPD_MAX_FNAME_LENGTH+1];
+char dirName[HTTPD_MAX_DNAME_LENGTH+1];
+char *uristr;
+char *eou;
+char *qrystr;
+
+FILE *fp;
+int rdCnt;
+
+DigitalOut led1(D10); //server listning status
+DigitalOut led2(D11); //socket connecting status
+
+Ticker ledTick;
+
+void ledTickfunc()
+{
+ led1 = !led1;
+}
+
+
+
+void getFile(char* uri)
+{
+ uart.printf("getFile %s\n", uri);
+ char *lstchr = strrchr(uri, NULL) -1;
+ if ('/' == *lstchr) {
+ uart.printf("Open directory /sd%s\r\n", uri);
+ *lstchr = 0;
+ sprintf(fileName, "/sd%s", uri);
+ DIR *d = opendir(fileName);
+ if (d != NULL) {
+ 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>Directory Listing</title></head><body><h1>%s Directory Listing</h1><ul>", uri);
+ client.send(httpHeader,strlen(httpHeader));
+ struct dirent *p;
+ while((p = readdir(d)) != NULL) {
+ sprintf(dirName, "%s/%s", fileName, p->d_name);
+ uart.printf("%s\n", dirName);
+ DIR *subDir = opendir(dirName);
+ if (subDir != NULL) {
+ sprintf(httpHeader,"<li><a href=\"./%s/\">%s/</a></li>", p->d_name, p->d_name);
+ } else {
+ sprintf(httpHeader,"<li><a href=\"./%s\">%s</a></li>", p->d_name, p->d_name);
+ }
+ client.send(httpHeader,strlen(httpHeader));
+ }
+ }
+ closedir(d);
+ uart.printf("Directory closed\r\n");
+ sprintf(httpHeader,"</ul></body></html>");
+ client.send(httpHeader,strlen(httpHeader));
+ } else {
+ sprintf(fileName, "/sd%s", uri);
+ fp = fopen(fileName, "r");
+ if (fp == NULL) {
+ uart.printf("File not found\r\n");
+ sprintf(httpHeader,"HTTP/1.1 404 Not Found \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
+ client.send(httpHeader,strlen(httpHeader));
+ client.send(uri,strlen(uri));
+ } else {
+ uart.printf("Sending: header");
+ sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
+ client.send(httpHeader,strlen(httpHeader));
+ uart.printf(" file");
+ while ((rdCnt = fread(buffer, sizeof( char ), 1024, fp)) == 1024) {
+ client.send(buffer, rdCnt);
+ uart.printf(".");
+ }
+ client.send(buffer, rdCnt);
+ fclose(fp);
+ uart.printf("done\r\n");
+ }
+ }
+}
+
+int main (void)
+{
+ ledTick.attach(&ledTickfunc,0.5);
+// Serial Interface eth;
+ uart.baud(115200);
+ uart.printf("Initializing\r\n");
+
+// Check File System
+ uart.printf("Checking File System\r\n");
+ DIR *d = opendir("/sd/");
+ if (d != NULL) {
+ uart.printf("SD Card Present\r\n");
+ } else {
+ uart.printf("SD Card Root Directory Not Found\r\n");
+ }
+
+// EthernetInterface eth;
+ uart.printf("Initializing Ethernet\r\n");
+ //eth.init(); //Use DHCP
+ //eth.init((uint8_t*)MAC); // Use DHCP for WIZnetInterface
+ eth.init((uint8_t*)MAC,IP,MASK,GATEWAY); //IP,mask,Gateway
+ uart.printf("Connecting\r\n");
+ eth.connect();
+ uart.printf("IP Address is %s\r\n", eth.getIPAddress());
+
+// TCPSocketServer server;
+ server.bind(HTTPD_SERVER_PORT);
+ server.listen();
+ uart.printf("Server Listening\r\n");
+
+ while (true) {
+ uart.printf("\nWait for new connection...\r\n");
+ server.accept(client);
+ client.set_blocking(false, 1500); // Timeout after (1.5)s
+
+ uart.printf("Connection from: %s\r\n", client.get_address());
+ while (true) {
+ led2 = true;
+ int n = client.receive(buffer, sizeof(buffer));
+ if (n <= 0) break;
+ uart.printf("Recieved Data: %d\r\n\r\n%.*s\r\n",n,n,buffer);
+ if (n >= 1024) {
+ sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
+ client.send(httpHeader,strlen(httpHeader));
+ client.send(buffer,n);
+ break;
+ } else {
+ buffer[n]=0;
+ }
+ if (!strncmp(buffer, "GET ", 4)) {
+ uristr = buffer + 4;
+ eou = strstr(uristr, " ");
+ if (eou == NULL) {
+ sprintf(httpHeader,"HTTP/1.1 400 Bad Request \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
+ client.send(httpHeader,strlen(httpHeader));
+ client.send(buffer,n);
+ } else {
+ *eou = 0;
+ getFile(uristr);
+ }
+ }
+ }
+ led2 = false;
+ client.close();
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Fri Jul 15 08:01:43 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/aae6fcc7d9bb \ No newline at end of file
