TCPEchoServer for Murata Type-YD WiFi module

Dependencies:   PowerControl SNICInterface mbed-rtos mbed

Fork of TCPEchoServer by Toyomasa Watarai

mbed 上で動作する TCPEchoServer です(ムラタ製 TypeYDモジュール使用)。

ターミナルを接続して実行すると、割り当てられたIPアドレスが表示されます。 対向機から実行する Python スクリプトは、こちら です(IPアドレスを書き換えて使用して下さい)。

Committer:
MACRUM
Date:
Thu Apr 02 02:47:18 2015 +0000
Revision:
9:26c84c9352f1
Parent:
8:6e121d9ce02b
Update libraries

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:38cbb854d85f 1 #include "mbed.h"
MACRUM 7:27b297e209a6 2 #include "SNIC_WifiInterface.h"
MACRUM 7:27b297e209a6 3 #include "TCPSocketServer.h"
MACRUM 7:27b297e209a6 4
MACRUM 7:27b297e209a6 5 #if defined(TARGET_LPC1768)
MACRUM 7:27b297e209a6 6 #include "PowerControl/EthernetPowerControl.h"
MACRUM 7:27b297e209a6 7 #endif
emilmont 1:5cebe0e38cd2 8
emilmont 3:36fd3cfad85a 9 #define ECHO_SERVER_PORT 7
emilmont 3:36fd3cfad85a 10
MACRUM 7:27b297e209a6 11 #define DEMO_AP_SSID "SSID"
MACRUM 7:27b297e209a6 12 #define DEMO_AP_SECURITY_TYPE e_SEC_WPA2_AES
MACRUM 7:27b297e209a6 13 #define DEMO_AP_SECUTIRY_KEY "PASSWORD"
MACRUM 7:27b297e209a6 14
MACRUM 7:27b297e209a6 15 C_SNIC_WifiInterface wifi( p9, p10, NC, NC, p30 );
MACRUM 7:27b297e209a6 16
emilmont 1:5cebe0e38cd2 17 int main (void) {
MACRUM 7:27b297e209a6 18 #if defined(TARGET_LPC1768)
MACRUM 7:27b297e209a6 19 PHY_PowerDown();
MACRUM 7:27b297e209a6 20 #endif
MACRUM 7:27b297e209a6 21
MACRUM 7:27b297e209a6 22 wifi.init(); //Use DHCP
MACRUM 7:27b297e209a6 23
MACRUM 7:27b297e209a6 24 wait(0.5);
MACRUM 7:27b297e209a6 25 int s = wifi.disconnect();
MACRUM 7:27b297e209a6 26 if( s != 0 )
MACRUM 7:27b297e209a6 27 {
MACRUM 7:27b297e209a6 28 return -1;
MACRUM 7:27b297e209a6 29 }
MACRUM 7:27b297e209a6 30
MACRUM 7:27b297e209a6 31 wait(0.3);
MACRUM 7:27b297e209a6 32 // Connect AP
MACRUM 7:27b297e209a6 33 wifi.connect( DEMO_AP_SSID
MACRUM 7:27b297e209a6 34 , strlen(DEMO_AP_SSID)
MACRUM 7:27b297e209a6 35 , DEMO_AP_SECURITY_TYPE
MACRUM 7:27b297e209a6 36 , DEMO_AP_SECUTIRY_KEY
MACRUM 7:27b297e209a6 37 , strlen(DEMO_AP_SECUTIRY_KEY) );
MACRUM 7:27b297e209a6 38 wait(0.5);
MACRUM 7:27b297e209a6 39
MACRUM 7:27b297e209a6 40 wifi.setIPConfig( true );
MACRUM 7:27b297e209a6 41 wait(0.5);
MACRUM 7:27b297e209a6 42
MACRUM 7:27b297e209a6 43 printf("IP Address is %s\n", wifi.getIPAddress());
emilmont 1:5cebe0e38cd2 44
emilmont 1:5cebe0e38cd2 45 TCPSocketServer server;
emilmont 3:36fd3cfad85a 46 server.bind(ECHO_SERVER_PORT);
MACRUM 7:27b297e209a6 47 server.listen();
MACRUM 7:27b297e209a6 48
emilmont 1:5cebe0e38cd2 49 while (true) {
emilmont 1:5cebe0e38cd2 50 printf("\nWait for new connection...\n");
emilmont 1:5cebe0e38cd2 51 TCPSocketConnection client;
MACRUM 8:6e121d9ce02b 52 server.accept(client);
emilmont 3:36fd3cfad85a 53 client.set_blocking(false, 1500); // Timeout after (1.5)s
emilmont 1:5cebe0e38cd2 54
emilmont 1:5cebe0e38cd2 55 printf("Connection from: %s\n", client.get_address());
emilmont 1:5cebe0e38cd2 56 char buffer[256];
emilmont 1:5cebe0e38cd2 57 while (true) {
emilmont 3:36fd3cfad85a 58 int n = client.receive(buffer, sizeof(buffer));
emilmont 1:5cebe0e38cd2 59 if (n <= 0) break;
emilmont 1:5cebe0e38cd2 60
emilmont 2:ec5ae99791da 61 client.send_all(buffer, n);
emilmont 1:5cebe0e38cd2 62 if (n <= 0) break;
emilmont 1:5cebe0e38cd2 63 }
emilmont 3:36fd3cfad85a 64
emilmont 1:5cebe0e38cd2 65 client.close();
emilmont 1:5cebe0e38cd2 66 }
emilmont 1:5cebe0e38cd2 67 }