Ethernet connection between 2 FRDM-K66F

Hi I am trying to establish communication between 2 mbeds
I am using this example:
https://os.mbed.com/users/JohnnyK/code/TCP_EchoServer//file/8c10d8f4e3aa/main.cpp/
as a server and for client based on the following example

https://os.mbed.com/questions/77703/Ethernet-connection-between-two-mbeds/

I am receiving -3004 error on socket.send.

void client()

{

    int err;

    TCPSocket socket;

    const char* ECHO_SERVER_ADDRESS = "192.168.1.6";    

    EthernetInterface eth;

    size_t BUFFSIZE = 64;

    const char* IP = "192.168.1.177";    

    const char* MASK = "255.255.255.0";

    const char* GATEWAY = "192.168.1.1";

    err = eth.set_network(IP, MASK, GATEWAY);

    printf("eth.set_network er = %d\n",err);

    SocketAddress a;

    printf("set IP status: %i \r\n",i);

    err=eth.connect();

    printf("connect status: %i \r\n",err);

    eth.get_ip_address(&a);

    const char *ip = a.get_ip_address();

    const char *mac = eth.get_mac_address();

    printf("IP address is: %s\n\r", ip ? ip : "No IP");

    printf("MAC address is: %s\n\r", mac ? mac : "No MAC");

    

    while(true) {

        err = socket.open(&eth);

        printf("socket.open %d\n",err);

        SocketAddress remote;

        remote.set_port(ECHO_SERVER_PORT);

        remote.set_ip_address(ECHO_SERVER_ADDRESS);

        while((err = socket.connect(remote)) < 0)

        {

            printf("connect %d\n",err);

            osDelay(1000);

        }

        
        printf("connect %d\n",err);

        char sbuffer[] = "GET /checkPin.php?\r\n";

        while((err = socket.send(sbuffer, sizeof sbuffer)) <0)

        {

            printf("send %d\n",err);

            osDelay(5000);

        }

        char rbuffer[64];

        int rcount = socket.recv(rbuffer, sizeof rbuffer);

        printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);

        osDelay(5000);

    }

}

Please help me to figure out the how to make it working.

Thank you,
Igor

Ahoj,

please be so kind and use ``` before and after your code for correct code presentation.

-3004, /*!< not connected to a network */
The Enum you can found here.

Currently I am not at home so I can not try it but I have additional questions, for sure.

  • Your boards are connected directly without a gateway via a cross cable?
  • Did you tried establish connection between both boards and pc separately? (Mbed client >> PC server and PC client >> Mbed Server). For verification of both codes/boards are working.

BR, Jan

Hi Jan.
Thank you for helping to resolve my issue.
I am trying to have a communication between 2 boards directly connected with Ethernet cable.
I did try to connect K66F to PC (with running Python script from https://os.mbed.com/handbook/Socket ). Since this did not work I assume that the problem on the client side.

When I connect to switch both mbeds it works OK (client and server on K66Fs). Is there an option to make it work connected directly without switch?

Thank you,
Igor

Ok, I am home now so…

I took the code from my example (TCP echo server), which you linked above and commented out the line 6 (//#define ROUTER) because we want to set the IP ourselves and use it without a gateway.

Then I made a simple client

#include "mbed.h"
#include "NetworkInterface.h"
 
#define BUFFSIZE 50
#define SERVERIP         "192.168.1.1"   //Here place your static IP of Mbed Server
 
//#define ROUTER // commented = Static IP uncomented = address assigned by router
#ifndef ROUTER
    #define IP          "192.168.1.10"   //Here place your static IP of Mbed
    #define GATEWAY     "0.0.0.0"
    #define MASK        "255.255.255.0"
#endif
#define PORT            20
 
DigitalOut led(LED1);
 
NetworkInterface *net = NetworkInterface::get_default_instance();
TCPSocket client;
SocketAddress clientAddress;
 SocketAddress serverAddress(SERVERIP, PORT);
 
int main (void){
    printf("TCP Client starting...\n");
    int net_stat;
#ifndef ROUTER
    net->disconnect();
    net_stat = net->set_network((SocketAddress)IP,(SocketAddress)MASK,(SocketAddress)GATEWAY);
    printf("set IP status: %i\n",net_stat);
#endif
    net_stat = net->connect();
    printf("connect status: %i\n",net_stat);
 
    SocketAddress ip; 
    net->get_ip_address(&ip);
    const char *p_ip = ip.get_ip_address();
    printf("IP address: %s and Port: %d\n", p_ip ? p_ip : "None" , PORT );
    SocketAddress mask;
    net->get_netmask(&mask);
    const char *p_mask = mask.get_ip_address();
    printf("Netmask: %s\n", p_mask ? p_mask : "None");
    SocketAddress gateway;
    net->get_gateway(&gateway);
    const char *p_gateway = gateway.get_ip_address();
    printf("Gateway: %s\n", p_gateway ? p_gateway : "None");
    
    int scount = 0;
    int rcount = 0;
    int dummy = 0;
    char sbuffer[100];
    char rbuffer[100];
    
    while (1) {
        if(client.open(net)== NSAPI_ERROR_OK ){
            if (client.connect(serverAddress) < 0) {
                printf("Failed to connect with server\n\r");
    
            }else{
                printf("Connected to server\n");
                int n = sprintf(sbuffer,"Test String with a dummy number - %d", dummy++);
                scount = client.send(sbuffer, n);
                printf("sent [%s] - %d bytes\n", sbuffer, scount);
                rcount = client.recv(rbuffer, sizeof rbuffer);
                printf("recv [%s] - %d bytes\n", rbuffer, rcount);
                client.close();
            }
        }else{
            printf("No Server\n");
            net->disconnect();
            printf("Program end\n");
            break;
        }
        led = !led;
        thread_sleep_for(5000);
    }
}

I also checked and tried your code.
Your part sent the string only first time and then was received error -3015, /*!< socket is already connected */ because you not closed the socket at the end of loop. Just add socket.close(); before the delay at the end of loop.
However I never received the error -3004 like you. It seems you have badly set an IP address or something similar.

Server console:

Server bound and listening
Client connected from IP address: 192.168.1.177
Received message from Client :'GET /checkPin.php?'
Sending echo to client
Client disconnected
Server bound and listening

Client console:

socket.open 0
connect 0
recv 27 [Echo - GET /checkPin.php?]

I hope it will help you.

BR, Jan

Hi Jan.
Thank you for your help.
With your example I have similar error (3004). If I loop connect - it is getting connected from the 2-nd time however send command failed anyway. Once I place router in between 2 mbeds - it works perfect. May be it is related to signal strength ?

I have no Idea I use old 5m long cross cable without an issue between two Nucleo boards (powered via USB). Your distance?

Short cable 1-2 m (CAT5). Can’t explain why it happening. Thank you for your support. I will post if would be able to make it work otherwise will use switch in between.
Regards,
Igor

Ok, just for sure, because sometimes people not see what they have before the face.

  • Do you have commented out the //#define ROUTER macro on server side?
  • The server is in the state "Server bound and listening"?
  • The leds of ethernet ports are lit?
  • Do you have Cross cable?
  • Do you have commented out the //#define ROUTER macro on server side?
    yes
  • The server is in the state "Server bound and listening" ?
    yes
  • The leds of ethernet ports are lit?
    yes
  • Do you have Cross cable?
    no. Regular Ethernet cable

Ok, the last chance was the cable but probably it is not the reason because modern Eth interfaces have a function called Auto MDI-X and that solve the problem around what cable user use.
According to this the board use the KSZ8081RNA which also has the Auto MDI-X and it is enabled in default.

Now I am out of ideas, sorry.

BR, Jan

the 100 MBit Ethernet has usually no automatic detection, you need to use a cross patch cable. Gigabit Ethernet does that all automatically, so adding a Gigabit Ethernetswitch should solve this problem also.

But from my understanding that is solved by the HP Auto MDI/MDI-X or ?

Page 17 section 3.6 of the datasheet KSZ8081RNA

HP Auto MDI/MDI-X
HP Auto MDI/MDI-X configuration eliminates the need to decide whether to use a straight cable or a crossover cable
between the KSZ8081RNA/RND and its link partner. This feature allows the KSZ8081RNA/RND to use either type of
cable to connect with a link partner that is in either MDI or MDI-X mode. The auto-sense function detects transmit and
receive pairs from the link partner and assigns transmit and receive pairs to the KSZ8081RNA/RND accordingly.
HP Auto MDI/MDI-X is enabled by default. It is disabled by writing a ‘1’ t

Got today cross over cable. Unfortunately it did not help. Looks like I have to use switch
Regards,
Igor