..
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@1:a79f264f321f, 2015-07-14 (annotated)
- Committer:
- SteveKim
- Date:
- Tue Jul 14 10:18:05 2015 +0000
- Revision:
- 1:a79f264f321f
- Parent:
- 0:0543bf604693
Dual Network Interface for WIZwiki-W7500;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SteveKim | 0:0543bf604693 | 1 | #include "mbed.h" |
SteveKim | 0:0543bf604693 | 2 | |
SteveKim | 1:a79f264f321f | 3 | #include "ESP8266Interface.h" |
SteveKim | 1:a79f264f321f | 4 | #include "EthernetInterface.hpp" |
SteveKim | 0:0543bf604693 | 5 | |
SteveKim | 0:0543bf604693 | 6 | DigitalOut led1(LED1); |
SteveKim | 0:0543bf604693 | 7 | DigitalOut led2(LED2); |
SteveKim | 1:a79f264f321f | 8 | |
SteveKim | 1:a79f264f321f | 9 | ESP8266Interface wifi(D1, D0, D2, "WizFiDemoAP","12345678",115200); // tx, rx for |
SteveKim | 1:a79f264f321f | 10 | wiznet_space::EthernetInterface eth; |
SteveKim | 1:a79f264f321f | 11 | |
SteveKim | 1:a79f264f321f | 12 | RawSerial pc(USBTX, USBRX); // tx, rx |
SteveKim | 1:a79f264f321f | 13 | |
SteveKim | 1:a79f264f321f | 14 | extern void ProcessDataViaEthernet(); |
SteveKim | 1:a79f264f321f | 15 | extern void ProcessDataViaWiFi(); |
SteveKim | 1:a79f264f321f | 16 | |
SteveKim | 1:a79f264f321f | 17 | // For monitoring data from ESP8266 |
SteveKim | 1:a79f264f321f | 18 | Timeout timer_buffer_debug; |
SteveKim | 1:a79f264f321f | 19 | CircBuffer<char> buffer_ESP8266_recv(1024); |
SteveKim | 1:a79f264f321f | 20 | |
SteveKim | 1:a79f264f321f | 21 | void print_buffer_ESP8266() |
SteveKim | 0:0543bf604693 | 22 | { |
SteveKim | 1:a79f264f321f | 23 | char c=0; |
SteveKim | 1:a79f264f321f | 24 | while ( buffer_ESP8266_recv.available() ) { |
SteveKim | 1:a79f264f321f | 25 | buffer_ESP8266_recv.dequeue(&c); |
SteveKim | 1:a79f264f321f | 26 | pc.putc(c); |
SteveKim | 1:a79f264f321f | 27 | } |
SteveKim | 1:a79f264f321f | 28 | timer_buffer_debug.attach(&print_buffer_ESP8266, 0.1); |
SteveKim | 1:a79f264f321f | 29 | } |
SteveKim | 1:a79f264f321f | 30 | |
SteveKim | 1:a79f264f321f | 31 | |
SteveKim | 1:a79f264f321f | 32 | bool InitializeWiznetEthernet() |
SteveKim | 1:a79f264f321f | 33 | { |
SteveKim | 0:0543bf604693 | 34 | uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0xFF, 0x11, 0x22}; |
SteveKim | 1:a79f264f321f | 35 | |
SteveKim | 1:a79f264f321f | 36 | //eth.init(mac_addr); //Use DHCP |
SteveKim | 1:a79f264f321f | 37 | eth.init(mac_addr, "192.168.3.102", "255.255.255.0", "192.168.3.1"); //Use DHCP |
SteveKim | 0:0543bf604693 | 38 | |
SteveKim | 1:a79f264f321f | 39 | // Check Ethenret Link |
SteveKim | 1:a79f264f321f | 40 | if( eth.link()!=true ) |
SteveKim | 1:a79f264f321f | 41 | { |
SteveKim | 1:a79f264f321f | 42 | printf("- Ethernet PHY Link- Fail\r\n"); |
SteveKim | 1:a79f264f321f | 43 | return false; |
SteveKim | 1:a79f264f321f | 44 | } |
SteveKim | 1:a79f264f321f | 45 | |
SteveKim | 1:a79f264f321f | 46 | // Start Ethernet connecting: Trying to get an IP address using DHCP |
SteveKim | 1:a79f264f321f | 47 | if ( eth.connect() < 0 ){ |
SteveKim | 1:a79f264f321f | 48 | printf("Fail - Ethernet Connecing"); |
SteveKim | 1:a79f264f321f | 49 | return false; |
SteveKim | 0:0543bf604693 | 50 | } |
SteveKim | 0:0543bf604693 | 51 | |
SteveKim | 1:a79f264f321f | 52 | //pc.printf("IP Address(Ethernet Interface) is %s\r\n", eth.getIPAddress()); |
SteveKim | 1:a79f264f321f | 53 | |
SteveKim | 1:a79f264f321f | 54 | return true; |
SteveKim | 1:a79f264f321f | 55 | } |
SteveKim | 1:a79f264f321f | 56 | |
SteveKim | 1:a79f264f321f | 57 | bool InitializeESP8266() |
SteveKim | 1:a79f264f321f | 58 | { |
SteveKim | 1:a79f264f321f | 59 | // if you don't want to monitor ESP-module-data, remove below line. |
SteveKim | 1:a79f264f321f | 60 | //timer_buffer_debug.attach(&print_buffer_ESP8266, 0.5); |
SteveKim | 1:a79f264f321f | 61 | |
SteveKim | 1:a79f264f321f | 62 | wifi.init(); |
SteveKim | 1:a79f264f321f | 63 | |
SteveKim | 1:a79f264f321f | 64 | bool result = wifi.connect(); |
SteveKim | 1:a79f264f321f | 65 | if ( !result ) { |
SteveKim | 1:a79f264f321f | 66 | pc.printf("wifi.connect error\r\n"); |
SteveKim | 1:a79f264f321f | 67 | return false; |
SteveKim | 0:0543bf604693 | 68 | } |
SteveKim | 1:a79f264f321f | 69 | |
SteveKim | 1:a79f264f321f | 70 | //pc.printf("IP Address(WiFi Interface) is %s\r\n", wifi.getIPAddress()); |
SteveKim | 1:a79f264f321f | 71 | return true; |
SteveKim | 1:a79f264f321f | 72 | } |
SteveKim | 1:a79f264f321f | 73 | |
SteveKim | 1:a79f264f321f | 74 | |
SteveKim | 1:a79f264f321f | 75 | int main() |
SteveKim | 1:a79f264f321f | 76 | { |
SteveKim | 1:a79f264f321f | 77 | // Initialize LED |
SteveKim | 1:a79f264f321f | 78 | for (int i=0; i<20; i++) |
SteveKim | 1:a79f264f321f | 79 | { |
SteveKim | 1:a79f264f321f | 80 | led1 = !led1; |
SteveKim | 1:a79f264f321f | 81 | led2 = !led2; |
SteveKim | 1:a79f264f321f | 82 | wait(0.05); |
SteveKim | 1:a79f264f321f | 83 | } |
SteveKim | 1:a79f264f321f | 84 | |
SteveKim | 1:a79f264f321f | 85 | // Initialize UART |
SteveKim | 1:a79f264f321f | 86 | pc.baud(115200); |
SteveKim | 1:a79f264f321f | 87 | pc.printf("\r\nDual Network Interface Test.\r\n"); |
SteveKim | 1:a79f264f321f | 88 | |
SteveKim | 1:a79f264f321f | 89 | // Ethernet : WIZnet hardwired TCP/IP in W7500 |
SteveKim | 1:a79f264f321f | 90 | if ( InitializeWiznetEthernet() ) |
SteveKim | 1:a79f264f321f | 91 | { |
SteveKim | 1:a79f264f321f | 92 | ProcessDataViaEthernet(); |
SteveKim | 1:a79f264f321f | 93 | pc.printf("WIZnet hardwired TCP/IP in W7500 Test Done.\r\n"); |
SteveKim | 1:a79f264f321f | 94 | } |
SteveKim | 0:0543bf604693 | 95 | |
SteveKim | 1:a79f264f321f | 96 | // WiFi : ESP8266 |
SteveKim | 1:a79f264f321f | 97 | if ( InitializeESP8266() ) |
SteveKim | 1:a79f264f321f | 98 | { |
SteveKim | 1:a79f264f321f | 99 | ProcessDataViaWiFi(); |
SteveKim | 1:a79f264f321f | 100 | pc.printf("ESP8266 Test Done.\r\n"); |
SteveKim | 1:a79f264f321f | 101 | } |
SteveKim | 0:0543bf604693 | 102 | |
SteveKim | 0:0543bf604693 | 103 | |
SteveKim | 0:0543bf604693 | 104 | while(1) { |
SteveKim | 1:a79f264f321f | 105 | wait(1); |
SteveKim | 0:0543bf604693 | 106 | } |
SteveKim | 0:0543bf604693 | 107 | } |
SteveKim | 1:a79f264f321f | 108 |