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
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(ð);
/* 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.
I do not know where are any examples but I read what was changed
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
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.