A quick example of a simple WiFi application using the WiFi and network-socket APIs that is provided as a part of mbed-os.

Dependencies:   C12832

Fork of mbed-os-example-mbed5-wifi by mbed-os-examples

Revision:
56:bea82468efd3
Parent:
44:63be19b7a3db
--- a/main.cpp	Fri Feb 16 10:15:02 2018 +0000
+++ b/main.cpp	Fri Mar 09 11:30:49 2018 +0000
@@ -16,11 +16,12 @@
 
 #include "mbed.h"
 #include "TCPSocket.h"
+#include "C12832.h"
 
 #define WIFI_ESP8266    1
 #define WIFI_IDW0XX1    2
 
-#if TARGET_UBLOX_EVK_ODIN_W2
+#if 1
 #include "OdinWiFiInterface.h"
 OdinWiFiInterface wifi;
 
@@ -39,6 +40,16 @@
 #endif // MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1
 
 #endif
+Serial pc(USBTX, USBRX); // tx, rx
+
+C12832  lcd(PE_14, PE_12, PD_12, PD_11, PE_9);
+
+void lcd_print(const char* message)
+{
+    lcd.cls();
+    lcd.locate(0, 3);
+    pc.printf(message);
+}
 
 const char *sec2str(nsapi_security_t sec)
 {
@@ -63,7 +74,7 @@
 {
     WiFiAccessPoint *ap;
 
-    printf("Scan:\n");
+    pc.printf("Scan:\n");
 
     int count = wifi->scan(NULL,0);
 
@@ -72,13 +83,12 @@
 
     ap = new WiFiAccessPoint[count];
     count = wifi->scan(ap, count);
-    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());
+    for (int i = 0; i < count; i++) {
+        pc.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);
+    pc.printf("%d networks available.\n", count);
 
     delete[] ap;
     return count;
@@ -104,8 +114,7 @@
     char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n";
     nsapi_size_t size = strlen(sbuffer);
     response = 0;
-    while(size)
-    {
+    while(size) {
         response = socket.send(sbuffer+response, size);
         if (response < 0) {
             printf("Error sending data: %d\n", response);
@@ -135,22 +144,22 @@
 {
     int count = 0;
 
-    printf("WiFi example\n\n");
+    pc.printf("WiFi example\n\n");
 
     count = scan_demo(&wifi);
     if (count == 0) {
-        printf("No WIFI APNs found - can't continue further.\n");
+        pc.printf("No WIFI APNs found - can't continue further.\n");
         return -1;
     }
 
     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\n");
+        pc.printf("\nConnection error\n");
         return -1;
     }
 
-    printf("Success\n\n");
+    pc.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());