Go Down

Topic: Send jpg file to webserver via MultiPart (Read 164 times) previous topic - next topic

AndreasGabler

Hi all,

I'm currently implementing a weather station for our model flying club.

I'm using a
- Arduino MEGA
- weatherPiArduino shield with their weather station
- cc3000 Wifi shield with SD card
- two Arducam mini with OV5642.

There are three major parts of my programme

1) the two cameras take jpg images and store them to a SD card
2) Weather data is collected and directly posted to a MySQL database on our webserver
3) The jpg files are uploaded to the website to be displayed (every 20 minutes as long as light is available)

As we have no fixed line internet at the club house, we are using a 3G wires router to connect to the internet.

So far so good.

Item 1 and 2 are done and working fine. But I have my problems with item 3.

I have searched the web and found some examples, but I could not get them working.

I get the message that the file was sent, but I cannot see the file on the webserver. If I use the same php on the server using a html file to POST the jpg file is uploaded.


Please see the sketch and the PHP below, maybe you can see what is going wrong here.

Many thanks!!


Code: [Select]

#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"
#include<stdlib.h>
#include <SD.h>

// Define CC3000 chip pins
#define ADAFRUIT_CC3000_IRQ 3
#define ADAFRUIT_CC3000_VBAT 5
#define ADAFRUIT_CC3000_CS 10

// WiFi network (change with your settings !)
#define WLAN_SSID "mywirelessnetwork"
#define WLAN_PASS "mypassword"
#define WLAN_SECURITY WLAN_SEC_WPA2


// Create CC3000 instances
// WIFi setup
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,SPI_CLOCK_DIV2);
Adafruit_CC3000_Client client;

char filename[14] = "CAM1.jpg";
int i = 0;
int keyIndex = 1;
int port = 80; //Server Port
uint32_t MANip = 0;

void setup() {

  Serial.begin(115200);

  // Initialise the module
  Serial.println(F("\nInitializing..."));
  if (!cc3000.begin()){
    Serial.println(F("Couldn't begin()! Check your wiring?"));
    while(1);
  }


  // Connect to WiFi network
  cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
  Serial.println(F("Connected!"));

  // Display connection details
  Serial.println(F("Request DHCP"));
  while (!cc3000.checkDHCP()){
    delay(100);
  }
  Serial.println("Ready");
 Serial.println("-------");

 //Get MAN IP address

  getMANip();


  // Prepare HTTP request
  String start_request = "";
  String end_request = "";
  start_request = start_request + "\n" + "--AaB03x" + "\n" + "Content-Disposition: form-data; name=\"fileToUpload\"; filename="+filename+"\n" + "Content-Type: file" + "\n" + "Content-Transfer-Encoding: binary" + "\n" + "\n";
  end_request = end_request + "\n" + "--AaB03x--" + "\n";



  //Initialise SD Card
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization of SD done.");
 
 File myFile = SD.open("CAM1.jpg");
  uint16_t jpglen = myFile.size();
  uint16_t extra_length;
  extra_length = start_request.length() + end_request.length();
  uint16_t len = jpglen + extra_length;

  // Set up TCP connection with web server
  client = cc3000.connectTCP(MANip, port);


 
  if (client.connected()) {
    Serial.println("Start uploading...");

    client.println(F("POST /~modelavi/plugins/upload.php HTTP/1.1"));

    client.println(F("Host: www.modelaviationnorthland.co.nz"));
    client.println(F("Content-Type: multipart/form-data; boundary=AaB03x"));
    client.print(F("Content-Length: "));
    client.println(len);
    client.println(start_request);

    Serial.println(F("Host: www.modelaviationnorthland.co.nz"));
    Serial.println(F("Content-Type: multipart/form-data; boundary=AaB03x"));
    Serial.print(F("Content-Length: "));
    Serial.println(len);
    Serial.println(start_request);
 
 if (myFile) {
  byte clientBuf[32];
  int clientCount = 0;

  while(myFile.available())
  {
    clientBuf[clientCount] = myFile.read();
    clientCount++;

    if(clientCount > 31)
    {
      client.write(clientBuf,32);
      clientCount = 0;
    }
  }
  if(clientCount > 0) client.write(clientBuf,clientCount);

  client.print(end_request);
  client.println();
  }
  else{
       Serial.println("File not found");
      }
  }   
  else{
    Serial.println("Web server connection failed");
  }
 
  myFile.close(); 
  client.close();
 
  Serial.println("done...");

 
}

void loop() {



delay(10000);

}


void getMANip(){
#ifndef CC3000_TINY_DRIVER
  /* Try looking up www.modelaviationnorthland.co.nz */
    //Serial.print(F("www.modelaviationnorthland.co.nz -> "));
  while  (MANip  ==  0)  {
    if  (!  cc3000.getHostByName("www.modelaviationnorthland.co.nz", &MANip))  {
      //Serial.println(F("Couldn't resolve!"));
    }
    delay(500);
  } 
  //cc3000.printIPdotsRev(MANip);
#endif
}


PHP file
Code: [Select]

<?php
$target_dir 
"/home/modelavi/public_html/images/weathercam/";
$target_file $target_dir basename($_FILES["fileToUpload"]["name"]);
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file); 
echo(
"Done");
?>





Cheers
Andreas

PaulS

What is that PHP file supposed to do with the POSTed data? Nothing that I can see...

AndreasGabler

Hi Paul,
The posted file gets moved from temp to the target folder with the move_uploaded_file() function.

PaulS

Quote
The posted file gets moved from temp to the target folder with the move_uploaded_file() function.
Is that a standard function? I don't recognize it.

How do the files get in the temp folder? I don't see anything that makes use of the POSTed data.

AndreasGabler

Hi Paul,
have a look here.

http://www.w3schools.com/php/php_file_upload.asp

When you remove the checks, that's what is left.

<?php
$target_dir = "/home/modelavi/public_html/images/weathercam/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
?>

macoy-marc

Hi all,

I'm currently implementing a weather station for our model flying club.

I'm using a
- Arduino MEGA
- weatherPiArduino shield with their weather station
- cc3000 Wifi shield with SD card
- two Arducam mini with OV5642.

There are three major parts of my programme

1) the two cameras take jpg images and store them to a SD card
2) Weather data is collected and directly posted to a MySQL database on our webserver
3) The jpg files are uploaded to the website to be displayed (every 20 minutes as long as light is available)

As we have no fixed line internet at the club house, we are using a 3G wires router to connect to the internet.

So far so good.

Item 1 and 2 are done and working fine. But I have my problems with item 3.

I have searched the web and found some examples, but I could not get them working.

I get the message that the file was sent, but I cannot see the file on the webserver. If I use the same php on the server using a html file to POST the jpg file is uploaded.


Please see the sketch and the PHP below, maybe you can see what is going wrong here.

Many thanks!!


Code: [Select]

#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"
#include<stdlib.h>
#include <SD.h>

// Define CC3000 chip pins
#define ADAFRUIT_CC3000_IRQ 3
#define ADAFRUIT_CC3000_VBAT 5
#define ADAFRUIT_CC3000_CS 10

// WiFi network (change with your settings !)
#define WLAN_SSID "mywirelessnetwork"
#define WLAN_PASS "mypassword"
#define WLAN_SECURITY WLAN_SEC_WPA2


// Create CC3000 instances
// WIFi setup
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,SPI_CLOCK_DIV2);
Adafruit_CC3000_Client client;

char filename[14] = "CAM1.jpg";
int i = 0;
int keyIndex = 1;
int port = 80; //Server Port
uint32_t MANip = 0;

void setup() {

  Serial.begin(115200);

  // Initialise the module
  Serial.println(F("\nInitializing..."));
  if (!cc3000.begin()){
    Serial.println(F("Couldn't begin()! Check your wiring?"));
    while(1);
  }


  // Connect to WiFi network
  cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
  Serial.println(F("Connected!"));

  // Display connection details
  Serial.println(F("Request DHCP"));
  while (!cc3000.checkDHCP()){
    delay(100);
  }
  Serial.println("Ready");
 Serial.println("-------");

 //Get MAN IP address

  getMANip();


  // Prepare HTTP request
  String start_request = "";
  String end_request = "";
  start_request = start_request + "\n" + "--AaB03x" + "\n" + "Content-Disposition: form-data; name=\"fileToUpload\"; filename="+filename+"\n" + "Content-Type: file" + "\n" + "Content-Transfer-Encoding: binary" + "\n" + "\n";
  end_request = end_request + "\n" + "--AaB03x--" + "\n";



  //Initialise SD Card
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization of SD done.");
 
 File myFile = SD.open("CAM1.jpg");
  uint16_t jpglen = myFile.size();
  uint16_t extra_length;
  extra_length = start_request.length() + end_request.length();
  uint16_t len = jpglen + extra_length;

  // Set up TCP connection with web server
  client = cc3000.connectTCP(MANip, port);


 
  if (client.connected()) {
    Serial.println("Start uploading...");

    client.println(F("POST /~modelavi/plugins/upload.php HTTP/1.1"));

    client.println(F("Host: www.modelaviationnorthland.co.nz"));
    client.println(F("Content-Type: multipart/form-data; boundary=AaB03x"));
    client.print(F("Content-Length: "));
    client.println(len);
    client.println(start_request);

    Serial.println(F("Host: www.modelaviationnorthland.co.nz"));
    Serial.println(F("Content-Type: multipart/form-data; boundary=AaB03x"));
    Serial.print(F("Content-Length: "));
    Serial.println(len);
    Serial.println(start_request);
 
 if (myFile) {
  byte clientBuf[32];
  int clientCount = 0;

  while(myFile.available())
  {
    clientBuf[clientCount] = myFile.read();
    clientCount++;

    if(clientCount > 31)
    {
      client.write(clientBuf,32);
      clientCount = 0;
    }
  }
  if(clientCount > 0) client.write(clientBuf,clientCount);

  client.print(end_request);
  client.println();
  }
  else{
       Serial.println("File not found");
      }
  }   
  else{
    Serial.println("Web server connection failed");
  }
 
  myFile.close(); 
  client.close();
 
  Serial.println("done...");

 
}

void loop() {



delay(10000);

}


void getMANip(){
#ifndef CC3000_TINY_DRIVER
  /* Try looking up www.modelaviationnorthland.co.nz */
    //Serial.print(F("www.modelaviationnorthland.co.nz -> "));
  while  (MANip  ==  0)  {
    if  (!  cc3000.getHostByName("www.modelaviationnorthland.co.nz", &MANip))  {
      //Serial.println(F("Couldn't resolve!"));
    }
    delay(500);
  } 
  //cc3000.printIPdotsRev(MANip);
#endif
}


PHP file
Code: [Select]

<?php
$target_dir 
"/home/modelavi/public_html/images/weathercam/";
$target_file $target_dir basename($_FILES["fileToUpload"]["name"]);
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file); 
echo(
"Done");
?>





Cheers
Andreas
Hi AndreasGabler. any updates on this one? Did you find the answer to your problem? I'm also trying to send a photo to my server. but when trying you code I got stuck in the "Start uploading..." as I have monitored it on the Serial monitor. Really need this one. Thanks

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy