6 years, 7 months ago.

EthernetInterface MBED-OS 5.6 static IP

Error -3004 is occured when I'm trying to establish socket connection. Board NUCLEO F767ZI. Please, help me^.^

include the mbed library with this snippet

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

EthernetInterface eth;
TCPSocket socket;

#define IP         "192.168.1.177"
#define GATEWAY    "192.168.1.1"
#define MASK       "255.255.255.0"

int main()
{
    printf("Example network-socket TCP Server\r\n");
    eth.disconnect();
    int i=eth.set_network(IP,MASK,GATEWAY);
    printf("set IP status: %i \r\n",i);
    i=eth.connect();
    printf("connect status: %i \r\n",i);
    const char *ip = eth.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) {
        socket.close();
        socket.open(&eth);

        socket.connect("93.175.29.245", 80);

        char sbuffer[] = "GET /checkPin.php?\r\n";
        int scount = socket.send(sbuffer, sizeof sbuffer);

        char rbuffer[64];
        int rcount = socket.recv(rbuffer, sizeof rbuffer);
        printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
    }
}

If I'm using dhcp, it works, but sometimes it can't get ip address and returns -3009.

2 Answers

5 years, 8 months ago.

I am here by searching the same problem solution, Error -3004 has occurred when I'm trying to establish a socket connection. I got here the absolute solution from Hudak. Thanks Hudak. Check out VOIP Cloud-Based Business Phone Service at http://www.cebodtelecom.com/

6 years, 7 months ago.

Hello Vladimir,

Static IP worked for me in this demo with MBED OS 5.6.1 after including (uncommenting) line #221 as below:

    ethernet.set_network("192.168.1.181","255.255.255.0","192.168.1.1");  // use static IP address, netmask, gateway

To make it work in your application, try to remove (or comment out) line #15:

    ...
    //eth.disconnect();
    ...

Thank you, your example works, but as a dhcp server. I need client (dhcp or static). Removing line #15 does not change anything.

posted by Vladimir Berzin 11 Oct 2017

That's strange. Below is what I could see on my PC's serial terminal when running your snippet on my NUCLEO-767ZI board

  • with eth.disconnect(); included:
[00]Example network-socket TCP Server
set IP status: 0 
connect status: 0 
IP address is: 192.168.1.177

MAC address is: 00:80:e1:38:00:25

recv -3004 [`[0E]]
recv -3004 [`[0E]]


  • after removing eth.disconnect();:
[00]Example network-socket TCP Server
set IP status: 0 
connect status: 0 
IP address is: 192.168.1.177

MAC address is: 00:80:e1:38:00:25

recv 1 [N[0E]]
recv 1 [N[0E]]
...


  • and when using DHCP to assign an IP address with a code modified as below:

...
int main()
{
    printf("Example network-socket TCP Server\r\n");
    //eth.disconnect();
    //int i=eth.set_network(IP,MASK,GATEWAY);
    //printf("set IP status: %i \r\n",i);
    int i=eth.connect();
...
Example network-socket TCP Server
connect status: 0 
IP address is: 192.168.1.54

MAC address is: 00:80:e1:38:00:25

recv 1 [N[0E]]
recv 1 [N[0E]]
...
posted by Zoltan Hudak 11 Oct 2017

I updated st-link firmware and had it worked today! And with eth.disconnect() too. But sometimes I have error -3004 even without eth.disconnect(). I try to reset controller when this is happening.

posted by Vladimir Berzin 11 Oct 2017

Have you found problem caused -3004 Error? Also can use mbed-os 5.5.4 version with Static IP. Got working UDP static IP communication with that version.

posted by Daniel Klioc 18 Oct 2017