version 1.6

Dependents:   iot_water_monitor_v2

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_RTW          6
00012 #define CELLULAR_ONBOARD  7
00013 #define WIFI_IDW0XX1      8
00014 
00015 #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ESP8266
00016 #include "ESP8266Interface.h"
00017 
00018 #ifdef MBED_CONF_APP_ESP8266_DEBUG
00019 ESP8266Interface wifi(MBED_CONF_APP_ESP8266_TX, MBED_CONF_APP_ESP8266_RX, MBED_CONF_APP_ESP8266_DEBUG);
00020 #else
00021 ESP8266Interface wifi(MBED_CONF_APP_ESP8266_TX, MBED_CONF_APP_ESP8266_RX);
00022 #endif
00023 
00024 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ODIN
00025 #include "OdinWiFiInterface.h"
00026 
00027 OdinWiFiInterface wifi;
00028 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_RTW
00029 #include "RTWInterface.h"
00030 RTWInterface wifi;
00031 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_IDW0XX1
00032 #include "SpwfSAInterface.h"
00033 SpwfSAInterface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
00034 #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
00035 #include "EthernetInterface.h"
00036 EthernetInterface eth;
00037 #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND
00038 #define MESH
00039 #include "NanostackInterface.h"
00040 LoWPANNDInterface mesh;
00041 #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD
00042 #define MESH
00043 #include "NanostackInterface.h"
00044 ThreadInterface mesh;
00045 #elif MBED_CONF_APP_NETWORK_INTERFACE == CELLULAR_ONBOARD
00046 #include "OnboardCellularInterface.h"
00047 OnboardCellularInterface cellular;
00048 #else
00049 #error "No connectivity method chosen. Please add 'config.network-interfaces.value' to your mbed_app.json (see README.md for more information)."
00050 #endif
00051 
00052 #if defined(MESH)
00053 
00054 // Define macros for radio type
00055 #define ATMEL   1
00056 #define MCR20   2
00057 #define SPIRIT1 3
00058 #define EFR32   4
00059 
00060 #if MBED_CONF_APP_MESH_RADIO_TYPE == ATMEL
00061 #include "NanostackRfPhyAtmel.h"
00062 NanostackRfPhyAtmel rf_phy(ATMEL_SPI_MOSI, ATMEL_SPI_MISO, ATMEL_SPI_SCLK, ATMEL_SPI_CS,
00063                            ATMEL_SPI_RST, ATMEL_SPI_SLP, ATMEL_SPI_IRQ, ATMEL_I2C_SDA, ATMEL_I2C_SCL);
00064 #elif MBED_CONF_APP_MESH_RADIO_TYPE == MCR20
00065 #include "NanostackRfPhyMcr20a.h"
00066 NanostackRfPhyMcr20a rf_phy(MCR20A_SPI_MOSI, MCR20A_SPI_MISO, MCR20A_SPI_SCLK, MCR20A_SPI_CS, MCR20A_SPI_RST, MCR20A_SPI_IRQ);
00067 #elif MBED_CONF_APP_MESH_RADIO_TYPE == SPIRIT1
00068 #include "NanostackRfPhySpirit1.h"
00069 NanostackRfPhySpirit1 rf_phy(SPIRIT1_SPI_MOSI, SPIRIT1_SPI_MISO, SPIRIT1_SPI_SCLK,
00070                  SPIRIT1_DEV_IRQ, SPIRIT1_DEV_CS, SPIRIT1_DEV_SDN, SPIRIT1_BRD_LED);
00071 #elif MBED_CONF_APP_MESH_RADIO_TYPE == EFR32
00072 #include "NanostackRfPhyEfr32.h"
00073 NanostackRfPhyEfr32 rf_phy;
00074 #endif //MBED_CONF_APP_RADIO_TYPE
00075 #endif //MESH
00076 
00077 #ifndef MESH
00078 // This is address to mbed Device Connector
00079 #define MBED_SERVER_ADDRESS "coap://api.connector.mbed.com:5684"
00080 #else
00081 // This is address to mbed Device Connector
00082 #define MBED_SERVER_ADDRESS "coaps://[2607:f0d0:2601:52::20]:5684"
00083 #endif
00084 
00085 #ifdef MBED_CONF_APP_ESP8266_SSID
00086 #define MBED_CONF_APP_WIFI_SSID MBED_CONF_APP_ESP8266_SSID
00087 #endif
00088 
00089 #ifdef MBED_CONF_APP_ESP8266_PASSWORD
00090 #define MBED_CONF_APP_WIFI_PASSWORD MBED_CONF_APP_ESP8266_PASSWORD
00091 #endif
00092 
00093 /* \brief print_MAC - print_MAC  - helper function to print out MAC address
00094  * in: network_interface - pointer to network i/f
00095  *     bool log-messages   print out logs or not
00096  * MAC address is print, if it can be acquired & log_messages is true.
00097  *
00098  */
00099 void print_MAC(NetworkInterface* network_interface, bool log_messages) {
00100 #if MBED_CONF_APP_NETWORK_INTERFACE != CELLULAR_ONBOARD
00101     const char *mac_addr = network_interface->get_mac_address();
00102     if (mac_addr == NULL) {
00103         if (log_messages) {
00104             printf("[EasyConnect] ERROR - No MAC address\n");
00105         }
00106         return;
00107     }
00108     if (log_messages) {
00109         printf("[EasyConnect] MAC address %s\n", mac_addr);
00110     }
00111 #endif
00112 }
00113 
00114 
00115 /* \brief easy_connect - easy_connect function to connect the pre-defined network bearer,
00116  *                       config done via mbed_app.json (see README.md for details).
00117  * IN: bool log_messages  print out diagnostics or not.
00118  *
00119  */
00120 NetworkInterface* easy_connect(bool log_messages = false) {
00121     NetworkInterface* network_interface = 0;
00122     int connect_success = -1;
00123     /// This should be removed once mbedOS supports proper dual-stack
00124 #if defined (MESH) || (MBED_CONF_LWIP_IPV6_ENABLED==true)
00125     printf("[EasyConnect] IPv6 mode\n");
00126 #else
00127     printf("[EasyConnect] IPv4 mode\n");
00128 #endif
00129 
00130 #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ESP8266
00131     if (log_messages) {
00132         printf("[EasyConnect] Using WiFi (ESP8266) \n");
00133         printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID);
00134     }
00135     network_interface = &wifi;
00136     connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00137 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ODIN
00138     if (log_messages) {
00139         printf("[EasyConnect] Using WiFi (ODIN) \n");
00140         printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID);
00141     }
00142     network_interface = &wifi;
00143     connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00144 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_RTW
00145     if (log_messages) {
00146         printf("[EasyConnect] Using WiFi (RTW)\n");
00147         printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID);
00148     }
00149     network_interface = &wifi;
00150     connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00151 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_IDW0XX1
00152     if (log_messages) {
00153         printf("[EasyConnect] Using WiFi (X-NUCLEO-IDW0XX1)\n");
00154         printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID);
00155     }
00156     network_interface = &wifi;
00157     connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00158 #elif MBED_CONF_APP_NETWORK_INTERFACE == CELLULAR_ONBOARD
00159 #  ifdef MBED_CONF_APP_CELLULAR_SIM_PIN
00160     cellular.set_sim_pin(MBED_CONF_APP_CELLULAR_SIM_PIN);
00161 #  endif
00162 #  ifdef MBED_CONF_APP_CELLULAR_APN
00163 #    ifndef MBED_CONF_APP_CELLULAR_USERNAME
00164 #      define MBED_CONF_APP_CELLULAR_USERNAME 0
00165 #    endif
00166 #    ifndef MBED_CONF_APP_CELLULAR_PASSWORD
00167 #      define MBED_CONF_APP_CELLULAR_PASSWORD 0
00168 #    endif
00169     cellular.set_credentials(MBED_CONF_APP_CELLULAR_APN, MBED_CONF_APP_CELLULAR_USERNAME, MBED_CONF_APP_CELLULAR_PASSWORD);
00170     if (log_messages) {
00171         printf("[EasyConnect] Connecting using Cellular interface and APN %s\n", MBED_CONF_APP_CELLULAR_APN);
00172     }
00173 #  else
00174     if (log_messages) {
00175         printf("[EasyConnect] Connecting using Cellular interface and default APN\n");
00176     }
00177 #  endif
00178     connect_success = cellular.connect();
00179     network_interface = &cellular;
00180 #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
00181     if (log_messages) {
00182         printf("[EasyConnect] Using Ethernet\n");
00183     }
00184     network_interface = ð
00185     connect_success = eth.connect();
00186 #endif
00187 
00188 #ifdef MESH
00189     if (log_messages) {
00190         printf("[EasyConnect] Using Mesh\n");
00191         printf("[EasyConnect] Connecting to Mesh..\n");
00192     }
00193     network_interface = &mesh;
00194     mesh.initialize(&rf_phy);
00195     connect_success = mesh.connect();
00196 #endif
00197     if(connect_success == 0) {
00198         if (log_messages) {
00199             printf("[EasyConnect] Connected to Network successfully\n");
00200             print_MAC(network_interface, log_messages);
00201         }
00202     } else {
00203         if (log_messages) {
00204             print_MAC(network_interface, log_messages);
00205             printf("[EasyConnect] Connection to Network Failed %d!\n", connect_success);
00206         }
00207         return NULL;
00208     }
00209     const char *ip_addr  = network_interface->get_ip_address();
00210     if (ip_addr == NULL) {
00211         if (log_messages) {
00212             printf("[EasyConnect] ERROR - No IP address\n");
00213         }
00214         return NULL;
00215     }
00216 
00217     if (log_messages) {
00218         printf("[EasyConnect] IP address %s\n", ip_addr);
00219     }
00220     return network_interface;
00221 }
00222 
00223 #endif // __EASY_CONNECT_H__