..
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.