Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: NNN50_WIFI_API
main.cpp
00001 /******************** (C) COPYRIGHT 2016 Delta Electronics, Inc. *************** 00002 * 00003 * File Name : main.cpp 00004 * Authors : Tsungta Wu - CPBG (tsungta.wu@deltaww.com) 00005 * Version : V.1.0.0 00006 * Date : 2016/Nov/24 00007 * 00008 * This example only show the most basic WiFi operation include AP scan and connect 00009 * The usage of TCP/UDP socket please refer to the mbed Handbook from the link below 00010 * https://developer.mbed.org/handbook/Socket 00011 * 00012 *******************************************************************************/ 00013 00014 #include "mbed.h" 00015 #include "EthernetInterface.h" 00016 #include "WIFIDevice.h" 00017 00018 const char* ECHO_SERVER_ADDRESS = "192.168.2.13"; 00019 const int ECHO_SERVER_PORT = 1030; 00020 00021 void scanCallback(tstrM2mWifiscanResult result) 00022 { 00023 printf("SSID: %s \n", result.au8SSID); 00024 printf("RSSI: %i \n", result.s8rssi); 00025 } 00026 00027 int main() { 00028 00029 EthernetInterface eth; 00030 WIFIDevice wifi; 00031 00032 eth.init(); 00033 00034 wifi.apScan(scanCallback); 00035 00036 wifi.setNetwork(M2M_WIFI_SEC_WPA_PSK, "TP-LINK_2.4G_TTWU", "0972753720"); 00037 00038 eth.connect(); 00039 00040 if(wifi.is_AP_connected()) 00041 printf("Connect Success! \n"); 00042 else 00043 printf("Connect Fail! \n"); 00044 00045 printf("MAC: %s\n", eth.getMACAddress()); 00046 printf("IP: %s\n", eth.getIPAddress()); 00047 printf("Gateway: %s\n", eth.getGateway()); 00048 printf("NetworkMask: %s\n", eth.getNetworkMask()); 00049 00050 UDPSocket sock; 00051 sock.init(); 00052 00053 Endpoint echo_server; 00054 echo_server.set_address(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT); 00055 00056 char out_buffer[] = "Hello World"; 00057 printf("Sending message '%s' to server (%s)\n",out_buffer,ECHO_SERVER_ADDRESS); 00058 sock.sendTo(echo_server, out_buffer, sizeof(out_buffer)); 00059 00060 char in_buffer[256]; //IMPORTANT, array size MUST >= the actual received data size or set to maximum of 1400 if there is uncertainty 00061 int n = sock.receiveFrom(echo_server, in_buffer, sizeof(in_buffer)); 00062 00063 if(n <0) 00064 in_buffer[0] = '\0';//IMPORTANT, in case n = -1 when set_bloacking is timeout, prevent the illegal array in_buffer[-1] access 00065 else 00066 in_buffer[n] = '\0'; 00067 00068 printf("Received message from server: '%s'\n", in_buffer); 00069 00070 sock.close(); 00071 00072 eth.disconnect(); 00073 00074 wifi.sleep(); 00075 00076 while(1) { 00077 } 00078 } 00079
Generated on Thu Jul 14 2022 01:48:34 by
1.7.2