0

i have a climate control software which reports data back through the web browser. Everything seems to be working however I am running into an issue where

1 - after a bit it seems the page doesnt refresh completely and instead displays a long list of the same webpage, so i have like 30 pages of the same thing as you scroll down

2 - when i woke up in the morning i could not load the page at all

What i am wondering is how can i get this to be stable and reliable so that the climate control software / sensors can run non-stop.

Currently as long as i dont use the ethernet shield reporting everything else seems to work great, i leave it on for 12 -20 hours and it still runs, but not with the ethernet reporting.

any help wold be apreciated thanks.

this is my code:

//Define Start
#define NOTE_D2  73 //sound note for beep
#define DHTPIN 2 
#define DHTTYPE DHT22
#define DHT2PIN 3 
#define DHTTYPE2 DHT22 
#define MAGENTA //Define a color for screen 
#define sclk 8 //screen pin
#define mosi 5 //screen pin
#define cs   6 //screen pin
#define dc   7 //screen pin
#define rst  rstpin  // you can also connect this to the Arduino reset
#define ANALOG_IN 0 // for cds light sensor

//Define End
//Include start
#include <DHT.h>//dht sensor library
#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>//needed to run screen
#include <Wire.h>
#include "RTClib.h"
#include <Ethernet.h>
//Include end
//declare start
DHT dht2(DHT2PIN, DHTTYPE2);
DHT dht(DHTPIN, DHTTYPE);
int powerswitch1 = A0;
int powerswitch2 = A1;
const int ledPin =  A2;
const int ledPin2 =  A3;
Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, mosi, sclk);
char* myString = { "OFF" };//relay1 on / off status
char* myString2 = { "OFF" };//relay2 on / off status
const int chipSelect = 4;
RTC_DS1307 RTC;
byte mac[] = {
  0x90, 0xA2, 0xDA, 0x00, 0x23, 0x36 }; //MAC address found on the back of your ethernet shield.
IPAddress ip(192,168,1,40);
//decalare end
EthernetServer server(80);


void setup() {
pinMode(ledPin, OUTPUT); 
pinMode(ledPin2, OUTPUT);
tft.initR(INITR_BLACKTAB);   // initialize a ST7735S chip, black tab
tft.fillScreen(ST7735_RED);//make screen red
  pinMode(powerswitch1, OUTPUT);
  pinMode(powerswitch2, OUTPUT);
  Serial.begin(9600);
  Wire.begin();
  RTC.begin();
if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
 //   RTC.adjust(DateTime(__DATE__, __TIME__));
  }

// get time to display "sensor up time"
  uint16_t time = millis();
 // tft.fillScreen(ST7735_BLACK); //black screen
  time = millis() - time;

Serial.println(time, DEC);

  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}
void loop(){
  //Tone Start  
    tone(9, NOTE_D2);
    delay(65);
    noTone(9);
//Tone End  

//clock start
  DateTime now = RTC.now();

    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();

 //hour conversion to 12 hour start
   int x;
   int timeresult;
   x = (now.hour());
   if (x > 12) {
   timeresult = x - 12;
   }
   else 
   {
    timeresult = x;
   }
   Serial.println(timeresult);
    //hour conversion to 24 hour start
//clock end
//sensor 1 start
    //Serial.println("read DHT22 #1");
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    float f = dht.readTemperature(true);
    //sensor 1 end
    //sensor 2 start
    //Serial.println("read DHT22 #2");
    float h2 = dht2.readHumidity();
    float t2 = dht2.readTemperature();
    float f2 = dht2.readTemperature(true);
    //sensor 2 end
    //temp average begin
    //Serial.println("average calculation");
    float havg = (h + h2) / 2;
    float favg = (f + f2) / 2;
    float tavg = (t + t2) / 2;
    //temp average end

//Relay 1 Start
    if (h < 42) //top threshold
    {
        if (h < 39) //bottom threshhold
        {
            digitalWrite(powerswitch1, HIGH);
            // Turn the Powertail on
            Serial.println("Switch ON");
digitalWrite(ledPin, HIGH);
            myString = "ON";
        }

    }
    else
    {
                myString = "OFF";
        digitalWrite(powerswitch1, LOW);    // turn the Powertail off
digitalWrite(ledPin, LOW);
        Serial.println("Switch OFF");

    }
//Relay 1 End
//Relay 2 Start
    if (f < 60) //top threshold
    {
        if (h < 60) //bottom threshhold
        {
            digitalWrite(powerswitch2, HIGH);
            // Turn the Powertail on
            Serial.println("Switch ON");
digitalWrite(ledPin2, HIGH);
            myString2 = "ON";
        }

    }
    else
    {
                myString2 = "OFF";
        digitalWrite(powerswitch2, LOW);    // turn the Powertail off
digitalWrite(ledPin2, LOW);
        Serial.println("Switch OFF");

    }
//Relay 2 End
//Screen start

delay(500);

tft.fillRect(20,19,80,140, ST7735_RED);//clear text
//(left wall,top wall px from top,right wall,bottom wall px from top)
tft.drawRect(20,19,80,140, ST7735_RED);//cleartext
//active text
tft.setCursor(35, 20);
tft.setTextSize(2);
tft.setTextColor(ST7735_WHITE);
tft.println(f);
tft.setCursor(35, 40);
tft.setTextSize(2);
tft.setTextColor(ST7735_WHITE);
tft.println(t);
tft.setCursor(35, 60);
tft.setTextSize(2);
tft.setTextColor(ST7735_WHITE);
tft.println(h);
tft.setCursor(25, 120);
tft.setTextSize(2);
tft.setTextColor(ST7735_WHITE);
tft.println(timeresult);
tft.setCursor(50, 120);
tft.setTextSize(2);
tft.setTextColor(ST7735_WHITE);
tft.println(now.minute(), DEC);
tft.setCursor(75, 120);
tft.setTextSize(2);
tft.setTextColor(ST7735_WHITE);
tft.println(now.second(), DEC);

//static text

tft.setCursor(105, 20);
tft.setTextSize(2);
tft.setTextColor(ST7735_WHITE);
tft.println("F");
tft.setCursor(105, 40);
tft.setTextSize(2);
tft.setTextColor(ST7735_WHITE);
tft.println("C");
tft.setCursor(105, 60);
tft.setTextSize(2);
tft.setTextColor(ST7735_WHITE);
tft.println("%");
tft.setCursor(52, 80);
tft.setTextSize(2);
tft.setTextColor(ST7735_WHITE);
tft.println(myString);
tft.setCursor(105, 80);
tft.setTextSize(2);
tft.setTextColor(ST7735_WHITE);
tft.println("H");
tft.setCursor(52, 100);
tft.setTextSize(2);
tft.setTextColor(ST7735_WHITE);
tft.println(myString2);
tft.setCursor(105, 100);
tft.setTextSize(2);
tft.setTextColor(ST7735_WHITE);
tft.println("T");
tft.setCursor(52, 140);
tft.setTextSize(2);
tft.setTextColor(ST7735_WHITE);
tft.println(millis() / 1000);
tft.setCursor(105, 140);
tft.setTextSize(2);
tft.setTextColor(ST7735_WHITE);
tft.println("-");






tft.print(millis() / 1000);
//Screen end




 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 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.println("<body>");
          /////CSS
            client.println("<style>");
            client.println("#left {");
            client.println("float:left;");
            client.println("}");
            client.println("text1 {");
            client.println("color: #07BBF2;");
            client.println("font-size: 200%;");
            client.println("}");
            client.println("text2 {");
            client.println("color: #FC5400;");
            client.println("font-size: 200%;");
            client.println("}");
            client.println("text3 {");
            client.println("color: #02BA3C;");
            client.println("font-size: 200%;");
            client.println("}");
            client.println("text4 {");
            client.println("color: #EBF700;");
            client.println("font-size: 200%;");
            client.println("}");
            client.println("text5 {");
            client.println("color: #F7C600;");
            client.println("font-size: 200%;");
            client.println("}");
            client.println("text6 {");
            client.println("color: #000000;");
            client.println("font-size: 200%;");
            client.println("}");
            if (myString == "ON") {
            client.println("humidifier {");
            client.println("color: #02F04E;");
            client.println("font-size: 100%;");
            client.println("}");
            }
            else {
              client.println("humidifier {");
            client.println("color: #FC5400;");
            client.println("font-size: 200%;");
            client.println("}");
            }
            if (myString2 == "ON") {
            client.println("heater {");
            client.println("color: #02F04E;");
            client.println("font-size: 200%;");
            client.println("}");
            }
            else {
             client.println("heater {");
            client.println("color: #FC5400;");
            client.println("font-size: 200%;");
            client.println("}");
            }

             client.println("#right {");
            client.println("float:right;");
            //client.println("color:red;");
            client.println("}");
            // client.println("#header {");
           // client.println("background-color:black;");
           // client.println("}");



            client.println("#content {");
            client.println("background-color:grey;");
            client.println("width:500px;");
            client.println("height:200px;");
            client.println("}");
            client.println("</style>");
          ///CSS

          client.println("<div id = content>");
          client.println("<body bgcolor=""#000000::>"); //how to change color adruino version

            client.println("<div id = left>");
            client.println("<text4>");
            client.print("Sensor 1");
            client.println("</text4>");
            client.println("<p />");
            client.println("<text1>");
            client.print(h);
            client.print(" %\t");
            client.println("</text1>");
            client.println("<p />"); 
            client.println("<text2>");
            client.print(f);
            client.println(" &#176;");
            client.println("F");
            client.println("</text2>");
            client.println("<p />");
            client.println("<text2>");
            client.print(t);
            client.println(" &#176;");
            client.println("C");
            client.println("</text2>");
            client.println("<p />");
            client.println("</div>");
            client.println("<div id = right>");
            client.println("<text4>");
            client.print("Sensor 2");
            client.println("</text4>");
            client.println("<p />");
            client.println("<text1>");
            client.print(h2);
            client.print(" %\t");
            client.println("</text1>");
            client.println("<p />"); 
            client.println("<text2>");
            client.print(f2);
            client.println(" &#176;");
            client.println("F");
            client.println("</text2>");
            client.println("<p />");
            client.println("<text2>");
            client.print(t);
            client.println(" &#176;");
            client.println("C");
            client.println("</text2>");
            client.println("<p />");
            client.println("</div>");
            client.println("<div align=center>");
            client.println("<text5>");
            client.print("Humidifier: ");
            client.println("</text5>");
            client.println("<humidifier>");
            client.print(myString);
            client.println("</humidifier>");
            client.println("<text5>");
            client.print("Heater: ");
            client.println("</text5>");
            client.println("<heater>");
            client.print(myString2);
            client.println("</heater>");
            client.println("<text6>");
            client.println("<p />");
            client.print(millis() / 1000);
            client.println("</text6>");
            client.println("<text6>");
            client.print(timeresult);
            client.print(":");
            client.print(now.minute(), DEC);
            client.print(":");
            client.print(now.second(), DEC);
            client.println("</text6>");
             client.println("</div>"); 
              client.println("</div>");
          client.println("</body>");
          client.println("</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");
  }


delay(3000);

}
1

I have had to program in a reset for the network connection on quite a few of my ethernet projects. I am not entirely sure the cause, but adding a close connection; open connection to run every x minutes seems to have helped prevent both the duplicating content and the timeout issues.

0

I would go for a memory issue - try to rewrite your code in a way that consumes less memory, the easiest first step is to avoid any Serial.printl("String"); and replace that with Serial.println(F("String"));. But in general you are using a lot of String objects which should be avoided if possible (try to use char() instead).

To check memory consumption of your sketch you can use a function like this and log the values from specific points in the code:

int freeRam () {
extern int __heap_start, *__brkval; 
int v; 
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); 
}
0

I used to encounter weird issues, e.g. starting a new instance of a program with Arduino Yun. The issue in the end was power supply, a large capacitor before the voltage regulator has solved the problem. What do you use for power supply?

  • Hey, i just started this arduino stuff like 3 weeks ago and have literally 0 knowledge of terminology or anything what so ever. I know this power issue is something of concern. Currently it has been powered directly from USB, now starting today, (a few hours after this post) i am using the ac power adapter directly to the arduino Uno. I will leave it on till tomorrow to see what happens. I dont really understand much about the voltage regulation or anything like that. Im learning though. :P – user3169964 Jan 26 '15 at 0:18
  • I have noticed lower than 5V voltages when powering Arduino from the computer's USB. Better results were with ordinary mobile phone chargers/power adapters (USB), but the most stable power supply was with a dedicated AC/DC transformer/rectifier to 9V and then reduced to 5V with a linear regulator (LM7805 or similar + capacitors). The last version has the highest losses, needs additional circuit, but works in rather harsh conditions. Might be overkill for home, if ordinary AC power adapter works, stick to it. And as Stephan pointed out, checking free memory is your friend, too. – Robert Špendl Jan 26 '15 at 12:10
  • Thanks for the info. So far its been 2+ days and it has been running non stop with AC power brick, i am going to give it 3 days until i consider it stable. – user3169964 Jan 28 '15 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.