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]);
}
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]);
}