Storage card micro SD and internet W5100 working together

Hi all,

I have a big problem very hard to understand and to make it to work. I was searching on internet and forum a lot to find a solution to my problem but, still nothing.

My device is Arduino UNO with internet shield attached W5100. WebServer works great, I mean reading from microSD files and displaying to internet clients has no conflicts. The problem is when I try to make a trace with an internet variable. I connect my arduino to internet, get the variable value and then I try to write the value to SD card. This is not working. I tried with all suggestions to activate and disable digital pins 10 and 4 for w5100 and SD, high/low all tests but nothing.

The most approach result was that the variable is got from internet, and when I try to write to my micro SD card file, it is created an empty file with the name trace.txt, nothing inside. If I try to write more variables to SD card without using internet, the file is written very well.

What can be the problem as soon as the file is created but nothing is written inside???

Thank you,

The best thing to do is post your sketch code and mention which Arduino you are using.

edit: My bad. I see you mentioned the Uno. You have probably run out of SRAM. Post your code.

Here below is the code:

#include <SPI.h>        
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <dht11.h>
#include <SD.h>

File myFile;
/*-----( Declare objects )-----*/
dht11 DHT11;

/*-----( Declare Constants, Pin Numbers )-----*/
#define DHT11PIN A1
int count_filew = 0;

byte mac[] = {  
  0xDE, 0xAD, 0xAC, 0xDE, 0xFE, 0xED };

unsigned int localPort = 8888;      // local port to listen for UDP packets

IPAddress timeServer(132, 163, 4, 101); // time-a.timefreq.bldrdoc.gov NTP server

const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets

// A UDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup()
{
  Serial.begin(9600);
   while (!Serial) {
    ; 
  }
  pinMode(10, OUTPUT);//sets w5100chip
  digitalWrite(10,HIGH);//disables w5100
  pinMode(4, OUTPUT);//sets SD chip
  digitalWrite(4,HIGH);//disables SD chip

  // start Ethernet and UDP
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    for(;;)
      ;
  }
  Udp.begin(localPort);
  
  Serial.begin(9600);
  Serial.println("DHT11 TEST PROGRAM ");
  Serial.print(F("LIBRARY VERSION: "));
  Serial.println(DHT11LIB_VERSION);
  Serial.println();
  Serial.print("Initializing SD card...");
  if (!SD.begin(4)) {
   Serial.println("initialization failed");
   return; 
  }
  Serial.println("Initialization done");
  
  
}

void loop()
{
  sendNTPpacket(timeServer); // send an NTP packet to a time server
int no_years = 0;
int no_months = 0;
int month = 1;
int theday;
int themonth;
int theyear;
int curfeb;
int epoch_st = 1970;
String date;
int partdate;
  delay(1000);  
  if ( Udp.parsePacket() ) {  
    // We've received a packet, read the data from it
    Udp.read(packetBuffer,NTP_PACKET_SIZE);  // read the packet into the buffer

    unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
    unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);  
    unsigned long secsSince1900 = highWord << 16 | lowWord;               

    const unsigned long seventyYears = 2208988800UL;    
    unsigned long epoch = secsSince1900 - seventyYears;                      
    Serial.print("today is: ");
    epoch=epoch+3*60*60;
    int u_days=epoch/60/60/24+1;
    while(u_days > 364){
      if(epoch_st%4 == 0){
         u_days-=366;
         epoch_st+=1;
      }
      else{
        u_days-=365;
        epoch_st+=1;
      }  
    }
    theyear=epoch_st;
    if(theyear%4 == 0){
       curfeb=29;
    }
    else{
       curfeb=28;
    }
    //ianuarie
    if(u_days > 31){
    month+=1;
    u_days-=31;
    }
    else{
    theday=u_days;
    }
    //februarie
    if(u_days>curfeb){
    month+=1;
    u_days-=curfeb;
    }
    else{
    theday=u_days;
    }
    //martie
    if(u_days > 31){
    month+=1;
    u_days-=31;
    }
    else{
    theday=u_days;
    }
    //aprilie
    if(u_days > 30){
    month+=1;
    u_days-=30;
    }
    else{
    theday=u_days;
    }
    //mai
    if(u_days > 31){
    month+=1;
    u_days-=31;
    }
    else{
    theday=u_days;
    }    
    //iunie
    if(u_days > 30){
    month+=1;
    u_days-=30;
    }
    else{
    theday=u_days;
    }   
    //iulie
    if(u_days > 31){
    month+=1;
    u_days-=31;
    }
    else{
    theday=u_days;
    }   
    //august
    if(u_days > 31){
    month+=1;
    u_days-=31;
    }
    else{
    theday=u_days;
    }   
    //septembrie
    if(u_days > 30){
    month+=1;
    u_days-=30;
    }
    else{
    theday=u_days;
    }   
    //octombrie
    if(u_days > 31){
    month+=1;
    u_days-=31;
    }
    else{
    theday=u_days;
    }   
    //noiembrie
    if(u_days > 30){
    month+=1;
    u_days-=30;
    }
    else{
    theday=u_days;
    }   
    //decembrie
    if(u_days > 31){
    month+=1;
    u_days-=31;
    }
    else{
    theday=u_days;
    }   
    themonth=month;
    theday=u_days;
    if(theday < 10){
    date="0";
    }    
    date=date + theday + '/';
        if(themonth < 10){
    date+='0';
    }

    date=date + themonth + '/';
    date=date + theyear + ' ';

    partdate = (epoch  % 86400L) / 3600;
    date = date + partdate + ':';
    if ( ((epoch % 3600) / 60) < 10 ) {
      date+='0';
    }

    partdate=(epoch  % 3600) / 60;
    date=date + partdate + ':';
    if ( (epoch % 60) < 10 ) {
      date+="0";
    }
    partdate=epoch %60;
    date+=partdate;
  }
  Serial.println(date);
  count_filew=count_filew+1;
  Serial.println("\n");

  int chk = DHT11.read(DHT11PIN);

  Serial.print("Read sensor: ");
  switch (chk)
  {
    case 0: Serial.println("OK"); break;
    case -1: Serial.println("Checksum error"); break;
    case -2: Serial.println("Time out error"); break;
    default: Serial.println("Unknown error"); break;
  }

  Serial.print("Humidity (%): ");
  Serial.println((float)DHT11.humidity, 2);

  Serial.print("Temperature (oC): ");
  Serial.println((float)DHT11.temperature, 2);

  Serial.print("Temperature (oF): ");
  Serial.println(Fahrenheit(DHT11.temperature), 2);

  Serial.print("Temperature (K): ");
  Serial.println(Kelvin(DHT11.temperature), 2);

  Serial.print("Dew Point (oC): ");
  Serial.println(dewPoint(DHT11.temperature, DHT11.humidity));

  Serial.print("Dew PointFast (oC): ");
  Serial.println(dewPointFast(DHT11.temperature, DHT11.humidity));

    Serial.print("count is ");
    Serial.println(count_filew);
    Serial.println(count_filew % 6);
   delay(10000);
  if (count_filew % 6 == 0){
  Serial.println("Just wrote to the file");
//digitalWrite(10,HIGH);
//digitalWrite(4,LOW);
  myFile = SD.open("monit.txt", FILE_WRITE);
     if (myFile) {
        myFile.print(date);
        myFile.print(" ===> Humidity (%):");
        myFile.print((float)DHT11.humidity, 2);
        myFile.print(", Temperature (oC):");
        myFile.print((float)DHT11.temperature, 2);
        myFile.print(", Temperature (oF):");
        myFile.print(Fahrenheit(DHT11.temperature), 2);
        myFile.print(", Temperature (K):");
        myFile.print(Kelvin(DHT11.temperature), 2);
        myFile.print(", DewPoint (oC):");
        myFile.print(dewPoint(DHT11.temperature, DHT11.humidity));
        myFile.print(", DewPointFast (oC):");
        myFile.print(dewPointFast(DHT11.temperature, DHT11.humidity)); 
        myFile.println(";");
        myFile.close();  
      //digitalWrite(10,LOW);
      //digitalWrite(4,HIGH);  
     }  
  delay(20000);
  }
}/* --(end main loop )-- */
/*-----( Declare User-written Functions )-----*/

unsigned long sendNTPpacket(IPAddress& address)
{
  // set all bytes in the buffer to 0
  memset(packetBuffer, 0, NTP_PACKET_SIZE);
  // Initialize values needed to form NTP request
  packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum, or type of clock
  packetBuffer[2] = 6;     // Polling Interval
  packetBuffer[3] = 0xEC;  // Peer Clock Precision
  // 8 bytes of zero for Root Delay & Root Dispersion
  packetBuffer[12]  = 49;
  packetBuffer[13]  = 0x4E;
  packetBuffer[14]  = 49;
  packetBuffer[15]  = 52;

  // all NTP fields have been given values, now
  // you can send a packet requesting a timestamp:         
  Udp.beginPacket(address, 123); //NTP requests are to port 123
  Udp.write(packetBuffer,NTP_PACKET_SIZE);
  Udp.endPacket();
}

//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
{
        return 1.8 * celsius + 32;
}

//Celsius to Kelvin conversion
double Kelvin(double celsius)
{
        return celsius + 273.15;
}

// dewPoint function NOAA
// reference: http://wahiduddin.net/calc/density_algorithms.htm 
double dewPoint(double celsius, double humidity)
{
        double A0= 373.15/(273.15 + celsius);
        double SUM = -7.90298 * (A0-1);
        SUM += 5.02808 * log10(A0);
        SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
        SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
        SUM += log10(1013.246);
        double VP = pow(10, SUM-3) * humidity;
        double T = log(VP/0.61078);   // temp var
        return (241.88 * T) / (17.558-T);
}

// delta max = 0.6544 wrt dewPoint()
// 5x faster than dewPoint()
// reference: http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double celsius, double humidity)
{
        double a = 17.271;
        double b = 237.7;
        double temp = (a * celsius) / (b + celsius) + log(humidity/100);
        double Td = (b * temp) / (a - temp);
        return Td;
}

You are probably running out of SRAM. Try using the F() macro to keep the static strings in program memory. And call Serial.begin() only once.

Here are examples from your code. Do the same to all other static strings in your code. See if that helps.

  Serial.println(F("DHT11 TEST PROGRAM "));
  Serial.print(F("LIBRARY VERSION: "));
  Serial.println(DHT11LIB_VERSION);
  Serial.println();
  Serial.print(F("Initializing SD card..."));
  if (!SD.begin(4)) {
   Serial.println(F("initialization failed"));
   return; 
  }
  Serial.println(F("Initialization done"));

Hi,

I can't believe it. Now it's working like smooth! It's great, thank you. I just deleted one Serial.begin because it was two times as mistake, and I used for print "F" function. This is strange to not use SDRAM, why not any error indication, why not a different function name than "F" attached to print???

Thank you again for your help, now I'm happy that NTP and my sensor is working great to trace logs to SD card.

Best regards,