Go Down

Topic: need help with dht11 webserver (Read 1 time) previous topic - next topic


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);
int puertos[]={2,3,4};

  pinMode(10, OUTPUT);
 if (!SD.begin(4))
   Serial.println("Error inicializando SD");
   Serial.println("SD initializada.");

void getdata(int iIndice)
 int chk = DHT11.read(puertos[iIndice]);
 Serial.print("Sensor ");
 Serial.print(" ");
 switch (chk)
   case 0:  
     Serial.print(fHumedades[iIndice], 2);
     Serial.print(" % ");
     Serial.print(fTemperaturas[iIndice], 2);
     Serial.println(" o C");
   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");
         // 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(": ");            
             client.print(" error leyendo el sensor");
             client.print(fHumedades[i], 2);
             client.print(" % ");
             client.print(fTemperaturas[i], 2);
             client.println(" o C");            
           client.println("<br />");            
         client.println(" visitas <br />");

       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
   // close the connection:
   //if(nFiles>=0 && file)
      file= SD.open("datalog.txt", FILE_WRITE);
   String data="";
   for(int i=0;i<nSensores;i++)


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?
The art of getting good answers lies in asking good questions.


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


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.).
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

Go Up