Can't get Date/Time using Serial on Ubuntu

First, I’ve gone through the forum trying to find similar issues that might have a solution and so far no luck.

Problem: I have tried the old and new DataTime.h & DateTimeString.h, even deleting the .o file and having it recreated. If I run the example to try to time sync the Arduino with the PC, it won’t error but it runs forever because the Date.Time is never available.

The Arduino is connected to an Ubuntu laptop through USB and I am reading the measurements from a temperature and humidity sensor. I even used the example Date/Time program alone in case the sensors were causing a problem.

I am using the Arduino IDE 16 and the libraries are all in the proper directories.

I will try anything! Help, Please!

TIA,

desNotes

desNotes,

What program are you running on your Ubuntu laptop to communicate with your Arduino?

Regards,

-Mike

I'm using the same code that is part of DateTime.h & DateTime Strings.h

The main function I'm using is getPCtime() through the USB/Serial connection. In the loop process the function will keep getting called but I never get anything from the PC. I was wondering if this works on Windows PCs only.

I am able to retrieve sensor information from the Arduino so I know the serial sonnection is working so why can't I retrieve the date/time? My sensor measurements aren't much use without knowing the time it occurred.

TIA,

desnotes

I've used the DateTime library with Ubuntu. I'm now using the newer Time library on Ubuntu. They have both worked well. There is nothing Windows specific about them.

Can you get the DateTime.pde sketch included with the library to run?

-transfinite

The sketch runs but never returns any time/date information. When I run the getPCtime() function it returns -1. I would think it would need to send a command through the serial first. In getPCtime():

while(Serial.available() >= TIME_MSG_LEN {
          if(Serial.read() == TIME_HEADER) {
             <PARSING CODE>

This is where it always fails. I have added ‘Serial.print’ statements and TIME_MSG_LEN always returns -1.

Is there anything I can try at this point?

TIME_MSG_LEN always returns -1.

From:
http://www.arduino.cc/playground/Code/DateTime

Example sketch:

The test sketch included in the download demonstrates many of the capabilities of the library.
#include <DateTime.h>
#include <DateTimeStrings.h>

#define TIME_MSG_LEN 11 // time sync to PC is HEADER and unix time_t as ten ascii digits
#define TIME_HEADER 255 // Header tag for serial time sync message

So, TIME_MSG_LEN is not a function that returns -1. Can you clarify what you meant by that statement?

What I portrayed was a little confusing and not what I meant. In the function getPCtime() it starts with the following:

while(Serial.available() >= TIME_MSG_LEN) }

more code

     }
          DateTime.sync(pctime);
          return true;

   }
}

Serial.println("Returns False");
return false;

}

I could not put a Serial.print on Serial.available but I did try to do a Serial.print of Serial.read() which returned -1 but the bottom line is the function never returns true on the "while" statement.

Does this make sense? Should I include all of the code being run? The code is just one of the examples from DateTime.h

The example sketch sends data to the PC that triggers the listening application to respond with a 10 digit date/time response. You are running the Processing sketch that the Arduino is expecting to talk to, aren't you?

The Processing sketch is talking to the port that the Arduino is listening to, isn't it?

Yes, I believe it is. I am connected to Serial Port /dev/tty/USB1 which is the only one that displays when looking at Tools - Serial Ports.

Is there a way to verify the connection?

Is that the port that you use to upload to the Arduino?

Can you modify the Processing sketch to show what it gets from the Arduino? Can you assure that the Arduino is trying to talk to the Processing sketch?

I have been able to verify the port is used to upload the code to the Arduino and that the Arduino is able to send back data such as temperature & humidity from sensors attached to the Arduino.

However, after trying every way I know of to get the date or time, the return is 0 or null (not exactly sure which because the variable is initialized to 0. The only thing I have not tried is pulling out all of the USB cords except the Arduino. That will be my next test. If there is still no change, I will connect to a Windows PC and see if there is anything different.

Any ideas would be appreciated.

Thanks,

desnotes

What I meant was for you to add some println statements to the Processing application, to make sure it is getting the request to provide the time. It is not clear that you did that.

You are not opening the Serial Monitor window at the same time, are you?

Yes, I did add a bunch of println statements and bypassed some of the code to force it to read and process what came through. Here are the code snippets that printed:

 Serial.print(DateTime.Hour,DEC);
  printDigits(DateTime.Minute);
  printDigits(DateTime.Second);
  Serial.print(" ");
.
.
.

 Serial.print(DateTimeStrings.dayStr(DateTime.DayofWeek));
  Serial.print(" ");
  Serial.print(DateTimeStrings.monthStr(DateTime.Month));
  Serial.print(" ");
  Serial.println(DateTime.Day,DEC); 
  Serial.print("TIME_HEADER,BYTE= ");                               Serial.println( TIME_HEADER,BYTE); 
  Serial.print("Date.time.now= ");
  Serial.println(DateTime.now());

Here was what typically displayed:

0:00:01 Thursday January 1 TIME_HEADER,BYTE= ÿ Date.time.now= 1 Clock synced at: 0

As can be seen, all that I'm getting for a date.time is null or 0. If you think I'm missing something, let me know. Otherwise I will try to remove all other USB cables and or try it on a Windows computer.

Thanks,

desnotes

This is (part of) the Arduino code. In loop, the example sends data to the Processing application. Is that code still part of your code?

Does the Processing application receive the request?

All of the code is from the DateTime.pde example. I had to comment out " if(DateTime.available())" and had to change "#define TIME_MSG_LEN" from 11 to 10 in order for it to process. Otherwise the code is the same, which is clearly fairly different.

Any suggestions on what to try next to give me more info on what might be wrong would be sincerely appreciated. I'm guessing this should be fairly straightforward, correct?

I tried everything I could think of. I tried using the examples from the original (or older) DateTime package with DateTime.h and the newer Time package with Time.h.

In the newer Time.h there was an example called TimeSerial.pde to sync between the PC & Arduino. When running it, a sync request would be sent but would never get past, if(Serial.available() )…(see code below)

#include <Time.h>  

#define TIME_MSG_LEN  11   // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER  'T'   // Header tag for serial time sync message
#define TIME_REQUEST  7    // ASCII bell character requests a time sync message 

void setup()  {
  Serial.begin(9600);
  setSyncProvider( requestSync);  //set function to call when sync required
  Serial.println("Waiting for sync message");
}

void loop(){    
  if(Serial.available() )  <----FAILED HERE
  {
    Serial.println("serial.available");
    processSyncMessage();
  }

I tried taking all USB cables out of PC except the one going to the Arduino. I also swapped out the Arduino but no luck there also.

Is it typically difficult to obtain the Date/Time from the serial port?

As has been asked several times now...

Do you run an application on your PC that listens to your arduino? (note: you can't have the serial monitor open, as it would confiscate the serial line to your arduino, which the application needs to send the time info!)

Also, posting your complete arduino sketch, aswell as the code from your PC application would likely speed the help process up tremendously, as we'd be able to see where issues might be, as opposed to geussing.

Okay, now that I’ve re-read this post over and over it is starting to sink in, although I still don’t understand what the solution will be. Thanks to all for being beyond patient. If I had bothered to read and tried to understand what was said instead of thinking I already understood, the issue would be solved by now.

I am running the Arduino IDE on my PC (arduino-0016), which is uploading my code to the Arduino and does monitor the serial output. So once the code is uploaded to the Arduino, then what comes next?

Here is the code example I am using:

#include <Time.h>  

#define TIME_MSG_LEN  11   // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER  'T'   // Header tag for serial time sync message
#define TIME_REQUEST  7    // ASCII bell character requests a time sync message 

void setup()  {
  Serial.begin(15200);
  setSyncProvider( requestSync);  //set function to call when sync required
  Serial.println("Waiting for sync message");
}

void loop(){    
  if(Serial.available() ) 
  {
    processSyncMessage();
  }
  if(timeStatus()!= timeNotSet)   
  {
    digitalWrite(13,timeStatus() == timeSet); // on if synced, off if needs refresh  
    digitalClockDisplay();  
  }
  delay(1000);
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year()); 
  Serial.println(); 
}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

void processSyncMessage() {
  // if time sync available from serial port, update time and return true
  while(Serial.available() >=  TIME_MSG_LEN ){  // time message consists of a header and ten ascii digits
    char c = Serial.read() ; 
    Serial.print(c);  
    if( c == TIME_HEADER ) {       
      time_t pctime = 0;
      for(int i=0; i < TIME_MSG_LEN -1; i++){   
        c = Serial.read();          
        if( c >= '0' && c <= '9'){   
          pctime = (10 * pctime) + (c - '0') ; // convert digits to a number    
        }
      }   
      setTime(pctime);   // Sync Arduino clock to the time received on the serial port
    }  
  }
}

time_t requestSync()
{
  Serial.print(TIME_REQUEST,BYTE);  
  return 0; // the time will be sent later in response to serial mesg
}

The requestSync function writes data to the serial port. There needs to be some application running on the PC that reads that serial data, understands it, and responds appropriately.

That application is a Processing sketch that was provided as part of the DateTime library.

But that is where I am running into the problem. In the setup() routine of the sketch, requestSync() is being called, which does a Serial.print (TIME_REQUEST, BYTE).

In the loop() routine, there is a check:

if (Serial.available() )
      processSyncMessage();
  }

But Serial.available is never true so the code stops there. What is holding it up?