ARD2PMD Web Server Web Server using the ARD2PMD adapter and the SPI2SD adapter with the Seeeduino Arch Pro to serve files from the micro SD card and provide RESTful access to the Pmod connector

Dependencies:   ARD2PMD EthernetInterface MAX14661 PmodInterface SDFileSystem mbed-rtos mbed

Fork of ARD2PMD_WebServer by Greg Steiert

Revision:
4:7fdb64fe6419
Parent:
3:4f71a37a1ed2
Child:
5:892b329134ca
--- a/main.cpp	Sat Apr 19 05:26:40 2014 +0000
+++ b/main.cpp	Tue Apr 29 04:46:29 2014 +0000
@@ -1,6 +1,6 @@
 #include "mbed.h"
 #include "EthernetInterface.h"
-#include "SDFileSystem.h"
+#include "PmodInterface.h"
 #include <stdio.h>
 #include <string.h>
 
@@ -9,11 +9,16 @@
 #define HTTPD_MAX_HDR_LENGTH   255
 #define HTTPD_MAX_FNAME_LENGTH   127
 #define HTTPD_MAX_DNAME_LENGTH   127
+#define CGI_MAX_RESP_LENGTH  127
+#define UART_MAX_CMD_LENGTH  127
 
 Serial uart(USBTX, USBRX);
+I2C i2c(p28, p27);
+mbd2pmd m2p;
+PmodInterface pInt;
 
 //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(P0_18, P0_17, P0_15, P0_16, "sd"); // Seeeduino Arch Pro SPI2SD
 
 EthernetInterface eth;
 TCPSocketServer server;
@@ -23,6 +28,8 @@
 char httpHeader[HTTPD_MAX_HDR_LENGTH+1];
 char fileName[HTTPD_MAX_FNAME_LENGTH+1];
 char dirName[HTTPD_MAX_DNAME_LENGTH+1];
+char obuf[CGI_MAX_RESP_LENGTH+1];
+char ibuf[UART_MAX_CMD_LENGTH+1];
 char *uristr;
 char *eou;
 char *qrystr;
@@ -80,11 +87,51 @@
     }
 }
 
+void get_cgi(char* uri)
+{
+    pInt.call(uri, obuf);
+    sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
+    client.send(httpHeader,strlen(httpHeader));
+    client.send(obuf,strlen(obuf));
+    uart.printf("resp:  %s", obuf);
+}
+
+void uart_cmd_thread(void const *args)
+{
+    int i = 0;
+    while(true) {
+        if (uart.readable()) {
+            ibuf[i]=uart.getc();
+            if (ibuf[i]!='\r') {
+                if (i < 255) {
+                    i += 1;
+                }
+            } else {
+                if (i < 255) {
+                    ibuf[i]=0;
+                    uart.printf("UART CMD:  %s=", ibuf);
+                    pInt.call(ibuf, obuf);
+                    uart.printf("%s\n", obuf);
+                } else {
+                    uart.printf("UART Input Command Too Long\n");
+                }
+                i=0;
+            }
+        }
+    }
+}
+
 int main (void)
 {
-//    Serial Interface eth;
+//    Serial Interface Setup;
     uart.baud(115200);
-    uart.printf("RAPM Serial Started\n");
+    uart.printf("mbd2pmd Serial Started\n");
+
+//    Mbed to Pmod Initialization;
+    m2p.init();
+
+//    Pmod Interface Setup
+    pInt.init(&m2p.pmd[0], &i2c, &m2p.mux, m2p.mux_a, m2p.mux_p);
 
 //    Check File System
     DIR *d = opendir("/sd/");
@@ -102,7 +149,11 @@
 //    TCPSocketServer server;
     server.bind(HTTPD_SERVER_PORT);
     server.listen();
-    uart.printf("RAPM Server Listening\n");
+    uart.printf("mbd2pmd Server Listening\n");
+
+//    Start UART Command Thread
+    Thread thread(uart_cmd_thread);
+    uart.printf("UART Commands Listening\n");
 
     while (true) {
         uart.printf("\nWait for new connection...\r\n");
@@ -131,7 +182,11 @@
                     client.send(buffer,n);
                 } else {
                     *eou = 0;
-                    get_file(uristr);
+                    if (!strncmp(uristr, "/cgi/", 5)) {
+                        get_cgi(uristr+4);
+                    } else {
+                        get_file(uristr);
+                    }
                 }
             }
         }