Arduino Forum

Products => Arduino Yún => Topic started by: theDuke540 on Mar 13, 2014, 03:55 am

Title: [SOLVED]Setting Yun's Date/Time
Post by: theDuke540 on Mar 13, 2014, 03:55 am
I have looked high and low for any information on how to properly set the Linino Date/Time. So far, I've tried:
Code: [Select]
date +%F -s "20140312"
(returns=2016-08-14)
Code: [Select]
date +%D -s "03122014"
(returns=03/12/16)
and even  
Code: [Select]
date +%Y -s "2014"
(returns=2016). Can someone please tell me exactly what I'm doing wrong?

Edit:
I'm trying to figure out how to do this so that I can run a Process to update system time from gps every 5 minutes or so. If there is a easier way, please let me know! Also, I can't use NTP time. This yun will be outdoors far away from networks...
Title: Re: Setting Yun's Date/Time
Post by: sonnyyu on Mar 13, 2014, 07:13 am
Stop and disable NTP server:

Code: [Select]
/etc/init.d/sysntpd stop
/etc/init.d/sysntpd disable


Change Yun back to UTC time zone.

Read UTC time from GPS:

Code: [Select]
$GPRMC,020545.600,A,3157.8299,N,03551.5057,E,18.18,37.45,130314,,,A*6C

020545.600:  hhmmss.sss
130314:     ddmmyy

Set date and time:
Code: [Select]
date --set "2014-03-13 02:05:45"

Title: Re: Setting Yun's Date/Time
Post by: theDuke540 on Mar 13, 2014, 03:12 pm
Thanks for the reply! I was successful in stopping, disabling and initially setting the time. My question now is, what is the best way to parse NMEA sentences and set linux time with that? I prefer TinyGPS so I was thinking of maybe declaring a variable for hour:minute:second and another for mm:dd:Y and then calling a process to set the date using those variables. Would this be a viable solution? Thanks again.
Title: Re: Setting Yun's Date/Time
Post by: Chagrin on Mar 13, 2014, 03:37 pm
The TinyGPS examples include a print_date() function that outputs a string in a format that would work fine with the linux date command.

I don't see any other shortcuts you could do. The date command will take a wide variety of formats but chokes on ambiguous strings of numbers so some reformatting will always be necessary.
Title: Re: Setting Yun's Date/Time
Post by: sonnyyu on Mar 13, 2014, 03:39 pm
http://forum.arduino.cc/index.php?topic=208117.msg1532061#msg1532061 (http://forum.arduino.cc/index.php?topic=208117.msg1532061#msg1532061)

I prefer hook up GPS directly to linino side(no GPS shield), once it is at linux parse data is a piece of cake.
We could use Arduino to switch on/off GPS power supply for energy saver.

Biggest problem Yun did not break out any GPIO pin (total of 29).  We can't directly connect GPS module to AR9331.
Title: Re: Setting Yun's Date/Time
Post by: theDuke540 on Mar 13, 2014, 05:40 pm
So i tried modifying the "simple_test" sketch from TinyGPS examples. Here is the code:
Code: [Select]
#include <SoftwareSerial.h>
#include <Bridge.h>
#include <TinyGPS.h>

/* This sample code demonstrates the normal use of a TinyGPS object.
  It requires the use of SoftwareSerial, and assumes that you have a
  4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
*/

TinyGPS gps;
SoftwareSerial ss(9, 8);

void setup()
{
 Serial.begin(115200);
 ss.begin(9600);
 Bridge.begin();
 
 Serial.print("Simple TinyGPS library v. "); Serial.println(TinyGPS::library_version());
 Serial.println("by Mikal Hart");
 Serial.println();
}

void loop()
{
 bool newData = false;
 unsigned long chars;
 unsigned short sentences, failed;

 // For one second we parse GPS data and report some key values
 for (unsigned long start = millis(); millis() - start < 1000;)
 {
   while (ss.available())
   {
     char c = ss.read();
     // Serial.write(c); // uncomment this line if you want to see the GPS data flowing
     if (gps.encode(c)) // Did a new valid sentence come in?
       newData = true;
   }
 }

 if (newData)
 {
   float flat, flon;
   unsigned long age;
   gps.f_get_position(&flat, &flon, &age);
   Serial.print("LAT=");
   Serial.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
   Serial.print(" LON=");
   Serial.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
   Serial.print(" SAT=");
   Serial.print(gps.satellites() == TinyGPS::GPS_INVALID_SATELLITES ? 0 : gps.satellites());
   Serial.print(" PREC=");
   Serial.print(gps.hdop() == TinyGPS::GPS_INVALID_HDOP ? 0 : gps.hdop());
   Serial.print(" ");
   int year;
   byte month, day, hour, minute, second, hundredths;
   gps.crack_datetime(&year, &month, &day, &hour, &minute, &second);
//    Serial.print(" ");
//    Serial.print(month, DEC); Serial.print("/");
//    Serial.print(day, DEC); Serial.print("/"); Serial.print(year);
//    Serial.print(" ");
//    Serial.print(hour, DEC); Serial.print(":");
//    Serial.print(minute, DEC); Serial.print(":"); Serial.println(second, DEC);
   SetTime();
 }
 
//  gps.stats(&chars, &sentences, &failed);
//  Serial.print(" CHARS=");
//  Serial.print(chars);
//  Serial.print(" SENTENCES=");
//  Serial.print(sentences);
//  Serial.print(" CSUM ERR=");
//  Serial.println(failed);
//  if (chars == 0)
//    Serial.println("** No characters received from GPS: check wiring **");
}
void SetTime()
{
 int year;
 byte month, day, hour, minute, second, hundredths;
 gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths);
 char sz[32];
 sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d  ",
 month, day, year, hour, minute, second);
 Serial.println(sz);
 Process Set;
 Set.begin("date --set");
 Set.addParameter("year-month-day hour:minute:second");
 Set.run();  
}

No luck though. This is seriously frustrating. When I SSH into the Linino side, the "date" command returns a date from like Aug 2013. BTW, the modified part is in the function SetTime(). I'm still able to manually set the date though.
Title: Re: Setting Yun's Date/Time
Post by: bjarne on Mar 13, 2014, 06:04 pm
I don't have a lot of experience with addParameter, but I think you are sending the string "year-month-day hour:minute:second" rather than the variables.  Have you tried
Set.addParameter(sz); instead?

Also I seem to have much more luck with runShellCommand rather than run.
Title: Re: Setting Yun's Date/Time
Post by: theDuke540 on Mar 13, 2014, 06:06 pm
I hadn't thought of that. I'll try it now.

Quote
Also I seem to have much more luck with runShellCommand rather than run.

Can you explain or show an example for me please?
Title: Re: Setting Yun's Date/Time
Post by: bjarne on Mar 13, 2014, 06:12 pm
Here is a code snippet from my sketch
Code: [Select]

void sendSMS(String message) {
  Process smsSend;
  String command;
 
  command = "/mnt/sd/data/sms.py 1408XXXXXXX '" + message + "'";
  smsSend.runShellCommand(command);
}


Obviously, in my code 1408XXXXXXX is a real phone number (my cell phone).
Title: Re: Setting Yun's Date/Time
Post by: theDuke540 on Mar 13, 2014, 06:35 pm
Ok i tried the runShellCommand and no joy. Here is what i tried based off of bjarne's example:
Quote
#include <SoftwareSerial.h>
#include <Bridge.h>
#include <TinyGPS.h>

/* This sample code demonstrates the normal use of a TinyGPS object.
   It requires the use of SoftwareSerial, and assumes that you have a
   4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
*/

TinyGPS gps;
SoftwareSerial ss(9, 8);

void setup()
{
  Serial.begin(115200);
  ss.begin(9600);
  Bridge.begin();
 
  Serial.print("Simple TinyGPS library v. "); Serial.println(TinyGPS::library_version());
  Serial.println("by Mikal Hart");
  Serial.println();
}

void loop()
{
  bool newData = false;
  unsigned long chars;
  unsigned short sentences, failed;

  // For one second we parse GPS data and report some key values
  for (unsigned long start = millis(); millis() - start < 1000;)
  {
    while (ss.available())
    {
      char c = ss.read();
      // Serial.write(c); // uncomment this line if you want to see the GPS data flowing
      if (gps.encode(c)) // Did a new valid sentence come in?
        newData = true;
    }
  }

  if (newData)
  {
    float flat, flon;
    unsigned long age;
    gps.f_get_position(&flat, &flon, &age);
    Serial.print("LAT=");
    Serial.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
    Serial.print(" LON=");
    Serial.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
    Serial.print(" SAT=");
    Serial.print(gps.satellites() == TinyGPS::GPS_INVALID_SATELLITES ? 0 : gps.satellites());
    Serial.print(" PREC=");
    Serial.print(gps.hdop() == TinyGPS::GPS_INVALID_HDOP ? 0 : gps.hdop());
    Serial.print(" ");
    int year;
    byte month, day, hour, minute, second, hundredths;
    gps.crack_datetime(&year, &month, &day, &hour, &minute, &second);
//    Serial.print(" ");
//    Serial.print(month, DEC); Serial.print("/");
//    Serial.print(day, DEC); Serial.print("/"); Serial.print(year);
//    Serial.print(" ");
//    Serial.print(hour, DEC); Serial.print(":");
//    Serial.print(minute, DEC); Serial.print(":"); Serial.println(second, DEC);
    SetTime();
  }
 
//  gps.stats(&chars, &sentences, &failed);
//  Serial.print(" CHARS=");
//  Serial.print(chars);
//  Serial.print(" SENTENCES=");
//  Serial.print(sentences);
//  Serial.print(" CSUM ERR=");
//  Serial.println(failed);
//  if (chars == 0)
//    Serial.println("** No characters received from GPS: check wiring **");
}
void SetTime()
{
  int year;
  byte month, day, hour, minute, second, hundredths;
  gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths);
  char sz[32];
  sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d  ",
  month, day, year, hour, minute, second);
  Serial.println(sz);
  [font=Verdana]Process Set;
  command = "date --set 'year-month-day hour:minute:second'";
  Set.runShellCommand(command);[/font]
}


I'm at my wits end with this one. If my time isn't able to be set, this project is over.
Title: Re: Setting Yun's Date/Time
Post by: bjarne on Mar 13, 2014, 07:39 pm
Again I think you are send the string   'year-month-day hour:minute:second' instead of the values.
Maybe try
 command = "date --set '" + year  + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second + "'";
I know pretty long, but try.  And I am in a hurry so there might be typos.

Title: Re: Setting Yun's Date/Time
Post by: theDuke540 on Mar 13, 2014, 07:41 pm
OK thank you. I will try this and let everyone know the results.
Title: Re: Setting Yun's Date/Time
Post by: theDuke540 on Mar 13, 2014, 08:01 pm

Again I think you are send the string   'year-month-day hour:minute:second' instead of the values.
Maybe try
 command = "date --set '" + year  + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second + "'";
I know pretty long, but try.  And I am in a hurry so there might be typos.




OK i've tried this and here is the error I get:
Quote
simple_test.ino: In function 'void SetTime()':
simple_test:89: error: invalid operands of types 'const char*' and 'const char [2]' to binary 'operator+'
Title: Re: Setting Yun's Date/Time
Post by: Chagrin on Mar 13, 2014, 08:20 pm
I'm also quite poor with the String type myself; you'd assuredly get a better answer in the Programming forum as this isn't specifically Yun-related. But I think what you're after is:

Code: [Select]
void SetTime()
{
  int year;
  byte month, day, hour, minute, second, hundredths;
  gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths);
  char sz[32];
  sprintf(sz, "date --set '%02d/%02d/%02d %02d:%02d:%02d'", month, day, year, hour, minute, second);
  Serial.println(sz);
  Process Set;
  Set.runShellCommand(sz);
}
Title: Re: Setting Yun's Date/Time
Post by: bjarne on Mar 13, 2014, 08:37 pm
You are right, I am getting the same when I try your code.  Not exactly sure why, but I created the command the long way, and at least it compiles.
Code: [Select]

   command = "date --set '";
   command += year;
   command += "-";
   command += month;
   command += "-";
   command += day;
   command += " ";
   command += hour;
   command += ":";
   command += minute;
   command += ":";
   command += second;
   command += "'";
   Set.runShellCommand(command);


If this works we can always figure out what was wrong with the first way and create a little more elegant syntax.
Title: Re: Setting Yun's Date/Time
Post by: bjarne on Mar 13, 2014, 08:58 pm
I think what Chagrin suggests should also work, similar to what I suggested with Set.addParameter(sz)
Title: Re: Setting Yun's Date/Time
Post by: theDuke540 on Mar 14, 2014, 02:18 am
I tried what Chagrin suggested earlier and there were no results. I'm thinking it is due to the variables being of type 'byte'. I'm done pulling my hair out over it tonight  :smiley-sleep:. I will pick up from here tomorrow. I'm thinking of converting the year, month, day, etc. variables to strings and then trying it. Thanks to all who replied today.
Title: Re: Setting Yun's Date/Time
Post by: bjarne on Mar 14, 2014, 06:30 am
I played around a little tonight with the date stuff, and the following worked for me.

Code: [Select]

void setTime()
{
  Process Set;
  String command;
  int year;
  byte month, day, hour, minute, second, hundredths;
  //gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths);
  year = 2012;
  day = 2;
  month = 9;
  hour = 18;
  minute = 5;
  second = 46;

  command = "date --set '";
  command += year;
  command += "-";
  command += month;
  command += "-";
  command += day;
  command += " ";
  command += hour;
  command += ":";
  command += minute;
  command += ":";
  command += second;
  command += "'";

  Set.runShellCommand(command);
}


Since I don't have a gps, I just set the value directly.  As you can see they are still int and bytes, but the command += <byte> will automatically be re-cast as a string.
Maybe this is a place to start.
Title: Re: Setting Yun's Date/Time
Post by: theDuke540 on Mar 14, 2014, 12:15 pm
Code: [Select]
void setTime()
{
   Process Set;
   String command;
   int year;
   byte month, day, hour, minute, second, hundredths;
   //gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths);
   year = 2012;
   day = 2;
   month = 9;
   hour = 18;
   minute = 5;
   second = 46;

   command = "date --set '";
   command += year;
   command += "-";
   command += month;
   command += "-";
   command += day;
   command += " ";
   command += hour;
   command += ":";
   command += minute;
   command += ":";
   command += second;
   command += "'";

   Set.runShellCommand(command);
}

That is definitely a step in the right direction! Thank you. You know what my next question will be, right? How can I reduce the size of this? The script I'm  using is ~95% full  :smiley-eek:! But I believe I can trim some fat from it. I will try this and let you know. Thanks again bjarne!
Title: Re: Setting Yun's Date/Time
Post by: theDuke540 on Mar 14, 2014, 12:45 pm
Ok i just tried bjarne's suggestion and it works  $). Thanks bjarne (+1)! The problem now is fitting it into my existing code. I will keep everyone updated on my progress.
Title: Re: Setting Yun's Date/Time
Post by: theDuke540 on Mar 14, 2014, 04:48 pm
To anyone who is interested, I will post my code here. It is not even close to being complete, so only constructive criticism please!  :smiley-sweat:

Code: [Select]
#include <TimeAlarms.h>
#include <FileIO.h>
#include <Time.h>
#include <SoftwareSerial.h>
#include <TinyGPS.h>

#define aref_voltage 3.3 //reference voltage for tmp36 sensor

SoftwareSerial mySerial(9, 8);//pins for GPS. 9: TX, 8: RX.
TinyGPS gps;//TinyGPS object
const int tempPin = 1;// Analog Pin for TMP36 Temp sensor (Vout)
int tempReading;//analog reading from the sensor.
int tipped = 0;
const int tip1 = 0; // input pin. Interrupt pin 0 == Digital Pin 3!
const int tip2 = 1; // input pin. Interrupt pin 1 == Digital Pin 2!
int incomingByte;
int tipcountA = 0;
int tipcountB = 0;

//time_t prevDisplay = 0; // when the digital clock was displayed

volatile int state0 = LOW; // interrupt input state for gauge A
volatile int state1 = LOW; // interrupt input state for gauge B

void setup() {
  Serial.begin(115200);
  mySerial.begin(9600);
  Alarm.alarmRepeat(18,00,00, SetTime); //Alarm to sync Arduino time to GPS time @ 23:59:59 every day.
  Alarm.timerOnce(20, SetTime);
  //In the future, I may use a debouncing feature(software side) for erroneous tips.
//  pinMode(LedNote, OUTPUT);
  analogReference(EXTERNAL);//for using AREF pin for anything other than 5V.
  Bridge.begin();
  FileSystem.begin();
//  if (!FileSystem.exists("/mnt/sda1/data/ErrorsLog.txt")) {
//    File errorslog = FileSystem.open("/mnt/sda1/data/ErrorsLog.txt", FILE_WRITE);
//    errorslog.print("\t");
//    errorslog.println("Gauge Errors");
//    errorslog.println("----------------------------");
//    errorslog.close();
//  }
  attachInterrupt(tip1, stateChange1, CHANGE);
  attachInterrupt(tip2, stateChange2, CHANGE);

//  if (!FileSystem.exists("/mnt/sda1/data/tiplogA.txt")) {//Create tiplogA IF it doesn't exist.
//    File logFileA = FileSystem.open("/mnt/sda1/data/tiplogA.txt", FILE_WRITE);
//    logFileA.println("Gauge A Tips");
//    logFileA.close();
//  }
//  if (!FileSystem.exists("/mnt/sda1/data/tiplogB.txt")) {
//    File logFileB = FileSystem.open("/mnt/sda1/data/tiplogB.txt", FILE_WRITE);
//    logFileB.println("Gauge B Tips");
//    logFileB.close();
//  }
}
void loop() {
    Alarm.delay(10);
    while (mySerial.available())
    {
      gps.encode(mySerial.read()); // Begin reading NMEA sentences
      }
     
  tempReading = analogRead(tempPin);
  String LogData;
//  String Error = "Error Code "; //Simple string for error code observation.
//  File errorslog = FileSystem.open("/mnt/sda1/data/ErrorsLog.txt", FILE_APPEND); //open ErrorLog for any possible errors.
  File logFileA = FileSystem.open("/mnt/sda1/data/tiplogA.txt", FILE_APPEND); //opens tiplogA.txt for appending to.
  File logFileB = FileSystem.open("/mnt/sda1/data/tiplogB.txt", FILE_APPEND); //opens tiplogB.txt for appending to.

  float voltage = tempReading * aref_voltage;//for TMP36
  voltage /= 1024.0;//for TMP36
  float temperatureC = (voltage - 0.5) * 100;  //converting from 10 mv per degree with 500 mV offset
  //to degrees ((volatge - 500mV) times 100)

//  for (int i = 0; i < 100; i++) {
//    // do nothing but waste some time
//    delay(15);
//  }
  if (tipped == 1) {
    LogData += getTimeStamp();
    tipcountA++;
    LogData += "  ";
    LogData += tipcountA;
    LogData += "  ";
    LogData += temperatureC;
    LogData += " degrees";
    Serial.print("tipped A ");
    Serial.println(tipcountA, DEC);
    tipped = 0;
    if (logFileA) {
      logFileA.println(LogData);
      logFileA.close();
    }
  }
    else if (tipped == 2) {
      LogData += getTimeStamp();
      tipcountB++;
      LogData += "  ";
      LogData += tipcountB;
      LogData += "  ";
      LogData += temperatureC;
      LogData += " degrees";
      Serial.print("tipped B ");
      Serial.println(tipcountB, DEC);
      tipped = 0;
      if (logFileB) {
        logFileB.println(LogData);
        logFileB.close();
      }
    }
//  if (Serial.available() >= 1) {
//    // read the oldest byte in the serial buffer:
//    incomingByte = Serial.read();
//    // if it's a 'G' or 'g', goto gps_get() function
//    if (incomingByte == 'G' || incomingByte == 'g') {
//      Serial.flush();
////      gps_get();
//    }
//   }
}
void stateChange1() {
  state0 = !state0;
  tipped = 1;
}
void stateChange2() {
  state1 = !state1;
  tipped = 2;
}

String getTimeStamp() {
  String result;
  Process time;//create a process called time.
  time.begin("date");// date is a command line utility to get the date and the time
  // in different formats depending on the additional parameter
  time.addParameter("+%Y  %m  %d  %j  %H  %M  %S");  /* parameters: Y-full century year, m-2 digit month,
  d-day, j-julian calendar day, H-24 hour clock,M-minute, S-second */
  time.run();  // run the command
  // read the output of the command
  while (time.available() > 0) {
    char c = time.read();
    if (c != '\n')
      result += c;
  }
  return result;
}

//void gps_get() {
//  float voltage = tempReading * aref_voltage;//for TMP36
//  voltage /= 1024.0;//for TMP36
//  float temperatureC = (voltage - 0.5) * 100 ;  //converting from 10 mv per degree wit 500 mV offset
//  //to degrees ((volatge - 500mV) times 100)
//  float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
// 
//  bool newData = false;
// 
//  Serial.println("System Info");
////  Serial.println("------------");
////  Serial.print("Temperature: ");
//  Serial.println(temperatureF);
//  int counter = 5;//counter that will decrement
// 
//  while (counter > 0) {
//    for (unsigned long start = millis(); millis() - start < 3000;)
//    {
//      while (mySerial.available())
//      {
//        char e = mySerial.read();
//        //      Serial.write(c); // uncomment this line if you want to see the GPS data flowing
//        if (gps.encode(e)) // Did a new valid sentence come in?
//          newData = true;
//      }
//    }
//    if (newData)
//    {
//     
//      float flat, flon;
//      unsigned long age;
//      gps.f_get_position(&flat, &flon, &age);
////      Serial.print("N ");
//      Serial.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
//      Serial.print(" ");
//      Serial.println(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
//     
//      int year;
//      byte month, day, hour, minute, second, hundredths;
//      gps.crack_datetime(&year, &month, &day, &hour, &minute, &second);
////      Serial.print("Date: ");
//      Serial.print(month, DEC); Serial.print("/");
//      Serial.print(day, DEC); Serial.print("/"); Serial.print(year);
//      Serial.print(" ");
//      Serial.print(hour, DEC); Serial.print(":");
//      Serial.print(minute, DEC); Serial.print(":"); Serial.println(second, DEC);
////      Serial.println(" ");
//      counter--;
//    }
//    else if (!newData) {
//      Serial.println("Check GPS connections");
//      break;
//    }
//   
//  }
//  counter = 5;
//}
void SetTime()
{
  int year;
  byte month, day, hour, minute, second, hundredths;
  gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths);
  char sz[32];
  sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d  ",
  year, month, day, hour, minute, second);
  Serial.println(sz);
  Process Set;
  String command; //This command is what sets the Arduino's Linux system clock.
  command = "date --set '";
  command += year;
  command += "-";
  command += month;
  command += "-";
  command += day;
  command += " ";
  command += hour;
  command += ":";
  command += minute;
  command += ":";
  command += second;
  command += "'";
  Set.runShellCommand(command);
}


As it stands now, this fills up 99% of my YUN's memory! Any suggestions on how to shave off some unneeded stuff is very welcome! Also there is a lot of commented code- please disregard that. Many thanks to bjarne for his help!
Title: Re: Setting Yun's Date/Time
Post by: bjarne on Mar 14, 2014, 05:04 pm
So I played at little more with it this morning to see if I could come up with something a little less verbose.  And this what I came up with.

Code: [Select]

void setTime()
{
  Process Set;
  int year;
  byte month, day, hour, minute, second, hundredths;
  //gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths);
  year = 2012;
  day = 2;
  month = 9;
  hour = 18;
  minute = 5;
  second = 46;

  char command[36];
  sprintf(command, "date --set '%04d-%02d-%02d %02d:%02d:%02d'\0",
     year, month, day, hour, minute, second);
  Set.runShellCommand(command);
/*
  String command;
  command = "date --set '";
  command += year;
  command += "-";
  command += month;
  command += "-";
  command += day;
  command += " ";
  command += hour;
  command += ":";
  command += minute;
  command += ":";
  command += second;
  command += "'";
  Set.runShellCommand(command);
*/
}


That also works for me, but it actually uses 1278 bytes more code space and 138 more variable space, so I would stay with the verbose version.  I think it is the sprintf function that add the space, so if you use it extensively other places, it might not add as much.

I see that you use the sprintf function one place in your code for diagnostic output.  You might try to comment it out to see how much space you can regain from that.
Title: Re: Setting Yun's Date/Time
Post by: theDuke540 on Mar 14, 2014, 05:26 pm
Thanks for the input. I think you are right about the more verbose structure.

Quote
I see that you use the sprintf function one place in your code for diagnostic output.  You might try to comment it out to see how much space you can regain from that.

I totally missed that one at the end! Thanks for spotting that. Should free up a little space.
Title: Re: [SOLVED]Setting Yun's Date/Time
Post by: SyukriY on Sep 13, 2016, 10:09 am
Hi sir theDuke540,

I just want to make sure something.
Do you use time and date from the Arduino Yun only?

Because I also want to print the date and time
for my project.