HTTP Server, WebSocket support
Dependencies: EthernetInterface HTTPD mbed-rtos mbed mbed-rpc
HTTP Server library
for EthernetInterface or compatible interface
multi connection, keep alive support.
Filesystem:
http://192.168.1.2/mbed.htm
CGI:
http://192.168.1.2/cgi-bin/test?query
WebSocket:
ws://192.168.1.2/ws/test
RPC:
http://192.168.1.2/rpc/DigitalOut/new%20LED4%20myled http://192.168.1.2/rpc/myled/write%201
Import libraryHTTPD
HTTP Server, WebSocket support
HTTPD.h
#define HTTPD_PORT 80 #define HTTPD_MAX_CLIENTS 2 #define HTTPD_KEEPALIVE 10
Revision 0:c4a353ed707d, committed 2013-11-13
- Comitter:
- okini3939
- Date:
- Wed Nov 13 02:00:05 2013 +0000
- Child:
- 1:2971dd3e5168
- Commit message:
- 1st build
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EthernetInterface.lib Wed Nov 13 02:00:05 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/EthernetInterface/#cba86db5ab96
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HTTPD.lib Wed Nov 13 02:00:05 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/okini3939/code/HTTPD/#d18dff347122
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Wed Nov 13 02:00:05 2013 +0000
@@ -0,0 +1,65 @@
+#include "mbed.h"
+#include "rtos.h"
+#include "EthernetInterface.h"
+#include "HTTPD.h"
+
+EthernetInterface *eth;
+HTTPD *httpd;
+
+Serial pc(USBTX, USBRX);
+LocalFileSystem local("local");
+DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4);
+
+void callback_cgi (int id) {
+ int i, n;
+ char buf[256];
+
+ strcpy(buf, httpd->getFilename(id));
+ strcat(buf, "\r\n");
+ strcat(buf, httpd->getQueryString(id));
+ strcat(buf, "\r\n");
+ n = strlen(buf);
+
+ i = httpd->receive(id, &buf[n], sizeof(buf) - n);
+ if (i < 0) return;
+ i += n;
+ buf[i] = 0;
+
+ printf("CGI %d %s\r\n", id, buf);
+ httpd->send(id, buf, i, "Content-Type: text/plain\r\n");
+}
+
+void callback_ws (int id) {
+ int i;
+ char buf[256];
+
+ i = httpd->receive(id, buf, sizeof(buf));
+ if (i < 0) return;
+ buf[i] = 0;
+
+ printf("WS %d %s\r\n", id, buf);
+ httpd->sendWebsocket(id, buf, i);
+}
+
+int main () {
+
+ pc.baud(115200);
+ printf("HTTP Server...\r\n");
+
+ eth = new EthernetInterface;
+ eth->init(); //Use DHCP
+// eth->init("192.168.1.2", "255.255.255.0", "192.168.1.1");
+ if (eth->connect()) return -1;
+ printf("IP Address is %s\r\n", eth->getIPAddress());
+
+ httpd = new HTTPD;
+ httpd->attach("/cgi-bin/", &callback_cgi);
+ httpd->attach("/ws/", &callback_ws);
+ httpd->attach("/", "/local/");
+ httpd->start(80);
+ printf("httpd ready\r\n");
+ led1 = 1;
+
+ for (;;) {
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Wed Nov 13 02:00:05 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#ee87e782d34f
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Nov 13 02:00:05 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/f37f3b9c9f0b \ No newline at end of file
Suga koubou
