Teensy 4.1 Failed to get IP address from DHCP

sinusco

New member
I have a teensy 4.1 with the pjrc rj45 connector connected to my computer through a netgear smart switch.
I've managed do pass OSC messages from the teensy to my computer by giving the teensy a static ip address, but i'd like for it to have a dynamic ip address, so that i can just plug and play. I have tried with the QNEthernet library as well as with the NativeEthernet one, but no luck. I'm also not sure if I understand comletely how this works…

I was hoping someone here could help me out with this, or point me in the right direction, as i've been going at it unsuccessfully for the past few hours.
Using this code i found on this forum by @shawn i get the following in my serial:

Starting Ethernet with DHCP...
Failed to get IP address from DHCP

C++:
#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() {
}


and with the following code i get:

Starting Ethernet…
Failed to obtain IP via DHCP
Teensy IP: 0.0.0.0


C++:
#include <QNEthernet.h>
#include <OSCMessage.h>

using namespace qindesign::network;

EthernetUDP udp;

const uint16_t destPort = 9000;

const uint32_t sendInterval = 33;
uint32_t lastSend = 0;

void setup() {
  Serial.begin(115200);
  while (!Serial && millis() < 3000) {}

  Serial.println("Starting Ethernet…");

  Ethernet.begin();
  if (!Ethernet.waitForLocalIP(5000)) {
    Serial.println("Failed to obtain IP via DHCP");
  }

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

  udp.begin(0);

  analogReadResolution(10);
  analogReadAveraging(16);
}

void loop() {
  uint32_t now = millis();
  if (now - lastSend < sendInterval) return;
  lastSend = now;

  int potValue = analogRead(A0);

  OSCMessage msg("/pot/1");
  msg.add((int32_t)potValue);

  IPAddress bcast = Ethernet.broadcastIP();

  udp.beginPacket(bcast, destPort);
  msg.send(udp);
  udp.endPacket();
  msg.empty();
}
 
Last edited:
There has to be a DHCP server somewhere on the network for the Teensy to get an address from. A switch doesn't include a DHCP server, it just forwards packets between machines.
 
Thank you, that was it!
switch doesn't include a DHCP server
The problem was that I had set <sharing > internet sharing > share your connection from> in my macbook settings as "USB LAN", while it had to be sharing from Wi-Fi, so that my mac hands out the ip's. don't understand why that is, but now it works.
 
Back
Top