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 Rohm

Committer:
tousaki
Date:
Wed Nov 30 08:13:24 2016 +0000
Revision:
4:0d2d07863347
Parent:
2:d1f57f718b0c
Modified comments

Who changed what in which revision?

UserRevisionLine numberNew 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 4:0d2d07863347 7 Warning!
tousaki 4:0d2d07863347 8 When exporting and using it, increase the following stack size.
tousaki 4:0d2d07863347 9
tousaki 4:0d2d07863347 10 [EthernetInterface/lwip/lwipopts.h]---------
tousaki 4:0d2d07863347 11 #define TCPIP_THREAD_STACKSIZE 1024
tousaki 4:0d2d07863347 12 ->
tousaki 4:0d2d07863347 13 #define TCPIP_THREAD_STACKSIZE 2048
tousaki 4:0d2d07863347 14 --------------------------------------------
tousaki 4:0d2d07863347 15 */
tousaki 4:0d2d07863347 16
tousaki 4:0d2d07863347 17 /*
tousaki 2:d1f57f718b0c 18 This works with the following library.
tousaki 2:d1f57f718b0c 19 mbed-rtos : revision 115
tousaki 2:d1f57f718b0c 20 */
tousaki 0:64fab22ec392 21
tousaki 0:64fab22ec392 22 #include "mbed.h"
tousaki 0:64fab22ec392 23 #include "rtos.h"
tousaki 1:423b42d70d3f 24 #include "GR_PEACH_WlanBP3595.h"
tousaki 0:64fab22ec392 25
tousaki 0:64fab22ec392 26 /* Please change the following macro definition to your setting. */
tousaki 0:64fab22ec392 27 #define WLAN_SSID ("SSIDofYourAP") // SSID
tousaki 0:64fab22ec392 28 #define WLAN_PSK ("PSKofYourAP") // PSK(Pre-Shared Key)
tousaki 0:64fab22ec392 29 #define SERVER_IP ("192.168.1.200") // Server IP address
tousaki 0:64fab22ec392 30 #define SERVER_PORT (50000) // TCP server socket port number
tousaki 0:64fab22ec392 31 #define SUBNET_MASK ("255.255.255.0") // Subnet mask
tousaki 0:64fab22ec392 32 #define DEFAULT_GATEWAY ("192.168.1.1") // Default gateway
tousaki 0:64fab22ec392 33 #define SEND_MESSAGE ("Hello, world!\r\n") // Send-message
tousaki 0:64fab22ec392 34
tousaki 0:64fab22ec392 35 static void _wlan_inf_callback(uint8_t ucType, uint16_t usWid, uint16_t usSize, uint8_t *pucData);
tousaki 0:64fab22ec392 36
tousaki 0:64fab22ec392 37 DigitalOut red_led(LED1); // On: error
tousaki 0:64fab22ec392 38 DigitalOut green_led(LED2); // On: WLAN has been connected
tousaki 0:64fab22ec392 39
tousaki 0:64fab22ec392 40 /** Main function
tousaki 0:64fab22ec392 41 *
tousaki 0:64fab22ec392 42 */
tousaki 0:64fab22ec392 43 int main() {
tousaki 1:423b42d70d3f 44 GR_PEACH_WlanBP3595 wlan;
tousaki 0:64fab22ec392 45 TCPSocketServer server;
tousaki 0:64fab22ec392 46 TCPSocketConnection connection;
tousaki 0:64fab22ec392 47 int ret;
tousaki 0:64fab22ec392 48
tousaki 1:423b42d70d3f 49 wlan.setWlanCbFunction(_wlan_inf_callback);
tousaki 1:423b42d70d3f 50
tousaki 1:423b42d70d3f 51 /* Initialize GR_PEACH_WlanBP3595 */
tousaki 1:423b42d70d3f 52 ret = wlan.init(SERVER_IP, SUBNET_MASK, DEFAULT_GATEWAY);
tousaki 0:64fab22ec392 53 if (ret != 0) {
tousaki 0:64fab22ec392 54 /* error */
tousaki 0:64fab22ec392 55 red_led = 1;
tousaki 0:64fab22ec392 56 while (1) { Thread::wait(1000); }
tousaki 0:64fab22ec392 57 }
tousaki 0:64fab22ec392 58
tousaki 1:423b42d70d3f 59 /* Connect(GR_PEACH_WlanBP3595) */
tousaki 1:423b42d70d3f 60 ret = wlan.connect(WLAN_SSID, WLAN_PSK);
tousaki 0:64fab22ec392 61 if (ret != 0) {
tousaki 0:64fab22ec392 62 /* error */
tousaki 0:64fab22ec392 63 red_led = 1;
tousaki 0:64fab22ec392 64 while (1) { Thread::wait(1000); }
tousaki 0:64fab22ec392 65 }
tousaki 0:64fab22ec392 66
tousaki 0:64fab22ec392 67 /* Bind and listen */
tousaki 0:64fab22ec392 68 server.bind(SERVER_PORT);
tousaki 0:64fab22ec392 69 server.listen();
tousaki 0:64fab22ec392 70
tousaki 0:64fab22ec392 71 /* Loop */
tousaki 0:64fab22ec392 72 while (1) {
tousaki 0:64fab22ec392 73 /* Accept */
tousaki 0:64fab22ec392 74 server.accept(connection);
tousaki 0:64fab22ec392 75 printf("Connection from: %s\n", connection.get_address());
tousaki 0:64fab22ec392 76
tousaki 0:64fab22ec392 77 /* Send a message */
tousaki 0:64fab22ec392 78 connection.send_all((char *)SEND_MESSAGE, sizeof(SEND_MESSAGE)-1);
tousaki 0:64fab22ec392 79
tousaki 0:64fab22ec392 80 /* Close */
tousaki 0:64fab22ec392 81 connection.close();
tousaki 0:64fab22ec392 82 }
tousaki 0:64fab22ec392 83 }
tousaki 0:64fab22ec392 84
tousaki 0:64fab22ec392 85 /** WLAN Information callback function
tousaki 0:64fab22ec392 86 *
tousaki 0:64fab22ec392 87 */
tousaki 0:64fab22ec392 88 static void _wlan_inf_callback(uint8_t ucType, uint16_t usWid, uint16_t usSize, uint8_t *pucData)
tousaki 0:64fab22ec392 89 {
tousaki 1:423b42d70d3f 90 if (ucType == 'I') {
tousaki 1:423b42d70d3f 91 if (usWid == 0x0005) { // WID_STATUS
tousaki 1:423b42d70d3f 92 if (pucData[0] == 0x01) { // CONNECTED
tousaki 0:64fab22ec392 93 green_led = 1;
tousaki 1:423b42d70d3f 94 } else {
tousaki 0:64fab22ec392 95 green_led = 0;
tousaki 0:64fab22ec392 96 }
tousaki 0:64fab22ec392 97 }
tousaki 0:64fab22ec392 98 }
tousaki 0:64fab22ec392 99 }