this is using the mbed os version 5-13-1

Dependencies:   mbed-http

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers network-helper.h Source File

network-helper.h

00001 #ifndef _MBED_HTTP_EXAMPLE_H_
00002 #define _MBED_HTTP_EXAMPLE_H_
00003 
00004 #include "mbed.h"
00005 #ifdef TARGET_SIMULATOR
00006 #include "EthernetInterface.h"
00007 #endif
00008 
00009 
00010 const char * sec2str(nsapi_security_t sec)
00011 {
00012     switch (sec) {
00013         case NSAPI_SECURITY_NONE:
00014             return "None";
00015         case NSAPI_SECURITY_WEP:
00016             return "WEP";
00017         case NSAPI_SECURITY_WPA:
00018             return "WPA";
00019         case NSAPI_SECURITY_WPA2:
00020             return "WPA2";
00021         case NSAPI_SECURITY_WPA_WPA2:
00022             return "WPA/WPA2";
00023         case NSAPI_SECURITY_UNKNOWN:
00024         default:
00025             return "Unknown";
00026     }
00027 }
00028 #define MAX_CHANNEL_NUM 4
00029 const int channel_list [] = {1,11, 36, 44};
00030 int getAPcount(WiFiInterface *wifi)
00031 {
00032     int count = 0;
00033     for(uint8_t idx=0; idx< MAX_CHANNEL_NUM;idx++){
00034         printf("\n Scanning channel %d - APs so far = %d\n", channel_list[idx], count);
00035         wifi->set_channel(channel_list[idx]);
00036         count += wifi->scan(NULL,0);
00037         wait_ms(150); // wait for 150 ms between scans
00038     }
00039     printf("\n Total APs found = %d\n", count);
00040     return count;
00041 }
00042 
00043 #define MAX_APs_PER_CHAN 3
00044 int getAPdetails(WiFiAccessPoint *ap, WiFiInterface *wifi, int apCount)
00045 {
00046      int count =0;
00047      for(uint8_t idx=0; idx< MAX_CHANNEL_NUM;idx++){
00048         printf("\n Scanning channel %d - APs so far = %d  apCount =%d\n", channel_list[idx], count, apCount);
00049         wifi->set_channel(channel_list[idx]);
00050         count += wifi->scan(&ap[count],(apCount - count) % MAX_APs_PER_CHAN);
00051         wait_ms(150); // wait for 150 ms between scans
00052         if(count >= apCount) break;
00053     }
00054     wifi->set_channel(0);
00055     printf("\n Total APs found = %d\n", count);
00056     return count;
00057 }
00058 
00059 int scan_demo2(WiFiInterface *wifi)
00060 {
00061     WiFiAccessPoint *ap;
00062 
00063     printf("Scan:\n");
00064 
00065     //int count = wifi->scan(NULL,0);
00066     int count;
00067     count = getAPcount(wifi);
00068     if (count <= 0) {
00069         printf("scan() failed with return value: %d\n", count);
00070         return 0;
00071     }
00072 
00073     /* Limit number of network arbitrary to 15 */
00074     count = count < 15 ? count : 15;
00075 
00076     ap = new WiFiAccessPoint[count];
00077     //count = wifi->scan(ap, count);
00078     count = getAPdetails(ap, wifi, count);
00079 
00080     if (count <= 0) {
00081         printf("scan() failed with return value: %d\n", count);
00082         return 0;
00083     }
00084 
00085     for (int i = 0; i < count; i++) {
00086         printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(),
00087                sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
00088                ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
00089     }
00090     printf("%d networks available.\n", count);
00091 
00092     delete[] ap;
00093     return count;
00094 }
00095 
00096 
00097 int scan_demo(WiFiInterface *wifi)
00098 {
00099     WiFiAccessPoint *ap;
00100 
00101     printf("Scan:\n");
00102 
00103     int count = wifi->scan(NULL,0);
00104 
00105     if (count <= 0) {
00106         printf("scan() failed with return value: %d\n", count);
00107         return 0;
00108     }
00109 
00110     /* Limit number of network arbitrary to 15 */
00111     count = count < 15 ? count : 15;
00112 
00113     ap = new WiFiAccessPoint[count];
00114     count = wifi->scan(ap, count);
00115 
00116     if (count <= 0) {
00117         printf("scan() failed with return value: %d\n", count);
00118         return 0;
00119     }
00120 
00121     for (int i = 0; i < count; i++) {
00122         printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(),
00123                sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
00124                ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
00125     }
00126     printf("%d networks available.\n", count);
00127 
00128     delete[] ap;
00129     return count;
00130 }
00131 
00132 
00133 
00134 /**
00135  * Connect to the network using the default networking interface,
00136  * you can also swap this out with a driver for a different networking interface
00137  * if you use WiFi: see mbed_app.json for the credentials
00138  */
00139 NetworkInterface *connect_to_default_network_interface() {
00140     printf("[NWKH] Connecting to network...\n");
00141 
00142 #ifdef TARGET_SIMULATOR
00143     NetworkInterface* network = new EthernetInterface();
00144 #else
00145     //NetworkInterface* network = NetworkInterface::get_default_instance();
00146     WiFiInterface *network = WiFiInterface::get_default_instance();
00147     if (!network) {
00148         printf("ERROR: No WiFiInterface found.\n");
00149         return NULL;
00150     }
00151     nsapi_error_t error;
00152     
00153     error = network->set_blocking(false);
00154     printf("Wifi Interface set to Non-Blocking .\n");
00155     printf("Waiting for 5 seconds before starting scan...\n");
00156     for(int i=0;i<5;i++){
00157         printf("%d", i);
00158         wait(1);
00159     }
00160     int count;
00161     count = scan_demo(network);
00162     //count = scan_demo2(network);
00163     if (count == 0) {
00164         printf("No WIFI APNs found - can't continue further.\n");
00165         return NULL;
00166     }
00167 
00168     printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID);
00169     //ret = network->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00170     //if (ret != 0) {
00171     //    printf("\nConnection error: %d\n", ret);
00172     //    return -1;
00173     //}
00174 #endif
00175     printf("Waiting for 5 seconds before connecting...\n");
00176     for(int i=0;i<5;i++){
00177         printf("%d", i);
00178         wait(1);
00179     }
00180 
00181     //nsapi_error_t connect_status = network->connect();
00182     nsapi_error_t connect_status = network->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00183 
00184     if (connect_status != NSAPI_ERROR_OK) {
00185         printf("[NWKH] Failed to connect to network (%d)\n", connect_status);
00186         return NULL;
00187     }
00188 
00189     printf("[NWKH] Connected to the network\n");
00190     printf("[NWKH] IP address: %s\n", network->get_ip_address());
00191     return network;
00192 }
00193 
00194 #endif // _MBED_HTTP_EXAMPLE_H_