Teensy 4.1 NativeEthernet fixed address problem

Status
Not open for further replies.

KD0RC

Well-known member
I have a T4.1 and it works great if I am connected to a router at home. It gets a good DHCP served IP address and connects to the server (a Flex radio). Traffic passes bidirectionally, and life is good. If I take it on the road and operate with everything connected via a switch, but no DHCP server, I cannot give it an IP and have it work. In the following code, hooked to DHCP, getIpAddress() works great. Without DHCP, it properly flows to getFixedIpAddress() and gives me an address if I use Ethernet.setLocalIP(ip). That address looks good and I get a connect to the server and a little data back, then it just stops like the socket times out or something. Ethernet.begin in any form returns 0.0.0.0, so I tried the setLocalIP(ip) to try to make it work.

I have the same code compiled for an Arduino Due with the Ethernet Shield using Ethernet.h and it works great at home or away (i.e. getFixedIpAddress() works in the other lib and I don't need setLocalIP(ip)). I am not sure if that implies that there is a bug in NativeEthernet, or if I am just using it wrong.


So I feel like there is something that I should be doing or not doing to make this work. Anyone have any insights into this?



/*********************** getIpAddress *********************/
void getIpAddress()
{
// start the Ethernet connection:
Serial.println("=== Waiting for IP address");

teensyMAC(mac); // Get MAC address from Teensy

TimeIt = millis();
while (Ethernet.begin(mac) == 0)
{
if(millis() - TimeIt > 10000) // Five seconds to get a DHCP served address
{
Serial.println("Failed to configure Ethernet using DHCP");
getFixedIpAddress();
break;
}
}

addr=String(Ethernet.localIP()[0]) + "." +
String(Ethernet.localIP()[1]) + "." +
String(Ethernet.localIP()[2]) + "." +
String(Ethernet.localIP()[3]);

Serial.print("IP address is: "); Serial.println(Ethernet.localIP());
}

/*********************** getFixedIpAddress *********************/
void getFixedIpAddress()
{
IPAddress ip(169, 254, 0, 25);
IPAddress gateway(169, 254, 0, 1);
IPAddress dns(169, 254, 27, 252);
IPAddress subnet(255, 255, 0, 0);

Serial.println(ip);
Serial.println(dns);
Serial.println(gateway);
Serial.println(subnet);

// start the Ethernet connection:
Serial.println("=== Setting fixed IP address");

Ethernet.setLocalIP(ip); // Lets me get an address, connection to server works for a min, then fails
Ethernet.setSubnetMask(subnet);
Ethernet.setGatewayIP(gateway);
Ethernet.setDnsServerIP(dns);

// Ethernet.begin(mac, ip, dns, gateway, subnet); // None of these work - always return 0.0.0.0
// Ethernet.begin(mac, ip, dns);
// Ethernet.begin(mac, ip);

Serial.print("IP address is: ");
Serial.println(Ethernet.localIP());

} // end getFixedIpAddress

/*********************** teensyMAC *********************/
void teensyMAC(uint8_t *mac)
{
for(uint8_t by=0; by<2; by++) mac[by]=(HW_OCOTP_MAC1 >> ((1-by)*8)) & 0xFF;
for(uint8_t by=0; by<4; by++) mac[by+2]=(HW_OCOTP_MAC0 >> ((3-by)*8)) & 0xFF;
Serial.printf("MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}
 
Your code doesn't compile. A lot of variables are not declared. You've probably left out a lot of declarations from the beginning of the file.

Pete
 
Hi Pete, this is part of thousands of lines of code. Let me see if I can put something together that is short but will compile and illustrates the issue.

Thanks,
Len
 
Here is version that will compile:

Code:
// Test of NativeEthernet - trying to get a good ip address

#include "NativeEthernet.h"

#define BAUD_RATE 115200

String addr;
byte mac[] = {0xA8, 0x61, 0x0A, 0xAE, 0x5D, 0xAE};
unsigned long TimeIt;

void setup() 
{
  

}

void loop() 
{


}

/*********************** getIpAddress *********************/
void getIpAddress() 
{
  // start the Ethernet connection:
  Serial.println("=== Waiting for IP address");

  teensyMAC(mac); // Get MAC address from Teensy

  TimeIt = millis();
  while (Ethernet.begin(mac) == 0) 
  {
    if(millis() - TimeIt > 10000)  // Five seconds to get a DHCP served address
    {
      Serial.println("Failed to configure Ethernet using DHCP");
      getFixedIpAddress();
      break;
    }
  }

  addr=String(Ethernet.localIP()[0]) + "." +
       String(Ethernet.localIP()[1]) + "." +
       String(Ethernet.localIP()[2]) + "." +
       String(Ethernet.localIP()[3]);

  Serial.print("IP address is: "); Serial.println(Ethernet.localIP());
}

/*********************** getFixedIpAddress *********************/
void getFixedIpAddress() 
{
  IPAddress ip(169, 254, 0, 25);
  IPAddress gateway(169, 254, 0, 1);
  IPAddress dns(169, 254, 27, 252);
  IPAddress subnet(255, 255, 0, 0);
  
  Serial.println(ip);
  Serial.println(dns);
  Serial.println(gateway);
  Serial.println(subnet);
  
  // start the Ethernet connection:
  Serial.println("=== Setting fixed IP address");

  Ethernet.setLocalIP(ip);  // Won't get an address without this...
  Ethernet.setSubnetMask(subnet);
  Ethernet.setGatewayIP(gateway);
  Ethernet.setDnsServerIP(dns);

//  Ethernet.begin(mac, ip, dns, gateway, subnet);  // None of these work - always return 0.0.0.0
//  Ethernet.begin(mac, ip, dns);
//  Ethernet.begin(mac, ip);

  Serial.print("IP address is: ");
  Serial.println(Ethernet.localIP());

} // end getFixedIpAddress

/*********************** teensyMAC *********************/
void teensyMAC(uint8_t *mac) 
{
    for(uint8_t by=0; by<2; by++) mac[by]=(HW_OCOTP_MAC1 >> ((1-by)*8)) & 0xFF;
    for(uint8_t by=0; by<4; by++) mac[by+2]=(HW_OCOTP_MAC0 >> ((3-by)*8)) & 0xFF;
    Serial.printf("MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}
 
Last edited:
The problem is solved... I can't call Ethernet.begin() looking for a DHCP served IP address, fail, then call it again setting a fixed IP address. @Rya asked the same question in another thread and was told this by @VJMUZIK. It would be nice if NativeEthernet would allow this (or is there a way to close and start over with .begin?). I currently get around this by coding a parm on my SD card depending on whether I have a DHCP server or not. I would prefer to have this be more automatic, but at least I can now connect at home or on the road with a quick edit of the SD card.
Thanks,
Len
 
I’ll look into adding this as well in the next week and make sure it doesn’t break anything, if there are no problems I’ll allow this since it works with the original shield.
 
Hello everyone :)
Thank you vj for the NativeEthernet library. I sadly encounter the same problem with the begin function even with the new library files. It alwys sets the IP to 0.0.0.0 if I call the begin function again. Is there any workaround or something I should definetly check?
 
Status
Not open for further replies.
Back
Top