gs fan / Mbed 2 deprecated GSwifiInterface_HTTPServer

Dependencies:   GSwifiInterface SDFileSystem 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 "GSwifi.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 GSwifi gs(p13, p14, p12, P0_22, p20, NC, 115200); // mbed LPC1768
00025 //GSwifi gs(PTD3, PTD2, NC, PTD5, PTA13); // FRDM KL25Z
00026 
00027 
00028 void cgi (int cid) {
00029     int i;
00030     char buf[100];
00031 
00032     led2 = 1;
00033     i = gs.recv(cid, buf, sizeof(buf));
00034     if (i < 0) return;
00035     buf[i] = 0;
00036 //    pc.printf("CGI %d: %s ? %s '%s' %d\r\n", cid, gs.httpdGetFilename(cid), gs.httpdGetQuerystring(cid), buf, i);
00037 
00038     gs.send(cid, "HTTP/1.1 200 OK\r\n", 17);
00039     gs.send(cid, "Content-type: text/plain\r\n", 26);
00040     gs.send(cid, "\r\n", 2);
00041 
00042     gs.send(cid, "BODY: ", 6);
00043     gs.send(cid, buf, strlen(buf));
00044     gs.send(cid, "\r\n", 2);
00045     gs.close(cid);
00046 }
00047 
00048 void ws_server (int cid) {
00049     int i;
00050     char buf[100];
00051 
00052     led2 = 1;
00053     i = gs.recv(cid, buf, sizeof(buf));
00054     if (i < 0) return;
00055     buf[i] = 0;
00056     pc.printf("WS %d: '%s' %d\r\n", cid, buf, i);
00057 
00058     gs.wsSend(cid, buf, i);
00059 }
00060 
00061 int main () {
00062     char ip[16], netmask[16], gateway[16];        
00063 
00064     pc.baud(115200);
00065 //    pc.baud(921600);
00066     printf("HTTP Server...\r\n");
00067     led1 = 1;
00068     gs.setAddress(); //Use DHCP
00069 //    gs.setAddress("192.168.1.50", "255.255.255.0", "192.168.1.1", "192.168.1.1");
00070     gs.setSsid(SEC, SSID, PASS);
00071     if (gs.join()) return -1; // join the network
00072 //    if (gs.limitedap()) return -1; // access point
00073     gs.getAddress(ip, netmask, gateway);
00074     printf("IP Address is %s\r\n", ip);
00075 
00076     printf("ready\r\n");
00077     gs.httpd();
00078     gs.httpdAttach("/cgi-bin/", &cgi);
00079     gs.httpdAttach("/ws/", &ws_server, 1);
00080     gs.httpdAttach("/test/", "/sd/www/");
00081     gs.httpdAttach("/", "/local/");
00082 
00083     for (;;) {
00084         gs.poll();
00085         if (pc.readable()) {
00086             if (pc.getc() == 'q') break;
00087         }
00088 
00089         wait_ms(50);
00090         led1 = !led1;
00091         led2 = 0;
00092     }
00093     gs.dissociate();
00094     return 0;
00095 }