web control + temp page

Hello,

I'm new to Arduino and i'm having problems with my project. I Know somethings wrong with reading the sensor, because the rest works when I leave that part out of the script. Does anyone has the solution for me?

#include <SPI.h>
#include <DHT11.h>
int pin=4;
DHT11 dht11(pin);
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0x3F, 0xF9, 0xED }; //physical mac address
IPAddress ip(192,168,2, 113);

EthernetServer server(80); //server port

String readString;

void setup(){

  pinMode(5, OUTPUT); //pin selected to control
  pinMode(6, OUTPUT); //pin selected to control

  //start Ethernet
  Ethernet.begin(mac);
  server.begin();

  //enable serial data print
  Serial.begin(9600);
}

void loop(){
  
  float temp, humi;
  dht11.read(humi, temp);
   
{
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string
          readString += c;
          //Serial.print(c);
        }

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging

          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino Control page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");
          client.println("<CENTER>");
          
          client.println("<H1>Arduino Control</H1>");
         
          // output 5       
          client.print("<input type=submit value=ON style=width:100px;height:45px onClick=location.href='/?on2;'>");
          client.print("<input type=submit value=OFF style=width:100px;height:45px onClick=location.href='/?off3;'>
");

          // output 6       
          client.print("<input type=submit value=ON style=width:100px;height:45px onClick=location.href='/?on4;'>");
          client.print("<input type=submit value=OFF style=width:100px;height:45px onClick=location.href='/?off5;'>

");
          
          client.print("Temperature : ");
          client.print(temp);
          client.print("<sup>0</sup>");
          client.print("C");
          client.println("
");
          client.print("Humidity : ");
          client.print(humi);
          client.print("%");
          
          client.println("</CENTER>");
          client.println("</BODY>");
          client.println("</HTML>");

          delay(1);
          //stopping client
          client.stop();

        }
      }
    }
  }
}
}

How are you connecting to LAN? The Ethernet shield may be wanting to use some of the pins you're using for other things... 4, 5 and 6, it seemed to me, on a quick scan of code.

By the way....

int pin=4;
DHT11 dht11(pin);

... is the WayToGo (though I'd use a fancier name).

It would be good if you did something similar in connection with your use of 5 & 6. Shouldn't "matter" to the final result, of course... but it may help you get where you want to be, if you make that an "automatic reflex".

The script works if I leave the following part out of it.

dht11.read(humi, temp);

Only then the result of the temp and humanity stays out of course.

I'm not familiar with the DHT11 library, where is the source? The call to read() looks wonky.

perhaps it is this one?

if so instead of this:

float temp, humi;
dht11.read(humi, temp);

you could try this instead:

float temp, humi;
dht11.read(pin);  // or just leave pin out
temp = dht11.temperature();
humi = dht11.humidity();

Also, and this is relevant, there is no good reason to read the sensor EVERY time through loop(). It will be constantly reading the sensor over and over again, potentially heating it up and giving bad data. Make the humidity and temperature variables Global and then put it on a millis() timer to read every 60 seconds or so. The DHT11 is already horribly inaccurate (±2C°). Heating it up by constantly reading it won't help you.