My data logger freezes/crashs at random

I am trying to make an environmental controller/data logger for a house.
But it randomly crashs.

I want to be able to control the house energy usage e.g. turn the radiators on and off based on the house usage i.e. if there is someone there, the weather outside etc.
Later I want this data to be online and the house to be controllable.
I want to use: Ethernet shield (with micro SD)+ sensor shield+ DS18B20's + lots of relays +DHT11+ IR motion+ RFDI's+ water Flow meters+ power meters.

But at the moment I have stripped this back to make a simple data logging radiator switch, in hope to find the problem. But no such luck. It freezes randomly???

So I have an Arduino Uno + Ethernet shield (with micro SD) + sensor shield+ DS18B20 +2x relays.
Here is the code (in next post,its to long) and I have attached the last data set recorded (it worked two days).

Any ideas?

DATALOG.CSV (1.52 MB)

/-----( Import needed libraries )-----/
#include <Wire.h>
#include "RTClib.h"
#include <OneWire.h> //DS18B20
#include <DallasTemperature.h> //DS18B20
#include <SD.h>

/-----( Declare objects )-----/
RTC_DS1307 rtc;
int Radiator = 0;

/-----( Declare Constants, Pin Numbers )-----/
#define ONE_WIRE_BUS 3 //DS18B20 connected to..
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);// Pass our oneWire reference to Dallas Temperature.

//* SD card attached to SPI bus as follows:
//** MOSI - pin 11
//** MISO - pin 12
//** CLK - pin 13
//** CS - pin 4
// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work
const int chipSelect = 4;

void setup () {
Serial.begin(9600);
//RTC=================================================
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#ifdef AVR
Wire.begin();
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
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));
}

// locate DS18B20 on the bus
Serial.print("Locating DS18B20...");
sensors.begin();

//relays======
//There are two relays connected the live and netreul wires (there is no way to know which wire is live on a european plug) on a radiator.
pinMode(7, OUTPUT);
digitalWrite(7, LOW);
pinMode(6, OUTPUT);
digitalWrite(6, LOW);

//SD Card===============================
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);

// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");

}

///FINALLY THE LOOP===========================================================

void loop ()
{
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();

//DS18B20=================================================
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");

// print out the data
Serial.println(sensors.getTempCByIndex(0));

//relay=================================================
if(sensors.getTempCByIndex(0)<19){
//turn on the radiator, the room is cold
digitalWrite(7, HIGH);
digitalWrite(6, HIGH);
Radiator=1; //this is used to datalog wheather the radiator is on or off
}
else if (sensors.getTempCByIndex(0)>21){
//turn off the radiator, the room is warm
digitalWrite(7, LOW);
digitalWrite(6, LOW);
Radiator=0;
}

Serial.print("Radiator is ");
Serial.println(Radiator);

//SD Card===============================
// This is datalogging the above sensors

// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.csv", FILE_WRITE);

// if the file is available, write to it:
if (dataFile) {
//Save the date and time:
dataFile.print(now.year(), DEC);
dataFile.print('/');
dataFile.print(now.month(), DEC);
dataFile.print('/');
dataFile.print(now.day(), DEC);
dataFile.print(' ');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
dataFile.print(now.second(), DEC);
dataFile.print(',');

//Save the temp
dataFile.print(sensors.getTempCByIndex(0));
dataFile.print(',');

dataFile.println(Radiator);

dataFile.close();

}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}

Serial.println();
Serial.println();
delay(30000);
}

//END of LOOP code

/-----( Declare User-written Functions )-----/

//none

I want to use: Ethernet shield (with micro SD)+ sensor shield+ DS18B20's + lots of relays +DHT11+ IR motion+ RFDI's+ water Flow meters+ power meters.

Are you using an ethernet shield for the uSD slot now?

I have also tried changing the power supply.
I first used the 12v 1A which is meant to be for the Arduino, into the power jack on the board.
Now I am using an old mobile phone charger which is 5v 0.7A, into the gnd and vcc on the sensor shield, this seams to be better but still crashs.

note: It doesn't appear to crash if it is connected to my laptop through the usb cable.
However I haven't done this for really prolonged periods (at most one day).

Yes its data logging into the micro SD

If it is power related, that would explain the randomness of the fail. The w5100 uses quite a bit of power. The SD uses a bunch of power to write. If you are using a 12v 1A power supply, then I would recommend changing to a power supply at no more than 9 volts. I use a 7.5v wall wart to power my Mega, ethernet and sd combo, and the regulator still gets pretty warm.

These are linear voltage regulators, so the higher the input voltage, the hotter they get to control the output voltage. Most new regulators have an overtemp shutdown. I have not checked the ones used on the Uno or Mega.

edit: Do not use less than 7 volts into the power jack.

ok thanks
I'll try and find a 9v power supply.
But what about the 5v 0.75A I am presently using?

Also
with the over temp in mind (and without another power at this moment)
I am going to log the onboard temp sensor http://forum.arduino.cc/index.php/topic,8140.0.html.
I know this can only show if the over temp is the issue, as it fails, but will be nice to see.

I'll let you know if this is the case.

I also have the same problem with freezing and besides I get weird shapes instead of numbers! could you solve your problem?

Edit the post with your code in to use the code tags not the quote tags it will make your code readable!

Mark

The odds are your using more ram than you have!

The strings constants are using ram, eg Serial.print(" thingy thingy"); use the F() macro (look in the playground) this will move these strings out of RAM and into program memory and free up a lot of your ram. On the uno you have only 2k of SRAM, and not that much more on any of the other AVR chips.

Mark