Bonjour, pour mon projet de fin d'année je doit réaliser un serveur web arduino html + css Le soucis c'est que dans mon programme j'ai actuellement un problème :
Etant donnée que je peux pas écrire de variable liée au code arduino a l'intérieur de mon code html dans ma carte sd j'ai decidé de "réécrire" a chaque fois le programme en changeant juste la variable qui m'interresse. Je sais que c'est assez bourrin mais je suis un novice dans la matière. Sauf que dans le programme que j'ai, rien ne se passe et j'ai beau relire le code je ne trouve pas l'erreur.
Merci beaucoup :3
Voici le code
/*--------------------------------------------------------------
Program: eth_websrv_SD_image
Description: Arduino web server that serves up a basic web
page that displays an image.
Hardware: Arduino Uno and official Arduino Ethernet
shield. Should work with other Arduinos and
compatible Ethernet shields.
2Gb micro SD card formatted FAT16
Software: Developed using Arduino 1.0.5 software
Should be compatible with Arduino 1.0 +
Requires index.htm, page2.htm and pic.jpg to be
on the micro SD card in the Ethernet shield
micro SD card socket.
References: - WebServer example by David A. Mellis and
modified by Tom Igoe
- SD card examples by David A. Mellis and
Tom Igoe
- Ethernet library documentation:
http://arduino.cc/en/Reference/Ethernet
- SD Card library documentation:
http://arduino.cc/en/Reference/SD
Date: 7 March 2013
Modified: 17 June 2013
Author: W.A. Smith, http://startingelectronics.org
--------------------------------------------------------------*/
- include <SPI.h>
- include <Ethernet.h>
- include <SD.h>
File fichierSD;
// size of buffer used to capture HTTP requests
- define REQ_BUF_SZ 20
int test = 0 ;
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x14, 0x26 };
IPAddress ip(169, 254, 250, 22); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
File webFile;
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()
{
// disable Ethernet chip
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
Serial.begin(9600); // for debugging
// initialize SD card
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
}
void loop()
{
sdwrite(); //Ouverture du Sous programme d'ecriture de la carte sd
EthernetClient client = server.available(); // Debut procedure site web
if (client) { // got client?
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) { // client data available to read
char c = client.read(); // read 1 byte (character) from client
// buffer first part of HTTP request in HTTP_req array (string)
// leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1)
if (req_index < (REQ_BUF_SZ - 1)) {
HTTP_req[req_index] = c; // save HTTP request character
req_index++;
}
// print HTTP request character to serial monitor
Serial.print(c);
// last line of client request is blank and ends with \n
// respond to client only after last line received
if (c == '\n' && currentLineIsBlank) {
// open requested web page file
if (StrContains(HTTP_req, "GET / ")
|| StrContains(HTTP_req, "GET /index.htm")) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
webFile = SD.open("index.htm"); // open web page file
}
else if (StrContains(HTTP_req, "GET /style.css")) {
webFile = SD.open("style.css"); // je modifie le code pour essayer d'inclure le css
if (webFile) {
client.println("HTTP/1.1 200 OK");
client.println();
}
}
else if (StrContains(HTTP_req, "GET /apple.jpg")) {
webFile = SD.open("apple.jpg"); // j'insert l'image de la poubelle
if (webFile) {
client.println("HTTP/1.1 200 OK");
client.println();
}
}
else if (StrContains(HTTP_req, "GET /logo2.jpg")) {
webFile = SD.open("logo2.jpg"); // j'insert l'image du titre
if (webFile) {
client.println("HTTP/1.1 200 OK");
client.println();
}
}
else if (StrContains(HTTP_req, "GET /bg_menu.gif")) {
webFile = SD.open("bg_menu.gif"); // je met le bg menu
if (webFile) {
client.println("HTTP/1.1 200 OK");
client.println();
}
}
if (webFile) {
while(webFile.available()) {
client.write(webFile.read()); // send web page to client
}
webFile.close();
}
// reset buffer index and all buffer elements to 0
req_index = 0;
StrClear(HTTP_req, REQ_BUF_SZ);
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)
delay(10000);
}
// 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;
}
void sdwrite() { //Sous programme ecriture carte sd
if(SD.exists("index.htm"))
{
SD.remove("index.htm"); // J'enleve le precedent fichier "index"
}
fichierSD = SD.open("index.htm", FILE_WRITE); // Je créer un nouveau fichier "index"
//Test pour écriture
if(fichierSD) {
Serial.println(F("Ecriture en cours"));
//Ecriture du code html
fichierSD.println("<!DOCTYPE html\">");
fichierSD.println("<html lang=\"fr\">");
fichierSD.println("<head>");
fichierSD.println("<meta charset=\"utf-8\" />");
fichierSD.println("<title>La Poubelle Intelligente</title>");
fichierSD.println("<link href=\"style.css\" rel=\"stylesheet\" media=\"all\" type=\"text/css\"> ");
fichierSD.println("</head>");
fichierSD.println("<body>");
fichierSD.println("<div id=\"conteneur\"> ");
fichierSD.println("<h1 id=\"header\"><a href=\"#\" title=\"Poubelle intelligente\"><span>La poubelle intelligente</span></a></h1>");
fichierSD.println("<nav>");
fichierSD.println(" <ul id=\"menu\">");
fichierSD.println("<li></li>");
fichierSD.println("<li></li>");
fichierSD.println("<li></li>");
fichierSD.println("<li></li>");
fichierSD.println("<li></li>");
fichierSD.println("</ul>");
fichierSD.println("</nav>");
fichierSD.println("<div id=\"contenu\">");
fichierSD.println("<h2>Titre de page, d'article...</h2>");
fichierSD.println("<p>");
fichierSD.println("<p></p>");
fichierSD.println("<h3>Sous titre à cet article</h3>");
fichierSD.println("<p></p>");
fichierSD.println("<h3>Autre sous-titre</h3>");
fichierSD.println("<p>");
fichierSD.print("<p>");
fichierSD.print(test);
fichierSD.println("</p>");
fichierSD.println("</div>");
fichierSD.println("<p id=\"footer\"></p>");
fichierSD.println("</div>");
fichierSD.println("</body>");
fichierSD.println("</html>");
test = test + 1; //variable pour tester la boucle
Serial.println("Ca a fini");
Serial.println("ca recommence");
fichierSD.close();
delay(50);
}
}