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

Thread: Teensy 4.1 Native Ethernet Sending Files from SD Card - Missing Bytes or Packets

  1. #1
    Junior Member
    Join Date
    Feb 2016
    Posts
    5

    Teensy 4.1 Native Ethernet Sending Files from SD Card - Missing Bytes or Packets

    Hi Everyone!

    I am having some problems when trying to send a file from the SD card over the network. This worked fine for a T3.6 and WIZ850io hardware module but not with the T4.1 and Native Ethernet. Any ideas or help is appreciated!

    Attached is some sample code I made to replicate the issue. I have a T4.1 with the original Ethernet kit from PJRC. Using Teensyduino 1.53 and Arduino IDE 1.8.9

    The sample code tries to pull an image off an SD card and transmit it over the network. Normally i parse the request for an image but for simplicity I made any request to port 80 return the image.

    To test use a T4.1 + Ethernet Kit from PJRC, add an image to an SD card called "engine.jpg" or change the name in the sketch to match your image name. Run the program and check the serial monitor for the IP. After entering the IP to a web browser the image should display on screen but i get missing or corrupt data.....

    Code:
    //Ethernet Includes
    #include <NativeEthernet.h>
    //SD Includes
    #include <SD.h>
    
    const int chipSelect = BUILTIN_SDCARD;  //SD Card T4.1
    
    //TCP Ethernet Variables
    #define REQ_BUF_SZ   256          //127
    
    EthernetServer server(80);        //HTTP Server Port
    EthernetClient client;            //HTTP Client 
    
    char HTTP_req[REQ_BUF_SZ] = {0};  // buffered HTTP request stored as null terminated string
    int req_index = 0;               // index into HTTP_req buffer
    
    byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  //Store in EEPROM
    
    
    void setup() {
      //Initialize Serial Communication
      Serial.begin(921600);
      //SD Card Setup
      Serial.print("Initializing SD card...");
      if (!SD.begin(chipSelect)) {
        Serial.println("Card failed, or not present");
        return; // don't do anything more:
      }
      Serial.println("card initialized.");
      delay(1000); //Random Delay
      
      //Setup Ethernet
      Ethernet.begin(mac);
      server.begin();           // start to listen for clients
    
      Serial.print("IP = ");
        Serial.println(Ethernet.localIP());
    }
    
    void loop() {
      //Maintain DHCP
      Ethernet.maintain();
      //Run Ethernet Loop
      eth_loop();
    }
    //File Sender
    void sendFile(EthernetClient client, char str[]){
      File outFile;
      int count = 0;
      outFile = SD.open(str);
        byte clientBuf[512];
        int clientCount = 0;
        while(outFile.available()){
            clientBuf[clientCount] = outFile.read();
            clientCount++;
            if (clientCount >= 511)
            {
              count += clientCount;
              client.write(clientBuf, clientCount);
              clientCount = 0;
              Serial.print("Sent bytes...");
              Serial.println(count);
            }
        }
        if (clientCount > 0) client.write(clientBuf, clientCount);
        outFile.close();
    }
    //JPEG Image Header
    void jpgHeader(EthernetClient client) {
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: image/jpg");
      client.println("Connection: close");
      client.println();
    }
    //HTTP Ethernet Loop
    void eth_loop()
    {
      EthernetClient client = server.available();  // try to get client
      //client.setConnectionTimeout(1000);  // set the timeout duration for client.connect() and client.stop()
      if (client) {  // got client?
        Serial.print("Connected..");
        //Serial.print(client.remoteIP());
        boolean currentLineIsBlank = true;
        while (client.connected()) {
          if (client.available()) {   // client data available to read
            char c = client.read(); // read 1 byte (character) from client
            Serial.print(c);
    
            // leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1)
            if (req_index < (REQ_BUF_SZ - 1)) {
              HTTP_req[req_index] = c;          // save HTTP request character
              req_index++;
            }
            // last line of client request is blank and ends with \n
            // respond to client only after last line received
            if (c == '\n' && currentLineIsBlank) {
    
              //Send File no matter what request for testing
              jpgHeader(client);
              sendFile(client, "engine.jpg");          
            }
            // every line of text received from the client ends with \r\n
            if (c == '\n') currentLineIsBlank = true;
            else if (c != '\r') currentLineIsBlank = false;
          }
        }
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
        Serial.println(" - Disconnected Client");
      }
    }
    Appreciate any help with this.

  2. #2
    Senior Member vjmuzik's Avatar
    Join Date
    Apr 2017
    Posts
    640
    Try updating to the latest version first, the one included with Teensyduino has an overflow bug: https://github.com/vjmuzik/NativeEthernet

  3. #3
    Junior Member
    Join Date
    Feb 2016
    Posts
    5
    That easy....Required updating both NativeEthernet and FNET, all is working well. Excellent job with this library! Thanks for your help!

Posting Permissions

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