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
- the two cameras take jpg images and store them to a SD card
- Weather data is collected and directly posted to a MySQL database on our webserver
- 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!!
#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
<?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