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