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");
}
}