Code only works with client?

Finally got everything to work. I've got a space heater, and after two days, I've managed to control the temperature in my office with the Arduino - I blew a few relays - mostly my fault, so I got a Solid State Relay - works great!

But now after a solid 3 days of running, I've noticed the code does not really work, unless there is someone actually on the web page of the arduino - which displays the temperature. I'm using the ethernet shield with the Uno, and I notice that the arduino does not actually respond unless someone looks on the page.

For example, I'll be in the office, and notice it's getting a little bit cold, and check my thermostat (not arduino, stand alone) and it'll be 20 degrees Celsius - then I'll log into the page, and my arduino displays the same temperature, but the heater only turns on when I load the page. I'm using a MAX6675 with a K+ thermocouple for my temp sensor by the way: Here is the code - there's nothing here which would force that, is there?

#include <max6675.h>
#include <SPI.h>
#include <Ethernet.h>
 
// 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, 0xFE, 0xED };
IPAddress ip(192,168,0,17);
 
// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);

 int thermoDO = 3;
int thermoCS = 5;
int thermoCLK = 6;
const int lightpin=8;

MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
int vccPin = 7;
int gndPin = 4;

void setup() 
{
    pinMode(vccPin, OUTPUT); digitalWrite(vccPin, HIGH);
  pinMode(gndPin, OUTPUT); digitalWrite(gndPin, LOW);
  pinMode(8,OUTPUT);

  Serial.begin(9600);
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
 
}
 
void loop() 
{

float t = thermocouple.readCelsius();
          Serial.println(t);
  if(t>0){
    if(t<28){
      digitalWrite(lightpin,HIGH);
    }
    else{
      digitalWrite(lightpin,LOW);
    }}
  


EthernetClient client = server.available();
 
 if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // 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("Connection: close");  // the connection will be closed after completion of the response
    client.println("Refresh: 5");  // refresh the page automatically every 30 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
 
          
          
 
          // from here we can enter our own HTML code to create the web page
          client.print("<head><title>Office Weather</title></head><body><h1>Office Temperature</h1><p>Temperature - ");
          client.print(t);
          client.print(" degrees Celsius</p>");
          if(t>0){
          if(t<28) client.print("Light On");
          else client.print("Light Off");
          }
          client.print("<p><em>Page refreshes every 5 seconds.</em></p></body></html>");
          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();
    Serial.println("client disonnected");
  }

}

To make clean, good and working code, please make the source code clean and look good.
Use Ctrl+T for the auto-text-layout tool, it is also in the Tools menu.
Always use your indents and brackets and spaces in the same way.

Is it an Arduino Uno and a Ethernet Shield ?
Pin 10 is the chip-select for the W5100 chip (Ethernet) and pin 4 is the chip-select for the microSD memory card.
You better not use pin 4 for something else.

All the analog pins of the Arduino Uno can be used as digital pins as well.

  if(t>0){
    if(t<28){
      digitalWrite(lightpin,HIGH);
    }

If it is between 0 and 28, turn the lights on. Why?

What does the temperature have to do with turning the lights on or off?

Why those limits? Surely, 0 degrees C is too cold to not have the heat on.

Comrade_Oscar:

  pinMode(vccPin, OUTPUT); 

digitalWrite(vccPin, HIGH);
 pinMode(gndPin, OUTPUT);
 digitalWrite(gndPin, LOW);

What is that controlling? This also begs the question, why not just connect the device to GND (and VCC) of the Arduino directly? If you want to do this bad practice, at least turn your ground pin on FIRST.

Is "lightpin" the LED inside of the solid state relay? Why in setup() do you directly use "8" and not the constant you created?

Have you removed the Ethernet code to see if the behavior changes?

For debugging, you could also make use of the LED on pin 13. Set it as an OUTPUT and then in loop add: digitalWrite(13, digitalRead(lightpin));
That way whatever state the lightpin is set to, your Pin 13 LED will reflect it. You can then just glance at the board to see if it should be on or not.

For debugging, you could also make use of the LED on pin 13. Set it as an OUTPUT and then in loop add: digitalWrite(13, digitalRead(lightpin));

Not if you are using the Ethernet shield...

I've noticed the code does not really work, unless there is someone actually on the web page of the arduino - which displays the temperature.

I see a meta refresh in your web page code. Is this supposed to somehow make the code monitor/control the temperature?

PaulS:

  if(t>0){

if(t<28){
      digitalWrite(lightpin,HIGH);
    }



If it is between 0 and 28, turn the lights on. Why?

What does the temperature have to do with turning the lights on or off?

Why those limits? Surely, 0 degrees C is too cold to not have the heat on.

Sorry I was not clear. The light pin, is a pin to the heat source - originally I did this code for an LED to test it out. I think I figured it out though.