CE_Test_UDP add mult-AP setting
Dependencies: NNN50_WIFI_API mbed
Fork of NNN50_CE_Test_UDP by
main.cpp@2:92946804ed6f, 2016-12-19 (annotated)
- Committer:
- tsungta
- Date:
- Mon Dec 19 03:56:13 2016 +0000
- Revision:
- 2:92946804ed6f
- Parent:
- 1:a357a8f9ac8b
- Child:
- 3:ad0fd3c1247f
add wifi.sleep() in the sample code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tsungta | 0:b5f183111420 | 1 | /******************** (C) COPYRIGHT 2016 Delta Electronics, Inc. *************** |
tsungta | 0:b5f183111420 | 2 | * |
tsungta | 0:b5f183111420 | 3 | * File Name : main.cpp |
tsungta | 0:b5f183111420 | 4 | * Authors : Tsungta Wu - CPBG (tsungta.wu@deltaww.com) |
tsungta | 0:b5f183111420 | 5 | * Version : V.1.0.0 |
tsungta | 0:b5f183111420 | 6 | * Date : 2016/Nov/24 |
tsungta | 0:b5f183111420 | 7 | * |
tsungta | 0:b5f183111420 | 8 | * This example only show the most basic WiFi operation include AP scan and connect |
tsungta | 0:b5f183111420 | 9 | * The usage of TCP/UDP socket please refer to the mbed Handbook from the link below |
tsungta | 0:b5f183111420 | 10 | * https://developer.mbed.org/handbook/Socket |
tsungta | 0:b5f183111420 | 11 | * |
tsungta | 0:b5f183111420 | 12 | *******************************************************************************/ |
tsungta | 0:b5f183111420 | 13 | |
tsungta | 0:b5f183111420 | 14 | #include "mbed.h" |
tsungta | 0:b5f183111420 | 15 | #include "EthernetInterface.h" |
tsungta | 0:b5f183111420 | 16 | #include "WIFIDevice.h" |
tsungta | 0:b5f183111420 | 17 | |
tsungta | 1:a357a8f9ac8b | 18 | const char* ECHO_SERVER_ADDRESS = "192.168.1.100"; |
tsungta | 1:a357a8f9ac8b | 19 | const int ECHO_SERVER_PORT = 1030; |
tsungta | 1:a357a8f9ac8b | 20 | |
tsungta | 0:b5f183111420 | 21 | Serial uart(p17, p16);//temporary define for alpha release |
tsungta | 0:b5f183111420 | 22 | |
tsungta | 0:b5f183111420 | 23 | void scanCallback(tstrM2mWifiscanResult result) |
tsungta | 0:b5f183111420 | 24 | { |
tsungta | 0:b5f183111420 | 25 | uart.printf("SSID: %s \n", result.au8SSID); |
tsungta | 0:b5f183111420 | 26 | uart.printf("RSSI: %i \n", result.s8rssi); |
tsungta | 0:b5f183111420 | 27 | } |
tsungta | 0:b5f183111420 | 28 | |
tsungta | 0:b5f183111420 | 29 | int main() { |
tsungta | 0:b5f183111420 | 30 | |
tsungta | 0:b5f183111420 | 31 | EthernetInterface eth; |
tsungta | 0:b5f183111420 | 32 | WIFIDevice wifi; |
tsungta | 0:b5f183111420 | 33 | |
tsungta | 0:b5f183111420 | 34 | uart.baud(9600); |
tsungta | 0:b5f183111420 | 35 | |
tsungta | 0:b5f183111420 | 36 | eth.init(); |
tsungta | 0:b5f183111420 | 37 | uart.printf("MAC: %s\n", eth.getMACAddress()); |
tsungta | 0:b5f183111420 | 38 | |
tsungta | 0:b5f183111420 | 39 | wifi.apScan(scanCallback); |
tsungta | 0:b5f183111420 | 40 | |
tsungta | 1:a357a8f9ac8b | 41 | wifi.setNetwork(M2M_WIFI_SEC_WPA_PSK, "TP-LINK_2.4G_TTWU", "0972753720"); |
tsungta | 0:b5f183111420 | 42 | eth.connect(); |
tsungta | 0:b5f183111420 | 43 | printf("IP: %s\n", eth.getIPAddress()); |
tsungta | 0:b5f183111420 | 44 | printf("Gateway: %s\n", eth.getGateway()); |
tsungta | 0:b5f183111420 | 45 | printf("NetworkMask: %s\n", eth.getNetworkMask()); |
tsungta | 0:b5f183111420 | 46 | |
tsungta | 0:b5f183111420 | 47 | if(wifi.is_AP_connected()) |
tsungta | 0:b5f183111420 | 48 | uart.printf("Connect Success! \n"); |
tsungta | 0:b5f183111420 | 49 | else |
tsungta | 0:b5f183111420 | 50 | uart.printf("Connect Fail! \n"); |
tsungta | 1:a357a8f9ac8b | 51 | |
tsungta | 1:a357a8f9ac8b | 52 | UDPSocket sock; |
tsungta | 1:a357a8f9ac8b | 53 | sock.init(); |
tsungta | 1:a357a8f9ac8b | 54 | |
tsungta | 1:a357a8f9ac8b | 55 | Endpoint echo_server; |
tsungta | 1:a357a8f9ac8b | 56 | echo_server.set_address(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT); |
tsungta | 1:a357a8f9ac8b | 57 | |
tsungta | 1:a357a8f9ac8b | 58 | char out_buffer[] = "Hello World"; |
tsungta | 1:a357a8f9ac8b | 59 | uart.printf("Sending message '%s' to server (%s)\n",out_buffer,ECHO_SERVER_ADDRESS); |
tsungta | 1:a357a8f9ac8b | 60 | sock.sendTo(echo_server, out_buffer, sizeof(out_buffer)); |
tsungta | 1:a357a8f9ac8b | 61 | |
tsungta | 1:a357a8f9ac8b | 62 | char in_buffer[256]; |
tsungta | 1:a357a8f9ac8b | 63 | int n = sock.receiveFrom(echo_server, in_buffer, sizeof(in_buffer)); |
tsungta | 1:a357a8f9ac8b | 64 | |
tsungta | 1:a357a8f9ac8b | 65 | in_buffer[n] = '\0'; |
tsungta | 1:a357a8f9ac8b | 66 | uart.printf("Received message from server: '%s'\n", in_buffer); |
tsungta | 1:a357a8f9ac8b | 67 | |
tsungta | 1:a357a8f9ac8b | 68 | sock.close(); |
tsungta | 1:a357a8f9ac8b | 69 | |
tsungta | 1:a357a8f9ac8b | 70 | eth.disconnect(); |
tsungta | 2:92946804ed6f | 71 | |
tsungta | 2:92946804ed6f | 72 | wifi.sleep(); |
tsungta | 1:a357a8f9ac8b | 73 | |
tsungta | 0:b5f183111420 | 74 | while(1) { |
tsungta | 0:b5f183111420 | 75 | } |
tsungta | 0:b5f183111420 | 76 | } |
tsungta | 0:b5f183111420 | 77 |