Not able to log startup after cold boot

Hi all,

I have a really strange behavior of the Yun after cold boot. I would like to write a log entry to a logfile on the sd card in the method "setup" of my sketch. The strange thing is that no log entry is written to the sd card when I power on the Yun (cold boot). If the Yun is already running and I reset the ATmega32u4 by pressing the reset button, the setup method is logged correctly. On cold boot, I am absolutely sure that the setup method is also called because otherwise my application wouldn't work.
Can someone explain this behavior?

Here is a shortened sketch which I am using:

#include <Bridge.h>
#include <FileIO.h>

void setup()
{
  FileSystem.begin();
  Bridge.begin();  
  writeToLog("Setup");
}

void loop()
{
}

void writeToLog(String message)
{
  String dateTime;
  Process time;
  time.begin("date");
  time.addParameter("+%D %T");  
  time.run();

  while(time.available()>0)
  {
    char c = time.read();
    if(c != '\n')
      dateTime += c;
  }
  
  File file = FileSystem.open("/mnt/sd/arduino/www/WSN/log.txt", FILE_APPEND);

  if (file)
  { 
    file.println(dateTime + ": " + message);
    file.close();
  }
}

Thanks in advance!

Bye,
Sebastian

Hi Sebastian,

it tooks some time for the SD card to be auto-mounted, maybe the sketch starts too early?
Would you like to test by adding a delay(20000) between Bridge.begin() and writeToLog("Setup")?

The Leonardo side starts up much faster than the Linino side, I think mine takes a little over 60 seconds to complete all the startup work. The last thing I see when rebooting (the button by the leds) with the YunSerialTerminal sketch running is wlan0: associated which means Linino has a DHCP lease from my router.
While for a different purpose some people are doing this which waits for the chatter to pause for 1 second to know Uboot has completed:
http://forum.arduino.cc//index.php?topic=191820.msg1436262#msg1436262
There are other processes at work that pause for more than 1 second, you may need to modify this to detect when Linino is ready to write your info to the SD card.

Thanks for your replies!
You are right, the file system was not yet ready!
I am simply waiting as long as my logging directory gets available. After that the log entry is written.
But, I had another problem. In my log file I saw the log entry with a timestamp of "08/08/13 22:42:19". It seems that the clock of the Yun falls back to it's factory settings on every reboot.
Therfore I am checking the time in a loop till the date is synchronized with a time server.
I have modified my sketch like the following:

#include <Bridge.h>
#include <FileIO.h>

void setup()
{
  Bridge.begin();
  FileSystem.begin();
  
  while(!FileSystem.exists("/mnt/sd/arduino/www/SDCardTest"))
    delay(1000);
  
  while(getDateTime().startsWith("08/08/13"))
    delay(1000);
  
  writeToLog("Setup");
}

void loop()
{  
}

void writeToLog(String message)
{
  String dateTime = getDateTime();  
  File file = FileSystem.open("/mnt/sd/arduino/www/SDCardTest/log.txt", FILE_APPEND);

  if (file)
  { 
    file.println(dateTime + ": " + message);
    file.close();
  }
}

String getDateTime()
{
  String dateTime;
  Process time;
  time.begin("date");
  time.addParameter("+%D %T");
  time.run();

  while(time.available()>0)
  {
    char c = time.read();
    if(c != '\n')
      dateTime += c;
  }
  
  return dateTime;
}

Bye,
Sebastian

Yun does not have real time clock on board so its clock gets reset at every reboot. I don't have a yun at hand right now, but you can try forcing a quick clock sync right before your sketch starts: check this out NTP client / NTP server [Old OpenWrt Wiki]

Hi Federico,

thanks for this hint! I will try this!
I hope that the network is already running when I call the ntpd command in the setup method.

Bye,
Sebastian

Good catch: the yun already synchronizes its clock as soon as it can reach an NTP server. You can try to speed it up by calling the ntp client after your while(!FileSystem.exists...)

skr1:

  while(!FileSystem.exists("/mnt/sd/arduino/www/SDCardTest"))

delay(1000);
 
  while(getDateTime().startsWith("08/08/13"))
    delay(1000);

Excellent! Nice solution!