Fork of my original MQTTGateway

Dependencies:   mbed-http

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 #elif MBED_CONF_APP_MESH_RADIO_TYPE == SPIRIT1
00048 #include "NanostackRfPhySpirit1.h"
00049 NanostackRfPhySpirit1 rf_phy(SPIRIT1_SPI_MOSI, SPIRIT1_SPI_MISO, SPIRIT1_SPI_SCLK,
00050                  SPIRIT1_DEV_IRQ, SPIRIT1_DEV_CS, SPIRIT1_DEV_SDN, SPIRIT1_BRD_LED);
00051 #endif //MBED_CONF_APP_RADIO_TYPE
00052 #endif //MESH
00053 
00054 #ifndef MESH
00055 // This is address to mbed Device Connector
00056 #define MBED_SERVER_ADDRESS "coap://api.connector.mbed.com:5684"
00057 #else
00058 // This is address to mbed Device Connector
00059 #define MBED_SERVER_ADDRESS "coaps://[2607:f0d0:2601:52::20]:5684"
00060 #endif
00061 
00062 #ifdef MBED_CONF_APP_ESP8266_SSID
00063 #define MBED_CONF_APP_WIFI_SSID MBED_CONF_APP_ESP8266_SSID
00064 #endif
00065 
00066 #ifdef MBED_CONF_APP_ESP8266_PASSWORD
00067 #define MBED_CONF_APP_WIFI_PASSWORD MBED_CONF_APP_ESP8266_PASSWORD
00068 #endif
00069 
00070 
00071 NetworkInterface* easy_connect(bool log_messages = false) {
00072     NetworkInterface* network_interface = 0;
00073     int connect_success = -1;
00074 #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ESP8266
00075     if (log_messages) {
00076         printf("[EasyConnect] Using WiFi (ESP8266) \n");
00077         printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID);
00078     }
00079     connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00080     network_interface = &wifi;
00081 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ODIN
00082     if (log_messages) {
00083         printf("[EasyConnect] Using WiFi (ODIN) \n");
00084         printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID);
00085     }
00086     connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00087     network_interface = &wifi;
00088 #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
00089     if (log_messages) {
00090         printf("[EasyConnect] Using Ethernet\n");
00091     }
00092     connect_success = eth.connect();
00093     network_interface = ð
00094 #endif
00095 #ifdef MESH
00096     if (log_messages) {
00097         printf("[EasyConnect] Using Mesh\n");
00098         printf("[EasyConnect] Connecting to Mesh..\n");
00099     }
00100     mesh.initialize(&rf_phy);
00101     connect_success = mesh.connect();
00102     network_interface = &mesh;
00103 #endif
00104     if(connect_success == 0) {
00105         if (log_messages) {
00106             printf("[EasyConnect] Connected to Network successfully\n");
00107         }
00108     } else {
00109         if (log_messages) {
00110             printf("[EasyConnect] Connection to Network Failed %d!\n", connect_success);
00111         }
00112         return NULL;
00113     }
00114     const char *ip_addr  = network_interface->get_ip_address();
00115     const char *mac_addr = network_interface->get_mac_address();
00116     if (ip_addr == NULL) {
00117         if (log_messages) {
00118             printf("[EasyConnect] ERROR - No IP address\n");
00119         }
00120         return NULL;
00121     }
00122     if (mac_addr == NULL) {
00123         if (log_messages) {
00124             printf("[EasyConnect] ERROR - No MAC address\n");
00125         }
00126         return NULL;
00127     }
00128     if (log_messages) {
00129         printf("[EasyConnect] IP address %s\n", ip_addr);
00130         printf("[EasyConnect] MAC address %s\n", mac_addr);
00131     }
00132     return network_interface;
00133 }
00134 
00135 #endif // __MAGIC_CONNECT_H__