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
main.cpp
- Committer:
- okini3939
- Date:
- 2013-11-13
- Revision:
- 1:2971dd3e5168
- Parent:
- 0:c4a353ed707d
File content as of revision 1:2971dd3e5168:
#include "mbed.h"
#include "rtos.h"
#include "EthernetInterface.h"
#include "mbed_rpc.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);
}
void callback_rpc (int id) {
char buf[40], outbuf[40];
strcpy(buf, "/");
httpd->urldecode(httpd->getFilename(id), &buf[1], sizeof(buf) - 2);
RPC::call(buf, outbuf);
printf("RPC id %d '%s' '%s'\r\n", id, buf, outbuf);
httpd->send(id, outbuf, strlen(outbuf), "Content-Type: text/plain\r\n");
}
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());
// RPC::add_rpc_class<RpcAnalogIn>();
// RPC::add_rpc_class<RpcAnalogOut>();
RPC::add_rpc_class<RpcDigitalIn>();
RPC::add_rpc_class<RpcDigitalOut>();
RPC::add_rpc_class<RpcDigitalInOut>();
RPC::add_rpc_class<RpcPwmOut>();
httpd = new HTTPD;
httpd->attach("/cgi-bin/", &callback_cgi);
httpd->attach("/ws/", &callback_ws);
httpd->attach("/rpc/", &callback_rpc);
httpd->attach("/", "/local/");
httpd->start(80);
printf("httpd ready\r\n");
led1 = 1;
for (;;) {
}
}
Suga koubou
