Partage
  • Partager sur Facebook
  • Partager sur Twitter

Problème Code

    26 mai 2013 à 14:32:34

    Bonjour, 

    Pour un projet Domotique mon but est d'héberger sur une carte SD un site web est afficher sur celui-ci les données de ma carte Weathershield (Température, Humidité et Pression) mais j'ai du mal a programmer donc j'ai quelques soucis au niveau de la compilation. C'est relativement urgent je dois finir ce projet dans une semaine. J'utilise une carte Arduino Uno, Une carte Shield Ethernet + carte SD, et une carte Weathershield. Merci de votre aide.

    /*
    Placer sur carte SD les fichiers index.htm et style.css
     */
    float valr =0;
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <Ethernet.h>
    #include <WeatherShield1.h>
    
    //Local ethernet setup
    byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x03, 0xAC }; 
    byte ip[] = {192, 168, 1, 15};   
    char rootFileName[] = "index.htm";
    EthernetServer server(80);
    
    // Variable
    
    int flghtm=0;
    
    //SD card stuff
    Sd2Card card;
    SdVolume volume;
    SdFile root;
    SdFile file;
    #define error(s) error_P(PSTR(s))
    
    void error_P(const char* str) {
      PgmPrint("error: ");
      SerialPrintln_P(str);
      if (card.errorCode()) {
        PgmPrint("SD error: ");
        Serial.print(card.errorCode(), HEX);
        Serial.print(',');
        Serial.println(card.errorData(), HEX);
      }
      while(1);
    }
    
    #define RXBUFFERLENGTH 4 
    WeatherShield1 weatherShield;  
    
    /* Class for sensors reading */  
    class WeatherData {  
        private: 
            float fTemperature;  
            unsigned short shTemperature;  
            float fPressure; 
            unsigned short shPressure; 
            float fHumidity;   
            unsigned short shHumidity;  
        public:  
            WeatherData() { bReady = false; } // constructor 
            // get methods
            float getAvgTemperature() { return fTemperature; }
            float getInstTemperature() { return ((float)shTemperature/16); }     
            float getAvgPressure() { return fPressure; } 
            float getInstPressure() { 
                 float value ;
                 value = (((float)shPressure/1024)+0.095)/0.009; 
                 return value;  
             }
            float getAvgHumidity() { return fHumidity; } 
            float getInstHumidity() {      
               float value; 
               value = (((float)shHumidity/1024)-0.1515)/0.00636; // without compensation       
            // compensation relative humidity with read temperature          
               value = value/(1.0546-0.00216*getInstTemperature());            
               return value; 
              }       
            // set methods 
              void setAvgTemperature(float Temperature) { fTemperature=Temperature; }
              void setInstTemperature(unsigned short Temperature) { shTemperature=Temperature; } 
              void setAvgPressure(float Pressure) { fPressure=Pressure; }   
              void setInstPressure(unsigned short Pressure) { shPressure=Pressure; }   
              void setAvgHumidity(float Humidity) { fHumidity=Humidity; }    
              void setInstHumidity(unsigned short Humidity) { shHumidity=Humidity; } 
              public:   
                 boolean bReady;
     };
     
           // server listening on port 80:  
           String readString;
           float temp; 
           WeatherData weatherData; 
           boolean currentLineIsBlank = true;  
           
    void setup() {
      
      Serial.begin(9600);
      PgmPrint("Free RAM: ");
      Serial.println(FreeRam());  
    //  pinMode(10, OUTPUT);              
    //  digitalWrite(10, HIGH);              
      if (!card.init(SPI_HALF_SPEED, 4)) error("card.init failed!");
      if (!volume.init(&card)) error("vol.init failed!");
      PgmPrint("Volume is FAT");
      Serial.println(volume.fatType(),DEC);
      Serial.println();
      if (!root.openRoot(&volume)) error("openRoot failed");
      PgmPrintln("Files found in root:");
      root.ls(LS_DATE | LS_SIZE);
      Serial.println();
      PgmPrintln("Files found in all dirs:");
      root.ls(LS_R);
      Serial.println();
      PgmPrintln("Done");
      Ethernet.begin(mac,ip);
      server.begin();
    }
    
    
    void loop()
    {
      char clientline[BUFSIZ];
      char *filename;
      int image = 0;
      int index = 0;
    
      EthernetClient client = server.available();
      if (client) {
        boolean current_line_is_blank = true;
        readString=""; 
    
        while (client.connected()) {
          if (client.available()) {
            char c = client.read();
            readString.concat(c); 
            if (c != '\n') {
              clientline[index] = c;
              index++;
               if (index >= BUFSIZ) 
                index = BUFSIZ -1;
    
              continue;
            }
    
            clientline[index] = 0;
            filename = 0;
    
            Serial.println(clientline);
    
            if (strstr(clientline, "GET / ") != 0)     // si ligne de commande reçue contient "GET / "
            {
              filename = rootFileName;   // on envoie le fichier index.htm
              flghtm = 1;
            } 
            else
            {
              flghtm = 0;
            }
            Serial.print("flghtm : ");
            Serial.print(flghtm);
            Serial.print("  ");
            if (strstr(clientline, "GET /") != 0)
            {
              if (!filename) filename = clientline + 5; 
    
              (strstr(clientline, " HTTP"))[0] = 0;
    
              Serial.println(filename);
    
              if (! file.open(&root, filename, O_READ)) {
                client.println("HTTP/1.1 404 Not Found");
                client.println("Content-Type: text/html");
                client.println();
                client.println("<h2>Error 404</h2>");
                client.println("<s2>The file does not exist.<s2>");
                client.println("");
                break;
              }
              
                     }  
             if (readValues(weatherShield))
               client.print(weatherData.getInstTemperature());
               client.print(":");   
               client.print(weatherData.getInstPressure());    
               client.print(":");  
               client.print(weatherData.getInstHumidity());  
               // delay for data acquiring   
               delay(1);  
               // close connection  
               client.stop(); 
                   }
      }
              
              Serial.println("Opened!");
              //File types
              client.println("HTTP/1.1 200 OK");
              if (strstr(filename, ".htm") != 0)
                client.println("Content-Type: text/html");
              else if (strstr(filename, ".css") != 0)
                client.println("Content-Type: text/css");
              else if (strstr(filename, ".png") != 0)
                client.println("Content-Type: image/png");
              else if (strstr(filename, ".jpg") != 0)
                client.println("Content-Type: image/jpeg");
              else if (strstr(filename, ".gif") != 0)
                client.println("Content-Type: image/gif");
              else if (strstr(filename, ".3gp") != 0)
                client.println("Content-Type: video/mpeg");
              else if (strstr(filename, ".pdf") != 0)
                client.println("Content-Type: application/pdf");
              else if (strstr(filename, ".js") != 0)
                client.println("Content-Type: application/x-javascript");
              else if (strstr(filename, ".xml") != 0)
                client.println("Content-Type: application/xml");
              else
                client.println("Content-Type: text");
              client.println();
    
              int16_t c;
              while ((c = file.read()) >= 0) {
                if ((c==0xff)&&(flghtm==1))
                {client.print(valr);
                }
                else
                {
                //Serial.print((char)c); //Prints all HTML code to serial (For debuging)
                client.print((char)c); //Prints all HTML code for web page
                }  
            }
             
          
              file.close();
    
            } 
            else {
              client.println("HTTP/1.1 404 Not Found");
              client.println("Content-Type: text/html");
              client.println();
              client.println("<h2>Error 404</h2>");
              client.println("");
            }
            break;
          }
        }
        
        
          /* this function reads sensors values and stores them in a WeatherData object    
          return value = false if there are some problems with Weather Shield.  */
          boolean readValues(WeatherShield1 &weatherShield) { 
          /* buffer */ 
          unsigned char ucBuffer[RXBUFFERLENGTH];
          /* connection check */  
          if (weatherShield.sendCommand(CMD_ECHO_PAR, 100, ucBuffer))  {
          /* temperature */
          if (weatherShield.sendCommand(CMD_GETTEMP_C_AVG, 0, ucBuffer))
          weatherData.setAvgTemperature(weatherShield.decodeFloatValue(ucBuffer)); 
          if (weatherShield.sendCommand(CMD_GETTEMP_C_RAW, PAR_GET_LAST_SAMPLE, ucBuffer))   
                 weatherData.setInstTemperature(weatherShield.decodeShortValue(ucBuffer));  
          /* pressure */  
          if (weatherShield.sendCommand(CMD_GETPRESS_AVG, 0, ucBuffer))  
                 weatherData.setAvgPressure(weatherShield.decodeFloatValue(ucBuffer)); 
          if (weatherShield.sendCommand(CMD_GETPRESS_RAW, PAR_GET_LAST_SAMPLE, ucBuffer))    
          weatherData.setInstPressure(weatherShield.decodeShortValue(ucBuffer));
          /* humidity  */
          if (weatherShield.sendCommand(CMD_GETHUM_AVG, 0, ucBuffer)) 
                 weatherData.setAvgHumidity(weatherShield.decodeFloatValue(ucBuffer));
          if (weatherShield.sendCommand(CMD_GETHUM_RAW, PAR_GET_LAST_SAMPLE, ucBuffer))
          weatherData.setInstHumidity(weatherShield.decodeShortValue(ucBuffer));
          return true;
      }
        return false;
       
        client.stop();
      }
    
    }
    
    //The End /* 
    
    
    
    



    -
    Edité par tyzz 26 mai 2013 à 14:42:47

    • Partager sur Facebook
    • Partager sur Twitter
      26 mai 2013 à 15:03:14

      J'ai essayé de faire un mix de deux étant "nul" en prog je pense que cela vien de la mais vu que c'est urgent je n'es pas le temps de tous reprendre de 0.
      • Partager sur Facebook
      • Partager sur Twitter
        26 mai 2013 à 17:44:27

        Ca aurais été pas mal d'avoir les messages d'erreurs de compilation qu'on sache un peu où chercher (parce que bon se taper le code ligne par ligne...)
        • Partager sur Facebook
        • Partager sur Twitter

        Retrouvez moi sur mon blog et ma chaine Youtube !

          26 mai 2013 à 21:07:26

          Oui essayé de re-modifié mon code (http://fr.sourcepod.com/nxfwrl65-18872) mais j'ai une erreur sur la librairies alors que je l'ai bien mise, voila quand je compil les erreurs que j'ai je ne comprends les erreurs sur le client.h.

          sketch_may26a:9: error: 'WeatherShield1' was not declared in this scope
          sketch_may26a:9: error: 'weatherShield' was not declared in this scope
          sketch_may26a.ino: In function 'void loop()':
          sketch_may26a:121: error: cannot allocate an object of abstract type 'Client'
          C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/Client.h:7: note:   because the following virtual functions are pure within 'Client':
          C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/Client.h:12: note:    virtual size_t Client::write(uint8_t)
          C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/Client.h:13: note:    virtual size_t Client::write(const uint8_t*, size_t)
          C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/Client.h:14: note:    virtual int Client::available()
          C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/Client.h:15: note:    virtual int Client::read()
          C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/Client.h:17: note:    virtual int Client::peek()
          C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/Client.h:18: note:    virtual void Client::flush()
          C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/Client.h:10: note:    virtual int Client::connect(IPAddress, uint16_t)
          C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/Client.h:11: note:    virtual int Client::connect(const char*, uint16_t)
          C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/Client.h:16: note:    virtual int Client::read(uint8_t*, size_t)
          C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/Client.h:19: note:    virtual void Client::stop()
          C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/Client.h:20: note:    virtual uint8_t Client::connected()
          C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/Client.h:21: note:    virtual Client::operator bool()
          sketch_may26a:121: error: cannot declare variable 'client' to be of abstract type 'Client'
          C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/Client.h:7: note:   since type 'Client' has pure virtual functions
          sketch_may26a:148: error: 'readValues' cannot be used as a function
          sketch_may26a:158: error: 'class HardwareSerial' has no member named 'stop'
          sketch_may26a:168: error: conflicting declaration 'EthernetClient client'
          sketch_may26a:121: error: 'client' has a previous declaration as 'Client client'
          sketch_may26a:274: error: a function-definition is not allowed here before '{' token
          sketch_may26a:300: error: expected `}' at end of input
          sketch_may26a:300: error: expected `}' at end of input
          • Partager sur Facebook
          • Partager sur Twitter
            26 mai 2013 à 21:53:39

            Tu aurais pas fait un copier/coller raté d'un code trouvé sur internet ? Tu as testé les différents trucs un par un pour ensuite petit à petit les mettres ensemble ? parce que là en terme d'erreurs on a le choix... :

            • Manque d'accolades fermantes
            • Redéclaration d'une variable (EthernetClient client -> renomme là en "cli" pour voir)
            • Tu essaies d'utiliser une variables comme une fonction
            • Tu essaies d'instancier une classe Abstraite (là on commence à taper dans les erreurs plus compliquées pour un débutant)
            • Ton include de WeatherShield1 ne marche pas... faut de frappe ? librairie pas au bonne endroit ?

            Bref, tout ca pour dire que :

            • Il faut tester les éléments un par un avant de brutalement tout mettre ensemble et serrer les fesses
            • Lire les erreurs de compil ca permet de dégrossir beaucoup de choses (surtout que les lignes sont indiquées...)
            • Partager sur Facebook
            • Partager sur Twitter

            Retrouvez moi sur mon blog et ma chaine Youtube !

            Problème Code

            × 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