SD card write very random, mostly not working

Hello everyone,
I am trying to read a bunch of DHT sensors, send the data to a web server, and back up the data to an SD card. I am using an Arduino MEGA Pro and a standard Ethernet shield. The Ethernet portion works just fine.

The problem I am having is I can open or create the data file, add the headers, and even enter the time stamp. When it comes to storing the actual data values, it only catches 1 out of maybe 200 attempts.

Here are my relevant code pieces, I think, if I missed something let me know:

#include <EEPROM.h>
#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <SD.h>
#include <MemoryFree.h>
#include <Adafruit_AM2315.h>
#include <DHT.h>
#include <RTClib.h>

const int chipSelect = 4;

File dataFile;

char filename[] = "D_Data.csv";
char transmission1[450];

void setup()
{
pinMode(SS, OUTPUT);

if (!SD.begin(chipSelect)) Serial.println("Card failed, or not present");
  
  if (!SD.exists(filename)) {
      // only open a new file if it doesn't exist
      dataFile = SD.open(filename, FILE_WRITE);
      delay(100);
      dataFile.print(F("Date,Time,"));
      dataFile.print(F("Temp1,Hum1,Temp2,Hum2,Temp3,Hum3,Temp4,Hum4,Temp5,Hum5,"));
      dataFile.print(F("Temp6,Hum6,Temp7,Hum7,Temp8,Hum8,Temp9,Hum9,Temp10,Hum10,"));
      dataFile.print(F("Temp11,Hum11,Temp12,Hum12,Temp13,Hum13,Temp14,Hum14,Temp15,Hum15,"));
      dataFile.println(F("Temp16,Hum16,Temp17,Hum17,Temp18,Hum18,Temp19,Hum19,Temp20,Hum20,ExtTemp,ExtHum"));
      dataFile.close(); 
           }


if (currentTime.unixtime() - lastConnectionTime.unixtime() > updateEmoncmsInterval)
  {
      startEthernet();        
     for (int i = 0; i < NUM_DHTS; i++)
     {
      // Read value from DHT Sensor
      float h = dhtList[i]->readHumidity();
      float t = dhtList[i]->readTemperature();
      if (isnan(t) || isnan(h)) 
        {
          memset(numhum[i], '\0', 6);
          memset(numtemp[i], '\0', 6);
        }
      else
       {
        
        dtostrf(h, 6, 1, numhum[i]);
        dtostrf(t, 6, 1, numtemp[i]);
        trim(numhum[i]);
        trim(numtemp[i]);
        }
      strcat(transmission1,numtemp[i]);
      strcat(transmission1, ",");
      strcat(transmission1, numhum[i]);
      strcat(transmission1, ",");
      
      
      Serial.print(i); Serial.print("     ");
      Serial.print((char*)numhum[i]); Serial.print("     ");
      Serial.println((char*)numtemp[i]);
      memset(numhum[i], '\0', 6);
      memset(numtemp[i], '\0', 6);
      }
       float eh = am2315.readHumidity();
       float et = am2315.readTemperature();
       if (isnan(et) || isnan(eh)) 
         {
          memset(exthum, '\0', 6);
          memset(exttemp, '\0', 6);

         }
      else
        {
         dtostrf(et, 6, 1, exttemp);
         dtostrf(eh, 6, 1, exthum);
         trim(exthum);
         trim(exttemp);
         }
      Serial.print("Ext Temp: ");
      Serial.println(exttemp);
      Serial.print("Ext Humidity: ");
      Serial.print(exthum);
       strcat(transmission1,exttemp);
       strcat(transmission1,",");
       strcat(transmission1,exthum);
     
     Serial.print(F("At the end of DHT read freeRam ="));
     Serial.println(freeRam());
     
  // Update Emoncms
  if(!client.connected())
  {
    Serial.println(transmission1);
        
    updateEmoncms(transmission1, writeNode);
    delay(100);
    client.stop();
    dataFile = SD.open(filename, FILE_WRITE);
    saveSDtime();
    dataFile.println(transmission1);
    delay(100);
    dataFile.close();

void saveSDtime()
{
  dataFile.print(currentTime.year(), DEC);
  dataFile.print(F("/"));
  dataFile.print(currentTime.month(), DEC);
  dataFile.print(F("/"));
  dataFile.print(currentTime.day(), DEC);
  dataFile.print(F(", "));
  dataFile.print(currentTime.hour(), DEC);
  dataFile.print(F(":"));
  if (currentTime.minute() < 10) dataFile.print(F("0"));
  dataFile.print(currentTime.minute(), DEC);
  dataFile.print(F(":"));
  if (currentTime.second() < 10) dataFile.print(F("0"));
  dataFile.print(currentTime.second(), DEC);
  dataFile.print(F(", "));
}

This is the original code. I am working also with another forum, but haven’t found a fix yet. I have also tried this code thinking maybe I needed to just print one value at a time:

      float h = dhtList[i]->readHumidity();
      float t = dhtList[i]->readTemperature();
      if (isnan(t) || isnan(h)) 
        {
          //memset(numhum[i], '\0', 6);
          //memset(numtemp[i], '\0', 6);
          h=0.0;
          t=0.0;
        }
 //     else
 //      {
        
        dtostrf(h, 6, 1, numhum[i]);
        dtostrf(t, 6, 1, numtemp[i]);
        trim(numhum[i]);
        trim(numtemp[i]);
 //       }
      strcat(transmission1,numtemp[i]);
      strcat(transmission1, ",");
      strcat(transmission1, numhum[i]);
      strcat(transmission1, ",");

//skip to this part
    saveSDtime();
    char* sddata;
    sddata = strtok(transmission1,",");
    while (sddata != NULL)
    {
      dataFile.print(sddata);
      dataFile.print(",");
      sddata = strtok(NULL,",");
      dataFile.flush();
      delay(10);
    }
    dataFile.println();
    delay(1000);
    dataFile.close();

I have included a sample data file to show what exactly I am talking about.
My string that gets sent to both the HTML call and the SD card generally looks like this:

21.1,90.6,21.4,83.8,0.0,0.0,21.4,71.4,20.9,81.4,21.6,63.7,21.6,68.3,21.6,65.1,21.7,72.5,21.8,69.7,21.3,71.7,21.4,81.5,21.0,83.1,21.7,63.9,21.2,72.1,21.4,66.4,21.7,67.5,21.5,66.2,21.3,69.7,21.2,70.1,20.6,65.5

Let me know your thoughts. Thanks.

Tim

D_DATA.csv (79.6 KB)

Here are my relevant code pieces, I think, if I missed something let me know

You missed the part about needing to post ALL of your code.

      Serial.print((char*)numhum[i]); Serial.print("     ");

It looks like numhum is a char array. Why do you feel the need to cast to a char pointer? If numhum isn't a char array, why isn't it?

In addition to PaulS, you must disable the w5100 SPI or the SD card read/write will be really erratic.
Also start the serial port before using it.

void setup()
{
  // disable w5100 SPI
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);

  Serial.begin(9600);

  pinMode(SS, OUTPUT);

  if (!SD.begin(chipSelect)) Serial.println("Card failed, or not present");

edit: On an Uno, the w5100 SPI disable is accomplished "by accident". The SD.begin() call sets D10 as OUTPUT and HIGH, disabling the w5100.

However, on a Mega, the SD.begin() call sets D53 to OUTPUT and HIGH, leaving D10 floating. This is bad news for the SD card.

They won’t let me post all my code (9500 character limit). I will post as much as I can with as little editing as possible.

#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <SD.h>
#include <Adafruit_AM2315.h>
#include <DHT.h>
#include <RTClib.h>


RTC_DS1307 rtc;

const int chipSelect = 4;

File dataFile;

// Box D Settings
   byte mac[] = {0xZZ, 0xAA, 0xDD, 0xFF, 0xD7, 0xWB};
    char writeNode[] = "node=3&csv=";
    char filename[] = "D_Data.csv";
char transmission1[450];

const long updateEmoncmsInterval = 90;      // Time interval in seconds to update Emoncms

DateTime lastConnectionTime = 0;
DateTime currentTime;
boolean lastConnected = false;
int failedCounter = 0;

EthernetClient client;

//DHT Settings

#define DHTTYPE DHT21

    DHT dht01(23, DHTTYPE);DHT dht02(22, DHTTYPE);DHT dht03(31, DHTTYPE);DHT dht04(30, DHTTYPE);
    DHT dht05(25, DHTTYPE);DHT dht06(24, DHTTYPE);DHT dht07(29, DHTTYPE);DHT dht08(28, DHTTYPE);
    DHT dht09(27, DHTTYPE);DHT dht10(26, DHTTYPE);DHT dht11(36, DHTTYPE);DHT dht12(37, DHTTYPE);
    DHT dht13(34, DHTTYPE);DHT dht14(35, DHTTYPE);DHT dht15(38, DHTTYPE);DHT dht16(39, DHTTYPE);
    DHT dht17(32, DHTTYPE);DHT dht18(33, DHTTYPE);DHT dht19(40, DHTTYPE);DHT dht20(41, DHTTYPE);

   DHT* dhtList[] = {&dht01, &dht02, &dht03, &dht04, &dht05, &dht06, &dht07, &dht08,
   &dht09, &dht10, &dht11, &dht12, &dht13, &dht14, &dht15, &dht16, &dht17, &dht18, &dht19, &dht20};


#define NUM_DHTS (sizeof(dhtList)/sizeof(DHT*))

typedef char humStr[7];  //4 digits + decimal point + 1 decimal + null terminator
typedef char tempStr[7]; //4 digits + decimal point + 1 decimal + null terminator


Adafruit_AM2315 am2315;
humStr numhum[NUM_DHTS];
tempStr numtemp[NUM_DHTS];
char exttemp[7];
char exthum[7];


void setup()
{
  Serial.begin(9600);
  pinMode(SS, OUTPUT);
  Wire.begin();
  rtc.begin();
  am2315.begin();
  
  if (!SD.begin(chipSelect)) Serial.println("Card failed, or not present");
  
  if (!SD.exists(filename)) {
      // only open a new file if it doesn't exist
      dataFile = SD.open(filename, FILE_WRITE);
      delay(100);
      dataFile.print(F("Date,Time,"));
      dataFile.print(F("Temp1,Hum1,Temp2,Hum2,Temp3,Hum3,Temp4,Hum4,Temp5,Hum5,"));
      dataFile.print(F("Temp6,Hum6,Temp7,Hum7,Temp8,Hum8,Temp9,Hum9,Temp10,Hum10,"));
      dataFile.print(F("Temp11,Hum11,Temp12,Hum12,Temp13,Hum13,Temp14,Hum14,Temp15,Hum15,"));
      dataFile.println(F("Temp16,Hum16,Temp17,Hum17,Temp18,Hum18,Temp19,Hum19,Temp20,Hum20,ExtTemp,ExtHum"));
      dataFile.close(); 
           }
    
  Serial.println(F("SD card initialized."));
  
  for (int i=0; i < NUM_DHTS; i ++)
  {
    dhtList[i]->begin();
  }

 
  Serial.print(F("After Setup freeRam ="));
  Serial.println(freeRam());
 
 }

void loop()
{
  currentTime = rtc.now();
  Serial.print(F("freeRam ="));
  Serial.println(freeRam());
  Serial.print(F("lastupdate:"));
  Serial.println(currentTime.unixtime() - lastConnectionTime.unixtime());
  
  serialprinttime();
  
  if (currentTime.unixtime() - lastConnectionTime.unixtime() > updateEmoncmsInterval)
  {
      startEthernet();        
     for (int i = 0; i < NUM_DHTS; i++)
     {
     
      float h = dhtList[i]->readHumidity();
      float t = dhtList[i]->readTemperature();
      if (isnan(t) || isnan(h)) 
        {
          //memset(numhum[i], '\0', 6);
          //memset(numtemp[i], '\0', 6);
          h=0.0;
          t=0.0;
        }
 //     else
 //      {
        
        dtostrf(h, 6, 1, numhum[i]);
        dtostrf(t, 6, 1, numtemp[i]);
        trim(numhum[i]);
        trim(numtemp[i]);
 //       }
      strcat(transmission1,numtemp[i]);
      strcat(transmission1, ",");
      strcat(transmission1, numhum[i]);
      strcat(transmission1, ",");
      
      
      Serial.print(i); Serial.print("     ");
      Serial.print((char*)numhum[i]); Serial.print("     ");
      Serial.println((char*)numtemp[i]);
      memset(numhum[i], '\0', 6);
      memset(numtemp[i], '\0', 6);
      }
       float eh = am2315.readHumidity();
       float et = am2315.readTemperature();
       if (isnan(et) || isnan(eh)) 
         {
          memset(exthum, '\0', 6);
          memset(exttemp, '\0', 6);

         }
      else
        {
         dtostrf(et, 6, 1, exttemp);
         dtostrf(eh, 6, 1, exthum);
         trim(exthum);
         trim(exttemp);
         }
      Serial.print("Ext Temp: ");
      Serial.println(exttemp);
      Serial.print("Ext Humidity: ");
      Serial.println(exthum);
       strcat(transmission1,exttemp);
       strcat(transmission1,",");
       strcat(transmission1,exthum);
     
     Serial.print(F("At the end of DHT read freeRam ="));
     Serial.println(freeRam());
     
  // Update Emoncms
  if(!client.connected())
  {
    Serial.println(transmission1);
        
    updateEmoncms(transmission1, writeNode);
    delay(1000);
    client.stop();
    dataFile = SD.open(filename, FILE_WRITE);
    saveSDtime();
    char* sddata;
    sddata = strtok(transmission1,",");
    while (sddata != NULL)
    {
      dataFile.print(sddata);
      dataFile.print(",");
      sddata = strtok(NULL,",");
      dataFile.flush();
      delay(10);
    }
    dataFile.println();
    delay(1000);
    dataFile.close();
    
    Serial.println(F("Sent Transmission"));
   }  
         
  memset(transmission1, '\0', 300);
  }
 
  if (!client.connected() && lastConnected)
  {
    
    Serial.println(F("...disconnected"));
    Serial.println();
    
    client.stop();
  }
  
  if (failedCounter > 3 ) 
  { 
    Serial.println(F("Checking failed counter"));
    startEthernet();

    }
  
  lastConnected = client.connected();
  
  
 delay(1000);
}

void updateEmoncms(char emData[450] , char writeNode[12])
{
  if (client.connect("mywebsite.com", 80))
  {         
    client.print(F("GET "));
    client.print(F("/emoncms/input/post.json?"));
    client.print(writeNode);
    client.print(emData);
    client.print(F("&apikey=dfXCXCXCXCXCXCXCXCXCXCXC2f29"));
    client.println(F(" HTTP/1.1"));
    client.println(F("Host: mywebsite.com"));
    client.print(F("User-Agent: Arduino-ethernet"));
    client.println(F("Connection: close"));
    client.println();
    
    lastConnectionTime = rtc.now();
    Serial.print(F("After Emoncms transmission freeRam ="));
    Serial.println(freeRam());
    memset(emData, '\0', 325);
    
    if (client.connected())
    {
      Serial.println(F("Connecting to Emoncms..."));
      Serial.println();
      
      failedCounter = 0;
    }
    else
    {
      failedCounter++;
  
      Serial.println("Connection to Emoncms failed ("+String(failedCounter, DEC)+")");   
      Serial.println();
    }
    
  }
  else
  {
    failedCounter++;
    
    Serial.println("Connection to Emoncms Failed ("+String(failedCounter, DEC)+")");   
    Serial.println();
    
     
  }
}


void startEthernet()
{
  
  client.stop();
 
  Serial.println(F("Connecting Arduino to network..."));
  Serial.println();  

  delay(1000);
  
  // Connect to network amd obtain an IP address using DHCP
  if (Ethernet.begin(mac) == 0)
  {
    Serial.println(F("DHCP Failed, reset Arduino to try again"));
    Serial.println();
  }
  else
  {
    Serial.println(F("Arduino connected to network using DHCP"));
    Serial.println();
  }
  
  delay(1000);
}

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

void serialprinttime()
{
  Serial.print(currentTime.year(), DEC);
    Serial.print('/');
    Serial.print(currentTime.month(), DEC);
    Serial.print('/');
    Serial.print(currentTime.day(), DEC);
    Serial.print(' ');
    Serial.print(currentTime.hour(), DEC);
    Serial.print(':');
    if (currentTime.minute() < 10) Serial.print(F("0"));
    Serial.print(currentTime.minute(), DEC);
    Serial.print(':');
    if (currentTime.second() < 10) Serial.print(F("0"));
    Serial.print(currentTime.second(), DEC);
    Serial.println();
}
void saveSDtime()
{
  dataFile.print(currentTime.year(), DEC);
  dataFile.print(F("/"));
  dataFile.print(currentTime.month(), DEC);
  dataFile.print(F("/"));
  dataFile.print(currentTime.day(), DEC);
  dataFile.print(F(", "));
  dataFile.print(currentTime.hour(), DEC);
  dataFile.print(F(":"));
  if (currentTime.minute() < 10) dataFile.print(F("0"));
  dataFile.print(currentTime.minute(), DEC);
  dataFile.print(F(":"));
  if (currentTime.second() < 10) dataFile.print(F("0"));
  dataFile.print(currentTime.second(), DEC);
  dataFile.print(F(", "));
}

void trim(char* _str)
{
  while( (strlen(_str) > 0) && (_str[0] == 32) )
  {
     strcpy(_str,&_str[1]);
  }

  while( (strlen(_str) > 0) && (_str[strlen(_str) - 1] == 32) )
  {
    _str[strlen(_str) - 1] = 0;
  }  
}

numhum and numtemp are typedef char. It wouldn’t serial.print until I cast them as char*.

I’m using both the W5100 and the SD card SPI, do I need to explicitly activate/deactivate each system before and after using it?

T

In addition to PaulS, you must disable the w5100 SPI

Hey. No, wait. You don't have to disable me!

typedef char humStr[7];  //4 digits + decimal point + 1 decimal + null terminator
typedef char tempStr[7]; //4 digits + decimal point + 1 decimal + null terminator

You clearly don't understand what typedef is for. Do not use it until you do.

PaulS:

In addition to PaulS, you must disable the w5100 SPI

Hey. No, wait. You don’t have to disable me!

LOL :slight_smile:

TatankaWilly:
I’m using both the W5100 and the SD card SPI, do I need to explicitly activate/deactivate each system before and after using it?

You must disable all SPI devices before starting any of them, or you will have problems. Try adding my mods to your code. Once they are started, the library functions will control the slave selects, but until you have them all running, you must disable them before calling any begin functions.

I did not disable the SD card (D4) because you are going to immediately start it, and that will leave that slave select disabled after the SD.begin(4) call.

No love.

Made the code modifications:

void setup()
{
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
  // Start Serial for debugging on the Serial Monitor
  Serial.begin(9600);
  pinMode(SS, OUTPUT);
  Wire.begin();
  rtc.begin();
  am2315.begin();
  
  if (!SD.begin(chipSelect)) Serial.println("Card failed, or not present");
  
  if (!SD.exists(filename)) {
      // only open a new file if it doesn't exist
      dataFile = SD.open(filename, FILE_WRITE);
      delay(100);
      dataFile.print(F("Date,Time,"));
      dataFile.print(F("Temp1,Hum1,Temp2,Hum2,Temp3,Hum3,Temp4,Hum4,Temp5,Hum5,"));
      dataFile.print(F("Temp6,Hum6,Temp7,Hum7,Temp8,Hum8,Temp9,Hum9,Temp10,Hum10,"));
      dataFile.print(F("Temp11,Hum11,Temp12,Hum12,Temp13,Hum13,Temp14,Hum14,Temp15,Hum15,"));
      dataFile.println(F("Temp16,Hum16,Temp17,Hum17,Temp18,Hum18,Temp19,Hum19,Temp20,Hum20,ExtTemp,ExtHum"));
      dataFile.close(); 
           }

And the same behavior. It opens/creates the file. Writes the header information just fine. Writes the time stamp. Not the data.

Now this was with the just print out the whole transmission1 in one big go, like this:

dataFile = SD.open(filename, FILE_WRITE);
    saveSDtime();
    dataFile.println(transmission1);

.
I may try parsing and saving one item at a time. No luck with this method either:

   dataFile = SD.open(filename, FILE_WRITE);
    saveSDtime();
 //   dataFile.println(transmission1);
    char* sddata;
    sddata = strtok(transmission1,",");
    while (sddata != NULL)
    {
      dataFile.print(sddata);
      dataFile.print(",");
      sddata = strtok(NULL,",");
      dataFile.flush();
      delay(10);
    }
    dataFile.println();
    delay(1000);
    dataFile.close();

Tim

D_DATA.CSV (356 Bytes)

Made the code modifications

I suggest that you check with the fine folks at http://snippets-r-us.com for help with your snippets.

Have you tried the SD without all those other devices connected to the Arduino? Have you considered the possibility it may be a power problem? Or one of those other devices is interfering with the SD in some way?

Did you bother to try each device separately before throwing that sketch together? I recommend starting with the ethernet shield. Get the w5100 and the SD working, then add one device at a time, insuring each device works with your current setup before adding another.

Somehow I have fumbled across a solution. Even went so far as to go back and start reading the build process, and came across this little statement:

To use tabs with a .h extension, you need to #include it (using “double quotes” not ).

Well, ALL my #include statements had a .h extension, so I made them quotes instead of brackets. I have to admit I don’t think that fixed anything but I thought it might bear mentioning just in the interest of full disclosure.

I moved things around a little bit in the data recording process. I moved the SD portion out of the if connected loop, and made it just after the completion of the transmission1 array. I also moved the start ethernet function to AFTER the SD card write. You already have the code from above. Here are the portions I changed:

#include "SPI.h"
#include "Ethernet.h"
#include "Wire.h"
#include "SD.h"
#include "MemoryFree.h"
#include "Adafruit_AM2315.h"
#include "DHT.h"
#include "RTClib.h"

////And my main update loop.....

if (currentTime.unixtime() - lastConnectionTime.unixtime() > updateEmoncmsInterval)
  {
             
     for (int i = 0; i < NUM_DHTS; i++)
     {
      // Read value from DHT Sensor
      float h = dhtList[i]->readHumidity();
      float t = dhtList[i]->readTemperature();
      if (isnan(t) || isnan(h)) 
        {
          h=0.0;
          t=0.0;
        }
         
      dtostrf(h, 6, 1, numhum[i]);
      dtostrf(t, 6, 1, numtemp[i]);
      trim(numhum[i]);
      trim(numtemp[i]);
 
      strcat(transmission1,numtemp[i]);
      strcat(transmission1, ",");
      strcat(transmission1, numhum[i]);
      strcat(transmission1, ",");
      
      
      Serial.print(i); Serial.print("     ");
      Serial.print((char*)numhum[i]); Serial.print("     ");
      Serial.println((char*)numtemp[i]);
      memset(numhum[i], '\0', 6);
      memset(numtemp[i], '\0', 6);
      }
       float eh = am2315.readHumidity();
       float et = am2315.readTemperature();
       if (isnan(et) || isnan(eh)) 
         {
          memset(exthum, '\0', 6);
          memset(exttemp, '\0', 6);

         }
      else
        {
         dtostrf(et, 6, 1, exttemp);
         dtostrf(eh, 6, 1, exthum);
         trim(exthum);
         trim(exttemp);
         }
      Serial.print("Ext Temp: ");
      Serial.println(exttemp);
      Serial.print("Ext Humidity: ");
      Serial.println(exthum);
       strcat(transmission1,exttemp);
       strcat(transmission1,",");
       strcat(transmission1,exthum);
     
     Serial.print(F("At the end of DHT read freeRam ="));
     Serial.println(freeRam());
  //Update SD card
  dataFile = SD.open(filename, FILE_WRITE);
  saveSDtime();
  dataFile.println(transmission1);
  delay(1000);
  dataFile.close();
     
  // Update Emoncms
   startEthernet();
  if(!client.connected())
  {
    Serial.println(transmission1);
        
    updateEmoncms(transmission1, writeNode);
    delay(1000);
    client.stop();
    
    Serial.println(F("Sent Transmission"));
   }  
           
  memset(transmission1, '\0', 300);
  }

PaulS- I tried getting rid of my wrong use of the typedef (which I have to admit, I don’t think it’s wrong and it works), and using just a standard char* array for the storage of my readings, but the controller just sat there and continuously reset itself. Please expand on how you would have made an array such as I use for storing the DHT readings.

Tim

PaulS- I tried getting rid of my wrong use of the typedef (which I have to admit, I don't think it's wrong and it works), and using just a standard char* array for the storage of my readings, but the controller just sat there and continuously reset itself. Please expand on how you would have made an array such as I use for storing the DHT readings.

char humStr[7];  //4 digits + decimal point + 1 decimal + null terminator
char tempStr[7]; //4 digits + decimal point + 1 decimal + null terminator

If you did that, and the controller kept resetting, you are doing something else wrong.

      Serial.print((char*)numhum[i]); Serial.print("     ");

numhum[ i ] is ALREADY a char *. The cast is pointless. Why are you doing it?

Paul,
Well, as I have come to expect, you are right. I remember I put those there for a reason. In fact, you can look at the actual conversation that led up to here:

https://forums.adafruit.com/viewtopic.php?f=19&t=48012

I was getting an "call of overloaded 'print(DHT*&)" error and I applied the "fix" to all of them. Well, at some point later I thought that was rather stupid and just had it print i during the loop and didn't bother to see of I still needed the other cast.

It's almost embarrassing.

Tim

Hey, I'm just a beginner. But I just had a similar problem getting the SD card to store data.

Is chipSelect= 4 for the SD? If yes:

In your code you got: pinMode(SS, OUTPUT);

"SS" is not correct. It should be "chipSelect".

You may also want to store the data more frequently using: dataFile.flush();

Gerry

Hey Gerry,
Yes, the chipSelect is for the SD card.
The variable SS (Slave Select) is built into the SPI library and each Arduino has its own standard SS pin. In the MEGA's instance, the standard pin that the SPI library tries to use is pin 53. I am not using that pin, but since I am using the SPI library, I have to declare that pin as an OUTPUT.
About 4 lines down from my declaration for SS, you will see my SD.begin statement. In that statement, when I put (chipSelect) in the begin call, I am telling the SPI library that the slave select for the SD card is Pin 4.
dataFile.close() and dataFile.flush() do the same thing in regards to telling the SD card to save the data. dataFile.close() goes ahead and closes the file as well. This project is planned to be in operation with no human interaction for about 2 years, I am only saving data every 5 minutes. I don't want to keep the file open if the box is just going to sit there; there is a lot of time for a power outage or something else to corrupt the data file. Plus, my experience with this project leans me towards keeping the SD card and Ethernet operations completely separate from one another. Completely shut one down before using the other.

Thanks for the thoughts.

Tim

Thanks for the info in SS Tim. I had the same problem as you have storing data to SD card. I don't now know exactly what fixed the problem. Evidently, changing pinMode(SS, OUTPUT); to "pinMode(chipSelect, OUTPUT);" was not the fix. I just went back to my code and tried "pinMode(SS, OUTPUT);". Either one works.

I used a different string conversion:
char data[50];
sprintf(data, "%d,%d,%d,%d,%d", count,x,y,z,MeasurementError);
dataFile.println(data);
dataFile.flush(); // write to SD card

I would use the flush() command after each dataFile.print and close the file at the end.

Gerry