Thermostat with ethernet optimization question.

So, I have a project that is over a year in the making. It used to be two Unos talking to each other over serial, and it worked fine for the most part. One was a stand alone thermostat and the other was a web server. Combining both sketches was too big for one Uno. Recently I purchased a mega and combined the sketches. It works pretty well but I'm wondering if there is any way to optimize the code and I'm looking for suggestions. It's a massive sketch so I really don't expect too many responses. Sometimes the web page is laggy and some times the buttons on the LCD are laggy and I think it's because one is busy and blocking the other. Not a huge deal, but I'm always tweaking it to save clock cycles here and there that might help it go a little faster. I looked into ISRs for the ethernet board but didn't find anything worth pursuing. Again, just looking for suggestions. Thanks!

My code can be found here: Thermostat for Mega.

My code can be found here

In the rubbish bin? Well, if that's where it belongs...

Use the Additional Options link to attach your code HERE if you really expect people to look at it.

It looks alright.
You might want to write the html page to client 64 bytes at a time instead of one at a time. It’s a really slow load. Just search around the forum.

Thank you, mistergreen. I will look into that.

Don’t read one character from the SD, then send one character in a packet to the client. That takes way too long. This sends 64 byte packets about 4 times faster.

                char tBuf[64];
                int clientCount;

                File myFile =;

                if(!myFile) {
                  Serial.println(F("open error"));
                else {
                  while(myFile.available()) {
                    clientCount =,64);

Awesome, thanks!

So with writing 64 byte packets, does that include other client.print()'s as well? Such as:

client.print(F("lots of HTML data")) or client.print(up to 64 bytes of variables) or should I just limit the 64 bytes to sending the web page?

Write as much at one time as you can. I picked 64 bytes so the array would not take a lot of SRAM.

client.println(F("Content-Type: text/xml"));
client.println(F("Connection: keep-alive"));

So how do I consolidate this to one line? “/n” between them?

Like this.

// change these
client.println(F("Content-Type: text/xml"));
client.println(F("Connection: keep-alive"));

// to this
client.println(F("Content-Type: text/xml\r\nConnection: keep-alive\r\n"));

// or this write instead of print is even faster
client.write("Content-Type: text/xml\r\nConnection: keep-alive\r\n\r\n");

Muchas gracias!

These are the kind of suggestions I’m looking for.

Sending the web page in 64 byte packets makes it load instantly!