Bonjour,
Je travaille actuellement sur un projet qui utilise la Linksprite Camera JPEG TTL infrarouge ((https://www.sparkfun.com/products/11610)).
J’ai récupéré un exemple de code qui permet de prendre une photo (une seule) : http://learn.linksprite.com/jpeg-camera/tutorial-of-using-linksprite-2mp-uart-jpeg-camera-with-arduino/ (rubrique “Sample Code” sous l’image si ça n’est pas ouvert par défaut).
J’ai retiré ce qui me semble être responsable du fait que ça ne prend qu’une seule photo (ligne 202 de l’exemple) : while(1);
Dans mon code ci-dessous, j’ai réinitialisé quelques variables également, mais rien n’y fait, pas moyen d’avoir plus d’une image correctement prise et sauvegardée. La boucle se fait mais, mise à part la 1ère image, les images suivantes sont tronquées. L’affichage des bytes capturés montre bien que l’image est complète la 1ère fois, mais pas les suivantes.
Comment faire pour que l’image soit complète à partir du second passage, comme elle l’est lors du premier passage dans la boucle ?
#include "DHT.h"
#define DHTPIN 8
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>
#include <Ethernet.h>
#define REQ_BUF_SZ 20
byte ZERO = 0x00;
byte incomingbyte;
SoftwareSerial mySerial(5,6);
long int a=0x0000,j=0,k=0,count=0,i=0;
uint8_t MH,ML;
boolean EndFlag=0;
File fichier;
void SendResetCmd();
void SetBaudRateCmd();
void SetImageSizeCmd();
void SendTakePhotoCmd();
void SendReadDataCmd();
void StopTakePhotoCmd();
int air = 0;
int seuil_air = 500;
int son = HIGH;
float hum;
float temp;
int interval = 1000;
char filename[] = "pic.jpg";
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 6);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
EthernetServer server(80);
char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
char req_index = 0; // index into HTTP_req buffer
void setup() {
Serial.begin(38400);
dht.begin();
mySerial.begin(38400);
pinMode(10, OUTPUT);
SD.begin(4);
Ethernet.begin(mac, ip);
}
void loop() {
delay(interval);
// AIR
air = analogRead(0);
// SON
son = digitalRead(9);
// HUMIDITE / TEMPERATURE
hum = dht.readHumidity();
temp = dht.readTemperature();
//CAMERA
byte a[32];
int ii = 0;
SendResetCmd();
delay(4000);
SendTakePhotoCmd();
while(mySerial.available()>0){ incomingbyte=mySerial.read(); }
fichier = SD.open(filename, O_WRITE | O_CREAT | O_TRUNC);
while(!EndFlag){
j=0;
k=0;
count=0;
SendReadDataCmd();
delay(20);
while(mySerial.available()>0){
incomingbyte=mySerial.read();
k++;
if((k>5)&&(j<32)&&(!EndFlag)){
a[j]=incomingbyte;
if((a[j-1]==0xFF)&&(a[j]==0xD9)) //tell if the picture is finished
EndFlag=1;
j++;
count++;
}
}
for(j=0;j<count;j++){
if(a[j]<0x10)
Serial.print("0");
Serial.print(a[j],HEX); // observe the image through serial port
Serial.print(" ");
}
for(ii=0; ii<count; ii++)
fichier.write(a[ii]);
Serial.println();
i++;
}
fichier.close();
StopTakePhotoCmd();
incomingbyte = char(0);
EndFlag = 0;
for( int x = 0; x < sizeof(a); ++x ){a[x] = (char)0;}
// ETHERNET
EthernetClient client = server.available();
if (client) {
boolean currentLineIsBlank = true;// an http request ends with a blank line
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (req_index < (REQ_BUF_SZ - 1)) {
HTTP_req[req_index] = c; // save HTTP request character
req_index++;
}
Serial.write(c);
if (c == '\n' && currentLineIsBlank) { // last line of client request is blank and ends with \n
client.println("HTTP/1.1 200 OK"); // send a standard http response header
if (StrContains(HTTP_req, "GET /pic.jpg")) {client.println("Content-Type: image/jpeg");} else{client.println("Content-Type: text/html");}
client.println("Connection: close"); // the connection will be closed after completion of the response
//client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
fichier = SD.open("PIC.JPG");
if (fichier && StrContains(HTTP_req, "GET /pic.jpg")) {
while(fichier.available()) { client.write(fichier.read()); }
} else{
client.println("Juste luuuu page");
}
fichier.close();
req_index = 0;
StrClear(HTTP_req, REQ_BUF_SZ);
break;
}
if (c == '\n') { currentLineIsBlank = true; }
else if (c != '\r') { currentLineIsBlank = false; }
}
}
delay(1);
client.stop();
}
}
void SendResetCmd(){
mySerial.write(0x56);
mySerial.write(ZERO);
mySerial.write(0x26);
mySerial.write(ZERO);
}
void SendTakePhotoCmd(){
mySerial.write(0x56);
mySerial.write(ZERO);
mySerial.write(0x36);
mySerial.write(0x01);
mySerial.write(ZERO);
}
void SendReadDataCmd(){
MH=a/0x100;
ML=a%0x100;
mySerial.write(0x56);
mySerial.write(ZERO);
mySerial.write(0x32);
mySerial.write(0x0c);
mySerial.write(ZERO);
mySerial.write(0x0a);
mySerial.write(ZERO);
mySerial.write(ZERO);
mySerial.write(MH);
mySerial.write(ML);
mySerial.write(ZERO);
mySerial.write(ZERO);
mySerial.write(ZERO);
mySerial.write(0x20);
mySerial.write(ZERO);
mySerial.write(0x0a);
a+=0x20;
}
void StopTakePhotoCmd(){
mySerial.write(0x56);
mySerial.write(ZERO);
mySerial.write(0x36);
mySerial.write(0x01);
mySerial.write(0x03);
}
// sets every element of str to 0 (clears array)
void StrClear(char *str, char length){
for (int i = 0; i < length; i++) { str[i] = 0; }
}
// searches for the string sfind in the string str returns 1 if string found returns 0 if string not found
char StrContains(char *str, char *sfind){
char found = 0;
char index = 0;
char len;
len = strlen(str);
if (strlen(sfind) > len) { return 0; }
while (index < len) {
if (str[index] == sfind[found]) {
found++;
if (strlen(sfind) == found) { return 1; }
}
else { found = 0; }
index++;
}
return 0;
}