NUCLEO-F767ZI Ethernet Sample mbed code not working

Hi I bought a NUCLEO-F767ZI dev board and though that I will design my project source with mbed.

I imported the Ethernet code into the online compiler from NUCLEO-F767ZI | Mbed

I see there were some issues because the source did not just have “mbed.h” included, but also EthernetInterface.h TCPSocket.h etc… I believe with the latest mbed-os these are all handled inside “mbed.h”
Anyways, It dit not compile before or after I removed the other includes.

I tried to keep everything as small as possible to see why it does not want to compile
It cannot find the below class even though I can see it inside the mbed-os folder.

Error: Identifier “EthernetInterface” is undefined in “main.cpp”, Line: 30, Col: 6

I removed almost all the code and just left the instantiation of the class.
please see below.

#include “mbed.h”
int main()
{
printf(“Basic HTTP server example\n”);
EthernetInterface eth;
eth.connect();

}

But still cannot compile as it cannot find EthernetInterface .
Identifier “EthernetInterface” is undefined in “main.cpp”
My target is selected as NUCLEO-F767ZI

Why is this happening? am I missing a reference?

This works on the F767.

Use some snips to get yours started.
There are some depreciated parts that need to be updated before OS6 is released.

1 Like

Hi there,

I recommend, when you want to try any example, as first check how old the example is and when was its last update. Then import it and compile it in the state and with a version of Mbed in which the code was build or updated.

So when I imported this example via link “Import into Compiler” it is working (created 2016 and latest update was in 2017, so the library will be older then MbedOS 5.10).
But when I only copy & paste the main.cpp into current Blinky example or any other project with latest version of Mbed - which is 5.15, it will not work, because the code is obsolete for the new version of MbedOS.

After update of MbedOS to latest version.
When I will follow warnings about what all is deprecated and then I will change these methods, according to documentation to new ones, you will see something like this.

/*#if !FEATURE_LWIP
    #error [NOT_SUPPORTED] LWIP not supported for this target
#endif*/ //ignore this

#include "mbed.h"
#include "EthernetInterface.h"
//#include "TCPServer.h" //deprecated - not needed anymore
//#include "TCPSocket.h" //not needed anymore

#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"

int main()
{
    printf("Basic HTTP server example\n");
    
    EthernetInterface eth;
    eth.connect();
    
    //printf("The target IP address is '%s'\n", eth.get_ip_address()); //deprecated - it not return char pointer but nsapi_error and address is filled to SocketAddress object
    SocketAddress srv_addr;
    eth.get_ip_address(&srv_addr);
    printf("The target IP address is '%s'\n", srv_addr.get_ip_address());
    
    //TCPServer srv;  //deprecated - TCPServer was migrate to TCPSocket
    TCPSocket srv;
    TCPSocket *clt_sock;  //srv.accept() will return pointer to socket
    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); // deprecated - use bind to Port or Port of a SockedAddress
    srv.bind(80);
    
    /* Can handle 5 simultaneous connections */
    srv.listen(1);
    
    while (true) {
        //srv.accept(&clt_sock, &clt_addr); // deprecated
        clt_sock = srv.accept();  //return pointer of a client socket
        clt_sock->getpeername(&clt_addr);  //this will fill address of client to the SocketAddress object
        printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port());
        clt_sock->send(HTTP_RESPONSE, strlen(HTTP_RESPONSE));
    }
}

Do not try to connect more than 3 times, after that will be crash, probably because the client pointer is not handled correctly but is only demo. :slight_smile:

Br, Jan

1 Like

Hi Jan,

Where are the non depreciated examples, I have tried here and they are all obsolete:

https://os.mbed.com/docs/mbed-os/v5.15/apis/network-socket.html

Can’t find how to set static IP like below.

eth.set_network (IP, NETMASK, GATEWAY);  // include to set network connection with static parameters.

These examples are wrong too, how do we get Netmask and Gateway now?

net.get_ip_address(&srv_addr);
printf("IP address: %s\n", srv_addr.get_ip_address() ? srv_addr.get_ip_address() : "None");
net.get_netmask(&srv_addr);
printf("Netmask: %s\n", srv_addr.get_ip_address() ? srv_addr.get_ip_address() : "None");
net.get_gateway(&srv_addr);
printf("Gateway: %s\n", srv_addr.get_ip_address() ? srv_addr.get_ip_address() : "None");

// Show the network address
const char *ip = net.get_ip_address();
const char *netmask = net.get_netmask();
const char *gateway = net.get_gateway();
printf("IP address: %s\n", ip ? ip : "None");
printf("Netmask: %s\n", netmask ? netmask : "None");
printf("Gateway: %s\n", gateway ? gateway : "None");

Any ideas?

Paul

2 Likes

Dear Paul,

I do not know where are any examples but I read what was changed :slight_smile:

add to set:
I do not what exactly you want to know but according to documentation was change type of input parameter from const char to const SocketAddress

    SocketAddress ip = "192.168.1.1";
    SocketAddress mask = "255.255.255.0";
    SocketAddress gateway = "10.0.1.138";
    eth.set_network(ip,mask,gateway); 
//or retype string
    eth.set_network((SocketAddress)"192.168.1.1",(SocketAddress)"255.255.255.0",(SocketAddress)"10.0.1.138");
//or retype macro
    eth_stat = eth.set_network((SocketAddress)IP,(SocketAddress)MASK,(SocketAddress)GATEWAY);

add to get:
the first one is ok from my point of view. The .get_ip_address was change to same design like .set_ip_address. It return nsapi_error and the address is returned via object of SocketAddress which need to be placed as input parameter and then read the content of that object.

    SocketAddress check; 
    eth.get_ip_address(&check);
    printf("IP address: %s\n", check.get_ip_address() ? check.get_ip_address() : "None");
    eth.get_netmask(&check);
    printf("Netmask: %s\n", check.get_ip_address() ? check.get_ip_address() : "None");
    eth.get_gateway(&check);
    printf("Gateway: %s\n", check.get_ip_address() ? check.get_ip_address() : "None");

I hope it will help.

BR, Jan

1 Like

Hi Jan,
Works just fine :slight_smile:
Thanks for your help (again).

BR
Paul