Go Down

Topic: need help with dht11 webserver (Read 906 times) previous topic - next topic

mortenx

Dec 11, 2012, 09:21 pm Last Edit: Dec 11, 2012, 09:40 pm by mortenx Reason: 1
can anyone finds why arduino older code is not working with newer arduino version?
... program works in arduino but won`t get connections with sensors...

Code: [Select]

#include <SPI.h>
#include <Ethernet.h>
#include <dht11.h>
#include <SD.h>


dht11 DHT11;

#define nSensores 3
int puertos[nSensores];
float fHumedades[nSensores];
float fTemperaturas[nSensores];

int nFilas=0;
int nFiles=0;

File file;

int iNVisitas=0;

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xEE, 0xEE };
byte ip[] = { 192,168,1, 177 };

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(666);
void setup()
{
 // start the Ethernet connection and the server:
 Ethernet.begin(mac, ip);
 server.begin();
 
 Serial.begin(115200);
int puertos[]={2,3,4};

 
  pinMode(10, OUTPUT);
 if (!SD.begin(4))
 {
   Serial.println("Error inicializando SD");
   nFiles=-1;
 }
 else
 {
   nFiles=0;
   Serial.println("SD initializada.");
 }
 
}

void getdata(int iIndice)
{
 int chk = DHT11.read(puertos[iIndice]);
 fHumedades[iIndice]=-1;
 fTemperaturas[iIndice]=-1;
 
 Serial.print("Sensor ");
 Serial.print(iIndice);
 Serial.print(" ");
 switch (chk)
 {
   case 0:  
     fHumedades[iIndice]=(float)DHT11.humidity;
     Serial.print(fHumedades[iIndice], 2);
     Serial.print(" % ");
     fTemperaturas[iIndice]=(float)DHT11.temperature;
     Serial.print(fTemperaturas[iIndice], 2);
     Serial.println(" o C");
       break;
   case -1: Serial.println(" Checksum error"); break;
   case -2: Serial.println(" Time out error"); break;
   default: Serial.println(" Unknown error"); break;
 }

 
}


void loop()
{
 // listen for incoming clients
 EthernetClient client = server.available();
 if (client) {
   // an http request ends with a blank line
   boolean currentLineIsBlank = true;
   while (client.connected()) {
     if (client.available()) {

       char c = client.read();
       // if you've gotten to the end of the line (received a newline
       // character) and the line is blank, the http request has ended,
       // so you can send a reply
       if (c == '\n' && currentLineIsBlank) {
         // send a standard http response header
         client.println("HTTP/1.1 200 OK");
         client.println("Content-Type: text/html");
         client.println();
         // output the value of each analog input pin
         for(int i=0;i<nSensores;i++){
//          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
           client.print("sensor ");
           client.print(i);
           client.print(": ");            
           if(fHumedades[i]==-1)
             client.print(" error leyendo el sensor");
           else
           {
             client.print(fHumedades[i], 2);
             client.print(" % ");
             client.print(fTemperaturas[i], 2);
             client.println(" o C");            
           }
           client.println("<br />");            
         }
         client.print((iNVisitas++)/2);
         client.println(" visitas <br />");

         break;
       }
       if (c == '\n') {
         // you're starting a new line
         currentLineIsBlank = true;
       }
       else if (c != '\r') {
         // you've gotten a character on the current line
         currentLineIsBlank = false;
       }
     }
   }
   // give the web browser time to receive the data
   delay(1);
   // close the connection:
   client.stop();
 }
 else
 {
   //if(nFiles>=0 && file)
   {
      file= SD.open("datalog.txt", FILE_WRITE);
   }
   String data="";
   for(int i=0;i<nSensores;i++)
   {
     getdata(i);
     
     data+=String(nFilas)+";"+String(i)+";"+String((int)fHumedades[i])+";"+String((int)fTemperaturas[i])+"\n";
   }
   if(file)
   {
     file.print(data);
     Serial.print(data);
     file.close();
     nFilas++;
   }
   delay(200);
 }
}

PaulS

Quote
can anyone finds why arduino older code is not working with newer arduino version?

When you explain what "is not working" means. Does not compile? Does not link? Does not upload? Whistles Dixie off key?

mortenx

program works in arduino but won`t get connections with sensors

Nick Gammon

Please note that, at present, the String library has bugs as discussed here and here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.).
http://www.gammon.com.au/electronics

Go Up