Ram Gandikota / Mbed OS ABCD
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 
00012 #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ESP8266
00013 #include "ESP8266Interface.h"
00014 
00015 #ifdef MBED_CONF_APP_ESP8266_DEBUG
00016 ESP8266Interface wifi(MBED_CONF_APP_ESP8266_TX, MBED_CONF_APP_ESP8266_RX, MBED_CONF_APP_ESP8266_DEBUG);
00017 #else
00018 ESP8266Interface wifi(MBED_CONF_APP_ESP8266_TX, MBED_CONF_APP_ESP8266_RX);
00019 #endif
00020 
00021 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ODIN
00022 #include "OdinWiFiInterface.h"
00023 OdinWiFiInterface wifi;
00024 #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
00025 #include "EthernetInterface.h"
00026 EthernetInterface eth;
00027 #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND
00028 #define MESH
00029 #include "NanostackInterface.h"
00030 LoWPANNDInterface mesh;
00031 #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD
00032 #define MESH
00033 #include "NanostackInterface.h"
00034 ThreadInterface mesh;
00035 #else
00036 #error "No connectivity method chosen. Please add 'config.network-interfaces.value' to your mbed_app.json (see README.md for more information)."
00037 #endif
00038 
00039 #if defined(MESH)
00040 #if MBED_CONF_APP_MESH_RADIO_TYPE == ATMEL
00041 #include "NanostackRfPhyAtmel.h"
00042 NanostackRfPhyAtmel rf_phy(ATMEL_SPI_MOSI, ATMEL_SPI_MISO, ATMEL_SPI_SCLK, ATMEL_SPI_CS,
00043                            ATMEL_SPI_RST, ATMEL_SPI_SLP, ATMEL_SPI_IRQ, ATMEL_I2C_SDA, ATMEL_I2C_SCL);
00044 #elif MBED_CONF_APP_MESH_RADIO_TYPE == MCR20
00045 #include "NanostackRfPhyMcr20a.h"
00046 NanostackRfPhyMcr20a rf_phy(MCR20A_SPI_MOSI, MCR20A_SPI_MISO, MCR20A_SPI_SCLK, MCR20A_SPI_CS, MCR20A_SPI_RST, MCR20A_SPI_IRQ);
00047 #endif //MBED_CONF_APP_RADIO_TYPE
00048 #endif //MESH
00049 
00050 #ifndef MESH
00051 // This is address to mbed Device Connector
00052 #define MBED_SERVER_ADDRESS "coap://api.connector.mbed.com:5684"
00053 #else
00054 // This is address to mbed Device Connector
00055 #define MBED_SERVER_ADDRESS "coaps://[2607:f0d0:2601:52::20]:5684"
00056 #endif
00057 
00058 #ifdef MBED_CONF_APP_ESP8266_SSID
00059 #define MBED_CONF_APP_WIFI_SSID MBED_CONF_APP_ESP8266_SSID
00060 #endif
00061 
00062 #ifdef MBED_CONF_APP_ESP8266_PASSWORD
00063 #define MBED_CONF_APP_WIFI_PASSWORD MBED_CONF_APP_ESP8266_PASSWORD
00064 #endif
00065 
00066 
00067 NetworkInterface* easy_connect(bool log_messages = false) {
00068     NetworkInterface* network_interface = 0;
00069     int connect_success = -1;
00070 #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ESP8266
00071     if (log_messages) {
00072         printf("[EasyConnect] Using WiFi (ESP8266) \n");
00073         printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID);
00074     }
00075     connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00076     network_interface = &wifi;
00077 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ODIN
00078     if (log_messages) {
00079         printf("[EasyConnect] Using WiFi (ODIN) \n");
00080         printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID);
00081     }
00082     connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00083     network_interface = &wifi;
00084 #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
00085     if (log_messages) {
00086         printf("[EasyConnect] Using Ethernet\n");
00087     }
00088     connect_success = eth.connect();
00089     network_interface = ð
00090 #endif
00091 #ifdef MESH
00092     if (log_messages) {
00093         printf("[EasyConnect] Using Mesh\n");
00094         printf("[EasyConnect] Connecting to Mesh..\n");
00095     }
00096     mesh.initialize(&rf_phy);
00097     connect_success = mesh.connect();
00098     network_interface = &mesh;
00099 #endif
00100     if(connect_success == 0) {
00101         if (log_messages) {
00102             printf("[EasyConnect] Connected to Network successfully\n");
00103         }
00104     } else {
00105         if (log_messages) {
00106             printf("[EasyConnect] Connection to Network Failed %d!\n", connect_success);
00107         }
00108         return NULL;
00109     }
00110     const char *ip_addr  = network_interface->get_ip_address();
00111     const char *mac_addr = network_interface->get_mac_address();
00112     if (ip_addr == NULL) {
00113         if (log_messages) {
00114             printf("[EasyConnect] ERROR - No IP address\n");
00115         }
00116         return NULL;
00117     }
00118     if (mac_addr == NULL) {
00119         if (log_messages) {
00120             printf("[EasyConnect] ERROR - No MAC address\n");
00121         }
00122         return NULL;
00123     }
00124     if (log_messages) {
00125         printf("[EasyConnect] IP address %s\n", ip_addr);
00126         printf("[EasyConnect] MAC address %s\n", mac_addr);
00127     }
00128     return network_interface;
00129 }
00130 
00131 #endif // __MAGIC_CONNECT_H__