..
Dependencies: ESP8266Interface WIZnetInterface_namespace mbed-src
Dual Network Interface
Some our customers want dual network interface like below.
So, I decided to implement dual network interface with ESP8266(WiFi) and WIZwiki-W7500(Ethernet).
Implementation
But, I had some troubles because of the below.
- There is no abstract class for all network interface
- Same header file name in two library WIZnetInterface(EthernetInterface) and ESP8266Interface
- Same class name in two library WIZnetInterface and ESP8266Interface
If I can modify Makefile, there will be better way to solve this problem. But, I couldn't modify Makefile in mbed.
So I solved this problem using namespace. Of course, there may be another way to change all class name and file name in the library. But, I wanted to minimize the changes of original library.
First, I changed the header file name in WIZnetInterface from <*.h> to <*.hpp>.
Declaration namespace in header file
// Endpoint.hpp namespace wiznet_space { class Endpoint { friend class UDPSocket; .................... } } // Socket.hpp namespace wiznet_space { class Socket { public: Socket(); .......................... } } .......................... ..........................
Using namespace in source file
// main.c .......................... ESP8266Interface wifi(D1, D0, D2, "WizFiDemoAP","12345678",115200); // tx, rx for wiznet_space::EthernetInterface eth; bool InitializeWiznetEthernet() { uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0xFF, 0x11, 0x22}; eth.init(mac_addr, "192.168.3.102", "255.255.255.0", "192.168.3.1"); //Use DHCP ....................................................................................................................... } bool InitializeESP8266() { wifi.init(); ....................................................................................................................... } int main() { .................................................................... // Ethernet : WIZnet hardwired TCP/IP in W7500 if ( InitializeWiznetEthernet() ) { // Send TCP/IP data via Ethernet ProcessDataViaEthernet(); } // WiFi : ESP8266 if ( InitializeESP8266() ) { // Send TCP/IP data via WiFi ProcessDataViaWiFi(); } .................................................................... } // ProcessDataViaEthernet.cpp void ProcessDataViaEthernet() { wiznet_space::TCPSocketConnection eth_sock; eth_sock.connect("192.168.3.64", 6000); char send_data[] = "This is from Ethernet Interface\r\n"; eth_sock.send_all(send_data, sizeof(send_data)-1); eth_sock.close(); } // ProcessDataViaWiFi.cpp void ProcessDataViaWiFi() { ::TCPSocketConnection wifi_sock; wifi_sock.connect("192.168.3.64", 6000); char send_data[] = "This is from WiFi Interface\r\n"; wifi_sock.send_all(send_data, sizeof(send_data)-1); wifi_sock.close(); }
And, here are screenshots of this test.
Conclusion
I know that this way is a kind of work-around way. Best way is to design a new network-abstract-class and hierarchical network-interface-classes based on the network-abstract-class.
- Ethernet : lwIP, WIZnet hardwired TCP/IP
- WiFi : ESP8266, WiFly, WizFi250, CC3000, ....
- Other interfaces(3G/4G, BT, .....)
I believe that mbed-team will release a new network classes based on a hierarchical architecture .
Until then, you can refer to the this code for dual network interface, temporarily.
main.cpp
- Committer:
- SteveKim
- Date:
- 2015-07-14
- Revision:
- 1:a79f264f321f
- Parent:
- 0:0543bf604693
File content as of revision 1:a79f264f321f:
#include "mbed.h" #include "ESP8266Interface.h" #include "EthernetInterface.hpp" DigitalOut led1(LED1); DigitalOut led2(LED2); ESP8266Interface wifi(D1, D0, D2, "WizFiDemoAP","12345678",115200); // tx, rx for wiznet_space::EthernetInterface eth; RawSerial pc(USBTX, USBRX); // tx, rx extern void ProcessDataViaEthernet(); extern void ProcessDataViaWiFi(); // For monitoring data from ESP8266 Timeout timer_buffer_debug; CircBuffer<char> buffer_ESP8266_recv(1024); void print_buffer_ESP8266() { char c=0; while ( buffer_ESP8266_recv.available() ) { buffer_ESP8266_recv.dequeue(&c); pc.putc(c); } timer_buffer_debug.attach(&print_buffer_ESP8266, 0.1); } bool InitializeWiznetEthernet() { uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0xFF, 0x11, 0x22}; //eth.init(mac_addr); //Use DHCP eth.init(mac_addr, "192.168.3.102", "255.255.255.0", "192.168.3.1"); //Use DHCP // Check Ethenret Link if( eth.link()!=true ) { printf("- Ethernet PHY Link- Fail\r\n"); return false; } // Start Ethernet connecting: Trying to get an IP address using DHCP if ( eth.connect() < 0 ){ printf("Fail - Ethernet Connecing"); return false; } //pc.printf("IP Address(Ethernet Interface) is %s\r\n", eth.getIPAddress()); return true; } bool InitializeESP8266() { // if you don't want to monitor ESP-module-data, remove below line. //timer_buffer_debug.attach(&print_buffer_ESP8266, 0.5); wifi.init(); bool result = wifi.connect(); if ( !result ) { pc.printf("wifi.connect error\r\n"); return false; } //pc.printf("IP Address(WiFi Interface) is %s\r\n", wifi.getIPAddress()); return true; } int main() { // Initialize LED for (int i=0; i<20; i++) { led1 = !led1; led2 = !led2; wait(0.05); } // Initialize UART pc.baud(115200); pc.printf("\r\nDual Network Interface Test.\r\n"); // Ethernet : WIZnet hardwired TCP/IP in W7500 if ( InitializeWiznetEthernet() ) { ProcessDataViaEthernet(); pc.printf("WIZnet hardwired TCP/IP in W7500 Test Done.\r\n"); } // WiFi : ESP8266 if ( InitializeESP8266() ) { ProcessDataViaWiFi(); pc.printf("ESP8266 Test Done.\r\n"); } while(1) { wait(1); } }