
this is using the mbed os version 5-13-1
source/network-helper.h@73:6f5021cbe752, 2019-02-28 (annotated)
- Committer:
- ocomeni
- Date:
- Thu Feb 28 18:13:48 2019 +0000
- Revision:
- 73:6f5021cbe752
- Child:
- 74:f26e846adfe9
commit current application code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ocomeni | 73:6f5021cbe752 | 1 | #ifndef _MBED_HTTP_EXAMPLE_H_ |
ocomeni | 73:6f5021cbe752 | 2 | #define _MBED_HTTP_EXAMPLE_H_ |
ocomeni | 73:6f5021cbe752 | 3 | |
ocomeni | 73:6f5021cbe752 | 4 | #include "mbed.h" |
ocomeni | 73:6f5021cbe752 | 5 | #ifdef TARGET_SIMULATOR |
ocomeni | 73:6f5021cbe752 | 6 | #include "EthernetInterface.h" |
ocomeni | 73:6f5021cbe752 | 7 | #endif |
ocomeni | 73:6f5021cbe752 | 8 | |
ocomeni | 73:6f5021cbe752 | 9 | |
ocomeni | 73:6f5021cbe752 | 10 | const char * sec2str(nsapi_security_t sec) |
ocomeni | 73:6f5021cbe752 | 11 | { |
ocomeni | 73:6f5021cbe752 | 12 | switch (sec) { |
ocomeni | 73:6f5021cbe752 | 13 | case NSAPI_SECURITY_NONE: |
ocomeni | 73:6f5021cbe752 | 14 | return "None"; |
ocomeni | 73:6f5021cbe752 | 15 | case NSAPI_SECURITY_WEP: |
ocomeni | 73:6f5021cbe752 | 16 | return "WEP"; |
ocomeni | 73:6f5021cbe752 | 17 | case NSAPI_SECURITY_WPA: |
ocomeni | 73:6f5021cbe752 | 18 | return "WPA"; |
ocomeni | 73:6f5021cbe752 | 19 | case NSAPI_SECURITY_WPA2: |
ocomeni | 73:6f5021cbe752 | 20 | return "WPA2"; |
ocomeni | 73:6f5021cbe752 | 21 | case NSAPI_SECURITY_WPA_WPA2: |
ocomeni | 73:6f5021cbe752 | 22 | return "WPA/WPA2"; |
ocomeni | 73:6f5021cbe752 | 23 | case NSAPI_SECURITY_UNKNOWN: |
ocomeni | 73:6f5021cbe752 | 24 | default: |
ocomeni | 73:6f5021cbe752 | 25 | return "Unknown"; |
ocomeni | 73:6f5021cbe752 | 26 | } |
ocomeni | 73:6f5021cbe752 | 27 | } |
ocomeni | 73:6f5021cbe752 | 28 | |
ocomeni | 73:6f5021cbe752 | 29 | int scan_demo(WiFiInterface *wifi) |
ocomeni | 73:6f5021cbe752 | 30 | { |
ocomeni | 73:6f5021cbe752 | 31 | WiFiAccessPoint *ap; |
ocomeni | 73:6f5021cbe752 | 32 | |
ocomeni | 73:6f5021cbe752 | 33 | printf("Scan:\n"); |
ocomeni | 73:6f5021cbe752 | 34 | |
ocomeni | 73:6f5021cbe752 | 35 | int count = wifi->scan(NULL,0); |
ocomeni | 73:6f5021cbe752 | 36 | |
ocomeni | 73:6f5021cbe752 | 37 | if (count <= 0) { |
ocomeni | 73:6f5021cbe752 | 38 | printf("scan() failed with return value: %d\n", count); |
ocomeni | 73:6f5021cbe752 | 39 | return 0; |
ocomeni | 73:6f5021cbe752 | 40 | } |
ocomeni | 73:6f5021cbe752 | 41 | |
ocomeni | 73:6f5021cbe752 | 42 | /* Limit number of network arbitrary to 15 */ |
ocomeni | 73:6f5021cbe752 | 43 | count = count < 15 ? count : 15; |
ocomeni | 73:6f5021cbe752 | 44 | |
ocomeni | 73:6f5021cbe752 | 45 | ap = new WiFiAccessPoint[count]; |
ocomeni | 73:6f5021cbe752 | 46 | count = wifi->scan(ap, count); |
ocomeni | 73:6f5021cbe752 | 47 | |
ocomeni | 73:6f5021cbe752 | 48 | if (count <= 0) { |
ocomeni | 73:6f5021cbe752 | 49 | printf("scan() failed with return value: %d\n", count); |
ocomeni | 73:6f5021cbe752 | 50 | return 0; |
ocomeni | 73:6f5021cbe752 | 51 | } |
ocomeni | 73:6f5021cbe752 | 52 | |
ocomeni | 73:6f5021cbe752 | 53 | for (int i = 0; i < count; i++) { |
ocomeni | 73:6f5021cbe752 | 54 | printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(), |
ocomeni | 73:6f5021cbe752 | 55 | sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2], |
ocomeni | 73:6f5021cbe752 | 56 | ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel()); |
ocomeni | 73:6f5021cbe752 | 57 | } |
ocomeni | 73:6f5021cbe752 | 58 | printf("%d networks available.\n", count); |
ocomeni | 73:6f5021cbe752 | 59 | |
ocomeni | 73:6f5021cbe752 | 60 | delete[] ap; |
ocomeni | 73:6f5021cbe752 | 61 | return count; |
ocomeni | 73:6f5021cbe752 | 62 | } |
ocomeni | 73:6f5021cbe752 | 63 | |
ocomeni | 73:6f5021cbe752 | 64 | |
ocomeni | 73:6f5021cbe752 | 65 | /** |
ocomeni | 73:6f5021cbe752 | 66 | * Connect to the network using the default networking interface, |
ocomeni | 73:6f5021cbe752 | 67 | * you can also swap this out with a driver for a different networking interface |
ocomeni | 73:6f5021cbe752 | 68 | * if you use WiFi: see mbed_app.json for the credentials |
ocomeni | 73:6f5021cbe752 | 69 | */ |
ocomeni | 73:6f5021cbe752 | 70 | NetworkInterface *connect_to_default_network_interface() { |
ocomeni | 73:6f5021cbe752 | 71 | printf("[NWKH] Connecting to network...\n"); |
ocomeni | 73:6f5021cbe752 | 72 | |
ocomeni | 73:6f5021cbe752 | 73 | #ifdef TARGET_SIMULATOR |
ocomeni | 73:6f5021cbe752 | 74 | NetworkInterface* network = new EthernetInterface(); |
ocomeni | 73:6f5021cbe752 | 75 | #else |
ocomeni | 73:6f5021cbe752 | 76 | //NetworkInterface* network = NetworkInterface::get_default_instance(); |
ocomeni | 73:6f5021cbe752 | 77 | WiFiInterface *network = WiFiInterface::get_default_instance(); |
ocomeni | 73:6f5021cbe752 | 78 | if (!network) { |
ocomeni | 73:6f5021cbe752 | 79 | printf("ERROR: No WiFiInterface found.\n"); |
ocomeni | 73:6f5021cbe752 | 80 | return NULL; |
ocomeni | 73:6f5021cbe752 | 81 | } |
ocomeni | 73:6f5021cbe752 | 82 | int count; |
ocomeni | 73:6f5021cbe752 | 83 | count = scan_demo(network); |
ocomeni | 73:6f5021cbe752 | 84 | if (count == 0) { |
ocomeni | 73:6f5021cbe752 | 85 | printf("No WIFI APNs found - can't continue further.\n"); |
ocomeni | 73:6f5021cbe752 | 86 | return NULL; |
ocomeni | 73:6f5021cbe752 | 87 | } |
ocomeni | 73:6f5021cbe752 | 88 | |
ocomeni | 73:6f5021cbe752 | 89 | //printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); |
ocomeni | 73:6f5021cbe752 | 90 | //ret = network->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
ocomeni | 73:6f5021cbe752 | 91 | //if (ret != 0) { |
ocomeni | 73:6f5021cbe752 | 92 | // printf("\nConnection error: %d\n", ret); |
ocomeni | 73:6f5021cbe752 | 93 | // return -1; |
ocomeni | 73:6f5021cbe752 | 94 | //} |
ocomeni | 73:6f5021cbe752 | 95 | #endif |
ocomeni | 73:6f5021cbe752 | 96 | |
ocomeni | 73:6f5021cbe752 | 97 | //nsapi_error_t connect_status = network->connect(); |
ocomeni | 73:6f5021cbe752 | 98 | nsapi_error_t connect_status = network->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
ocomeni | 73:6f5021cbe752 | 99 | |
ocomeni | 73:6f5021cbe752 | 100 | if (connect_status != NSAPI_ERROR_OK) { |
ocomeni | 73:6f5021cbe752 | 101 | printf("[NWKH] Failed to connect to network (%d)\n", connect_status); |
ocomeni | 73:6f5021cbe752 | 102 | return NULL; |
ocomeni | 73:6f5021cbe752 | 103 | } |
ocomeni | 73:6f5021cbe752 | 104 | |
ocomeni | 73:6f5021cbe752 | 105 | printf("[NWKH] Connected to the network\n"); |
ocomeni | 73:6f5021cbe752 | 106 | printf("[NWKH] IP address: %s\n", network->get_ip_address()); |
ocomeni | 73:6f5021cbe752 | 107 | return network; |
ocomeni | 73:6f5021cbe752 | 108 | } |
ocomeni | 73:6f5021cbe752 | 109 | |
ocomeni | 73:6f5021cbe752 | 110 | #endif // _MBED_HTTP_EXAMPLE_H_ |