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