7 years, 4 months ago.

EthernetInterface MBED-OS 5.3 static IP

It appears there is still no way to set a static IP in the Ethernet Interface. My application must be able to set a static IP, is there anyway to fix this? I just updated to the very latest MBED-OS 5.3 and would hate to go back to MBED 2 to regain this feature. I am using the FRDM-k64 board.

After digging through the github repository I figured out how to set a static IP address. Unfortunately I can't seem to create a TCP Server now (This worked when using DHCP).

My Code:

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

EthernetInterface eth;
TCPSocket socket;
TCPServer tcpserver;
RawSerial dbug(PTB17, PTB16); 
#define IP         "192.168.1.100"
#define GATEWAY    "192.168.1.1"
#define MASK       "255.255.255.0"
int main()
{
    dbug.baud(115200);
    dbug.printf("Example network-socket TCP Server\r\n");
    eth.disconnect();
    int i=eth.set_network(IP,MASK,GATEWAY);
    dbug.printf("set IP status: %i \r\n",i);
    i=eth.connect();
    dbug.printf("connect status: %i \r\n",i);
    const char *ip = eth.get_ip_address();
    const char *mac = eth.get_mac_address();
    dbug.printf("IP address is: %s\n\r", ip ? ip : "No IP");
    dbug.printf("MAC address is: %s\n\r", mac ? mac : "No MAC");
    SocketAddress sockaddr;
    i=tcpserver.open(&eth);
    if (i<0) 
    {
        dbug.printf("open error: %i \r\n",i);
        while(true) {}
    }
    dbug.printf("server open\r\n");
    i=tcpserver.bind(ip,23);
    if (i<0) 
    {
        dbug.printf("bind error: %i \r\n",i);
        while(true) {}
    }
    dbug.printf("Bound\r\n");
    if (tcpserver.listen()<0) dbug.printf("listen error\r\n");
    dbug.printf("Listening\r\n");
    i = tcpserver.accept(&socket);
    if (i<0) dbug.printf("accept error: %i \r\n",i);
    dbug.printf("Socket Accepted\r\n");
}

This is what I get on the debug terminal:

Example network-socket TCP Server
set IP status: 0
connect status: 0
IP address is: 192.168.1.100
MAC address is: 8a:42:7d:87:0c:b7
open error: -3004

Error -3004 is "not connected to a network"
However I can ping the module on it's assigned IP address and it responds to the ping so we know the network is indeed connected.

posted by Dan Sexton 31 Dec 2016

2 Answers

7 years, 3 months ago.

Static IP addresses were re-included around mbed-os-5.2 with the set_network function that Dan is correctly using.

Unfortunately, when IPv6 support was added to the EthernetInterface, a bug was introduced that prevented the network from working with static IPv4 addresses. This left the API in the worst state: disfunctional and misleading.

Sorry about the trouble this caused, the issue is being tracked with a fix up on the mbed-os repo:
https://github.com/ARMmbed/mbed-os/pull/3526

This static IPv4 addresses should be usable again soon.

Accepted Answer

Thanks Chris!

Andrea, team mbed

posted by Andrea Corrado 05 Jan 2017

Hello,

Is it usable again? I'm having problems using static IP. How can I check if I've the newest working version?

posted by Robin Moons 04 Apr 2017

Hi,

Static IP or IP via DHCP are acting same for me, I get an error -3004 after one successful transmission (either using UDP or TCP socket - no errors from socket), Any ideas, updates?

posted by Jam B 06 Apr 2017

Dear Christopher,

are there any updates regarding static IP in newest working version?

Cheers!

posted by Ema Zubac 11 Jul 2017

Hello,

Have got working static IP with Nucleo F767.

Daniel

posted by Daniel Klioc 25 Jul 2017
7 years, 4 months ago.

This has been an issue since 5.x was released. Not sure why it was not included. So far no announcements of static IP being re-included. Mbed? Anyone? Bueller?