http

Embed: (wiki syntax)

« Back to documentation index

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

easy-connect.h

00001 #ifndef __EASY_CONNECT_H__
00002 #define __EASY_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 WIFI_REALTEK    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 
00025 OdinWiFiInterface wifi;
00026 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_RTW
00027 #include "RTWInterface.h"
00028 RTWInterface wifi;
00029 #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
00030 #include "EthernetInterface.h"
00031 EthernetInterface eth;
00032 #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND
00033 #define MESH
00034 #include "NanostackInterface.h"
00035 LoWPANNDInterface mesh;
00036 #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD
00037 #define MESH
00038 #include "NanostackInterface.h"
00039 ThreadInterface mesh;
00040 #else
00041 #error "No connectivity method chosen. Please add 'config.network-interfaces.value' to your mbed_app.json (see README.md for more information)."
00042 #endif
00043 
00044 #if defined(MESH)
00045 #if MBED_CONF_APP_MESH_RADIO_TYPE == ATMEL
00046 #include "NanostackRfPhyAtmel.h"
00047 NanostackRfPhyAtmel rf_phy(ATMEL_SPI_MOSI, ATMEL_SPI_MISO, ATMEL_SPI_SCLK, ATMEL_SPI_CS,
00048                            ATMEL_SPI_RST, ATMEL_SPI_SLP, ATMEL_SPI_IRQ, ATMEL_I2C_SDA, ATMEL_I2C_SCL);
00049 #elif MBED_CONF_APP_MESH_RADIO_TYPE == MCR20
00050 #include "NanostackRfPhyMcr20a.h"
00051 NanostackRfPhyMcr20a rf_phy(MCR20A_SPI_MOSI, MCR20A_SPI_MISO, MCR20A_SPI_SCLK, MCR20A_SPI_CS, MCR20A_SPI_RST, MCR20A_SPI_IRQ);
00052 #elif MBED_CONF_APP_MESH_RADIO_TYPE == SPIRIT1
00053 #include "NanostackRfPhySpirit1.h"
00054 NanostackRfPhySpirit1 rf_phy(SPIRIT1_SPI_MOSI, SPIRIT1_SPI_MISO, SPIRIT1_SPI_SCLK,
00055                  SPIRIT1_DEV_IRQ, SPIRIT1_DEV_CS, SPIRIT1_DEV_SDN, SPIRIT1_BRD_LED);
00056 #elif MBED_CONF_APP_MESH_RADIO_TYPE == EFR32
00057 #include "NanostackRfPhyEfr32.h"
00058 NanostackRfPhyEfr32 rf_phy;
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 /* \brief print_MAC - print_MAC  - helper function to print out MAC address
00079  * in: network_interface - pointer to network i/f
00080  *     bool log-messages   print out logs or not
00081  * MAC address is print, if it can be acquired & log_messages is true.
00082  *
00083  */
00084 void print_MAC(NetworkInterface* network_interface, bool log_messages) {
00085     const char *mac_addr = network_interface->get_mac_address();
00086     if (mac_addr == NULL) {
00087         if (log_messages) {
00088             printf("[EasyConnect] ERROR - No MAC address\n");
00089         }
00090         return;
00091     }
00092     if (log_messages) {
00093         printf("[EasyConnect] MAC address %s\n", mac_addr);
00094     }
00095 }
00096 
00097 
00098 /* \brief easy_connect - easy_connect function to connect the pre-defined network bearer,
00099  *                       config done via mbed_app.json (see README.md for details).
00100  * IN: bool log_messages  print out diagnostics or not.
00101  *
00102  */
00103 NetworkInterface* easy_connect(bool log_messages = false) {
00104     NetworkInterface* network_interface = 0;
00105     int connect_success = -1;
00106     /// This should be removed once mbedOS supports proper dual-stack
00107 #if defined (MESH) || (MBED_CONF_LWIP_IPV6_ENABLED==true)
00108     printf("[EasyConnect] IPv6 mode\n");
00109 #else
00110     printf("[EasyConnect] IPv4 mode\n");
00111 #endif
00112 
00113  #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ESP8266
00114     if (log_messages) {
00115         printf("[EasyConnect] Using WiFi (ESP8266) \n");
00116         printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID);
00117     }
00118     network_interface = &wifi;
00119     connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00120 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ODIN
00121     if (log_messages) {
00122         printf("[EasyConnect] Using WiFi (ODIN) \n");
00123         printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID);
00124     }
00125     network_interface = &wifi;
00126     connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00127 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_RTW
00128     if (log_messages) {
00129         printf("[EasyConnect] Using WiFi (RTW)\n");
00130         printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID);
00131     }
00132     network_interface = &wifi;
00133     connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00134 #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
00135     if (log_messages) {
00136         printf("[EasyConnect] Using Ethernet\n");
00137     }
00138     network_interface = ð
00139     connect_success = eth.connect();
00140 #endif
00141 
00142 #ifdef MESH
00143     if (log_messages) {
00144         printf("[EasyConnect] Using Mesh\n");
00145         printf("[EasyConnect] Connecting to Mesh..\n");
00146     }
00147     network_interface = &mesh;
00148     mesh.initialize(&rf_phy);
00149     connect_success = mesh.connect();
00150 #endif
00151     if(connect_success == 0) {
00152         if (log_messages) {
00153             printf("[EasyConnect] Connected to Network successfully\n");
00154             print_MAC(network_interface, log_messages);
00155         }
00156     } else {
00157         if (log_messages) {
00158             print_MAC(network_interface, log_messages);
00159             printf("[EasyConnect] Connection to Network Failed %d!\n", connect_success);
00160         }
00161         return NULL;
00162     }
00163     const char *ip_addr  = network_interface->get_ip_address();
00164     if (ip_addr == NULL) {
00165         if (log_messages) {
00166             printf("[EasyConnect] ERROR - No IP address\n");
00167         }
00168         return NULL;
00169     }
00170     if (log_messages) {
00171         printf("[EasyConnect] IP address %s\n", ip_addr);
00172     }
00173     return network_interface;
00174 }
00175 
00176 #endif // __EASY_CONNECT_H__