Erling Lindholm / Mbed 2 deprecated Homerling_WIZwiki-W7500

Dependencies:   SDFileSystem WIZnetInterface 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 "readsensors.h"
00004 #include "SDFileSystem.h"
00005 #include <stdio.h>
00006 #include <string.h>
00007 
00008 
00009 //#define MAC     "\x00\x08\xDC\x11\x34\x78"
00010 #define MAC     "\x00\x08\xDC\x00\x01\x02"
00011 #define IP      "192.168.77.100"
00012 #define MASK    "255.255.255.0"
00013 #define GATEWAY "192.168.77.1"
00014 
00015 #define HTTPD_SERVER_PORT   80
00016 #define HTTPD_MAX_REQ_LENGTH   1023
00017 #define HTTPD_MAX_HDR_LENGTH   255
00018 #define HTTPD_MAX_FNAME_LENGTH   127
00019 #define HTTPD_MAX_DNAME_LENGTH   127
00020 
00021 Serial uart(USBTX, USBRX);
00022 Serial aurdino(D1,D0);
00023 
00024 SDFileSystem sd(PB_3, PB_2, PB_1, PB_0, "sd"); // WIZwiki-W7500 
00025 
00026 EthernetInterface eth;
00027 TCPSocketServer server;
00028 TCPSocketConnection client;
00029 ReadSensors sensor;
00030 
00031 char buffer[HTTPD_MAX_REQ_LENGTH+1];
00032 char sensorBuffer[42];
00033 char httpHeader[HTTPD_MAX_HDR_LENGTH+1];
00034 char fileName[HTTPD_MAX_FNAME_LENGTH+1];
00035 char dirName[HTTPD_MAX_DNAME_LENGTH+1];
00036 char *uristr;
00037 char *eou;
00038 char *qrystr;
00039 char *sensorData;
00040 
00041 FILE *fp;
00042 int rdCnt;
00043 
00044 DigitalOut led1(LED1); //server listning status
00045 DigitalOut led2(LED2); //socket connecting status
00046 
00047 Ticker ledTick;
00048 
00049 void ledTickfunc()
00050 {
00051     led1 = !led1;
00052 }
00053 
00054 
00055 
00056 void get_file(char* uri)
00057 {
00058     uart.printf("get_file %s\n", uri);
00059     char *lstchr = strrchr(uri, NULL) -1;
00060     if ('/' == *lstchr) {
00061         uart.printf("Open directory /sd%s\n", uri);
00062         *lstchr = 0;
00063         sprintf(fileName, "/sd%s", uri);
00064         DIR *d = opendir(fileName);
00065         if (d != NULL) {
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             sprintf(httpHeader,"<html><head><title>Directory Listing</title></head><body><h1>%s Directory Listing</h1><ul>", uri);
00069             client.send(httpHeader,strlen(httpHeader));
00070             struct dirent *p;
00071             while((p = readdir(d)) != NULL) {
00072                 sprintf(dirName, "%s/%s", fileName, p->d_name);
00073                 uart.printf("%s\n", dirName);
00074                 DIR *subDir = opendir(dirName);
00075                 if (subDir != NULL) {
00076                     sprintf(httpHeader,"<li><a href=\"./%s/\">%s/</a></li>", p->d_name, p->d_name);
00077                 } else {
00078                     sprintf(httpHeader,"<li><a href=\"./%s\">%s</a></li>", p->d_name, p->d_name);
00079                 }
00080                 client.send(httpHeader,strlen(httpHeader));
00081             }
00082         }
00083         closedir(d);
00084         uart.printf("Directory closed\n");
00085         sprintf(httpHeader,"</ul></body></html>");
00086         client.send(httpHeader,strlen(httpHeader));
00087     } else {
00088         sprintf(fileName, "/sd%s", uri);
00089         fp = fopen(fileName, "r");
00090         if (fp == NULL) {
00091             uart.printf("File not found\n");
00092             sprintf(httpHeader,"HTTP/1.1 404 Not Found \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
00093             client.send(httpHeader,strlen(httpHeader));
00094             client.send(uri,strlen(uri));
00095         } else {
00096             uart.printf("Sending: header");
00097             sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
00098             client.send(httpHeader,strlen(httpHeader));
00099             
00100             sprintf(httpHeader,"Temp sensors %s\r\n\r\n", sensorBuffer);
00101             client.send(httpHeader,strlen(httpHeader));            
00102             uart.printf(" file");
00103             while ((rdCnt = fread(buffer, sizeof( char ), 1024, fp)) == 1024) {
00104                 client.send(buffer, rdCnt);
00105                 uart.printf(".");
00106             }
00107             client.send(buffer, rdCnt);
00108             fclose(fp);
00109             uart.printf("done\n");
00110         }
00111     }
00112 }
00113 
00114 int main (void)
00115 {
00116     ledTick.attach(&ledTickfunc,0.5);
00117 //    Serial Interface eth;
00118     uart.baud(115200);
00119     uart.printf("Initializing\n");
00120     aurdino.baud(9600);
00121 
00122 //    Check File System
00123     uart.printf("Checking File System\n");
00124     DIR *d = opendir("/sd/");
00125     if (d != NULL) {
00126         uart.printf("SD Card Present\n");
00127     } else {
00128         uart.printf("SD Card Root Directory Not Found\n");
00129     }
00130 
00131     uart.printf("Initializing Ethernet\n");
00132     eth.init((uint8_t*)MAC,IP,MASK,GATEWAY);  //IP,mask,Gateway
00133     uart.printf("Connecting\n");
00134     eth.connect();
00135     uart.printf("IP Address is %s\n", eth.getIPAddress());
00136 
00137 //    TCPSocketServer server;
00138     server.bind(HTTPD_SERVER_PORT);
00139     server.listen();
00140     uart.printf("Server Listening\n");
00141 
00142     while (true) {
00143         uart.printf("\nWait for new connection...\r\n");
00144         server.accept(client);
00145         client.set_blocking(false, 6500); // Timeout after (1.5)s (6.5s)
00146 
00147         uart.printf("Connection from: %s\r\n", client.get_address());
00148         while (true) {
00149             led2 = true;
00150             int n = client.receive(buffer, sizeof(buffer));
00151             if (n <= 0) break;
00152             uart.printf("Recieved Data: %d\r\n\r\n%.*s\r\n",n,n,buffer);
00153             if (n >= 1024) {
00154                 sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
00155                 client.send(httpHeader,strlen(httpHeader));
00156                 client.send(buffer,n);
00157                 break;
00158             } else {
00159                 buffer[n]=0;
00160             }
00161             if (!strncmp(buffer, "GET ", 4)) {
00162                 uristr = buffer + 4;
00163                 eou = strstr(uristr, " ");
00164                 if (eou == NULL) {
00165                     sprintf(httpHeader,"HTTP/1.1 400 Bad Request \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
00166                     client.send(httpHeader,strlen(httpHeader));
00167                     client.send(buffer,n);
00168                 } else {
00169                     *eou = 0;
00170                     get_file(uristr);
00171                     sensor.readData(sensorData);
00172                 }
00173             }
00174         }
00175         led2 = false;
00176         client.close();
00177     }
00178 }