各ピンへのread/writeを提供するサーバサンプル
Dependencies: NySNICInterface mbed-rtos mbed
Fork of RESTServerSample2 by
main.cpp@1:e5d3bd4af9da, 2015-03-12 (annotated)
- Committer:
- komoritan
- Date:
- Thu Mar 12 12:40:48 2015 +0000
- Revision:
- 1:e5d3bd4af9da
- Parent:
- 0:998e2e00df0c
Bug fix - handle_request
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
komoritan | 0:998e2e00df0c | 1 | #include "mbed.h" |
komoritan | 0:998e2e00df0c | 2 | #include "SNIC_WifiInterface.h" |
komoritan | 0:998e2e00df0c | 3 | #include "HTTPServer.h" |
komoritan | 0:998e2e00df0c | 4 | |
komoritan | 0:998e2e00df0c | 5 | /** |
komoritan | 0:998e2e00df0c | 6 | * Wifi AP parameter |
komoritan | 0:998e2e00df0c | 7 | */ |
komoritan | 0:998e2e00df0c | 8 | |
komoritan | 0:998e2e00df0c | 9 | /* Set this */ |
komoritan | 0:998e2e00df0c | 10 | #define WIFI_SSID "" |
komoritan | 0:998e2e00df0c | 11 | #define WIFI_SECUTIRY_KEY "" |
komoritan | 0:998e2e00df0c | 12 | //#define WIFI_SECURITY_TYPE e_SEC_OPEN |
komoritan | 0:998e2e00df0c | 13 | //#define WIFI_SECURITY_TYPE e_SEC_WEP |
komoritan | 0:998e2e00df0c | 14 | //#define WIFI_SECURITY_TYPE e_SEC_WPA_TKIP |
komoritan | 0:998e2e00df0c | 15 | #define WIFI_SECURITY_TYPE e_SEC_WPA2_AES |
komoritan | 0:998e2e00df0c | 16 | //#define WIFI_SECURITY_TYPE e_SEC_WPA2_MIXED |
komoritan | 0:998e2e00df0c | 17 | //#define WIFI_SECURITY_TYPE e_SEC_WPA_AES |
komoritan | 0:998e2e00df0c | 18 | |
komoritan | 0:998e2e00df0c | 19 | |
komoritan | 0:998e2e00df0c | 20 | #define IP_ADDRESS "192.168.0.44" |
komoritan | 0:998e2e00df0c | 21 | #define NET_MASK "255.255.255.0" |
komoritan | 0:998e2e00df0c | 22 | #define DEFAULT_GATEWAY "192.168.0.1" |
komoritan | 0:998e2e00df0c | 23 | #define PORT_NUMBER 80 |
komoritan | 0:998e2e00df0c | 24 | |
komoritan | 0:998e2e00df0c | 25 | |
komoritan | 0:998e2e00df0c | 26 | Serial pc(USBTX, USBRX); // This is required when defined "_DEBUG" |
komoritan | 0:998e2e00df0c | 27 | /** Wi-Fi SNIC UART Interface*/ |
komoritan | 0:998e2e00df0c | 28 | C_SNIC_WifiInterface mSNICwifi( p13, p14, p12, p11, p20 ); |
komoritan | 0:998e2e00df0c | 29 | |
komoritan | 0:998e2e00df0c | 30 | |
komoritan | 0:998e2e00df0c | 31 | |
komoritan | 0:998e2e00df0c | 32 | |
komoritan | 0:998e2e00df0c | 33 | void wifi_connect() |
komoritan | 0:998e2e00df0c | 34 | { |
komoritan | 0:998e2e00df0c | 35 | // Initialize Wi-Fi interface |
komoritan | 0:998e2e00df0c | 36 | if(mSNICwifi.init()!=0){ |
komoritan | 0:998e2e00df0c | 37 | printf("Wi-Fi initial failed\r\n"); |
komoritan | 0:998e2e00df0c | 38 | mbed_die(); |
komoritan | 0:998e2e00df0c | 39 | } |
komoritan | 0:998e2e00df0c | 40 | wait(0.5); |
komoritan | 0:998e2e00df0c | 41 | |
komoritan | 0:998e2e00df0c | 42 | if(mSNICwifi.disconnect()!= 0 ) |
komoritan | 0:998e2e00df0c | 43 | { |
komoritan | 0:998e2e00df0c | 44 | printf("on the disconnect state\r\n"); |
komoritan | 0:998e2e00df0c | 45 | mbed_die(); |
komoritan | 0:998e2e00df0c | 46 | } |
komoritan | 0:998e2e00df0c | 47 | wait(0.3); |
komoritan | 0:998e2e00df0c | 48 | |
komoritan | 0:998e2e00df0c | 49 | // Connect to AP |
komoritan | 0:998e2e00df0c | 50 | if(mSNICwifi.connect( WIFI_SSID,strlen(WIFI_SSID), |
komoritan | 0:998e2e00df0c | 51 | WIFI_SECURITY_TYPE, |
komoritan | 0:998e2e00df0c | 52 | WIFI_SECUTIRY_KEY, |
komoritan | 0:998e2e00df0c | 53 | strlen(WIFI_SECUTIRY_KEY))!=0) |
komoritan | 0:998e2e00df0c | 54 | { |
komoritan | 0:998e2e00df0c | 55 | printf("Connect AP is failed\r\n"); |
komoritan | 0:998e2e00df0c | 56 | mbed_die(); |
komoritan | 0:998e2e00df0c | 57 | } |
komoritan | 0:998e2e00df0c | 58 | wait(0.5); |
komoritan | 0:998e2e00df0c | 59 | |
komoritan | 0:998e2e00df0c | 60 | int retIp = mSNICwifi.setIPConfig(false, IP_ADDRESS, NET_MASK, DEFAULT_GATEWAY); |
komoritan | 0:998e2e00df0c | 61 | } |
komoritan | 0:998e2e00df0c | 62 | |
komoritan | 0:998e2e00df0c | 63 | |
komoritan | 0:998e2e00df0c | 64 | int main() |
komoritan | 0:998e2e00df0c | 65 | { |
komoritan | 0:998e2e00df0c | 66 | // for debug |
komoritan | 0:998e2e00df0c | 67 | pc.baud( 115200 ); |
komoritan | 0:998e2e00df0c | 68 | |
komoritan | 0:998e2e00df0c | 69 | wifi_connect(); |
komoritan | 0:998e2e00df0c | 70 | |
komoritan | 0:998e2e00df0c | 71 | HTTPServer srv; |
komoritan | 0:998e2e00df0c | 72 | |
komoritan | 0:998e2e00df0c | 73 | pc.printf("server init.\r\n"); |
komoritan | 0:998e2e00df0c | 74 | srv.init(PORT_NUMBER); |
komoritan | 0:998e2e00df0c | 75 | |
komoritan | 0:998e2e00df0c | 76 | srv.add_request_handler("GET", new GetRequestHandler()); |
komoritan | 0:998e2e00df0c | 77 | srv.add_request_handler("DELETE", new DeleteRequestHandler()); |
komoritan | 0:998e2e00df0c | 78 | srv.add_request_handler("POST", new PostRequestHandler()); |
komoritan | 0:998e2e00df0c | 79 | |
komoritan | 0:998e2e00df0c | 80 | wait(1); |
komoritan | 0:998e2e00df0c | 81 | pc.printf("server running.\r\n"); |
komoritan | 0:998e2e00df0c | 82 | srv.run(); |
komoritan | 0:998e2e00df0c | 83 | } |