Masa Kovacevic 2020/0229
Dependencies: 19E042PIM_MB_PINS
Diff: main.cpp
- Revision:
- 1:c994530bdb3d
- Parent:
- 0:6380a1c94d6c
- Child:
- 2:c7fcce20f023
--- a/main.cpp Thu Nov 25 18:58:24 2021 +0000 +++ b/main.cpp Sat Nov 27 20:33:49 2021 +0000 @@ -34,7 +34,7 @@ InterruptIn sw1(MB_SW1); // Right LED on the motherboard: DigitalOut led2(MB_LED2); -// WiFi network handler: +// Pointer to a WiFi network object: WiFiInterface *wifi; // Creating TCP socket: TCPSocket socket; @@ -49,7 +49,9 @@ int arrivedcount = 0; // Flag indicating that button is not pressed: int button_pressed=0; - +// HiveMQ broker connectivity information: +const char* hostname = "broker.hivemq.com"; +int port = 1883; // Returning a string for a provided network encryption: const char *sec2str(nsapi_security_t sec) { @@ -71,35 +73,35 @@ } } -int scan_demo(WiFiInterface *wifi) +int scan_networks(WiFiInterface *wifi) { - // Creating an access point - WiFiAccessPoint *ap; - printf("Scan:\n"); - - int count = wifi->scan(NULL,0); - + + // Scan only for the number of networks, first parameter is NULL: + int count = wifi->scan(NULL, 0); + // If there are no networks, count == 0, if there is an error, counter < 0: if (count <= 0) { printf("scan() failed with return value: %d\n", count); return 0; } - // Limit number of network arbitrary to 15 + // Limit number of network arbitrary to some reasonable number: count = count < MAX_NETWORKS ? count : MAX_NETWORKS; - // Create a new object, array of WiFi APs, and assign it to ap variable: - ap = new WiFiAccessPoint[count]; + // Create a local pointer to an object, which is an array of WiFi APs: + WiFiAccessPoint *ap = new WiFiAccessPoint[count]; + // Now scan again for 'count' networks and populate the array of APs: count = wifi->scan(ap, count); - + + // This time, the number of entries to 'ap' is returned: if (count <= 0) { printf("scan() failed with return value: %d\n", count); return 0; } - // Print network information: + // Print out the parameters of each AP: 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(), @@ -108,7 +110,9 @@ thread_sleep_for(PRINTF_DELAY_MS); } printf("%d networks available.\n", count); - + + // Since 'ap' is dynamically allocated pointer to the array of objects, it + // needs to be deleted: delete[] ap; return count; } @@ -129,45 +133,51 @@ int main() { - sw1.fall(&buttonFunction); - const char* hostname = "broker.hivemq.com"; - int port = 1883; - // Network interface + // Set the interrupt event: + sw1.fall(&buttonFunction); + // Create a default network interface: wifi = WiFiInterface::get_default_instance(); if (!wifi) { printf("ERROR: No WiFiInterface found.\n"); return -1; } - - int count = scan_demo(wifi); + + // Scan for available networks and aquire information about Access Points: + int count = scan_networks(wifi); if (count == 0) { printf("No WIFI APs found - can't continue further.\n"); return -1; } - + + // Connect to the network with the parameters specified in 'mbed_app.json': printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); int ret = wifi->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; } - + + // Print out the information aquired: printf("Success\n\n"); printf("MAC: %s\n", wifi->get_mac_address()); printf("IP: %s\n", wifi->get_ip_address()); printf("Netmask: %s\n", wifi->get_netmask()); printf("Gateway: %s\n", wifi->get_gateway()); printf("RSSI: %d\n\n", wifi->get_rssi()); - + + // Open TCP socket using WiFi network interface: socket.open(wifi); + // Connect to the HiveMQ broker: socket.connect(hostname, port); - + // Fill connect data with default values: MQTTPacket_connectData data = MQTTPacket_connectData_initializer; + // Change only ID and protocol version: data.MQTTVersion = 3; data.clientID.cstring = "NUCLEO-L476RG-60"; - int rc=0; + // Connect the + int rc = 0; if ((rc = client.connect(data)) != 0) printf("rc from MQTT connect is %d\r\n", rc);