Hello World program for NNN50_WIFI_API library
Dependencies: NNN50_WIFI_API
This is a must try example for basic WIFI functions using DELTA DFCM-NNN50 platform. In order to test with this example, user first need to open UDP Server port (use 1030 in this example) on a PC or mobile with Socket test tool (download UDP Test Tool' for PC, and Socket X for iOS or anymore socket test tool program available) When this example is running, the module will connect to a pre-defined AP router and run as UDP Client. For more information on the usage of regular TCP and UDP Sockets, the mbed handbook can be found here
main.cpp@8:08230913074f, 2017-06-29 (annotated)
- Committer:
- tsungta
- Date:
- Thu Jun 29 04:21:01 2017 +0000
- Revision:
- 8:08230913074f
- Parent:
- 6:1dac7bcca23d
update NNN50_WIFI_API and mbed-os to sync with each other
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tsungta | 6:1dac7bcca23d | 1 | /******************** (C) COPYRIGHT 2016 Delta Electronics, Inc. *************** |
tsungta | 6:1dac7bcca23d | 2 | * |
tsungta | 6:1dac7bcca23d | 3 | * File Name : main.cpp |
tsungta | 6:1dac7bcca23d | 4 | * Authors : Tsungta Wu - CPBG (tsungta.wu@deltaww.com) |
tsungta | 6:1dac7bcca23d | 5 | * Version : V.1.0.0 |
tsungta | 6:1dac7bcca23d | 6 | * Date : 2016/Nov/24 |
tsungta | 6:1dac7bcca23d | 7 | * |
tsungta | 6:1dac7bcca23d | 8 | * This example only show the most basic WiFi operation include AP scan and connect |
tsungta | 6:1dac7bcca23d | 9 | * The usage of TCP/UDP socket please refer to the mbed Handbook from the link below |
tsungta | 6:1dac7bcca23d | 10 | * https://developer.mbed.org/handbook/Socket |
tsungta | 6:1dac7bcca23d | 11 | * |
tsungta | 6:1dac7bcca23d | 12 | *******************************************************************************/ |
tsungta | 6:1dac7bcca23d | 13 | |
tsungta | 6:1dac7bcca23d | 14 | #include "mbed.h" |
tsungta | 6:1dac7bcca23d | 15 | #include "EthernetInterface.h" |
tsungta | 6:1dac7bcca23d | 16 | #include "WIFIDevice.h" |
tsungta | 6:1dac7bcca23d | 17 | |
tsungta | 6:1dac7bcca23d | 18 | const char* ECHO_SERVER_ADDRESS = "192.168.2.13"; |
tsungta | 6:1dac7bcca23d | 19 | const int ECHO_SERVER_PORT = 1030; |
tsungta | 6:1dac7bcca23d | 20 | |
tsungta | 6:1dac7bcca23d | 21 | void scanCallback(tstrM2mWifiscanResult result) |
tsungta | 6:1dac7bcca23d | 22 | { |
tsungta | 6:1dac7bcca23d | 23 | printf("SSID: %s \n", result.au8SSID); |
tsungta | 6:1dac7bcca23d | 24 | printf("RSSI: %i \n", result.s8rssi); |
tsungta | 6:1dac7bcca23d | 25 | } |
tsungta | 6:1dac7bcca23d | 26 | |
tsungta | 6:1dac7bcca23d | 27 | int main() { |
tsungta | 6:1dac7bcca23d | 28 | |
tsungta | 6:1dac7bcca23d | 29 | EthernetInterface eth; |
tsungta | 6:1dac7bcca23d | 30 | WIFIDevice wifi; |
tsungta | 6:1dac7bcca23d | 31 | |
tsungta | 6:1dac7bcca23d | 32 | eth.init(); |
tsungta | 6:1dac7bcca23d | 33 | |
tsungta | 6:1dac7bcca23d | 34 | wifi.apScan(scanCallback); |
tsungta | 6:1dac7bcca23d | 35 | |
tsungta | 6:1dac7bcca23d | 36 | wifi.setNetwork(M2M_WIFI_SEC_WPA_PSK, "TP-LINK_2.4G_TTWU", "0972753720"); |
tsungta | 6:1dac7bcca23d | 37 | |
tsungta | 6:1dac7bcca23d | 38 | eth.connect(); |
tsungta | 6:1dac7bcca23d | 39 | |
tsungta | 6:1dac7bcca23d | 40 | if(wifi.is_AP_connected()) |
tsungta | 6:1dac7bcca23d | 41 | printf("Connect Success! \n"); |
tsungta | 6:1dac7bcca23d | 42 | else |
tsungta | 6:1dac7bcca23d | 43 | printf("Connect Fail! \n"); |
tsungta | 6:1dac7bcca23d | 44 | |
tsungta | 6:1dac7bcca23d | 45 | printf("MAC: %s\n", eth.getMACAddress()); |
tsungta | 6:1dac7bcca23d | 46 | printf("IP: %s\n", eth.getIPAddress()); |
tsungta | 6:1dac7bcca23d | 47 | printf("Gateway: %s\n", eth.getGateway()); |
tsungta | 6:1dac7bcca23d | 48 | printf("NetworkMask: %s\n", eth.getNetworkMask()); |
tsungta | 6:1dac7bcca23d | 49 | |
tsungta | 6:1dac7bcca23d | 50 | UDPSocket sock; |
tsungta | 6:1dac7bcca23d | 51 | sock.init(); |
tsungta | 6:1dac7bcca23d | 52 | |
tsungta | 6:1dac7bcca23d | 53 | Endpoint echo_server; |
tsungta | 6:1dac7bcca23d | 54 | echo_server.set_address(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT); |
tsungta | 6:1dac7bcca23d | 55 | |
tsungta | 6:1dac7bcca23d | 56 | char out_buffer[] = "Hello World"; |
tsungta | 6:1dac7bcca23d | 57 | printf("Sending message '%s' to server (%s)\n",out_buffer,ECHO_SERVER_ADDRESS); |
tsungta | 6:1dac7bcca23d | 58 | sock.sendTo(echo_server, out_buffer, sizeof(out_buffer)); |
tsungta | 6:1dac7bcca23d | 59 | |
tsungta | 6:1dac7bcca23d | 60 | char in_buffer[256]; //IMPORTANT, array size MUST >= the actual received data size or set to maximum of 1400 if there is uncertainty |
tsungta | 6:1dac7bcca23d | 61 | int n = sock.receiveFrom(echo_server, in_buffer, sizeof(in_buffer)); |
tsungta | 6:1dac7bcca23d | 62 | |
tsungta | 6:1dac7bcca23d | 63 | if(n <0) |
tsungta | 6:1dac7bcca23d | 64 | in_buffer[0] = '\0';//IMPORTANT, in case n = -1 when set_bloacking is timeout, prevent the illegal array in_buffer[-1] access |
tsungta | 6:1dac7bcca23d | 65 | else |
tsungta | 6:1dac7bcca23d | 66 | in_buffer[n] = '\0'; |
tsungta | 6:1dac7bcca23d | 67 | |
tsungta | 6:1dac7bcca23d | 68 | printf("Received message from server: '%s'\n", in_buffer); |
tsungta | 6:1dac7bcca23d | 69 | |
tsungta | 6:1dac7bcca23d | 70 | sock.close(); |
tsungta | 6:1dac7bcca23d | 71 | |
tsungta | 6:1dac7bcca23d | 72 | eth.disconnect(); |
tsungta | 6:1dac7bcca23d | 73 | |
tsungta | 6:1dac7bcca23d | 74 | wifi.sleep(); |
tsungta | 6:1dac7bcca23d | 75 | |
tsungta | 6:1dac7bcca23d | 76 | while(1) { |
tsungta | 6:1dac7bcca23d | 77 | } |
tsungta | 6:1dac7bcca23d | 78 | } |
tsungta | 6:1dac7bcca23d | 79 |