Partage
  • Partager sur Facebook
  • Partager sur Twitter

probleme d'affichage avec shield ethernet arduino

Sujet résolu
    21 mai 2019 à 11:47:49

    Bonjour à tous, dans mon projet de Terminale STI2D, je dois m'occuper de la partie base de données/affichage mais j'ai malheureusement quelques problèmes avec celle-ci, je m'explique :

    En utilisant ce programme :

    #include <SPI.h>
    #include <Ethernet.h>
    #include <SD.h>
    
    #define REQ_BUF_SZ   20
    
    // MAC address from Ethernet shield sticker under board
    byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x12, 0x1F };
    IPAddress ip(192, 168, 231, 68); // 192, 168, 231, 68IP 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;
    
    
    void setup()
    {
        pinMode(10, OUTPUT);
        digitalWrite(10, HIGH);
        Serial.begin(9600);       // for debugging
        
        // initialize SD card
        Serial.println("Initializing SD card...");
        if (!SD.begin(4)) {
            Serial.println("ERROR - SD card initialization failed!");
            return;    // init failed
        }
        Serial.println("SUCCESS - SD card initialized.");
        // check for index.htm file
        if (!SD.exists("Index.php")) {
            Serial.println("ERROR - Can't find index.htm file!");
            return;  // can't find index file
        }
        Serial.println("SUCCESS - Found index.htm file.");
        Ethernet.begin(mac, ip);  // initialize Ethernet device
        server.begin();           // start to listen for clients
    }
    
    void loop()
    {
        EthernetClient client = server.available();  // try to get client
    
        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.php")) {
                            webFile = SD.open("Index.php");        // open web page file
                        }
                        else if (StrContains(HTTP_req, "GET /Affichage.php")) {
                            webFile = SD.open("Affichage.php");        // open web page file
                        }
                        else if (StrContains(HTTP_req, "GET /Patient1.php")) {
                            webFile = SD.open("Patient1.php");        // open web page file
                        }
                        else if (StrContains(HTTP_req, "GET /ordonnance_type1.png")) {
                            webFile = SD.open("ordonnance_type1.png");
                        }
                        else if (StrContains(HTTP_req, "GET /traitement.php")) {
                            webFile = SD.open("traitement.php");
                            }
                        else if (StrContains(HTTP_req, "GET /Patient2.php")) {
                            webFile = SD.open("Patient2.php");        // open web page file
                        }
                        else if (StrContains(HTTP_req, "GET /Michel.png")) {
                            webFile = SD.open("Michel.png");
                        }
                        else if (StrContains(HTTP_req, "GET /Patient3.php")) {
                            webFile = SD.open("Patient3.php");
                        }
                        else if (StrContains(HTTP_req, "GET /ordonnance2.png")) {
                            webFile = SD.open("ordonnance2.png");
                        }
                        else if (StrContains(HTTP_req, "GET /Patient4.php")) {
                            webFile = SD.open("Patient4.php");
                        }
                        else if (StrContains(HTTP_req, "GET /download.png")) {
                            webFile = SD.open("download.png");
                        }
                        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)
    }
    
    // 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;
    }

    je suis capable d'accéder une un site hébergé par l'arduino, le problème est que comme avec cette exemple si dessous la page Affichage.php et des images d'autres pages n'apparaissent pas.

    ce que je veux :

    ce que j'ai :

    je suis donc bloqué et cherche toujours où sont les erreurs, merci d'avance pour vos réponses : )

    • Partager sur Facebook
    • Partager sur Twitter
      21 mai 2019 à 16:42:10

      Bonjour,

      Tu es censé envoyer une entête HTTP terminée par une ligne vide avant le contenu, par exemple:

      // Indique que la ressource est présente
      client.println("HTTP/1.1 200 OK");
      // Indique le type de contenu
      client.println("Content-Type: text/html; charset=utf-8");
      // Indique la fin de l'entête HTTP de la réponse
      client.println();

      pour du contenu html et même chose en remplaçant la ligne du milieu par 

      client.println("Content-Type: image/png");

      pour des images png.

      De plus la comparaison de chaîne de caractère doit être insensible à la casse (minuscule / majuscule), donc tu aurais intérêt à utiliser la classe String d'arduino, puis à convertir en minuscule avec toLowerCase et utiliser startsWith pour rechercher les différents "get /xxx", ou directement utiliser une bibliothèque serveur http pour arduino.

      • Partager sur Facebook
      • Partager sur Twitter
        23 mai 2019 à 9:28:28

        Bonjour, et merci pour ton aide

        J'ai du mal à comprendre comment est ce que je dois intégrer ce que tu m'as dis, dois je changer tout mon programme ou simplement modifier quelques lignes, car sache que vois pas comment faire.

        J'ai modifié les majuscules, tout est en minuscules plus de compromis néanmoins, je ne comprends toujours pas pourquoi sur certaines pages web tout est affiché correctement et sur d'autres les image n'apparaissent pas.

        merci pour le soutient et désolé du mon faible niveau de programmation qui n'aide pas forcément.

        • Partager sur Facebook
        • Partager sur Twitter

        probleme d'affichage avec shield ethernet arduino

        × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
        × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
        • Editeur
        • Markdown