Vergil Cola
/
MQTTGatewayK64
Fork of my MQTTGateway
easy-connect/easy-connect.h@0:f1d3878b8dd9, 2017-04-08 (annotated)
- Committer:
- vpcola
- Date:
- Sat Apr 08 14:45:51 2017 +0000
- Revision:
- 0:f1d3878b8dd9
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vpcola | 0:f1d3878b8dd9 | 1 | #ifndef __MAGIC_CONNECT_H__ |
vpcola | 0:f1d3878b8dd9 | 2 | #define __MAGIC_CONNECT_H__ |
vpcola | 0:f1d3878b8dd9 | 3 | |
vpcola | 0:f1d3878b8dd9 | 4 | #include "mbed.h" |
vpcola | 0:f1d3878b8dd9 | 5 | |
vpcola | 0:f1d3878b8dd9 | 6 | #define ETHERNET 1 |
vpcola | 0:f1d3878b8dd9 | 7 | #define WIFI_ESP8266 2 |
vpcola | 0:f1d3878b8dd9 | 8 | #define MESH_LOWPAN_ND 3 |
vpcola | 0:f1d3878b8dd9 | 9 | #define MESH_THREAD 4 |
vpcola | 0:f1d3878b8dd9 | 10 | #define WIFI_ODIN 5 |
vpcola | 0:f1d3878b8dd9 | 11 | |
vpcola | 0:f1d3878b8dd9 | 12 | #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ESP8266 |
vpcola | 0:f1d3878b8dd9 | 13 | #include "ESP8266Interface.h" |
vpcola | 0:f1d3878b8dd9 | 14 | |
vpcola | 0:f1d3878b8dd9 | 15 | #ifdef MBED_CONF_APP_ESP8266_DEBUG |
vpcola | 0:f1d3878b8dd9 | 16 | ESP8266Interface wifi(MBED_CONF_APP_ESP8266_TX, MBED_CONF_APP_ESP8266_RX, MBED_CONF_APP_ESP8266_DEBUG); |
vpcola | 0:f1d3878b8dd9 | 17 | #else |
vpcola | 0:f1d3878b8dd9 | 18 | ESP8266Interface wifi(MBED_CONF_APP_ESP8266_TX, MBED_CONF_APP_ESP8266_RX); |
vpcola | 0:f1d3878b8dd9 | 19 | #endif |
vpcola | 0:f1d3878b8dd9 | 20 | |
vpcola | 0:f1d3878b8dd9 | 21 | #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ODIN |
vpcola | 0:f1d3878b8dd9 | 22 | #include "OdinWiFiInterface.h" |
vpcola | 0:f1d3878b8dd9 | 23 | OdinWiFiInterface wifi; |
vpcola | 0:f1d3878b8dd9 | 24 | #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET |
vpcola | 0:f1d3878b8dd9 | 25 | #include "EthernetInterface.h" |
vpcola | 0:f1d3878b8dd9 | 26 | EthernetInterface eth; |
vpcola | 0:f1d3878b8dd9 | 27 | #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND |
vpcola | 0:f1d3878b8dd9 | 28 | #define MESH |
vpcola | 0:f1d3878b8dd9 | 29 | #include "NanostackInterface.h" |
vpcola | 0:f1d3878b8dd9 | 30 | LoWPANNDInterface mesh; |
vpcola | 0:f1d3878b8dd9 | 31 | #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD |
vpcola | 0:f1d3878b8dd9 | 32 | #define MESH |
vpcola | 0:f1d3878b8dd9 | 33 | #include "NanostackInterface.h" |
vpcola | 0:f1d3878b8dd9 | 34 | ThreadInterface mesh; |
vpcola | 0:f1d3878b8dd9 | 35 | #else |
vpcola | 0:f1d3878b8dd9 | 36 | #error "No connectivity method chosen. Please add 'config.network-interfaces.value' to your mbed_app.json (see README.md for more information)." |
vpcola | 0:f1d3878b8dd9 | 37 | #endif |
vpcola | 0:f1d3878b8dd9 | 38 | |
vpcola | 0:f1d3878b8dd9 | 39 | #if defined(MESH) |
vpcola | 0:f1d3878b8dd9 | 40 | #if MBED_CONF_APP_MESH_RADIO_TYPE == ATMEL |
vpcola | 0:f1d3878b8dd9 | 41 | #include "NanostackRfPhyAtmel.h" |
vpcola | 0:f1d3878b8dd9 | 42 | NanostackRfPhyAtmel rf_phy(ATMEL_SPI_MOSI, ATMEL_SPI_MISO, ATMEL_SPI_SCLK, ATMEL_SPI_CS, |
vpcola | 0:f1d3878b8dd9 | 43 | ATMEL_SPI_RST, ATMEL_SPI_SLP, ATMEL_SPI_IRQ, ATMEL_I2C_SDA, ATMEL_I2C_SCL); |
vpcola | 0:f1d3878b8dd9 | 44 | #elif MBED_CONF_APP_MESH_RADIO_TYPE == MCR20 |
vpcola | 0:f1d3878b8dd9 | 45 | #include "NanostackRfPhyMcr20a.h" |
vpcola | 0:f1d3878b8dd9 | 46 | NanostackRfPhyMcr20a rf_phy(MCR20A_SPI_MOSI, MCR20A_SPI_MISO, MCR20A_SPI_SCLK, MCR20A_SPI_CS, MCR20A_SPI_RST, MCR20A_SPI_IRQ); |
vpcola | 0:f1d3878b8dd9 | 47 | #elif MBED_CONF_APP_MESH_RADIO_TYPE == SPIRIT1 |
vpcola | 0:f1d3878b8dd9 | 48 | #include "NanostackRfPhySpirit1.h" |
vpcola | 0:f1d3878b8dd9 | 49 | NanostackRfPhySpirit1 rf_phy(SPIRIT1_SPI_MOSI, SPIRIT1_SPI_MISO, SPIRIT1_SPI_SCLK, |
vpcola | 0:f1d3878b8dd9 | 50 | SPIRIT1_DEV_IRQ, SPIRIT1_DEV_CS, SPIRIT1_DEV_SDN, SPIRIT1_BRD_LED); |
vpcola | 0:f1d3878b8dd9 | 51 | #endif //MBED_CONF_APP_RADIO_TYPE |
vpcola | 0:f1d3878b8dd9 | 52 | #endif //MESH |
vpcola | 0:f1d3878b8dd9 | 53 | |
vpcola | 0:f1d3878b8dd9 | 54 | #ifndef MESH |
vpcola | 0:f1d3878b8dd9 | 55 | // This is address to mbed Device Connector |
vpcola | 0:f1d3878b8dd9 | 56 | #define MBED_SERVER_ADDRESS "coap://api.connector.mbed.com:5684" |
vpcola | 0:f1d3878b8dd9 | 57 | #else |
vpcola | 0:f1d3878b8dd9 | 58 | // This is address to mbed Device Connector |
vpcola | 0:f1d3878b8dd9 | 59 | #define MBED_SERVER_ADDRESS "coaps://[2607:f0d0:2601:52::20]:5684" |
vpcola | 0:f1d3878b8dd9 | 60 | #endif |
vpcola | 0:f1d3878b8dd9 | 61 | |
vpcola | 0:f1d3878b8dd9 | 62 | #ifdef MBED_CONF_APP_ESP8266_SSID |
vpcola | 0:f1d3878b8dd9 | 63 | #define MBED_CONF_APP_WIFI_SSID MBED_CONF_APP_ESP8266_SSID |
vpcola | 0:f1d3878b8dd9 | 64 | #endif |
vpcola | 0:f1d3878b8dd9 | 65 | |
vpcola | 0:f1d3878b8dd9 | 66 | #ifdef MBED_CONF_APP_ESP8266_PASSWORD |
vpcola | 0:f1d3878b8dd9 | 67 | #define MBED_CONF_APP_WIFI_PASSWORD MBED_CONF_APP_ESP8266_PASSWORD |
vpcola | 0:f1d3878b8dd9 | 68 | #endif |
vpcola | 0:f1d3878b8dd9 | 69 | |
vpcola | 0:f1d3878b8dd9 | 70 | |
vpcola | 0:f1d3878b8dd9 | 71 | NetworkInterface* easy_connect(bool log_messages = false) { |
vpcola | 0:f1d3878b8dd9 | 72 | NetworkInterface* network_interface = 0; |
vpcola | 0:f1d3878b8dd9 | 73 | int connect_success = -1; |
vpcola | 0:f1d3878b8dd9 | 74 | #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ESP8266 |
vpcola | 0:f1d3878b8dd9 | 75 | if (log_messages) { |
vpcola | 0:f1d3878b8dd9 | 76 | printf("[EasyConnect] Using WiFi (ESP8266) \n"); |
vpcola | 0:f1d3878b8dd9 | 77 | printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID); |
vpcola | 0:f1d3878b8dd9 | 78 | } |
vpcola | 0:f1d3878b8dd9 | 79 | connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
vpcola | 0:f1d3878b8dd9 | 80 | network_interface = &wifi; |
vpcola | 0:f1d3878b8dd9 | 81 | #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ODIN |
vpcola | 0:f1d3878b8dd9 | 82 | if (log_messages) { |
vpcola | 0:f1d3878b8dd9 | 83 | printf("[EasyConnect] Using WiFi (ODIN) \n"); |
vpcola | 0:f1d3878b8dd9 | 84 | printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID); |
vpcola | 0:f1d3878b8dd9 | 85 | } |
vpcola | 0:f1d3878b8dd9 | 86 | connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
vpcola | 0:f1d3878b8dd9 | 87 | network_interface = &wifi; |
vpcola | 0:f1d3878b8dd9 | 88 | #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET |
vpcola | 0:f1d3878b8dd9 | 89 | if (log_messages) { |
vpcola | 0:f1d3878b8dd9 | 90 | printf("[EasyConnect] Using Ethernet\n"); |
vpcola | 0:f1d3878b8dd9 | 91 | } |
vpcola | 0:f1d3878b8dd9 | 92 | connect_success = eth.connect(); |
vpcola | 0:f1d3878b8dd9 | 93 | network_interface = ð |
vpcola | 0:f1d3878b8dd9 | 94 | #endif |
vpcola | 0:f1d3878b8dd9 | 95 | #ifdef MESH |
vpcola | 0:f1d3878b8dd9 | 96 | if (log_messages) { |
vpcola | 0:f1d3878b8dd9 | 97 | printf("[EasyConnect] Using Mesh\n"); |
vpcola | 0:f1d3878b8dd9 | 98 | printf("[EasyConnect] Connecting to Mesh..\n"); |
vpcola | 0:f1d3878b8dd9 | 99 | } |
vpcola | 0:f1d3878b8dd9 | 100 | mesh.initialize(&rf_phy); |
vpcola | 0:f1d3878b8dd9 | 101 | connect_success = mesh.connect(); |
vpcola | 0:f1d3878b8dd9 | 102 | network_interface = &mesh; |
vpcola | 0:f1d3878b8dd9 | 103 | #endif |
vpcola | 0:f1d3878b8dd9 | 104 | if(connect_success == 0) { |
vpcola | 0:f1d3878b8dd9 | 105 | if (log_messages) { |
vpcola | 0:f1d3878b8dd9 | 106 | printf("[EasyConnect] Connected to Network successfully\n"); |
vpcola | 0:f1d3878b8dd9 | 107 | } |
vpcola | 0:f1d3878b8dd9 | 108 | } else { |
vpcola | 0:f1d3878b8dd9 | 109 | if (log_messages) { |
vpcola | 0:f1d3878b8dd9 | 110 | printf("[EasyConnect] Connection to Network Failed %d!\n", connect_success); |
vpcola | 0:f1d3878b8dd9 | 111 | } |
vpcola | 0:f1d3878b8dd9 | 112 | return NULL; |
vpcola | 0:f1d3878b8dd9 | 113 | } |
vpcola | 0:f1d3878b8dd9 | 114 | const char *ip_addr = network_interface->get_ip_address(); |
vpcola | 0:f1d3878b8dd9 | 115 | const char *mac_addr = network_interface->get_mac_address(); |
vpcola | 0:f1d3878b8dd9 | 116 | if (ip_addr == NULL) { |
vpcola | 0:f1d3878b8dd9 | 117 | if (log_messages) { |
vpcola | 0:f1d3878b8dd9 | 118 | printf("[EasyConnect] ERROR - No IP address\n"); |
vpcola | 0:f1d3878b8dd9 | 119 | } |
vpcola | 0:f1d3878b8dd9 | 120 | return NULL; |
vpcola | 0:f1d3878b8dd9 | 121 | } |
vpcola | 0:f1d3878b8dd9 | 122 | if (mac_addr == NULL) { |
vpcola | 0:f1d3878b8dd9 | 123 | if (log_messages) { |
vpcola | 0:f1d3878b8dd9 | 124 | printf("[EasyConnect] ERROR - No MAC address\n"); |
vpcola | 0:f1d3878b8dd9 | 125 | } |
vpcola | 0:f1d3878b8dd9 | 126 | return NULL; |
vpcola | 0:f1d3878b8dd9 | 127 | } |
vpcola | 0:f1d3878b8dd9 | 128 | if (log_messages) { |
vpcola | 0:f1d3878b8dd9 | 129 | printf("[EasyConnect] IP address %s\n", ip_addr); |
vpcola | 0:f1d3878b8dd9 | 130 | printf("[EasyConnect] MAC address %s\n", mac_addr); |
vpcola | 0:f1d3878b8dd9 | 131 | } |
vpcola | 0:f1d3878b8dd9 | 132 | return network_interface; |
vpcola | 0:f1d3878b8dd9 | 133 | } |
vpcola | 0:f1d3878b8dd9 | 134 | |
vpcola | 0:f1d3878b8dd9 | 135 | #endif // __MAGIC_CONNECT_H__ |