Trouble with UDP Communication from PC to Teensy 4.1 via Ethernet

Jipsy

New member
Hello everyone,

I'm currently working on a project that involves sending DMX commands using Artnet from a Raspberry Pi to a Teensy 4.1 board. My immediate challenge is establishing a basic UDP connection, which seems to be malfunctioning at the moment.

To start simple, my goal was to send a string from my PC to the Teensy through an Ethernet connection. Despite using both NativeEthernet and QNEthernet libraries, I have been unsuccessful in my attempts.

Here's the outline of my current setup:

Teensy Code Status: The code is running on the Teensy, and it's indicating "LINK ON," which suggests that the hardware connection is active.
Sending Tool: I am utilizing Packet Sender to transmit a string to the Teensy, but there's no response or sign of receipt.
Additional Attempts: Numerous other example codes have been tried out to no avail; it appears that the Teensy is not receiving any communication from the PC.
Given these points, I’m reaching out to see if anyone might spot an obvious error in my approach or if someone could guide me on how to effectively troubleshoot this issue.

I am including a snippet of the code running on my Teensy below:

Code:
#include <QNEthernet.h>
using namespace qindesign::network;
constexpr bool kStartWithDHCP = false;
IPAddress staticIP{192, 168, 1, 101};
IPAddress subnetMask{255, 255, 255, 0};
IPAddress gateway{192, 168, 0, 1};
IPAddress dnsServer = gateway;
// The link timeout, in milliseconds. Set to zero to not wait and
// instead rely on the listener to inform us of a link.
constexpr uint32_t kLinkTimeout = 5000;  // 5 seconds

EthernetServer server(5005);       // telnet by default, the port is used
boolean alreadyConnected = false;  // regardless of whether the client was connected earlier or not

void setup() {
  Serial.begin(115200);
  while (!Serial && millis() < 10000 ) ; // wait for serial monitor
  Serial.println("\n" __FILE__ " " __DATE__ " " __TIME__);
  if ( CrashReport ) Serial.print( CrashReport );

  if ( !kStartWithDHCP ) {
    Serial.printf("Starting Ethernet with static IP...\r\n");
    Ethernet.setDNSServerIP(dnsServer);  // Set first so that the
    // listener sees it
    Ethernet.begin(staticIP, subnetMask, gateway);

    // When setting a static IP, the address is changed immediately,
    // but the link may not be up; optionally wait for the link here
    if (kLinkTimeout > 0) {
      if (!Ethernet.waitForLink(kLinkTimeout)) {
        printf("Warning: No link detected\r\n");
        // We may still see a link later, after the timeout, so
        // continue instead of returning
      }
      else {
        IPAddress ip = Ethernet.localIP();
        if (ip != INADDR_NONE) {
          IPAddress subnet = Ethernet.subnetMask();
          Serial.printf("Local IP: %u.%u.%u.%u\r\n", ip[0], ip[1], ip[2], ip[3], subnet[0], subnet[1], subnet[2], subnet[3]);
        }
      }
    }
  }
  else {
    Serial.printf("Starting Ethernet with DHCP...\r\n"); // Initialize Ethernet, in this case with DHCP
    if (!Ethernet.begin()) {
      Serial.printf("Failed to start Ethernet\r\n");
      return;
    }
  }
  Ethernet.onLinkState([](bool state) {
    Serial.printf("Link %s\r\n", state ? "ON" : "OFF");
  }); // setup callback
  Ethernet.onAddressChanged([]() {
    IPAddress ip = Ethernet.localIP();
    bool hasIP = (ip != INADDR_NONE);
    if (hasIP) {
      IPAddress subnet = Ethernet.subnetMask();
      Serial.printf("Local IP: %u.%u.%u.%u\r\n", ip[0], ip[1], ip[2], ip[3], subnet[0], subnet[1], subnet[2], subnet[3]);
    } else {
      Serial.printf("Address changed: No IP address\r\n");
    }
  }); // setup callback
  //Udp.begin(localPort);
  server.begin();  // start listening to customers
}

void loop() {
  EthernetClient client = server.available();  // we are waiting for a new client:
  if (client) {
    int iCnt = client.available();
    Serial.printf( "\n>>>>>\tclient.available chars = %d\n", iCnt );
    while (client.available() > 0) {  // reads bytes coming from the client:
      char thisChar = client.read();
      server.write(thisChar);  // pass the bytes back to the client:
      Serial.write(thisChar);
    }
    Serial.printf( "\n\tclient.available chars = %d\t<<<<<\n", iCnt );
  }
}

Skærmbillede 2023-11-05 211539.png
Skærmbillede 2023-11-05 211532.jpg

Here are some specifics:

Teensy version: 4.1
Libraries used: NativeEthernet/QNEthernet
PC Software: Packet Sender
Any guidance, insights, or resources you could provide would be immensely appreciated. I'm eager to learn from your expertise and finally get this communication up and running.

Thank you in advance!
 
Some code notes:
1. Add the listeners before starting and setting up Ethernet. This way, they’ll catch all events, including setting the static IP.
2. There’s extraneous arguments in the printf line that prints the local IP.
3. For code simplicity, you can use printf() instead of Serial.printf(), if you like.

Simple diagnostic questions to get them out of the way:
1. Are the IP address and subnet mask correct for your system?
2. Is everything connected to the same network your PC is on?

Note: If I'm recalling correctly, I believe you either had a slightly different post or another post, and I responded to the second. But I'm not seeing it, so maybe it was lost in the Big Forum Crash of '23.
 
Last edited:
Back
Top