CLI example for NNN50
Dependencies: NNN50_WIFI_API
Fork of NNN50_WiFi_HelloWorld by
main.cpp@3:9bd3e863e15b, 2017-04-02 (annotated)
- Committer:
- tsungta
- Date:
- Sun Apr 02 10:16:20 2017 +0000
- Revision:
- 3:9bd3e863e15b
- Parent:
- 2:92946804ed6f
- Child:
- 4:11b81280c65b
Revise main to fix bug (empty getIPAddress, etc); Update NNN50_WIFI_API to revision 18 (revision 20 have bug)
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 | 3:9bd3e863e15b | 35 | |
tsungta | 0:b5f183111420 | 36 | eth.init(); |
tsungta | 0:b5f183111420 | 37 | |
tsungta | 0:b5f183111420 | 38 | wifi.apScan(scanCallback); |
tsungta | 0:b5f183111420 | 39 | |
tsungta | 1:a357a8f9ac8b | 40 | wifi.setNetwork(M2M_WIFI_SEC_WPA_PSK, "TP-LINK_2.4G_TTWU", "0972753720"); |
tsungta | 3:9bd3e863e15b | 41 | |
tsungta | 3:9bd3e863e15b | 42 | eth.connect(); |
tsungta | 0:b5f183111420 | 43 | |
tsungta | 0:b5f183111420 | 44 | if(wifi.is_AP_connected()) |
tsungta | 0:b5f183111420 | 45 | uart.printf("Connect Success! \n"); |
tsungta | 0:b5f183111420 | 46 | else |
tsungta | 3:9bd3e863e15b | 47 | uart.printf("Connect Fail! \n"); |
tsungta | 3:9bd3e863e15b | 48 | |
tsungta | 3:9bd3e863e15b | 49 | uart.printf("MAC: %s\n", eth.getMACAddress()); |
tsungta | 3:9bd3e863e15b | 50 | uart.printf("IP: %s\n", eth.getIPAddress()); |
tsungta | 3:9bd3e863e15b | 51 | uart.printf("Gateway: %s\n", eth.getGateway()); |
tsungta | 3:9bd3e863e15b | 52 | uart.printf("NetworkMask: %s\n", eth.getNetworkMask()); |
tsungta | 1:a357a8f9ac8b | 53 | |
tsungta | 1:a357a8f9ac8b | 54 | UDPSocket sock; |
tsungta | 1:a357a8f9ac8b | 55 | sock.init(); |
tsungta | 1:a357a8f9ac8b | 56 | |
tsungta | 1:a357a8f9ac8b | 57 | Endpoint echo_server; |
tsungta | 1:a357a8f9ac8b | 58 | echo_server.set_address(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT); |
tsungta | 1:a357a8f9ac8b | 59 | |
tsungta | 1:a357a8f9ac8b | 60 | char out_buffer[] = "Hello World"; |
tsungta | 1:a357a8f9ac8b | 61 | uart.printf("Sending message '%s' to server (%s)\n",out_buffer,ECHO_SERVER_ADDRESS); |
tsungta | 1:a357a8f9ac8b | 62 | sock.sendTo(echo_server, out_buffer, sizeof(out_buffer)); |
tsungta | 1:a357a8f9ac8b | 63 | |
tsungta | 1:a357a8f9ac8b | 64 | char in_buffer[256]; |
tsungta | 1:a357a8f9ac8b | 65 | int n = sock.receiveFrom(echo_server, in_buffer, sizeof(in_buffer)); |
tsungta | 1:a357a8f9ac8b | 66 | |
tsungta | 1:a357a8f9ac8b | 67 | in_buffer[n] = '\0'; |
tsungta | 1:a357a8f9ac8b | 68 | uart.printf("Received message from server: '%s'\n", in_buffer); |
tsungta | 1:a357a8f9ac8b | 69 | |
tsungta | 1:a357a8f9ac8b | 70 | sock.close(); |
tsungta | 1:a357a8f9ac8b | 71 | |
tsungta | 1:a357a8f9ac8b | 72 | eth.disconnect(); |
tsungta | 2:92946804ed6f | 73 | |
tsungta | 2:92946804ed6f | 74 | wifi.sleep(); |
tsungta | 1:a357a8f9ac8b | 75 | |
tsungta | 0:b5f183111420 | 76 | while(1) { |
tsungta | 0:b5f183111420 | 77 | } |
tsungta | 0:b5f183111420 | 78 | } |
tsungta | 0:b5f183111420 | 79 |