HTTP and WebSocket Server with RTOS for GSwifiInterface library Please see: https://mbed.org/users/gsfan/notebook/gswifiinterface_httpd/

Dependencies:   GSwifiInterface SDFileSystem mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * Please modify "GSwifi_conf.h"
00003  * ----------
00004  * #define CFG_ENABLE_RTOS
00005  * #define CFG_ENABLE_HTTPD
00006  * #define CFG_ENABLE_WEBSOCKET
00007  * //#define CFG_ENABLE_SMTP
00008  * ----------
00009  */
00010 
00011 #include "mbed.h"
00012 #include "SDFileSystem.h"
00013 #include "GSwifiInterface.h"
00014 
00015 #define SEC  GSwifi::SEC_WPA_PSK
00016 #define SSID "SSPD"
00017 #define PASS "PASSPHRASE"
00018 
00019 Serial pc(USBTX, USBRX);
00020 DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4);
00021 LocalFileSystem local("local");
00022 SDFileSystem sd(p5, p6, p7, p8, "sd");
00023 
00024 GSwifiInterface *gs;
00025 
00026 void cgi (int cid) {
00027     int i;
00028     char buf[100];
00029 
00030     led2 = 1;
00031     i = gs->recv(cid, buf, sizeof(buf));
00032     if (i < 0) return;
00033     buf[i] = 0;
00034     pc.printf("CGI %d: %s ? %s '%s' %d\r\n", cid, gs->httpdGetFilename(cid), gs->httpdGetQuerystring(cid), buf, i);
00035 
00036     gs->send(cid, "HTTP/1.1 200 OK\r\n", 17);
00037     gs->send(cid, "Content-type: text/plain\r\n", 26);
00038     gs->send(cid, "\r\n", 2);
00039 
00040     gs->send(cid, "BODY: ", 6);
00041     gs->send(cid, buf, strlen(buf));
00042     gs->send(cid, "\r\n", 2);
00043     gs->close(cid);
00044 }
00045 
00046 void ws_server (int cid) {
00047     int i;
00048     char buf[100];
00049 
00050     led2 = 1;
00051     i = gs->recv(cid, buf, sizeof(buf));
00052     if (i < 0) return;
00053     buf[i] = 0;
00054     pc.printf("WS %d: '%s' %d\r\n", cid, buf, i);
00055 
00056     gs->wsSend(cid, buf, i);
00057 }
00058 
00059 int main () {
00060 
00061     pc.baud(115200);
00062 //    pc.baud(921600);
00063     printf("HTTP Server...\r\n");
00064     led1 = 1;
00065     gs = new GSwifiInterface(p13, p14, p12, P0_22, p21, NC, 9600);
00066     gs->init(); //Use DHCP
00067     if (gs->connect(SEC, SSID, PASS)) return -1; // join the network
00068     printf("IP Address is %s\n", gs->getIPAddress());
00069 
00070     printf("ready\r\n");
00071     gs->httpd();
00072     gs->httpdAttach("/cgi-bin/", &cgi);
00073     gs->httpdAttach("/ws/", &ws_server, 1);
00074     gs->httpdAttach("/test/", "/sd/www/");
00075     gs->httpdAttach("/", "/local/");
00076 
00077     for (;;) {
00078         if (pc.readable()) {
00079             if (pc.getc() == 'q') break;
00080         }
00081 
00082         wait_ms(50);
00083         led1 = !led1;
00084         led2 = 0;
00085     }
00086     gs->dissociate();
00087     return 0;
00088 }