A quick effort to get W5500 working with easy connect. I removed other dependencies to reduce confusion. It is generally not a fork you want to look at.

Dependents:   coap-example coap-example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers easy-connect.h Source File

easy-connect.h

00001 #ifndef __EASY_CONNECT_H__
00002 #define __EASY_CONNECT_H__
00003 
00004 #include "mbed.h"
00005 
00006 #define ETHERNET        1
00007 #define WIFI_ESP8266    0
00008 #define MESH_LOWPAN_ND  0
00009 #define MESH_THREAD     0
00010 #define WIFI_ODIN       0
00011 #define WIFI_REALTEK    0
00012 
00013 #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ESP8266
00014 #include "ESP8266Interface.h"
00015 
00016 #ifdef MBED_CONF_APP_ESP8266_DEBUG
00017 ESP8266Interface wifi(MBED_CONF_APP_ESP8266_TX, MBED_CONF_APP_ESP8266_RX, MBED_CONF_APP_ESP8266_DEBUG);
00018 #else
00019 ESP8266Interface wifi(MBED_CONF_APP_ESP8266_TX, MBED_CONF_APP_ESP8266_RX);
00020 #endif
00021 
00022 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ODIN
00023 #include "OdinWiFiInterface.h"
00024 
00025 OdinWiFiInterface wifi;
00026 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_REALTEK
00027 #include "RTWInterface.h"
00028 RTWInterface wifi;
00029 #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
00030 //#include "EthernetInterface.h"
00031 //EthernetInterface eth;
00032 
00033 SPI spi(PA_7,PA_6,PA_5); // mosi, miso, sclk
00034 #include "W5500Interface.h"
00035 W5500Interface eth(&spi, PB_6, NC); // mosi, miso, sclk, cs, reset
00036 
00037 #include "DHCPClient.h"
00038 DHCPClient dhcp;
00039 
00040 #include "DNSClient.h"
00041 DNSClient  dns;
00042 
00043 #include "nsapi_dns.h"
00044 
00045 #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND
00046 #define MESH
00047 #include "NanostackInterface.h"
00048 LoWPANNDInterface mesh;
00049 #elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD
00050 #define MESH
00051 #include "NanostackInterface.h"
00052 ThreadInterface mesh;
00053 #else
00054 #error "No connectivity method chosen. Please add 'config.network-interfaces.value' to your mbed_app.json (see README.md for more information)."
00055 #endif
00056 
00057 #if defined(MESH)
00058 #if MBED_CONF_APP_MESH_RADIO_TYPE == ATMEL
00059 #include "NanostackRfPhyAtmel.h"
00060 NanostackRfPhyAtmel rf_phy(ATMEL_SPI_MOSI, ATMEL_SPI_MISO, ATMEL_SPI_SCLK, ATMEL_SPI_CS,
00061                            ATMEL_SPI_RST, ATMEL_SPI_SLP, ATMEL_SPI_IRQ, ATMEL_I2C_SDA, ATMEL_I2C_SCL);
00062 #elif MBED_CONF_APP_MESH_RADIO_TYPE == MCR20
00063 #include "NanostackRfPhyMcr20a.h"
00064 NanostackRfPhyMcr20a rf_phy(MCR20A_SPI_MOSI, MCR20A_SPI_MISO, MCR20A_SPI_SCLK, MCR20A_SPI_CS, MCR20A_SPI_RST, MCR20A_SPI_IRQ);
00065 #elif MBED_CONF_APP_MESH_RADIO_TYPE == SPIRIT1
00066 #include "NanostackRfPhySpirit1.h"
00067 NanostackRfPhySpirit1 rf_phy(SPIRIT1_SPI_MOSI, SPIRIT1_SPI_MISO, SPIRIT1_SPI_SCLK,
00068                  SPIRIT1_DEV_IRQ, SPIRIT1_DEV_CS, SPIRIT1_DEV_SDN, SPIRIT1_BRD_LED);
00069 #endif //MBED_CONF_APP_RADIO_TYPE
00070 #endif //MESH
00071 
00072 #ifndef MESH
00073 // This is address to mbed Device Connector
00074 #define MBED_SERVER_ADDRESS "coap://api.connector.mbed.com:5684"
00075 #else
00076 // This is address to mbed Device Connector
00077 #define MBED_SERVER_ADDRESS "coaps://[2607:f0d0:2601:52::20]:5684"
00078 #endif
00079 
00080 #ifdef MBED_CONF_APP_ESP8266_SSID
00081 #define MBED_CONF_APP_WIFI_SSID MBED_CONF_APP_ESP8266_SSID
00082 #endif
00083 
00084 #ifdef MBED_CONF_APP_ESP8266_PASSWORD
00085 #define MBED_CONF_APP_WIFI_PASSWORD MBED_CONF_APP_ESP8266_PASSWORD
00086 #endif
00087 
00088 /* \brief print_MAC - print_MAC  - helper function to print out MAC address
00089  * in: network_interface - pointer to network i/f
00090  *     bool log-messages   print out logs or not
00091  * MAC address is print, if it can be acquired & log_messages is true.
00092  *
00093  */
00094 void print_MAC(NetworkInterface* network_interface, bool log_messages) {
00095     const char *mac_addr = network_interface->get_mac_address();
00096     if (mac_addr == NULL) {
00097         if (log_messages) {
00098             printf("[EasyConnect] ERROR - No MAC address\n");
00099         }
00100         return;
00101     }
00102     if (log_messages) {
00103         printf("[EasyConnect] MAC address %s\n", mac_addr);
00104     }
00105 }
00106 
00107 
00108 /* \brief easy_connect - easy_connect function to connect the pre-defined network bearer,
00109  *                       config done via mbed_app.json (see README.md for details).
00110  * IN: bool log_messages  print out diagnostics or not.
00111  *
00112  */
00113 NetworkInterface* easy_connect(bool log_messages = false) {
00114     NetworkInterface* network_interface = 0;
00115     int connect_success = -1;
00116     /// This should be removed once mbedOS supports proper dual-stack
00117 #if defined (MESH) || (MBED_CONF_LWIP_IPV6_ENABLED==true)
00118     printf("[EasyConnect] IPv6 mode\n");
00119 #else
00120     printf("[EasyConnect] IPv4 mode\n");
00121 #endif
00122 
00123  #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ESP8266
00124     if (log_messages) {
00125         printf("[EasyConnect] Using WiFi (ESP8266) \n");
00126         printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID);
00127     }
00128     network_interface = &wifi;
00129     connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00130 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ODIN
00131     if (log_messages) {
00132         printf("[EasyConnect] Using WiFi (ODIN) \n");
00133         printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID);
00134     }
00135     network_interface = &wifi;
00136     connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00137 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_REALTEK
00138     if (log_messages) {
00139         printf("[EasyConnect] Using WiFi (REALTEK)\n");
00140         printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID);
00141     }
00142     network_interface = &wifi;
00143     connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00144 #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
00145 
00146     if (log_messages) {
00147         printf("[EasyConnect] Ethernet...\n");
00148     }
00149 
00150     spi.format(32, 0);
00151     spi.frequency(100000);
00152 
00153     printf("Wait for SPI...\r\n");
00154     wait(1);
00155     
00156     printf("Network Setting DHCP\r\n");
00157     uint8_t mac_addr[6] = {0x00, 0x08, 0xdc, 0x45, 0x56, 0x67};
00158     
00159     printf("Init with MAC address\n");
00160     eth.init(mac_addr);
00161     
00162     int timeout_ms = 15*1000;
00163     int err = dhcp.setup(&eth, mac_addr, timeout_ms);
00164     if (err == (-1)) {
00165         printf("Timeout.\n");
00166         return NULL;
00167     }
00168     printf("DHCP completed\n");
00169     printf("Connected, IP: %d.%d.%d.%d\r\n", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]);
00170     char ip[24], gateway[24], netmask[24], dnsaddr[24];
00171     sprintf(ip,      "%d.%d.%d.%d", dhcp.yiaddr[0],  dhcp.yiaddr[1],  dhcp.yiaddr[2],  dhcp.yiaddr[3]);
00172     sprintf(gateway, "%d.%d.%d.%d", dhcp.gateway[0], dhcp.gateway[1], dhcp.gateway[2], dhcp.gateway[3]);
00173     sprintf(netmask, "%d.%d.%d.%d", dhcp.netmask[0], dhcp.netmask[1], dhcp.netmask[2], dhcp.netmask[3]);
00174     sprintf(dnsaddr, "%d.%d.%d.%d", dhcp.dnsaddr[0], dhcp.dnsaddr[1], dhcp.dnsaddr[2], dhcp.dnsaddr[3]);
00175     eth.init(ip, netmask, gateway);
00176 
00177     dns.setup(&eth);
00178     dns.set_server(dnsaddr);
00179     bool isOK = dns.lookup("coap.me");
00180     if (isOK) {
00181         printf("is ok\n");
00182         uint32_t hIP = dns.get_ip();
00183         printf("coap.me IP: %u\n", hIP);
00184     } else {
00185         printf("coap.me IP is not ok\n");
00186     }
00187 
00188     
00189     const SocketAddress dns_sa = SocketAddress((const void *)&dhcp.dnsaddr[0], NSAPI_IPv4, 53);
00190     nsapi_error_t nsErr = nsapi_dns_add_server(dns_sa);
00191     if (nsErr) {
00192         printf("[EasyConnect] error adding DNS entry: %d\n", nsErr);
00193     }
00194     printf("[EasyConnect] DNS IP: %s\n", dns_sa.get_ip_address());
00195     
00196     SocketAddress coap_sa;
00197     nsErr = nsapi_dns_query(&eth, "coap.me", &coap_sa, NSAPI_IPv4);
00198     if (nsErr) {
00199         printf("[EasyConnect] error resolving coap.me: %d\n", nsErr);
00200     }
00201 
00202     if (log_messages) {
00203         printf("[EasyConnect] Using Ethernet\n");
00204     }
00205     network_interface = ð
00206     connect_success = eth.connect();
00207 #endif
00208 
00209 #ifdef MESH
00210     if (log_messages) {
00211         printf("[EasyConnect] Using Mesh\n");
00212         printf("[EasyConnect] Connecting to Mesh..\n");
00213     }
00214     network_interface = &mesh;
00215     mesh.initialize(&rf_phy);
00216     connect_success = mesh.connect();
00217 #endif
00218     if(connect_success == 0) {
00219         if (log_messages) {
00220             printf("[EasyConnect] Connected to Network successfully\n");
00221             print_MAC(network_interface, log_messages);
00222         }
00223     } else {
00224         if (log_messages) {
00225             print_MAC(network_interface, log_messages);
00226             printf("[EasyConnect] Connection to Network Failed %d!\n", connect_success);
00227         }
00228         return NULL;
00229     }
00230     const char *ip_addr  = network_interface->get_ip_address();
00231     if (ip_addr == NULL) {
00232         if (log_messages) {
00233             printf("[EasyConnect] ERROR - No IP address\n");
00234         }
00235         return NULL;
00236     }
00237     if (log_messages) {
00238         printf("[EasyConnect] IP address %s\n", ip_addr);
00239     }
00240     return network_interface;
00241 }
00242 
00243 #endif // __EASY_CONNECT_H__