This is a sample application program for GR-PEACH_WlanBP3595STA library. GR-PEACH_WlanBP3595STA library only works with GR-PEACH. This sample works as TCP socket sever, and this program sends a message when a connection is accepted.
Dependencies: EthernetInterface GR-PEACH_WlanBP3595STA mbed-rtos mbed
Fork of GR-PEACH_WlanBP3595STA_sample by
main.cpp@1:423b42d70d3f, 2016-05-31 (annotated)
- Committer:
- tousaki
- Date:
- Tue May 31 07:23:03 2016 +0000
- Revision:
- 1:423b42d70d3f
- Parent:
- 0:64fab22ec392
- Child:
- 2:d1f57f718b0c
Changed the structure of the library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tousaki | 0:64fab22ec392 | 1 | /* This is a sample application program for WlanBP3595 library. */ |
tousaki | 0:64fab22ec392 | 2 | /* This program works as TCP socket sever, and this program sends a message */ |
tousaki | 0:64fab22ec392 | 3 | /* when a connection is accepted. The setting of the WLAN is WPA2+AES. */ |
tousaki | 0:64fab22ec392 | 4 | |
tousaki | 0:64fab22ec392 | 5 | #include "mbed.h" |
tousaki | 0:64fab22ec392 | 6 | #include "rtos.h" |
tousaki | 1:423b42d70d3f | 7 | #include "GR_PEACH_WlanBP3595.h" |
tousaki | 0:64fab22ec392 | 8 | |
tousaki | 0:64fab22ec392 | 9 | /* Please change the following macro definition to your setting. */ |
tousaki | 0:64fab22ec392 | 10 | #define WLAN_SSID ("SSIDofYourAP") // SSID |
tousaki | 0:64fab22ec392 | 11 | #define WLAN_PSK ("PSKofYourAP") // PSK(Pre-Shared Key) |
tousaki | 0:64fab22ec392 | 12 | #define SERVER_IP ("192.168.1.200") // Server IP address |
tousaki | 0:64fab22ec392 | 13 | #define SERVER_PORT (50000) // TCP server socket port number |
tousaki | 0:64fab22ec392 | 14 | #define SUBNET_MASK ("255.255.255.0") // Subnet mask |
tousaki | 0:64fab22ec392 | 15 | #define DEFAULT_GATEWAY ("192.168.1.1") // Default gateway |
tousaki | 0:64fab22ec392 | 16 | #define SEND_MESSAGE ("Hello, world!\r\n") // Send-message |
tousaki | 0:64fab22ec392 | 17 | |
tousaki | 0:64fab22ec392 | 18 | static void _wlan_inf_callback(uint8_t ucType, uint16_t usWid, uint16_t usSize, uint8_t *pucData); |
tousaki | 0:64fab22ec392 | 19 | |
tousaki | 0:64fab22ec392 | 20 | DigitalOut red_led(LED1); // On: error |
tousaki | 0:64fab22ec392 | 21 | DigitalOut green_led(LED2); // On: WLAN has been connected |
tousaki | 0:64fab22ec392 | 22 | |
tousaki | 0:64fab22ec392 | 23 | /** Main function |
tousaki | 0:64fab22ec392 | 24 | * |
tousaki | 0:64fab22ec392 | 25 | */ |
tousaki | 0:64fab22ec392 | 26 | int main() { |
tousaki | 1:423b42d70d3f | 27 | GR_PEACH_WlanBP3595 wlan; |
tousaki | 0:64fab22ec392 | 28 | TCPSocketServer server; |
tousaki | 0:64fab22ec392 | 29 | TCPSocketConnection connection; |
tousaki | 0:64fab22ec392 | 30 | int ret; |
tousaki | 0:64fab22ec392 | 31 | |
tousaki | 1:423b42d70d3f | 32 | wlan.setWlanCbFunction(_wlan_inf_callback); |
tousaki | 1:423b42d70d3f | 33 | |
tousaki | 1:423b42d70d3f | 34 | /* Initialize GR_PEACH_WlanBP3595 */ |
tousaki | 1:423b42d70d3f | 35 | ret = wlan.init(SERVER_IP, SUBNET_MASK, DEFAULT_GATEWAY); |
tousaki | 0:64fab22ec392 | 36 | if (ret != 0) { |
tousaki | 0:64fab22ec392 | 37 | /* error */ |
tousaki | 0:64fab22ec392 | 38 | red_led = 1; |
tousaki | 0:64fab22ec392 | 39 | while (1) { Thread::wait(1000); } |
tousaki | 0:64fab22ec392 | 40 | } |
tousaki | 0:64fab22ec392 | 41 | |
tousaki | 1:423b42d70d3f | 42 | /* Connect(GR_PEACH_WlanBP3595) */ |
tousaki | 1:423b42d70d3f | 43 | ret = wlan.connect(WLAN_SSID, WLAN_PSK); |
tousaki | 0:64fab22ec392 | 44 | if (ret != 0) { |
tousaki | 0:64fab22ec392 | 45 | /* error */ |
tousaki | 0:64fab22ec392 | 46 | red_led = 1; |
tousaki | 0:64fab22ec392 | 47 | while (1) { Thread::wait(1000); } |
tousaki | 0:64fab22ec392 | 48 | } |
tousaki | 0:64fab22ec392 | 49 | |
tousaki | 0:64fab22ec392 | 50 | /* Bind and listen */ |
tousaki | 0:64fab22ec392 | 51 | server.bind(SERVER_PORT); |
tousaki | 0:64fab22ec392 | 52 | server.listen(); |
tousaki | 0:64fab22ec392 | 53 | |
tousaki | 0:64fab22ec392 | 54 | /* Loop */ |
tousaki | 0:64fab22ec392 | 55 | while (1) { |
tousaki | 0:64fab22ec392 | 56 | /* Accept */ |
tousaki | 0:64fab22ec392 | 57 | server.accept(connection); |
tousaki | 0:64fab22ec392 | 58 | printf("Connection from: %s\n", connection.get_address()); |
tousaki | 0:64fab22ec392 | 59 | |
tousaki | 0:64fab22ec392 | 60 | /* Send a message */ |
tousaki | 0:64fab22ec392 | 61 | connection.send_all((char *)SEND_MESSAGE, sizeof(SEND_MESSAGE)-1); |
tousaki | 0:64fab22ec392 | 62 | |
tousaki | 0:64fab22ec392 | 63 | /* Close */ |
tousaki | 0:64fab22ec392 | 64 | connection.close(); |
tousaki | 0:64fab22ec392 | 65 | } |
tousaki | 0:64fab22ec392 | 66 | } |
tousaki | 0:64fab22ec392 | 67 | |
tousaki | 0:64fab22ec392 | 68 | /** WLAN Information callback function |
tousaki | 0:64fab22ec392 | 69 | * |
tousaki | 0:64fab22ec392 | 70 | */ |
tousaki | 0:64fab22ec392 | 71 | static void _wlan_inf_callback(uint8_t ucType, uint16_t usWid, uint16_t usSize, uint8_t *pucData) |
tousaki | 0:64fab22ec392 | 72 | { |
tousaki | 1:423b42d70d3f | 73 | if (ucType == 'I') { |
tousaki | 1:423b42d70d3f | 74 | if (usWid == 0x0005) { // WID_STATUS |
tousaki | 1:423b42d70d3f | 75 | if (pucData[0] == 0x01) { // CONNECTED |
tousaki | 0:64fab22ec392 | 76 | /* Notify the EthernetInterface driver that WLAN has been connected */ |
tousaki | 0:64fab22ec392 | 77 | green_led = 1; |
tousaki | 1:423b42d70d3f | 78 | } else { |
tousaki | 0:64fab22ec392 | 79 | /* Notify the EthernetInterface driver that WLAN has been disconnected */ |
tousaki | 0:64fab22ec392 | 80 | green_led = 0; |
tousaki | 0:64fab22ec392 | 81 | } |
tousaki | 0:64fab22ec392 | 82 | } |
tousaki | 0:64fab22ec392 | 83 | } |
tousaki | 0:64fab22ec392 | 84 | } |