Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By rbellini
#57309 Hi, my first post so please bear with me . . .

I am working on a Web cam using the Adafruit HUZZAH ESP8266. I am using the TTL Serial JPEG Camera to take an image. The web page initially sends a response with an <img> tag that then triggers the handle_picture function. This should simply send a jpeg file to the browser.

The problem I am having appears to be with the server.streamFile function. In the snippet of code below it appears that the function does not read and send the entire file.

Code: Select allvoid handle_picture() {
  byte clientBuf[64];
  char filename[13];
  strcpy(filename, "IMAGE01.JPG");
  filename[5] = '0' + FileNum / 10;
  filename[6] = '0' + FileNum % 10;
  int32_t time = millis();
  // strcpy(filename, "/test1.jpg");  // DEBUG only
  f = SPIFFS.open(filename, "r");
  if (!f) {
    Serial.print("file open failed: ");
    Serial.println(filename);
  } else {
    Serial.print("file opened: ");
    Serial.println(filename);
  }
  int filesize = f.size();
  Serial.print("File size ");
  Serial.println(filesize); //For debugging only
  String WebString = "";
  WebString += "HTTP/1.1 200 OK\r\n";
  WebString += "Content-Type: image/jpeg\r\n";
  WebString += "Content-Length: " + String(filesize) + "\r\n";
  WebString += "\r\n";
  server.sendContent(WebString);
  Serial.println (WebString);
  ESP.wdtFeed();
  count = 0;
  size_t sent = server.streamFile(f,"image/jpeg");
 
  Serial.print(sent);
  Serial.println(" Bytes sent!");
  f.close();
  time = millis() - time;
  Serial.print(time); Serial.println(" ms elapsed");
}// END handle_picture



As you can see, the output shows a file of 45012 bytes however, the streamFile only sends 4380 bytes!

Code: Select allImage size: 640x480
HTTP server started
Picture taken!
File number = 6
Filename: IMAGE06.JPG
Storing 45012 byte image.
...........................................*
File stored!
24623 ms elapsed
170853 mS Elasped for snapshot.
file opened: IMAGE06.JPG
File size 45012
HTTP/1.1 200 OK
Content-Type: image/jpeg
Content-Length: 45012


4380 Bytes sent!
43 ms elapsed



I have looked thru many of the related posts however, I could not find a solution to similar posts / problems. I would appreciate any solutions / advice!
User avatar
By martinayotte
#57331 Apparently, the 4380 number is 3 times MTU (3x1460), so, server.streamFile() probably broken during refactoring done on June 23 https://github.com/esp8266/Arduino/comm ... 51284f2240

In the mean time a fix can be apply, you can still use your own writing loop instead of server.streamFile() such :

Code: Select all    char buf[1024];
    int siz = f.size();
    while(siz > 0) {
      size_t len = std::min((int)(sizeof(buf) - 1), siz);
      f.read((uint8_t *)buf, len);
      server.client().write((const char*)buf, len);
      siz -= len;
    }
User avatar
By linh.truc.esp
#67591
martinayotte wrote:Apparently, the 4380 number is 3 times MTU (3x1460), so, server.streamFile() probably broken during refactoring done on June 23 https://github.com/esp8266/Arduino/comm ... 51284f2240

In the mean time a fix can be apply, you can still use your own writing loop instead of server.streamFile() such :

Code: Select all    char buf[1024];
    int siz = f.size();
    while(siz > 0) {
      size_t len = std::min((int)(sizeof(buf) - 1), siz);
      f.read((uint8_t *)buf, len);
      server.client().write((const char*)buf, len);
      siz -= len;
    }


Thank you sir, now I understand *.streamFile() mean is.