Bonjour,
Enseignant en Technologie au collège je suis toujours à la recherche de projets innovants. Je me débrouille pour le matériel, un peu moins pour la programmation même si je ne débute pas.
J’ai déjà envoyé sur une base de données « wamp »/« sql » avec un « arduino/ethernet+ methode get + fichier capteur de température » mais cette fois je bute sur la méthode post. Je vais essayer de détailler mon problème :
Je veux uploder un fichier « test.txt » de la sd « Arduino » (shield Internet + Arduino) vers un dossier du serveur Wamp(C:\wamp\www\arduino_sd\uploads) (méthode post form/data avec fichier php « camera.php »)
J’ai testé le « camera.php » sans l’arduino avec un formulaire html et cela fonctionne sur le serveur wamp.
Le code Arduino est compilé avec « succès » (moniteur série « transmission over ») mais le fichier de la sd« test.txt » n’est pas uploadé. Je loupe sûrement certaines subtilités pour comprendre complètement la requête passée par l’arduino.
Le fichier « camera.php » me renvoie sur le serveur wamp des messages d’erreur : "There was an error uploading the file, please try again!" et « undefined reference »
Pour le fichier « camera.php », j’ai essayé de rajouter « if (isset($_FILES["texte"])) le « undefined reference » disparaît mais le « post » ne passe toujours pas !!!
Je me suis documenté sur la requête post mais je n’arrive pas à mes fins.
Je me suis inspiré du code arduino-camera-wifi/camera_wifi.ino at master · makecademy/arduino-camera-wifi · GitHub et des commentaires des internautes pour l’adapter à mon projet avec le shield Internet.
Merci pour votre aide
/***************************************************
This is a sketch to test the camera module with the CC3000 WiFi chip
Written by Marco Schwartz for Open Home Automation
Code inspired by the work done on the Adafruit_VC0706 & CC3000 libraries
BSD license, all text above must be included in any redistribution
****************************************************/
// Include camera
#include <SoftwareSerial.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
//#include "utility/debug.h"
#include<stdlib.h>
#include <SD.h>
#include <Ethernet.h>
File myFile;
int port = 80;
String repository = "/arduino-camera-wifi/";
char outBuf[128];
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xF0, 0xD9 };
IPAddress server(192,168,1,21); // @IP du serveur Web
IPAddress ip(192,168,1,25); // @IP du shield ethernet sur la carte Arduino
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup() {
Serial.begin(9600);
delay(1000);
// start the Ethernet connection using a fixed IP address and DNS server:
Ethernet.begin(mac, ip);
// print the Ethernet board/shield's IP address:
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
//////////////////////////////////////// Partie SD (Début) ////////////////////////////////////////////
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(10, OUTPUT);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3 ok.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
//////////////////////////////////////// Partie SD (FIN) ////////////////////////////////////////////
// Get the size of the image (frame) taken
uint16_t jpglen;
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("Test.txt:");
jpglen = myFile.size();
Serial.print("Storing ");
Serial.print(jpglen, DEC);
Serial.print(" text.");
}
myFile.close();
// Prepare request
String start_request = "";
String end_request = "";
start_request = start_request + "\n" + "--AaB03x" + "\n" + "Content-Disposition: form-data; name=\"texte\"; filename=\"test.txt\"" + "\n" + "Content-Type: text/plain" + "\n" + "Content-Transfer-Encoding: Quoted-Printable" + "\n" + "\n";
end_request = end_request + "\n" + "--AaB03x--" + "\n";
uint16_t extra_length;
extra_length = start_request.length() + end_request.length();
Serial.println("Extra length:");
Serial.println(extra_length);
uint16_t len = jpglen + extra_length;
Serial.println("Full request:");
Serial.println(F("POST /arduino_sd/camera.php HTTP/1.1"));
Serial.println(F("Host: localhost"));
Serial.println(F("Content-Type: multipart/form-data; boundary=AaB03x"));
Serial.print(F("Content-Length: "));
Serial.println(len);
Serial.print(start_request);
Serial.print("binary data");
Serial.print(end_request);
//Serial.println("Starting connection to server...");
// Connect to the server, please change your IP address !
if (client.connect(server,80)) {
Serial.println(F("Connected !"));
client.println(F("POST /arduino_sd/camera2.php HTTP/1.1"));
client.println(F("Host: localhost"));
// client.println(F("Content-Type: multipart/form-data; boundary=AaB03x"));
client.print(F("Content-Length: "));
client.println(len);
client.print(start_request);
myFile = SD.open("test.txt");
// Read all the data
while (myFile.available()) {
Serial.write(myFile.read());
client.write(myFile.read());
Serial.print(".");
}
myFile.close();
client.print(end_request);
client.println();
Serial.println("Transmission over");
}
else {
Serial.println(F("Connection failed"));
}
while (client.connected()) {
while (client.available()) {
// Read answer
char c = client.read();
Serial.print(c);
//result = result + c;
}
}
client.stop();
}
void loop() {
}
<?php
$target_path = 'C:\wamp\www\arduino_sd\uploads/';
$target_path = $target_path . basename( $_FILES['texte']['name']);
if(move_uploaded_file($_FILES['texte']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['texte']['name']).
" has been uploaded";
}
else {
echo "There was an error uploading the file, please try again!";
}
?>