Version of easy-connect with the u-blox cellular platforms C027 and C030 added.

Dependents:   HelloMQTT HelloMQTT_Geneva

Committer:
RobMeades
Date:
Fri Nov 03 13:01:23 2017 +0000
Revision:
6:304d3ba87a01
Parent:
5:9da11cf868d9
Add comment concerning N2XX baud rate.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
group-ublox 0:19aa55d66228 1 #ifndef __EASY_CONNECT_H__
group-ublox 0:19aa55d66228 2 #define __EASY_CONNECT_H__
group-ublox 0:19aa55d66228 3
group-ublox 0:19aa55d66228 4 #include "mbed.h"
group-ublox 0:19aa55d66228 5
group-ublox 0:19aa55d66228 6 #define ETHERNET 1
group-ublox 0:19aa55d66228 7 #define WIFI_ESP8266 2
group-ublox 0:19aa55d66228 8 #define MESH_LOWPAN_ND 3
group-ublox 0:19aa55d66228 9 #define MESH_THREAD 4
group-ublox 0:19aa55d66228 10 #define WIFI_ODIN 5
group-ublox 0:19aa55d66228 11 #define WIFI_REALTEK 6
rob.meades@u-blox.com 3:1ee51369146a 12 #define CELLULAR_UBLOX 7
group-ublox 0:19aa55d66228 13
group-ublox 0:19aa55d66228 14 #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ESP8266
group-ublox 0:19aa55d66228 15 #include "ESP8266Interface.h"
group-ublox 0:19aa55d66228 16
group-ublox 0:19aa55d66228 17 #ifdef MBED_CONF_APP_ESP8266_DEBUG
group-ublox 0:19aa55d66228 18 ESP8266Interface wifi(MBED_CONF_APP_ESP8266_TX, MBED_CONF_APP_ESP8266_RX, MBED_CONF_APP_ESP8266_DEBUG);
group-ublox 0:19aa55d66228 19 #else
group-ublox 0:19aa55d66228 20 ESP8266Interface wifi(MBED_CONF_APP_ESP8266_TX, MBED_CONF_APP_ESP8266_RX);
group-ublox 0:19aa55d66228 21 #endif
group-ublox 0:19aa55d66228 22
group-ublox 0:19aa55d66228 23 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ODIN
group-ublox 0:19aa55d66228 24 #include "OdinWiFiInterface.h"
group-ublox 0:19aa55d66228 25
group-ublox 0:19aa55d66228 26 OdinWiFiInterface wifi;
group-ublox 0:19aa55d66228 27 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_RTW
group-ublox 0:19aa55d66228 28 #include "RTWInterface.h"
group-ublox 0:19aa55d66228 29 RTWInterface wifi;
group-ublox 0:19aa55d66228 30
group-ublox 0:19aa55d66228 31 #elif MBED_CONF_APP_NETWORK_INTERFACE == CELLULAR_UBLOX
rob.meades@u-blox.com 5:9da11cf868d9 32 // Note: we use UbloxAtCellularInterfaceExt here but the
rob.meades@u-blox.com 5:9da11cf868d9 33 // examples work with UbloxATCellularInterface,
rob.meades@u-blox.com 5:9da11cf868d9 34 // UbloxPPPCellularInterface or UbloxATCellularInterfaceN2xx
rob.meades@u-blox.com 5:9da11cf868d9 35 // (though noting that the N211 module supports only UDP and not TCP);
group-ublox 0:19aa55d66228 36 // simply uncomment/comment the correct options in the next few lines
group-ublox 0:19aa55d66228 37 #include "UbloxATCellularInterfaceExt.h"
group-ublox 0:19aa55d66228 38 //#include "UbloxPPPCellularInterface.h"
group-ublox 0:19aa55d66228 39 //#include "UbloxATCellularInterface.h"
rob.meades@u-blox.com 5:9da11cf868d9 40 //#include "UbloxATCellularInterfaceN2xx.h"
group-ublox 0:19aa55d66228 41
rob.meades@u-blox.com 5:9da11cf868d9 42 // Parameters added to the call here simply to show how debug can be switched on
RobMeades 6:304d3ba87a01 43 // For the N2xx interface, change xxx to MBED_CONF_UBLOX_CELL_N2XX_BAUD_RATE.
rob.meades@u-blox.com 5:9da11cf868d9 44 UbloxATCellularInterfaceExt cellular(MDMTXD, MDMRXD, MBED_CONF_UBLOX_CELL_BAUD_RATE, true);
group-ublox 0:19aa55d66228 45 //UbloxPPPCellularInterface cellular;
group-ublox 0:19aa55d66228 46 //UbloxATCellularInterface cellular;
rob.meades@u-blox.com 5:9da11cf868d9 47 //UbloxATCellularInterfaceN2xx cellular;
group-ublox 0:19aa55d66228 48 #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
group-ublox 0:19aa55d66228 49 #include "EthernetInterface.h"
group-ublox 0:19aa55d66228 50 EthernetInterface eth;
group-ublox 0:19aa55d66228 51 #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND
group-ublox 0:19aa55d66228 52 #define MESH
group-ublox 0:19aa55d66228 53 #include "NanostackInterface.h"
group-ublox 0:19aa55d66228 54 LoWPANNDInterface mesh;
group-ublox 0:19aa55d66228 55 #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD
group-ublox 0:19aa55d66228 56 #define MESH
group-ublox 0:19aa55d66228 57 #include "NanostackInterface.h"
group-ublox 0:19aa55d66228 58 ThreadInterface mesh;
group-ublox 0:19aa55d66228 59 #else
group-ublox 0:19aa55d66228 60 #error "No connectivity method chosen. Please add 'config.network-interfaces.value' to your mbed_app.json (see README.md for more information)."
group-ublox 0:19aa55d66228 61 #endif
group-ublox 0:19aa55d66228 62
group-ublox 0:19aa55d66228 63 #if defined(MESH)
group-ublox 0:19aa55d66228 64 #if MBED_CONF_APP_MESH_RADIO_TYPE == ATMEL
group-ublox 0:19aa55d66228 65 #include "NanostackRfPhyAtmel.h"
group-ublox 0:19aa55d66228 66 NanostackRfPhyAtmel rf_phy(ATMEL_SPI_MOSI, ATMEL_SPI_MISO, ATMEL_SPI_SCLK, ATMEL_SPI_CS,
group-ublox 0:19aa55d66228 67 ATMEL_SPI_RST, ATMEL_SPI_SLP, ATMEL_SPI_IRQ, ATMEL_I2C_SDA, ATMEL_I2C_SCL);
group-ublox 0:19aa55d66228 68 #elif MBED_CONF_APP_MESH_RADIO_TYPE == MCR20
group-ublox 0:19aa55d66228 69 #include "NanostackRfPhyMcr20a.h"
group-ublox 0:19aa55d66228 70 NanostackRfPhyMcr20a rf_phy(MCR20A_SPI_MOSI, MCR20A_SPI_MISO, MCR20A_SPI_SCLK, MCR20A_SPI_CS, MCR20A_SPI_RST, MCR20A_SPI_IRQ);
group-ublox 0:19aa55d66228 71 #elif MBED_CONF_APP_MESH_RADIO_TYPE == SPIRIT1
group-ublox 0:19aa55d66228 72 #include "NanostackRfPhySpirit1.h"
group-ublox 0:19aa55d66228 73 NanostackRfPhySpirit1 rf_phy(SPIRIT1_SPI_MOSI, SPIRIT1_SPI_MISO, SPIRIT1_SPI_SCLK,
group-ublox 0:19aa55d66228 74 SPIRIT1_DEV_IRQ, SPIRIT1_DEV_CS, SPIRIT1_DEV_SDN, SPIRIT1_BRD_LED);
group-ublox 0:19aa55d66228 75 #elif MBED_CONF_APP_MESH_RADIO_TYPE == EFR32
group-ublox 0:19aa55d66228 76 #include "NanostackRfPhyEfr32.h"
group-ublox 0:19aa55d66228 77 NanostackRfPhyEfr32 rf_phy;
group-ublox 0:19aa55d66228 78 #endif //MBED_CONF_APP_RADIO_TYPE
group-ublox 0:19aa55d66228 79 #endif //MESH
group-ublox 0:19aa55d66228 80
group-ublox 0:19aa55d66228 81 #ifndef MESH
group-ublox 0:19aa55d66228 82 // This is address to mbed Device Connector
group-ublox 0:19aa55d66228 83 #define MBED_SERVER_ADDRESS "coap://api.connector.mbed.com:5684"
group-ublox 0:19aa55d66228 84 #else
group-ublox 0:19aa55d66228 85 // This is address to mbed Device Connector
group-ublox 0:19aa55d66228 86 #define MBED_SERVER_ADDRESS "coaps://[2607:f0d0:2601:52::20]:5684"
group-ublox 0:19aa55d66228 87 #endif
group-ublox 0:19aa55d66228 88
group-ublox 0:19aa55d66228 89 #ifdef MBED_CONF_APP_ESP8266_SSID
group-ublox 0:19aa55d66228 90 #define MBED_CONF_APP_WIFI_SSID MBED_CONF_APP_ESP8266_SSID
group-ublox 0:19aa55d66228 91 #endif
group-ublox 0:19aa55d66228 92
group-ublox 0:19aa55d66228 93 #ifdef MBED_CONF_APP_ESP8266_PASSWORD
group-ublox 0:19aa55d66228 94 #define MBED_CONF_APP_WIFI_PASSWORD MBED_CONF_APP_ESP8266_PASSWORD
group-ublox 0:19aa55d66228 95 #endif
group-ublox 0:19aa55d66228 96
group-ublox 0:19aa55d66228 97 /* \brief print_MAC - print_MAC - helper function to print out MAC address
group-ublox 0:19aa55d66228 98 * in: network_interface - pointer to network i/f
group-ublox 0:19aa55d66228 99 * bool log-messages print out logs or not
group-ublox 0:19aa55d66228 100 * MAC address is print, if it can be acquired & log_messages is true.
group-ublox 0:19aa55d66228 101 *
group-ublox 0:19aa55d66228 102 */
rob.meades@u-blox.com 3:1ee51369146a 103 void print_MAC(NetworkInterface* network_interface, bool log_messages) {
rob.meades@u-blox.com 3:1ee51369146a 104 #if MBED_CONF_APP_NETWORK_INTERFACE != CELLULAR_UBLOX
group-ublox 0:19aa55d66228 105 const char *mac_addr = network_interface->get_mac_address();
group-ublox 0:19aa55d66228 106 if (mac_addr == NULL) {
group-ublox 0:19aa55d66228 107 if (log_messages) {
group-ublox 0:19aa55d66228 108 printf("[EasyConnect] ERROR - No MAC address\n");
group-ublox 0:19aa55d66228 109 }
group-ublox 0:19aa55d66228 110 return;
group-ublox 0:19aa55d66228 111 }
group-ublox 0:19aa55d66228 112 if (log_messages) {
group-ublox 0:19aa55d66228 113 printf("[EasyConnect] MAC address %s\n", mac_addr);
group-ublox 0:19aa55d66228 114 }
rob.meades@u-blox.com 3:1ee51369146a 115 #endif
group-ublox 0:19aa55d66228 116 }
group-ublox 0:19aa55d66228 117
group-ublox 0:19aa55d66228 118
group-ublox 0:19aa55d66228 119 /* \brief easy_connect - easy_connect function to connect the pre-defined network bearer,
group-ublox 0:19aa55d66228 120 * config done via mbed_app.json (see README.md for details).
group-ublox 0:19aa55d66228 121 * IN: bool log_messages print out diagnostics or not.
group-ublox 0:19aa55d66228 122 *
group-ublox 0:19aa55d66228 123 */
group-ublox 0:19aa55d66228 124 NetworkInterface* easy_connect(bool log_messages = false) {
group-ublox 0:19aa55d66228 125 NetworkInterface* network_interface = 0;
group-ublox 0:19aa55d66228 126 int connect_success = -1;
group-ublox 0:19aa55d66228 127 /// This should be removed once mbedOS supports proper dual-stack
group-ublox 0:19aa55d66228 128 #if defined (MESH) || (MBED_CONF_LWIP_IPV6_ENABLED==true)
group-ublox 0:19aa55d66228 129 printf("[EasyConnect] IPv6 mode\n");
group-ublox 0:19aa55d66228 130 #else
group-ublox 0:19aa55d66228 131 printf("[EasyConnect] IPv4 mode\n");
group-ublox 0:19aa55d66228 132 #endif
group-ublox 0:19aa55d66228 133
group-ublox 0:19aa55d66228 134 #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ESP8266
group-ublox 0:19aa55d66228 135 if (log_messages) {
group-ublox 0:19aa55d66228 136 printf("[EasyConnect] Using WiFi (ESP8266) \n");
group-ublox 0:19aa55d66228 137 printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID);
group-ublox 0:19aa55d66228 138 }
group-ublox 0:19aa55d66228 139 network_interface = &wifi;
group-ublox 0:19aa55d66228 140 connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
group-ublox 0:19aa55d66228 141 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ODIN
group-ublox 0:19aa55d66228 142 if (log_messages) {
group-ublox 0:19aa55d66228 143 printf("[EasyConnect] Using WiFi (ODIN) \n");
group-ublox 0:19aa55d66228 144 printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID);
group-ublox 0:19aa55d66228 145 }
group-ublox 0:19aa55d66228 146 network_interface = &wifi;
group-ublox 0:19aa55d66228 147 connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
group-ublox 0:19aa55d66228 148 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_RTW
group-ublox 0:19aa55d66228 149 if (log_messages) {
group-ublox 0:19aa55d66228 150 printf("[EasyConnect] Using WiFi (RTW)\n");
group-ublox 0:19aa55d66228 151 printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID);
group-ublox 0:19aa55d66228 152 }
group-ublox 0:19aa55d66228 153 network_interface = &wifi;
group-ublox 0:19aa55d66228 154 connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
group-ublox 0:19aa55d66228 155 #elif MBED_CONF_APP_NETWORK_INTERFACE == CELLULAR_UBLOX
group-ublox 0:19aa55d66228 156 if (log_messages) {
group-ublox 0:19aa55d66228 157 printf("[EasyConnect] Using Cellular (u-blox) \n");
rob.meades@u-blox.com 4:72079f2401ea 158 if (strlen(MBED_CONF_APP_APN) > 0) {
rob.meades@u-blox.com 4:72079f2401ea 159 printf("[EasyConnect] Connecting to Cellular %s\n", MBED_CONF_APP_APN);
rob.meades@u-blox.com 4:72079f2401ea 160 } else {
rob.meades@u-blox.com 4:72079f2401ea 161 printf("[EasyConnect] Connecting to Cellular\n");
rob.meades@u-blox.com 4:72079f2401ea 162 }
group-ublox 0:19aa55d66228 163 }
group-ublox 0:19aa55d66228 164 network_interface = &cellular;
group-ublox 0:19aa55d66228 165 connect_success = cellular.connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD);
group-ublox 0:19aa55d66228 166 #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
group-ublox 0:19aa55d66228 167 if (log_messages) {
group-ublox 0:19aa55d66228 168 printf("[EasyConnect] Using Ethernet\n");
group-ublox 0:19aa55d66228 169 }
group-ublox 0:19aa55d66228 170 network_interface = ð
group-ublox 0:19aa55d66228 171 connect_success = eth.connect();
group-ublox 0:19aa55d66228 172 #endif
group-ublox 0:19aa55d66228 173
group-ublox 0:19aa55d66228 174 #ifdef MESH
group-ublox 0:19aa55d66228 175 if (log_messages) {
group-ublox 0:19aa55d66228 176 printf("[EasyConnect] Using Mesh\n");
group-ublox 0:19aa55d66228 177 printf("[EasyConnect] Connecting to Mesh..\n");
group-ublox 0:19aa55d66228 178 }
group-ublox 0:19aa55d66228 179 network_interface = &mesh;
group-ublox 0:19aa55d66228 180 mesh.initialize(&rf_phy);
group-ublox 0:19aa55d66228 181 connect_success = mesh.connect();
group-ublox 0:19aa55d66228 182 #endif
group-ublox 0:19aa55d66228 183 if(connect_success == 0) {
group-ublox 0:19aa55d66228 184 if (log_messages) {
group-ublox 0:19aa55d66228 185 printf("[EasyConnect] Connected to Network successfully\n");
group-ublox 0:19aa55d66228 186 print_MAC(network_interface, log_messages);
group-ublox 0:19aa55d66228 187 }
group-ublox 0:19aa55d66228 188 } else {
group-ublox 0:19aa55d66228 189 if (log_messages) {
group-ublox 0:19aa55d66228 190 print_MAC(network_interface, log_messages);
group-ublox 0:19aa55d66228 191 printf("[EasyConnect] Connection to Network Failed %d!\n", connect_success);
group-ublox 0:19aa55d66228 192 }
group-ublox 0:19aa55d66228 193 return NULL;
group-ublox 0:19aa55d66228 194 }
group-ublox 0:19aa55d66228 195 const char *ip_addr = network_interface->get_ip_address();
group-ublox 0:19aa55d66228 196 if (ip_addr == NULL) {
group-ublox 0:19aa55d66228 197 if (log_messages) {
group-ublox 0:19aa55d66228 198 printf("[EasyConnect] ERROR - No IP address\n");
group-ublox 0:19aa55d66228 199 }
group-ublox 0:19aa55d66228 200 return NULL;
group-ublox 0:19aa55d66228 201 }
group-ublox 0:19aa55d66228 202 if (log_messages) {
group-ublox 0:19aa55d66228 203 printf("[EasyConnect] IP address %s\n", ip_addr);
group-ublox 0:19aa55d66228 204 }
group-ublox 0:19aa55d66228 205 return network_interface;
group-ublox 0:19aa55d66228 206 }
group-ublox 0:19aa55d66228 207
group-ublox 0:19aa55d66228 208 #endif // __EASY_CONNECT_H__