Go Down

Topic: [SOLVED]Setting Yun's Date/Time (Read 12437 times) previous topic - next topic

bjarne

I think what Chagrin suggests should also work, similar to what I suggested with Set.addParameter(sz)

theDuke540

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.

bjarne

#17
Mar 14, 2014, 06:30 am Last Edit: Mar 14, 2014, 06:37 am by bjarne Reason: 1
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.

theDuke540

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!

theDuke540

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.

theDuke540

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!

bjarne

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.

theDuke540

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.

SyukriY

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.

Go Up