Added support for the WNC M14A2A Cellular LTE Data Module.

Dependencies:   WNC14A2AInterface

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers easy-connect.h Source File

easy-connect.h

00001 #ifndef __MAGIC_CONNECT_H__
00002 #define __MAGIC_CONNECT_H__
00003 
00004 #include "mbed.h"
00005 
00006 #define ETHERNET        1
00007 #define WIFI_ESP8266    2
00008 #define MESH_LOWPAN_ND  3
00009 #define MESH_THREAD     4
00010 #define WIFI_ODIN       5
00011 #define WNC14A2A        6
00012 
00013 #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ESP8266
00014 #include "ESP8266Interface.h"
00015 
00016 #ifdef MBED_CONF_APP_ESP8266_DEBUG
00017 ESP8266Interface wifi(MBED_CONF_APP_ESP8266_TX, MBED_CONF_APP_ESP8266_RX, MBED_CONF_APP_ESP8266_DEBUG);
00018 #else
00019 ESP8266Interface wifi(MBED_CONF_APP_ESP8266_TX, MBED_CONF_APP_ESP8266_RX);
00020 #endif
00021 
00022 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ODIN
00023 #include "OdinWiFiInterface.h"
00024 OdinWiFiInterface wifi;
00025 
00026 #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
00027 #include "EthernetInterface.h"
00028 EthernetInterface eth;
00029 
00030 #elif MBED_CONF_APP_NETWORK_INTERFACE == WNC14A2A
00031 #include "WNC14A2AInterface.h"
00032 WNC14A2AInterface *wnc;
00033 
00034 #if MBED_CONF_APP_WNC_DEBUG == true
00035 extern BufferedSerial pc;
00036 #define Serial BufferedSerial
00037 #endif
00038 
00039 #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND
00040 #define MESH
00041 #include "NanostackInterface.h"
00042 LoWPANNDInterface mesh;
00043 #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD
00044 #define MESH
00045 #include "NanostackInterface.h"
00046 ThreadInterface mesh;
00047 #else
00048 #error "No connectivity method chosen. Please add 'config.network-interfaces.value' to your mbed_app.json (see README.md for more information)."
00049 #endif
00050 
00051 #if defined(MESH)
00052 #if MBED_CONF_APP_MESH_RADIO_TYPE == ATMEL
00053 #include "NanostackRfPhyAtmel.h"
00054 NanostackRfPhyAtmel rf_phy(ATMEL_SPI_MOSI, ATMEL_SPI_MISO, ATMEL_SPI_SCLK, ATMEL_SPI_CS,
00055                            ATMEL_SPI_RST, ATMEL_SPI_SLP, ATMEL_SPI_IRQ, ATMEL_I2C_SDA, ATMEL_I2C_SCL);
00056 #elif MBED_CONF_APP_MESH_RADIO_TYPE == MCR20
00057 #include "NanostackRfPhyMcr20a.h"
00058 NanostackRfPhyMcr20a rf_phy(MCR20A_SPI_MOSI, MCR20A_SPI_MISO, MCR20A_SPI_SCLK, MCR20A_SPI_CS, MCR20A_SPI_RST, MCR20A_SPI_IRQ);
00059 #endif //MBED_CONF_APP_RADIO_TYPE
00060 #endif //MESH
00061 
00062 #ifndef MESH
00063 // This is address to mbed Device Connector
00064 #define MBED_SERVER_ADDRESS "coap://api.connector.mbed.com:5684"
00065 #else
00066 // This is address to mbed Device Connector
00067 #define MBED_SERVER_ADDRESS "coaps://[2607:f0d0:2601:52::20]:5684"
00068 #endif
00069 
00070 #ifdef MBED_CONF_APP_ESP8266_SSID
00071 #define MBED_CONF_APP_WIFI_SSID MBED_CONF_APP_ESP8266_SSID
00072 #endif
00073 
00074 #ifdef MBED_CONF_APP_ESP8266_PASSWORD
00075 #define MBED_CONF_APP_WIFI_PASSWORD MBED_CONF_APP_ESP8266_PASSWORD
00076 #endif
00077 
00078 
00079 NetworkInterface* easy_connect(bool log_messages = false) {
00080     NetworkInterface* network_interface = 0;
00081     int connect_success = -1;
00082 #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ESP8266
00083     if (log_messages) {
00084         printf("[EasyConnect] Using WiFi (ESP8266) \n");
00085         printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID);
00086     }
00087     connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00088     network_interface = &wifi;
00089 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ODIN
00090     if (log_messages) {
00091         printf("[EasyConnect] Using WiFi (ODIN) \n");
00092         printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID);
00093     }
00094     connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00095     network_interface = &wifi;
00096 
00097 #elif MBED_CONF_APP_NETWORK_INTERFACE == WNC14A2A
00098     if (log_messages) {
00099         printf("[EasyConnect] Using WNC14A2A\n");
00100 #if MBED_CONF_APP_WNC_DEBUG == true
00101         printf("[EasyConnect] with debug output\n");
00102         printf("[WNC Driver ] debug = %d\n",MBED_CONF_APP_WNC_DEBUG_SETTING);
00103         wnc = new WNC14A2AInterface(&pc);
00104         wnc->doDebug(MBED_CONF_APP_WNC_DEBUG_SETTING);
00105 #else
00106         wnc = new WNC14A2AInterface;
00107 #endif
00108     }
00109     connect_success = wnc->connect();
00110     network_interface = wnc;
00111 #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
00112     if (log_messages) {
00113         printf("[EasyConnect] Using Ethernet\n");
00114     }
00115     connect_success = eth.connect();
00116     network_interface = ð
00117 #endif
00118 #ifdef MESH
00119     if (log_messages) {
00120         printf("[EasyConnect] Using Mesh\n");
00121         printf("[EasyConnect] Connecting to Mesh..\n");
00122     }
00123     mesh.initialize(&rf_phy);
00124     connect_success = mesh.connect();
00125     network_interface = &mesh;
00126 #endif
00127     if(connect_success == 0) {
00128         if (log_messages) {
00129             printf("[EasyConnect] Connected to Network successfully\n");
00130         }
00131     } else {
00132         if (log_messages) {
00133             printf("[EasyConnect] Connection to Network Failed %d!\n", connect_success);
00134         }
00135         return NULL;
00136     }
00137     const char *ip_addr  = network_interface->get_ip_address();
00138     const char *mac_addr = network_interface->get_mac_address();
00139     if (ip_addr == NULL) {
00140         if (log_messages) {
00141             printf("[EasyConnect] ERROR - No IP address\n");
00142         }
00143         return NULL;
00144     }
00145     if (mac_addr == NULL) {
00146         if (log_messages) {
00147             printf("[EasyConnect] ERROR - No MAC address\n");
00148         }
00149         return NULL;
00150     }
00151     if (log_messages) {
00152         printf("[EasyConnect] IP address %s\n", ip_addr);
00153         printf("[EasyConnect] MAC address %s\n", mac_addr);
00154     }
00155     return network_interface;
00156 }
00157 
00158 #endif // __MAGIC_CONNECT_H__