This is a sample application program for GR-PEACH_WlanBP3595AP library. GR-PEACH_WlanBP3595AP 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_WlanBP3595AP mbed-rtos mbed

Fork of GR-PEACH_WlanBP3595AP_sample by Rohm

Committer:
tousaki
Date:
Tue May 31 07:20:18 2016 +0000
Revision:
1:3eae92f063c3
Parent:
0:d1c30577e772
Child:
2:59a519fa59d0
Changed the structure of the library

Who changed what in which revision?

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