![](/media/cache/group/logo_thTzkVQ.jpg.50x50_q85.jpg)
NuMaker mbed cloud v1.2 example
This example has been used on the following platforms
Follow these instructions to get credentials https://cloud.mbed.com/docs/v1.2/quick-start/connecting-your-device-to-mbed-cloud.html
Diff: main.cpp
- Revision:
- 11:96f41e7c9638
- Parent:
- 10:3cad9e255e41
--- a/main.cpp Tue Mar 20 16:20:59 2018 +0800 +++ b/main.cpp Tue Apr 17 20:57:11 2018 +0800 @@ -28,6 +28,9 @@ //#define SDBLOCK_SPI_MODE +#define ETHERNET 1 +#define WIFI 2 + // Placeholder to hardware that trigger events (timer, button, etc) Ticker timer; @@ -41,6 +44,14 @@ NuSDBlockDevice sd; #endif +#if MBED_CONF_APP_NETWORK_INTERFACE == WIFI +#include "ESP8266Interface.h" +ESP8266Interface esp(D1, D0); +#elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET +#include "EthernetInterface.h" +EthernetInterface eth; +#endif + FATFileSystem fs("sd"); // Pointers to the resources that will be created in main_application(). @@ -85,8 +96,8 @@ srand(time(NULL)); // Placeholder for network - EthernetInterface net; - + NetworkInterface *network_interface = 0; + printf("Start Simple Mbed Cloud Client\n"); // Initialize SD card @@ -109,19 +120,29 @@ } } - printf("Connecting to the network using Ethernet...\n"); + printf("Connecting to the network ...\n"); - status = net.connect(); +#if MBED_CONF_APP_NETWORK_INTERFACE == WIFI + printf("\n\rUsing WiFi \r\n"); + printf("\n\rConnecting to WiFi..\r\n"); + status = esp.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); + network_interface = &esp; +#elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET + printf("Using Ethernet\r\n"); + status = eth.connect(); + network_interface = ð +#endif + if (status) { printf("Connection to Network Failed %d!\n", status); return -1; } else { - const char *ip_addr = net.get_ip_address(); + const char *ip_addr = network_interface->get_ip_address(); printf("Connected successfully\n"); printf("IP address %s\n", ip_addr); } - SimpleMbedCloudClient mbedClient(&net); + SimpleMbedCloudClient mbedClient(network_interface); // Save pointer to mbedClient so that other functions can access it. client = &mbedClient; @@ -171,6 +192,11 @@ } } +#if MBED_CONF_APP_NETWORK_INTERFACE == WIFI + ((ESP8266Interface *)network_interface)->disconnect(); +#elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET + ((EthernetInterface *)network_interface)->disconnect(); +#endif // Client unregistered, exit program. return 0; }