this is using the mbed os version 5-13-1
Diff: source/network-helper.h
- Revision:
- 73:6f5021cbe752
- Child:
- 74:f26e846adfe9
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/source/network-helper.h Thu Feb 28 18:13:48 2019 +0000
@@ -0,0 +1,110 @@
+#ifndef _MBED_HTTP_EXAMPLE_H_
+#define _MBED_HTTP_EXAMPLE_H_
+
+#include "mbed.h"
+#ifdef TARGET_SIMULATOR
+#include "EthernetInterface.h"
+#endif
+
+
+const char * sec2str(nsapi_security_t sec)
+{
+ switch (sec) {
+ case NSAPI_SECURITY_NONE:
+ return "None";
+ case NSAPI_SECURITY_WEP:
+ return "WEP";
+ case NSAPI_SECURITY_WPA:
+ return "WPA";
+ case NSAPI_SECURITY_WPA2:
+ return "WPA2";
+ case NSAPI_SECURITY_WPA_WPA2:
+ return "WPA/WPA2";
+ case NSAPI_SECURITY_UNKNOWN:
+ default:
+ return "Unknown";
+ }
+}
+
+int scan_demo(WiFiInterface *wifi)
+{
+ WiFiAccessPoint *ap;
+
+ printf("Scan:\n");
+
+ int count = wifi->scan(NULL,0);
+
+ if (count <= 0) {
+ printf("scan() failed with return value: %d\n", count);
+ return 0;
+ }
+
+ /* Limit number of network arbitrary to 15 */
+ count = count < 15 ? count : 15;
+
+ ap = new WiFiAccessPoint[count];
+ count = wifi->scan(ap, count);
+
+ if (count <= 0) {
+ printf("scan() failed with return value: %d\n", count);
+ return 0;
+ }
+
+ for (int i = 0; i < count; i++) {
+ printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(),
+ sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
+ ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
+ }
+ printf("%d networks available.\n", count);
+
+ delete[] ap;
+ return count;
+}
+
+
+/**
+ * Connect to the network using the default networking interface,
+ * you can also swap this out with a driver for a different networking interface
+ * if you use WiFi: see mbed_app.json for the credentials
+ */
+NetworkInterface *connect_to_default_network_interface() {
+ printf("[NWKH] Connecting to network...\n");
+
+#ifdef TARGET_SIMULATOR
+ NetworkInterface* network = new EthernetInterface();
+#else
+ //NetworkInterface* network = NetworkInterface::get_default_instance();
+ WiFiInterface *network = WiFiInterface::get_default_instance();
+ if (!network) {
+ printf("ERROR: No WiFiInterface found.\n");
+ return NULL;
+ }
+ int count;
+ count = scan_demo(network);
+ if (count == 0) {
+ printf("No WIFI APNs found - can't continue further.\n");
+ return NULL;
+ }
+
+ //printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID);
+ //ret = network->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
+ //if (ret != 0) {
+ // printf("\nConnection error: %d\n", ret);
+ // return -1;
+ //}
+#endif
+
+ //nsapi_error_t connect_status = network->connect();
+ nsapi_error_t connect_status = network->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
+
+ if (connect_status != NSAPI_ERROR_OK) {
+ printf("[NWKH] Failed to connect to network (%d)\n", connect_status);
+ return NULL;
+ }
+
+ printf("[NWKH] Connected to the network\n");
+ printf("[NWKH] IP address: %s\n", network->get_ip_address());
+ return network;
+}
+
+#endif // _MBED_HTTP_EXAMPLE_H_