Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 8 of 8

Thread: Teensy 4.1 UDP and web server drop packets

  1. #1
    Member
    Join Date
    Jul 2016
    Location
    Denmark
    Posts
    31

    Teensy 4.1 UDP and web server drop packets

    Hello
    I am trying to receive UDP messages and run a web server at the same time using the new T4.1 and native ethernet
    the server works fine but it drops a lot of udp messages.
    udp also works fine by it self
    any ideas?

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    23,485
    Are you running the NativeEthernet and FNET that come with Teensyduino 1.53?

    Updates have fixed issues since 1.53. You can grab the latest code from github, or give 1.54-beta a try.

    https://forum.pjrc.com/threads/64235...no-1-54-Beta-3

  3. #3
    Member
    Join Date
    Jul 2016
    Location
    Denmark
    Posts
    31
    i am using the FNET that come with Teensyduino 1.53
    and NativeEthernet from github https://github.com/vjmuzik/NativeEthernet

    i will try the beta now

  4. #4
    Member
    Join Date
    Jul 2016
    Location
    Denmark
    Posts
    31
    I have now tried with 1.54-beta-3 (Arduino 1.8.13)
    both only using the the included library of NativeEthernet and FNET or the newest from github
    and i still have the same problem
    here is the test code
    Code:
    #include <NativeEthernet.h>
    #include <NativeEthernetUdp.h>
    
    
    // Enter a MAC address and IP address for your controller below.
    // The IP address will be dependent on your local network:
    byte mac[] = {
      0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
    };
    IPAddress ip(192, 168, 1, 177);
    
    unsigned int localPort = 8888;  
    
    // buffers for receiving and sending data
    char packetBuffer[UDP_TX_PACKET_MAX_SIZE];  // buffer to hold incoming packet,
    char ReplyBuffer[] = "acknowledged";        // a string to send back
    
    // An EthernetUDP instance to let us send and receive packets over UDP
    EthernetUDP Udp;
    
    
    // Initialize the Ethernet server library
    // with the IP address and port you want to use
    // (port 80 is default for HTTP):
    EthernetServer server(80);
    
    void setup() {
      
    
      // Open serial communications and wait for port to open:
      Serial.begin(9600);
      while (!Serial) {
        ; // wait for serial port to connect. Needed for native USB port only
      }
      Serial.println("Ethernet WebServer Example");
    
      // start the Ethernet connection and the server:
      Ethernet.begin(mac, ip);
    
      // Check for Ethernet hardware present
      if (Ethernet.hardwareStatus() == EthernetNoHardware) {
        Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
        while (true) {
          delay(1); // do nothing, no point running without Ethernet hardware
        }
      }
      if (Ethernet.linkStatus() == LinkOFF) {
        Serial.println("Ethernet cable is not connected.");
      }
    
      // start UDP
      Udp.begin(localPort);
    
      // start the server
      server.begin();
      Serial.print("server is at ");
      Serial.println(Ethernet.localIP());
    }
    
    
    void loop() {
    
     // if there's data available, read a packet
      int packetSize = Udp.parsePacket();
      if (packetSize) {
        Serial.print("Received packet of size ");
        Serial.println(packetSize);
        Serial.print("From ");
        IPAddress remote = Udp.remoteIP();
        for (int i=0; i < 4; i++) {
          Serial.print(remote[i], DEC);
          if (i < 3) {
            Serial.print(".");
          }
        }
        Serial.print(", port ");
        Serial.println(Udp.remotePort());
    
        // read the packet into packetBufffer
        Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
        Serial.println("Contents:");
        Serial.println(packetBuffer);
    
        // send a reply to the IP address and port that sent us the packet we received
        Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
        Udp.write(ReplyBuffer);
        Udp.endPacket();
      }
    
      
      // listen for incoming clients
      EthernetClient client = server.available();
      if (client) {
        Serial.println("new client");
        // an http request ends with a blank line
        boolean currentLineIsBlank = true;
        while (client.connected()) {
          if (client.available()) {
            char c = client.read();
            // if you've gotten to the end of the line (received a newline
            // character) and the line is blank, the http request has ended,
            // so you can send a reply
            if (c == '\n' && currentLineIsBlank) {
              // send a standard http response header
              client.println("HTTP/1.1 200 OK");
              client.println("Content-Type: text/html");
              client.println("Connection: close");  // the connection will be closed after completion of the response
              client.println("Refresh: 5");  // refresh the page automatically every 5 sec
              client.println();
              client.println("<!DOCTYPE HTML>");
              client.println("<html>");
              client.print("HEY ");
              client.println("<br />");
              client.println("</html>");
              break;
            }
            if (c == '\n') {
              // you're starting a new line
              currentLineIsBlank = true;
            } else if (c != '\r') {
              // you've gotten a character on the current line
              currentLineIsBlank = false;
            }
          }
        }
        // give the web browser time to receive the data
        delay(1);
        // close the connection:
        client.stop();
        Serial.println("client disconnected");
      }
    }
    it is just Ethernet examples WebServer and UDPStringSendRecive put together

  5. #5
    Senior Member vjmuzik's Avatar
    Join Date
    Apr 2017
    Posts
    731
    This does appear to be a bug, so far I can verify the message is being received by FNET, but I haven’t pinpointed yet where it’s being lost and why.

  6. #6
    Senior Member vjmuzik's Avatar
    Join Date
    Apr 2017
    Posts
    731
    Alrighty I found where the problem was happening, I was checking for an error in the wrong place so it was catching an exception from something else and dropping the packet as a result. I tested this with the webpage running for about 2000 packets and none of them were lost when I fixed the error compared to it being lost within 5-10 packets. Update is on GitHub.

  7. #7
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    23,485
    I've updated to the latest NativeEthernet here, so it will be in the beta4 installer (probably later this week).

  8. #8
    Member
    Join Date
    Jul 2016
    Location
    Denmark
    Posts
    31
    Thank you vjmuzik!
    i also tested with about 25k packets while refreshing the web page and nothing droped

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •