WizFi310, WiFi module example, based NetworkSocketAPI TCP Server, TCP Client, AP mode, STA mode

Dependencies:   NetworkSocketAPI WizFi310Interface mbed-rtos mbed

Committer:
jehoon
Date:
Mon Jun 26 00:35:39 2017 +0000
Revision:
0:f9930710e655
WizFi310Interface example,; TCP Server, TCP Client Echo example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jehoon 0:f9930710e655 1 #include "mbed.h"
jehoon 0:f9930710e655 2 #include "rtos.h"
jehoon 0:f9930710e655 3 #include "TCPSocket.h"
jehoon 0:f9930710e655 4 #include "TCPServer.h"
jehoon 0:f9930710e655 5 #include "WizFi310Interface.h"
jehoon 0:f9930710e655 6
jehoon 0:f9930710e655 7
jehoon 0:f9930710e655 8 //#define SOFTAPMODE
jehoon 0:f9930710e655 9 //#define RUN_SERVER
jehoon 0:f9930710e655 10
jehoon 0:f9930710e655 11 #ifdef SOFTAPMODE
jehoon 0:f9930710e655 12 #define AP_SSID "AP_SSID"
jehoon 0:f9930710e655 13 #define AP_PASSWORD "AP_PASSWORD"
jehoon 0:f9930710e655 14 #define AP_SECURITY NSAPI_SECURITY_WPA2
jehoon 0:f9930710e655 15 #else
jehoon 0:f9930710e655 16 #define AP_SSID "AP_SSID"
jehoon 0:f9930710e655 17 #define AP_PASSWORD "AP_PASSWORD"
jehoon 0:f9930710e655 18 #define AP_SECURITY NSAPI_SECURITY_WPA2
jehoon 0:f9930710e655 19 #endif
jehoon 0:f9930710e655 20
jehoon 0:f9930710e655 21 //server ip, clinet mode
jehoon 0:f9930710e655 22 uint8_t ipaddr[4] = {192,168,1,194};
jehoon 0:f9930710e655 23 #define PORT 5000
jehoon 0:f9930710e655 24
jehoon 0:f9930710e655 25 Serial pc(USBTX,USBRX);
jehoon 0:f9930710e655 26 WizFi310Interface wifi(D8, D2, D7, D6, D9, NC, 115200);
jehoon 0:f9930710e655 27
jehoon 0:f9930710e655 28 char rx_buf[512] = {'\0',};
jehoon 0:f9930710e655 29
jehoon 0:f9930710e655 30 TCPServer server;
jehoon 0:f9930710e655 31 TCPSocket client;
jehoon 0:f9930710e655 32
jehoon 0:f9930710e655 33 int TryApUp();
jehoon 0:f9930710e655 34 int TryServerUpAndWaitClient();
jehoon 0:f9930710e655 35 int ClientExample();
jehoon 0:f9930710e655 36 int RunEcho();
jehoon 0:f9930710e655 37
jehoon 0:f9930710e655 38 typedef enum ConnectionStep { DISASSOCIATED = 1, ASSOCIATED, LISTENED, CONNECTED, DISCONNECTED } conStep;
jehoon 0:f9930710e655 39 conStep ConnectionStep = DISASSOCIATED;
jehoon 0:f9930710e655 40
jehoon 0:f9930710e655 41
jehoon 0:f9930710e655 42 void print_char(char c = '*')
jehoon 0:f9930710e655 43 {
jehoon 0:f9930710e655 44 printf("%c", c);
jehoon 0:f9930710e655 45 fflush(stdout);
jehoon 0:f9930710e655 46 }
jehoon 0:f9930710e655 47
jehoon 0:f9930710e655 48 DigitalOut led1(LED1);
jehoon 0:f9930710e655 49
jehoon 0:f9930710e655 50
jehoon 0:f9930710e655 51 void print_star(void const *argument)
jehoon 0:f9930710e655 52 {
jehoon 0:f9930710e655 53
jehoon 0:f9930710e655 54 while (true) {
jehoon 0:f9930710e655 55 Thread::wait(1000);
jehoon 0:f9930710e655 56 print_char();
jehoon 0:f9930710e655 57 }
jehoon 0:f9930710e655 58 }
jehoon 0:f9930710e655 59
jehoon 0:f9930710e655 60 void connection_thread(void const *arg)
jehoon 0:f9930710e655 61 {
jehoon 0:f9930710e655 62 while(true){
jehoon 0:f9930710e655 63
jehoon 0:f9930710e655 64 TryApUp();
jehoon 0:f9930710e655 65
jehoon 0:f9930710e655 66 #ifdef RUN_SERVER
jehoon 0:f9930710e655 67 TryServerUpAndWaitClient();
jehoon 0:f9930710e655 68 #else
jehoon 0:f9930710e655 69 ClientExample();
jehoon 0:f9930710e655 70 #endif
jehoon 0:f9930710e655 71
jehoon 0:f9930710e655 72 RunEcho();
jehoon 0:f9930710e655 73 }
jehoon 0:f9930710e655 74 }
jehoon 0:f9930710e655 75
jehoon 0:f9930710e655 76 int main()
jehoon 0:f9930710e655 77 {
jehoon 0:f9930710e655 78 pc.baud(115200);
jehoon 0:f9930710e655 79
jehoon 0:f9930710e655 80 printf("\n\n*** Run WizFi310 on RTOS ***\r\n");
jehoon 0:f9930710e655 81 Thread star_thread(print_star, NULL, osPriorityNormal, DEFAULT_STACK_SIZE);
jehoon 0:f9930710e655 82 Thread con_thread(connection_thread, NULL, osPriorityNormal, DEFAULT_STACK_SIZE);
jehoon 0:f9930710e655 83
jehoon 0:f9930710e655 84 while (true) {
jehoon 0:f9930710e655 85 led1 = !led1;
jehoon 0:f9930710e655 86 Thread::wait(500);
jehoon 0:f9930710e655 87 }
jehoon 0:f9930710e655 88 }
jehoon 0:f9930710e655 89
jehoon 0:f9930710e655 90
jehoon 0:f9930710e655 91
jehoon 0:f9930710e655 92 int TryApUp()
jehoon 0:f9930710e655 93 {
jehoon 0:f9930710e655 94
jehoon 0:f9930710e655 95 if( ConnectionStep == DISASSOCIATED){
jehoon 0:f9930710e655 96 printf(" AP UP\r\n");
jehoon 0:f9930710e655 97 printf("SSID: %s\r\nPASS: %s\r\n", AP_SSID, AP_PASSWORD);
jehoon 0:f9930710e655 98
jehoon 0:f9930710e655 99 for(int i= 0; i<5; i++)
jehoon 0:f9930710e655 100 {
jehoon 0:f9930710e655 101 #ifdef SOFTAPMODE
jehoon 0:f9930710e655 102 if ( wifi.connectAP(AP_SSID, AP_PASSWORD, AP_SECURITY) < 0 )
jehoon 0:f9930710e655 103 #else
jehoon 0:f9930710e655 104 if ( wifi.connect(AP_SSID, AP_PASSWORD, AP_SECURITY) < 0 )
jehoon 0:f9930710e655 105 #endif
jehoon 0:f9930710e655 106 continue;
jehoon 0:f9930710e655 107 else {
jehoon 0:f9930710e655 108 const char *ip = wifi.get_ip_address();
jehoon 0:f9930710e655 109 printf("my IP address is: %s\r\n", ip ? ip : "No IP");
jehoon 0:f9930710e655 110 ConnectionStep = ASSOCIATED;
jehoon 0:f9930710e655 111 return 0;
jehoon 0:f9930710e655 112 }
jehoon 0:f9930710e655 113 }
jehoon 0:f9930710e655 114 printf("Fail to make AP\r\n");
jehoon 0:f9930710e655 115 ConnectionStep = DISASSOCIATED;
jehoon 0:f9930710e655 116 }
jehoon 0:f9930710e655 117
jehoon 0:f9930710e655 118 return -1;
jehoon 0:f9930710e655 119 }
jehoon 0:f9930710e655 120
jehoon 0:f9930710e655 121 int ClientExample()
jehoon 0:f9930710e655 122 {
jehoon 0:f9930710e655 123 int ret = 0;
jehoon 0:f9930710e655 124 if( ConnectionStep == ASSOCIATED )
jehoon 0:f9930710e655 125 {
jehoon 0:f9930710e655 126 SocketAddress addr((void*)ipaddr, NSAPI_IPv4, PORT);
jehoon 0:f9930710e655 127 //SocketAddress addr(&wifi, "www.google.com", 80);
jehoon 0:f9930710e655 128
jehoon 0:f9930710e655 129 printf("server IP address is: %s\r\n", addr.get_ip_address());
jehoon 0:f9930710e655 130
jehoon 0:f9930710e655 131
jehoon 0:f9930710e655 132
jehoon 0:f9930710e655 133 client.set_timeout(1000); // Set Block Mode.
jehoon 0:f9930710e655 134
jehoon 0:f9930710e655 135 client.open(&wifi);
jehoon 0:f9930710e655 136
jehoon 0:f9930710e655 137 if(client.connect(addr) < 0){
jehoon 0:f9930710e655 138 printf("Connection fail\r\n");
jehoon 0:f9930710e655 139 }
jehoon 0:f9930710e655 140 else{
jehoon 0:f9930710e655 141 printf("Connection Success!!\r\nIP: %s, %d\r\n", addr.get_ip_address(), addr.get_port());
jehoon 0:f9930710e655 142 ConnectionStep = CONNECTED;
jehoon 0:f9930710e655 143 }
jehoon 0:f9930710e655 144 }
jehoon 0:f9930710e655 145 }
jehoon 0:f9930710e655 146
jehoon 0:f9930710e655 147
jehoon 0:f9930710e655 148 int TryServerUpAndWaitClient()
jehoon 0:f9930710e655 149 {
jehoon 0:f9930710e655 150 if( ConnectionStep == ASSOCIATED )
jehoon 0:f9930710e655 151 {
jehoon 0:f9930710e655 152
jehoon 0:f9930710e655 153 if( server.open(&wifi) < 0 )
jehoon 0:f9930710e655 154 {
jehoon 0:f9930710e655 155 printf("WiFi Binding fail..\r\n");
jehoon 0:f9930710e655 156 return -1;
jehoon 0:f9930710e655 157 }
jehoon 0:f9930710e655 158
jehoon 0:f9930710e655 159
jehoon 0:f9930710e655 160 if( server.bind(wifi.get_ip_address(), PORT) < 0 )
jehoon 0:f9930710e655 161 {
jehoon 0:f9930710e655 162 printf("Bind fail..\r\n");
jehoon 0:f9930710e655 163 return -1;
jehoon 0:f9930710e655 164 }
jehoon 0:f9930710e655 165
jehoon 0:f9930710e655 166 if( server.listen(0) < 0 )
jehoon 0:f9930710e655 167 {
jehoon 0:f9930710e655 168 printf("Listen fail..\r\n");
jehoon 0:f9930710e655 169 return -1;
jehoon 0:f9930710e655 170 } else {
jehoon 0:f9930710e655 171 printf("Listen PORT: %d\r\n",PORT);
jehoon 0:f9930710e655 172 ConnectionStep = LISTENED;
jehoon 0:f9930710e655 173 }
jehoon 0:f9930710e655 174 }
jehoon 0:f9930710e655 175
jehoon 0:f9930710e655 176 //server.set_blocking(true);
jehoon 0:f9930710e655 177
jehoon 0:f9930710e655 178 while(ConnectionStep == LISTENED)
jehoon 0:f9930710e655 179 {
jehoon 0:f9930710e655 180 if( server.accept(&client) < 0 )
jehoon 0:f9930710e655 181 {
jehoon 0:f9930710e655 182 printf("accept fail..\r\n");
jehoon 0:f9930710e655 183 return -1;
jehoon 0:f9930710e655 184 }else{
jehoon 0:f9930710e655 185 printf("Connection Success!!\r\n");
jehoon 0:f9930710e655 186 ConnectionStep = CONNECTED;
jehoon 0:f9930710e655 187 return 0;
jehoon 0:f9930710e655 188 }
jehoon 0:f9930710e655 189 }
jehoon 0:f9930710e655 190
jehoon 0:f9930710e655 191 return -1;
jehoon 0:f9930710e655 192 }
jehoon 0:f9930710e655 193
jehoon 0:f9930710e655 194 int RunEcho()
jehoon 0:f9930710e655 195 {
jehoon 0:f9930710e655 196 int rcv_length = 0;
jehoon 0:f9930710e655 197 if(ConnectionStep == CONNECTED)
jehoon 0:f9930710e655 198 {
jehoon 0:f9930710e655 199 memset(rx_buf, 0, sizeof(rx_buf));
jehoon 0:f9930710e655 200 rcv_length = client.recv(rx_buf, sizeof(rx_buf));
jehoon 0:f9930710e655 201 if(rcv_length > 0)
jehoon 0:f9930710e655 202 {
jehoon 0:f9930710e655 203 printf("RECV: %s\r\n", rx_buf);
jehoon 0:f9930710e655 204 client.send(rx_buf, rcv_length);
jehoon 0:f9930710e655 205 }
jehoon 0:f9930710e655 206 }
jehoon 0:f9930710e655 207 }