Low Memory HTTP Server (LPC1114FN28 + WIZ820io)
Dependencies: Tiny-HTTPD WIZ820ioInterface mbed-rpc mbed
Fork of HTTPD_sample by
main.cpp@2:3a7574ace580, 2014-02-04 (annotated)
- Committer:
- ban4jp
- Date:
- Tue Feb 04 03:26:08 2014 +0000
- Revision:
- 2:3a7574ace580
- Parent:
- 1:2971dd3e5168
LPC1114FN28 + WIZ820io support.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 0:c4a353ed707d | 1 | #include "mbed.h" |
ban4jp | 2:3a7574ace580 | 2 | #include "WIZ820ioInterface.h" |
okini3939 | 1:2971dd3e5168 | 3 | #include "mbed_rpc.h" |
okini3939 | 0:c4a353ed707d | 4 | #include "HTTPD.h" |
okini3939 | 0:c4a353ed707d | 5 | |
ban4jp | 2:3a7574ace580 | 6 | SPI *spi; |
ban4jp | 2:3a7574ace580 | 7 | WIZ820ioInterface *eth; |
okini3939 | 0:c4a353ed707d | 8 | HTTPD *httpd; |
okini3939 | 0:c4a353ed707d | 9 | |
ban4jp | 2:3a7574ace580 | 10 | #if defined(TARGET_LPC1768) |
okini3939 | 0:c4a353ed707d | 11 | Serial pc(USBTX, USBRX); |
okini3939 | 0:c4a353ed707d | 12 | LocalFileSystem local("local"); |
okini3939 | 0:c4a353ed707d | 13 | DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4); |
okini3939 | 0:c4a353ed707d | 14 | |
ban4jp | 2:3a7574ace580 | 15 | #elif defined(TARGET_LPC1114) |
ban4jp | 2:3a7574ace580 | 16 | Serial pc(dp16,dp15); |
ban4jp | 2:3a7574ace580 | 17 | |
ban4jp | 2:3a7574ace580 | 18 | #endif |
ban4jp | 2:3a7574ace580 | 19 | |
ban4jp | 2:3a7574ace580 | 20 | const char* const index_page = |
ban4jp | 2:3a7574ace580 | 21 | "<html>" |
ban4jp | 2:3a7574ace580 | 22 | "<body>" |
ban4jp | 2:3a7574ace580 | 23 | "<h1>Tiny HTTP Server</h1>" |
ban4jp | 2:3a7574ace580 | 24 | "<h2>RPC</h2>" |
ban4jp | 2:3a7574ace580 | 25 | "<a href=\"/rpc/DigitalOut/new%20LED1%20myled\">/rpc/DigitalOut/new LED1 myled</a><br><br>" |
ban4jp | 2:3a7574ace580 | 26 | "<a href=\"/rpc/myled/write%200\">/rpc/myled/write 0</a><br><br>" |
ban4jp | 2:3a7574ace580 | 27 | "<a href=\"/rpc/myled/write%201\">/rpc/myled/write 1</a><br><br>" |
ban4jp | 2:3a7574ace580 | 28 | "</body>" |
ban4jp | 2:3a7574ace580 | 29 | "</html>"; |
ban4jp | 2:3a7574ace580 | 30 | |
ban4jp | 2:3a7574ace580 | 31 | void callback_static (int id) { |
ban4jp | 2:3a7574ace580 | 32 | char buf[256]; |
ban4jp | 2:3a7574ace580 | 33 | |
ban4jp | 2:3a7574ace580 | 34 | strcpy(buf, httpd->getFilename(id)); |
ban4jp | 2:3a7574ace580 | 35 | printf("static %d %s\r\n", id, buf); |
ban4jp | 2:3a7574ace580 | 36 | |
ban4jp | 2:3a7574ace580 | 37 | if (strcmp(buf, "") == 0 || strcmp(buf, "index.html") == 0) { |
ban4jp | 2:3a7574ace580 | 38 | httpd->send(id, index_page, strlen(index_page), "Content-Type: text/html\r\n"); |
ban4jp | 2:3a7574ace580 | 39 | } else { |
ban4jp | 2:3a7574ace580 | 40 | httpd->httpdError(id, 404); |
ban4jp | 2:3a7574ace580 | 41 | } |
ban4jp | 2:3a7574ace580 | 42 | } |
ban4jp | 2:3a7574ace580 | 43 | |
okini3939 | 0:c4a353ed707d | 44 | void callback_cgi (int id) { |
okini3939 | 0:c4a353ed707d | 45 | int i, n; |
okini3939 | 0:c4a353ed707d | 46 | char buf[256]; |
okini3939 | 0:c4a353ed707d | 47 | |
okini3939 | 0:c4a353ed707d | 48 | strcpy(buf, httpd->getFilename(id)); |
okini3939 | 0:c4a353ed707d | 49 | strcat(buf, "\r\n"); |
okini3939 | 0:c4a353ed707d | 50 | strcat(buf, httpd->getQueryString(id)); |
okini3939 | 0:c4a353ed707d | 51 | strcat(buf, "\r\n"); |
okini3939 | 0:c4a353ed707d | 52 | n = strlen(buf); |
okini3939 | 0:c4a353ed707d | 53 | |
okini3939 | 0:c4a353ed707d | 54 | i = httpd->receive(id, &buf[n], sizeof(buf) - n); |
okini3939 | 0:c4a353ed707d | 55 | if (i < 0) return; |
okini3939 | 0:c4a353ed707d | 56 | i += n; |
okini3939 | 0:c4a353ed707d | 57 | buf[i] = 0; |
okini3939 | 0:c4a353ed707d | 58 | |
okini3939 | 0:c4a353ed707d | 59 | printf("CGI %d %s\r\n", id, buf); |
okini3939 | 0:c4a353ed707d | 60 | httpd->send(id, buf, i, "Content-Type: text/plain\r\n"); |
okini3939 | 0:c4a353ed707d | 61 | } |
okini3939 | 0:c4a353ed707d | 62 | |
okini3939 | 0:c4a353ed707d | 63 | void callback_ws (int id) { |
okini3939 | 0:c4a353ed707d | 64 | int i; |
okini3939 | 0:c4a353ed707d | 65 | char buf[256]; |
okini3939 | 0:c4a353ed707d | 66 | |
okini3939 | 0:c4a353ed707d | 67 | i = httpd->receive(id, buf, sizeof(buf)); |
okini3939 | 0:c4a353ed707d | 68 | if (i < 0) return; |
okini3939 | 0:c4a353ed707d | 69 | buf[i] = 0; |
okini3939 | 0:c4a353ed707d | 70 | |
okini3939 | 0:c4a353ed707d | 71 | printf("WS %d %s\r\n", id, buf); |
okini3939 | 0:c4a353ed707d | 72 | httpd->sendWebsocket(id, buf, i); |
okini3939 | 0:c4a353ed707d | 73 | } |
okini3939 | 0:c4a353ed707d | 74 | |
okini3939 | 1:2971dd3e5168 | 75 | void callback_rpc (int id) { |
okini3939 | 1:2971dd3e5168 | 76 | char buf[40], outbuf[40]; |
okini3939 | 1:2971dd3e5168 | 77 | |
okini3939 | 1:2971dd3e5168 | 78 | strcpy(buf, "/"); |
okini3939 | 1:2971dd3e5168 | 79 | httpd->urldecode(httpd->getFilename(id), &buf[1], sizeof(buf) - 2); |
okini3939 | 1:2971dd3e5168 | 80 | RPC::call(buf, outbuf); |
okini3939 | 1:2971dd3e5168 | 81 | |
okini3939 | 1:2971dd3e5168 | 82 | printf("RPC id %d '%s' '%s'\r\n", id, buf, outbuf); |
okini3939 | 1:2971dd3e5168 | 83 | httpd->send(id, outbuf, strlen(outbuf), "Content-Type: text/plain\r\n"); |
okini3939 | 1:2971dd3e5168 | 84 | } |
okini3939 | 1:2971dd3e5168 | 85 | |
okini3939 | 0:c4a353ed707d | 86 | int main () { |
ban4jp | 2:3a7574ace580 | 87 | int ret; |
okini3939 | 0:c4a353ed707d | 88 | |
ban4jp | 2:3a7574ace580 | 89 | //pc.baud(115200); |
ban4jp | 2:3a7574ace580 | 90 | pc.baud(9600); |
okini3939 | 0:c4a353ed707d | 91 | printf("HTTP Server...\r\n"); |
okini3939 | 0:c4a353ed707d | 92 | |
ban4jp | 2:3a7574ace580 | 93 | #if defined(TARGET_LPC1768) |
ban4jp | 2:3a7574ace580 | 94 | spi = new SPI(p5, p6, p7); // mosi, miso, sclk |
ban4jp | 2:3a7574ace580 | 95 | spi->frequency(4000000); // 4MHz |
ban4jp | 2:3a7574ace580 | 96 | eth = new WIZ820ioInterface(spi, p21, p22); // spi, cs, reset |
ban4jp | 2:3a7574ace580 | 97 | #elif defined(TARGET_LPC1114) |
ban4jp | 2:3a7574ace580 | 98 | spi = new SPI(dp2, dp1, dp6); // mosi, miso, sclk |
ban4jp | 2:3a7574ace580 | 99 | spi->frequency(4000000); // 4MHz |
ban4jp | 2:3a7574ace580 | 100 | eth = new WIZ820ioInterface(spi, dp25, dp26); // spi, cs, reset |
ban4jp | 2:3a7574ace580 | 101 | #endif |
ban4jp | 2:3a7574ace580 | 102 | |
ban4jp | 2:3a7574ace580 | 103 | eth->init(); //Use DHCP |
ban4jp | 2:3a7574ace580 | 104 | // eth->init("192.168.1.2", "255.255.255.0", "192.168.1.1"); |
ban4jp | 2:3a7574ace580 | 105 | |
ban4jp | 2:3a7574ace580 | 106 | ret = eth->connect(); |
ban4jp | 2:3a7574ace580 | 107 | if (ret) { |
ban4jp | 2:3a7574ace580 | 108 | printf("connect error %d", ret); |
ban4jp | 2:3a7574ace580 | 109 | return -1; |
ban4jp | 2:3a7574ace580 | 110 | } |
okini3939 | 0:c4a353ed707d | 111 | printf("IP Address is %s\r\n", eth->getIPAddress()); |
okini3939 | 0:c4a353ed707d | 112 | |
okini3939 | 1:2971dd3e5168 | 113 | // RPC::add_rpc_class<RpcAnalogIn>(); |
okini3939 | 1:2971dd3e5168 | 114 | // RPC::add_rpc_class<RpcAnalogOut>(); |
okini3939 | 1:2971dd3e5168 | 115 | RPC::add_rpc_class<RpcDigitalIn>(); |
okini3939 | 1:2971dd3e5168 | 116 | RPC::add_rpc_class<RpcDigitalOut>(); |
okini3939 | 1:2971dd3e5168 | 117 | RPC::add_rpc_class<RpcDigitalInOut>(); |
okini3939 | 1:2971dd3e5168 | 118 | RPC::add_rpc_class<RpcPwmOut>(); |
okini3939 | 1:2971dd3e5168 | 119 | |
okini3939 | 0:c4a353ed707d | 120 | httpd = new HTTPD; |
okini3939 | 0:c4a353ed707d | 121 | httpd->attach("/cgi-bin/", &callback_cgi); |
okini3939 | 0:c4a353ed707d | 122 | httpd->attach("/ws/", &callback_ws); |
okini3939 | 1:2971dd3e5168 | 123 | httpd->attach("/rpc/", &callback_rpc); |
ban4jp | 2:3a7574ace580 | 124 | #if defined(TARGET_LPC1768) |
ban4jp | 2:3a7574ace580 | 125 | httpd->attach("/local/", "/local/"); |
ban4jp | 2:3a7574ace580 | 126 | #endif |
ban4jp | 2:3a7574ace580 | 127 | httpd->attach("/", &callback_static); |
okini3939 | 0:c4a353ed707d | 128 | httpd->start(80); |
okini3939 | 0:c4a353ed707d | 129 | printf("httpd ready\r\n"); |
ban4jp | 2:3a7574ace580 | 130 | #if defined(TARGET_LPC1768) |
okini3939 | 0:c4a353ed707d | 131 | led1 = 1; |
ban4jp | 2:3a7574ace580 | 132 | #endif |
okini3939 | 0:c4a353ed707d | 133 | |
okini3939 | 0:c4a353ed707d | 134 | for (;;) { |
ban4jp | 2:3a7574ace580 | 135 | httpd->poll(); |
ban4jp | 2:3a7574ace580 | 136 | |
ban4jp | 2:3a7574ace580 | 137 | //printf("poll-loop\r\n"); |
okini3939 | 0:c4a353ed707d | 138 | } |
okini3939 | 0:c4a353ed707d | 139 | } |