7 years, 7 months ago.

EthernetInterface::init (mbed os 5.x) not working

Hi I have been trying to get some existing code to work under mbed 5.x but to no avail. I keep getting an error message that init() does not exist under EthernetInterface. It looks like it should be there when I search the github repo but I must be missing something obvious... can anyone explain why this is not working? Thanks Bill

#include "mbed.h"
#include "TCPSocket.h"
#include "EthernetInterface.h"

EthernetInterface eth;
TCPSocket socket;
// for static IP setting
const char * IP_Addr    = "192.168.1.102";
const char * IP_Subnet  = "255.255.255.0";
const char * IP_Gateway = "192.168.1.254";
int main()
{
    printf("Example network-socket HTTP client\n");

    // Brings up the network interface
    eth.init(IP_Addr, IP_Subnet, IP_Gateway);            //Gives error message during compile
    eth.connect();
    const char *ip = eth.get_ip_address();
    const char *mac = eth.get_mac_address();
.
.
.

I'm curious how you were able to get your program to build? I'm unable to build the EthernetInterface with mbed OS 5. Whenever I include "EthernetInterface.h", I get the error below. I get the same type of "no such file" error using either the online compiler or the mbed CLI.

Quote:

Error: Cannot open source input file "EthernetInterface.h": No such file or directory in "main.cpp", Line: 3, Col: 32

posted by David G 09 Sep 2016

2 Answers

7 years, 7 months ago.

Hi, the EthernetInterface has changed and is now built on top of the NetworkInterface class. Nice thing is that it allows you to write code that targets both ethernet, wifi, Thread and sub-GHz mesh under one API but the downside is that the ethernet interface changed a bit. Here's how you can connect to a network:

#include "EthernetInterface.h"
EthernetInterface eth;

int main(int, char**) {
    NetworkInterface* network_interface = eth.connect(); // network_interface will be NULLPTR when connection fails
    const char* ip = network_interface->get_ip_address();
}

Please note that on mbed OS 5 we only support DHCP for now...

Well that's clearly different than the mbed 5.1 docs here: https://docs.mbed.com/docs/mbed-os-api-reference/en/5.1/APIs/communication/ethernet/

And how does one statically define the ip address since init is gone?

posted by Bill Bellis 08 Sep 2016

Yeah, you're correct. I'll get the docs resolved, seems like half the comms APIs have been updated, and the other half hasn't... Regarding static IPs: as far as I know, not possible right now in mbed OS 5.1 (although it is in mbed 2)... Tracking bug here https://github.com/ARMmbed/mbed-os/issues/2649.

posted by Jan Jongboom 08 Sep 2016

FYI, setting a static IP is currently worked on, see the working branch link in the tracking bug I mentioned ^.

posted by Jan Jongboom 12 Sep 2016
7 years, 5 months ago.

Hi, i try to use the mbedos-5.2.2 version to enable the static ip with following code:

Code

#if !FEATURE_LWIP
    #error [NOT_SUPPORTED] LWIP not supported for this target
#endif
 
#include "mbed.h"
#include "EthernetInterface.h"
#include "TCPServer.h"
#include "TCPSocket.h"
 
#define HTTP_STATUS_LINE "HTTP/1.0 200 OK"
#define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8"
#define HTTP_MESSAGE_BODY ""                                     \
"<html>" "\r\n"                                                  \
"  <body style=\"display:flex;text-align:center\">" "\r\n"       \
"    <div style=\"margin:auto\">" "\r\n"                         \
"      <h1>Hello World</h1>" "\r\n"                              \
"      <p>It works !</p>" "\r\n"                                 \
"    </div>" "\r\n"                                              \
"  </body>" "\r\n"                                               \
"</html>"
 
#define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n"   \
                      HTTP_HEADER_FIELDS "\r\n" \
                      "\r\n"                    \
                      HTTP_MESSAGE_BODY "\r\n"
 
#define IP         "192.168.1.102"
#define GATEWAY    "192.168.1.1"
#define MASK       "255.255.255.0"
int main()
{
    printf("Basic HTTP server example\n");
    
    EthernetInterface eth;
    eth.init(IP, MASK, GATEWAY);
    eth.connect();
    
    printf("The target IP address is '%s'\n", eth.get_ip_address());
    
    TCPServer srv;
    TCPSocket clt_sock;
    SocketAddress clt_addr;
    
    /* Open the server on ethernet stack */
    srv.open(&eth);
    
    /* Bind the HTTP port (TCP 80) to the server */
    srv.bind(eth.get_ip_address(), 80);
    
    /* Can handle 5 simultaneous connections */
    srv.listen(1);
    
    while (true) {
        srv.accept(&clt_sock, &clt_addr);
        printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port());
        clt_sock.send(HTTP_RESPONSE, strlen(HTTP_RESPONSE));
    }
}

With this snippet my STM32F746ZGT6 uses the static ip but the function srv.accept(&clt_sock, &clt_addr); doesn't wait the connection of a client. Any Solution?