HTTPS(SSL) client and NTP client for GSwifi see: http://mbed.org/users/gsfan/notebook/gainspan_wifi/

Dependencies:   GSwifi mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * HTTPS(SSL) client and NTP client
00003  *
00004  * Please update the firmware.
00005  */
00006 
00007 #include "mbed.h"
00008 #include "GSwifi.h"
00009 
00010 #define HTTP_HOST "secure.sakura.ad.jp"
00011 #define HTTP_PORT 443
00012 #define HTTP_URI "/"
00013 #define NTP_HOST "ntp.jst.mfeed.ad.jp"
00014 
00015 #define SECURE GSwifi::GSSEC_WPA_PSK
00016 #define SSID "SSID"
00017 #define PASS "PASSPHRASE"
00018 
00019 GSwifi gs(p13, p14, p20); // TX, RX, Reset (no flow control)
00020 //GSwifi gs(p13, p14, p12, P0_22, p20, NC, 115200); // TX, RX, CTS, RTS, Reset, Alarm
00021 
00022 Serial pc(USBTX, USBRX);
00023 DigitalOut led1(LED1), led2(LED2);
00024 
00025 extern "C"
00026 void HardFault_Handler() {
00027     printf("Hard Fault!\r\n");
00028     gs.disconnect();
00029     exit(-1);
00030 }
00031 
00032 void onGsReceive (int cid, int len) {
00033     int i;
00034     char buf[GS_DATA_SIZE + 1];
00035     
00036     led2 = 1;
00037     i = gs.recv(cid, buf, len);
00038     buf[i] = 0;
00039     pc.printf(buf);
00040 }
00041 
00042 int main () {
00043     int r;
00044     IpAddr ipaddr, netmask, gateway, nameserver;
00045     Host host;
00046     time_t time;
00047     struct tm *t;
00048 
00049     led1 = 1;
00050     pc.baud(115200);
00051     
00052     pc.printf("connecting...\r\n");
00053     if (gs.connect(SECURE, SSID, PASS)) {
00054         return -1;
00055     }
00056     gs.getAddress(ipaddr, netmask, gateway, nameserver);
00057     pc.printf("ip %d.%d.%d.%d\r\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]);
00058 
00059     pc.printf("ntpdate\r\n");
00060     host.setName(NTP_HOST);
00061     gs.ntpdate(host);
00062     time = gs.getTime() + (9 * 60 * 60);
00063     t = localtime(&time);
00064     pc.printf("%04d-%02d-%02d, %02d:%02d:%02d\r\n", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
00065 
00066     pc.printf("httpsGet\r\n");
00067     host.setName(HTTP_HOST);
00068     host.setPort(HTTP_PORT);
00069     r = gs.httpGet(host, HTTP_URI, 1, &onGsReceive);
00070     if (r >= 0) {
00071         for (;;) {
00072             gs.poll();
00073             if (! gs.isConnected(r)) break;
00074 
00075             wait_ms(50);
00076             led1 = !led1;
00077             led2 = 0;
00078         }
00079     }
00080 
00081     pc.printf("exit\r\n");
00082 }