Teensy 4.1 with Ethernet - IP. Hostname, DHCP questions

Burtamus

Member
I'm using the NativeEthernet.h library.

Found the following in the Forums. Thanks Guys!!

Code:
byte mac[6];

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

void setup() {
    teensyMAC(mac);

    if (Ethernet.begin(mac) == 0) {
        Serial.println("Failed to configure Ethernet using DHCP");
        // Check for Ethernet hardware present
        if (Ethernet.hardwareStatus() == EthernetNoHardware) {
            Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
        } else if (Ethernet.linkStatus() == LinkOFF) {
            Serial.println("Ethernet cable is not connected.");
        }
        // no point in carrying on, so do nothing forevermore:
        Serial.println("Abnormal End...");
        while (true) {
            delay(1);
        }
    } else {
        Serial.print("IP address: ");
        Serial.println(Ethernet.localIP());
    }
}

Serial Monitor says my IP is 0.0.0.0. When I look at my router/DHCP registrations, it has "unknown" for the hostname and 192.168.0.66 associated with the proper MAC address. I can also ping .66 from my local computer successfully.

How can I provide it a hostname so it can update my DNS? How can I return the proper IP address given to me by DHCP?
Thanks, Burt
 
Well, I just re-uploaded the sketch and the IP is now showing correctly in Serial Monitor. Go figure! Still need to work on the hostname issue.

Thanks, Burt
 
QNEthernet has a "set DHCP option 12" feature that can set the hostname. See `Ethernet.setHostname()`. Note that there's also mDNS features where you can set a network-local name. For mDNS, see the OSCPrinter example (the name is set to "osc-example" there).

I like to use listeners for my programs that react to changes instead of checking everything once at startup — see the ServerWithListeners example — but here's an example that approximates your code (and yes, I'm using C++ features, but please adapt to your liking):

Code:
#include <QNEthernet.h>

using namespace qindesign::network;

constexpr uint32_t kDHCPTimeout = 10000;  // 10 seconds
constexpr char kHostname[]{"MyHostname"};

void setup() {
  Serial.begin(115200);
  while (!Serial && millis() < 4000) {
    // Wait for Serial connection or a timeout
  }

  uint8_t mac[6];
  Ethernet.macAddress(mac);  // This is informative; it retrieves, not sets
  Serial.printf("MAC = %02x:%02x:%02x:%02x:%02x:%02x\r\n",
                mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);

  Ethernet.setHostname(kHostname);  // Set this before starting DHCP

  Serial.println("Starting Ethernet with DHCP...");
  if (!Ethernet.begin()) {
    Serial.println("Failed to start Ethernet");
    return;
  }
  if (!Ethernet.waitForLocalIP(kDHCPTimeout)) {
    Serial.println("Failed to get IP address from DHCP");
    return;
  }

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

void loop() {
}
 
@shawn,
This worked perfectly. Thank You! I'll definitely take a look at ServerWithListeners as that is my end goal. One main server and lots of little clients.
 
See also the AppWithListenersTemplate example for a variety of things you can do for setup in a basic app. (Note that not all of the code is meant to be included, it’s just illustrative.)
 
Back
Top