Unbelievably ridiculously difficult to do basic things.

All I want for xmas is a time and date stamp in my sketch... My GOD HOW CAN THIS BE SO PAINFULLY DIFFICULT. No, I dont want a RTC or Ethernet shield (>$50 really???). I only want to get the time from the host (RPi2) over serial and print it in the serial monitor of the Arduino. From their I can customize my sketch to include this aspect.

This should be one of the most common and basic tasks for an Arduino yet it is painfully, stupidly difficult to do in practice. Im not the sharpest knife in the drawer (obviously) but I can get around pretty easily with most microcontrollers Ive played with over the years.

Ive got the latest Time library from Git but the docs are either incomplete (most always this way in this Arduino world) or it just doesnt work or its purposefully difficult.

I used terminal to send the message instead of SyncASrduinoClock.

 date +T%s\n > /dev/ttyACM0

Here is the TimeSerial code I used.

/* 
 * TimeSerial.pde
 * example code illustrating Time library set through serial port messages.
 *
 * Messages consist of the letter T followed by ten digit time (as seconds since Jan 1 1970)
 * you can send the text on the next line using Serial Monitor to set the clock to noon Jan 1 2013
 T1357041600  
 *
 * A Processing example sketch to automatically send the messages is inclided in the download
 * On Linux, you can use "date +T%s\n > /dev/ttyACM0" (UTC time zone)
 */ 
 
#include <Time.h>  

#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);
  while (!Serial) ; // Needed for Leonardo only
  pinMode(13, OUTPUT);
  setSyncProvider( requestSync);  //set function to call when sync required
  Serial.println("Waiting for sync message");
}

void loop(){    
  if (Serial.available()) {
    processSyncMessage();
  }
  if (timeStatus()!= timeNotSet) {
    digitalClockDisplay();  
  }
  if (timeStatus() == timeSet) {
    digitalWrite(13, HIGH); // LED on if synced
  } else {
    digitalWrite(13, LOW);  // LED off if needs refresh
  }
  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() {
  unsigned long pctime;
  const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013

  if(Serial.find(TIME_HEADER)) {
     pctime = Serial.parseInt();
     if( pctime >= DEFAULT_TIME) { // check the integer is a valid time (greater than Jan 1 2013)
       setTime(pctime); // Sync Arduino clock to the time received on the serial port
     }
  }
}

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

Here is the SyncArduinoClock code: It fails saying index is incorrect. I tried /dev/ttyACM0 and ACM0 instead of the 0 default but no luck). I use terminal to send the message instead but TimeSerial just stops at the "Waiting for sync message" and LED 13 shows off awaiting refresh.

/**
 * SyncArduinoClock. 
 *
 * portIndex must be set to the port connected to the Arduino
 * 
 * The current time is sent in response to request message from Arduino 
 * or by clicking the display window 
 *
 * The time message is 11 ASCII text characters; a header (the letter 'T')
 * followed by the ten digit system time (unix time)
 */
 

import processing.serial.*;
import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;

public static final short portIndex = 0;  // select the com port, 0 is the first port
public static final String TIME_HEADER = "T"; //header for arduino serial time message 
public static final char TIME_REQUEST = 7;  // ASCII bell character 
public static final char LF = 10;     // ASCII linefeed
public static final char CR = 13;     // ASCII linefeed
Serial myPort;     // Create object from Serial class

void setup() {  
  size(200, 200);
  println(Serial.list());
  println(" Connecting to -> " + Serial.list()[portIndex]);
  myPort = new Serial(this,Serial.list()[portIndex], 9600);
  println(getTimeNow());
}

void draw()
{
  textSize(20);
  textAlign(CENTER);
  fill(0);
  text("Click to send\nTime Sync", 0, 75, 200, 175);
  if ( myPort.available() > 0) {  // If data is available,
    char val = char(myPort.read());         // read it and store it in val
    if(val == TIME_REQUEST){
       long t = getTimeNow();
       sendTimeMessage(TIME_HEADER, t);   
    }
    else
    { 
       if(val == LF)
           ; //igonore
       else if(val == CR)           
         println();
       else  
         print(val); // echo everying but time request
    }
  }  
}

void mousePressed() {  
  sendTimeMessage( TIME_HEADER, getTimeNow());   
}


void sendTimeMessage(String header, long time) {  
  String timeStr = String.valueOf(time);  
  myPort.write(header);  // send header and time to arduino
  myPort.write(timeStr); 
  myPort.write('\n');  
}

long getTimeNow(){
  // java time is in ms, we want secs    
  Date d = new Date();
  Calendar cal = new GregorianCalendar();
  long current = d.getTime()/1000;
  long timezone = cal.get(cal.ZONE_OFFSET)/1000;
  long daylight = cal.get(cal.DST_OFFSET)/1000;
  return current + timezone + daylight; 
}

Can anyone help me get a timedate stamp in a sketch.

mitcha25:
Can anyone help me get a timedate stamp in a sketch.

First thing you have to do is deactivating the "auto reset" feature of the Arduino board:
http://playground.arduino.cc/Main/DisablingAutoResetOnSerialConnection

If you do not deactivate "auto reset", then this will happen:

  • sending "date +T%s\n > /dev/ttyACM0" opens the serial port
  • Arduino board does an automatic reset
  • bootloader is starting
  • while bootloader is running, you send the time to the Arduino board
  • bootloader recognizes: This is NOT a new sketch uploading on Serial
  • bootloader stops
  • now your sketch starts running
    ==> your sketch will NEVER EVER see the time

mitcha25:
Ethernet shield (>$50 really???)

Ethernet shield clones from China sellers sell at <10 USD.
But time of delivery from China to some countries may be up to 6 weeks.
RTC modules (DS3131) are meanwhile less than 3 USD.

BTW: TimeSerial.pde
If you find an Arduino sketch on the Internet which has a ".pde" file extension, this is a sketch from the DINOSAUR AGE!

So don't be too disappointed if sketches from the age of dinosaurs don't work with recent Arduino IDEs.
:wink:

mitcha25:
I only want to get the time from the host (RPi2) over serial and print it in the serial monitor of the Arduino. From their I can customize my sketch to include this aspect.

This should be one of the most common and basic tasks for an Arduino yet it is painfully, stupidly difficult to do in practice. Im not the sharpest knife in the drawer (obviously)

Quiet obviously. An Ethernet shield can be had for $8.58 and an RTC for about $2.
In the meantime you seem to be making everything as hard as possible and possibly complaining about an RPi2 problem in an Arduino forum.
You are right, it should be simple and, for everybody else, it is. About two lines of programming on Arduino, and I imagine it's much the same on RPi.

void setup() {
  Serial.begin(9600);
  Serial.println(__DATE__);
}

void loop() {
  Serial.println(__TIME__);
  delay(1000);
}

charliesixpack:
I assume you know that this will print the same thing over and over, namely the time that the program was compiled.

(I bet you did not run this!)

jurs:
First thing you have to do is deactivating the "auto reset" feature of the Arduino board:
Arduino Playground - HomePage

If you do not deactivate "auto reset", then this will happen:

  • sending "date +T%s\n > /dev/ttyACM0" opens the serial port
  • Arduino board does an automatic reset
  • bootloader is starting
  • while bootloader is running, you send the time to the Arduino board
  • bootloader recognizes: This is NOT a new sketch uploading on Serial
  • bootloader stops
  • now your sketch starts running
    ==> your sketch will NEVER EVER see the time
    Ethernet shield clones from China sellers sell at <10 USD.
    But time of delivery from China to some countries may be up to 6 weeks.
    RTC modules (DS3131) are meanwhile less than 3 USD.

BTW: TimeSerial.pde
If you find an Arduino sketch on the Internet which has a ".pde" file extension, this is a sketch from the DINOSAUR AGE!

So don't be too disappointed if sketches from the age of dinosaurs don't work with recent Arduino IDEs.
:wink:

don't put bear skin rugs and stone knives down to harshly. we had to use something !

eBay
DS3231 RTC
.99 cents USD
:wink:

mitcha25:
All I want for xmas is a time and date stamp in my sketch... My GOD HOW CAN THIS BE SO PAINFULLY DIFFICULT. No, I dont want a RTC or Ethernet shield (>$50 really???). I only want to get the time

data logging shield with rtc, battery, and sd card for data logging $3.31
http://www.ebay.com/itm/131176003874
or just an RTC for 82 cents

Tying an Arduino to a Linux computer is really not "basic". That's a pretty specialised usage of an Arduino. While it seems like it should be obvious, it's actually not. The vast majority of working Arduino projects don't need to know what time it is.

mitcha25:
All I want for xmas is a time and date stamp in my sketch... My GOD HOW CAN THIS BE SO PAINFULLY DIFFICULT.

Actually, a lot of quite expensive commercial devices don't know they time until you tell them what it is manually.

I have a microwave oven, a bedside clock, a VCR, an ordinary oven. They all need to be told the time after a power failure. I ask myself "how hard can it be to incorporate a RTC?". But no, to save a few cents, they don't have one.

The Arduino is the same. It doesn't have a RTC. Now you can spend a dollar and add one. That is the simple, obvious thing to do.

A year or so ago I bought 30 x DS1307 chips from eBay for $10. That's $0.30 for one RTC. Add a couple of pull-up resistors, a crystal for a few cents, a CR2032 backup battery, and you have a clock. Under a dollar, as I said. Why complicate things?

mitcha25:
I only want to get the time from the host (RPi2) over serial and print it in the serial monitor of the Arduino.

This is very simple if you are prepared to have a program running constantly on your RPi listening for a request from the Arduino. This Python - Arduino demo would be a good starting point.

If you don't want to have that sort of system get an RTC as others have suggested.

...R

This should be one of the most common and basic tasks for an Arduino yet it is painfully, stupidly difficult to do in practice. Im not the sharpest knife in the drawer (obviously) but I can get around pretty easily with most microcontrollers Ive played with over the years.

That implies two possibilities:

  1. It is a really easy task and you just don't have the brain power to figure it out, or

  2. You are engaging in fantasy/wishful thinking that it is a really easy task.