Go Down

Topic: Wifi Shield client HTTP POST binary data not working (Read 143 times) previous topic - next topic


I'm working on a project where I want to upload a picture taken with the Adafruit TTL camera to a webserver via the Wifi connection.

The camera: https://learn.adafruit.com/ttl-serial-camera/using-the-camera

I have followed this short tutorial to upload the picture and modified where necessary: https://www.openhomeautomation.net/wireless-camera/

Connecting to the wifi network and posting data (parameters like a=1&b=2) to the webserver works fine.
Taking pictures with the cam also works.
But uploading a 3.5kB picture doesn't seem to work. I tested with several number of bytes 16, 32 and 64 and with different delays 0, 100, 300ms between writes. (see code)

Anyone has advice on this? Or can provide example code?

My setup:
Arduino Uno
Arduino Wifi Shield
TTL camera

Code to post the image:

Code: [Select]
uint16_t jpglen = cam.frameLength();
  Serial.print(F("Storing "));
  Serial.print(jpglen, DEC);
  Serial.print(F(" byte image."));
  // Prepare request
  String start_request = "";
  String end_request = "";
  start_request = start_request + "\n" + "--AaB03x" + "\n" + "Content-Disposition: form-data; name=\"picture\"; filename=\"CAM.JPG\"" + "\n" + "Content-Type: image/jpeg" + "\n" + "Content-Transfer-Encoding: binary" + "\n" + "\n"; 
  end_request = end_request + "\n" + "--AaB03x--" + "\n";
  uint16_t extra_length;
  extra_length = start_request.length() + end_request.length();
  Serial.println(F("Extra length:"));
  uint16_t len = jpglen + extra_length;
   Serial.println(F("Content-Type: multipart/form-data; boundary=AaB03x"));
   Serial.print(F("Content-Length: "));
   Serial.print(F("binary data"));
  Serial.println(F("Starting connection to server..."));
  // Connect to the server, please change your IP address !
  if (client.connected()) {
      Serial.println(F("Connected !"));
      client.println(F("POST /api/upload.php HTTP/1.1"));
      client.println(F("Host: www.****.com"));
      client.println(F("Content-Type: multipart/form-data; boundary=AaB03x"));
      client.print(F("Content-Length: "));
  ///    client.print(start_request);
     client.print("\r\n--AaB03x\r\nContent-Disposition: form-data; name=\"picture\"; filename=\"CAM.JPG\"\r\nContent-Type: image/jpeg\r\nContent-Transfer-Encoding: binary\r\n\r\n");       
      // Read all the data up to # bytes!
      byte wCount = 0; // For counting # of writes
      while (jpglen > 0) {
        uint8_t *buffer;
        uint8_t bytesToRead = min(16, jpglen); // change 32 to 64 for a speedup but may not work with all setups!
        buffer = cam.readPicture(bytesToRead);
        client.write(buffer, bytesToRead);
        if(++wCount >= 64) { // Every 2K, give a little feedback so it doesn't appear locked up
          wCount = 0;
        jpglen -= bytesToRead;
  Serial.println(F("Transmission over"));

Upload code (upload.php) on webserver is never called (I check via fwrite in the file).

Code: [Select]

$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['picture']['name']);
if(move_uploaded_file($_FILES['picture']['tmp_name'], $target_path)) {
 echo "The file ". basename( $_FILES['picture']['name'])." has been uploaded";
else {
 echo "There was an error uploading the file, please try again!";

Thank you for your time reading this :-)


After a few more hours of debugging and checking my raw webserver logs I noticed that the Content-Length parameter was not correct.

If anyone encounters the same problem I can give you advice.

Go Up