Time stamping the sensor output

Hi everyone,
I wanted to know how to timestamp my sensor output in the serial monitor so that I may get the reading against the corrosponding date and time. I don't want to use the RTC chip for this purpose and I want the sketch to sync the arduino with the computer clock. As of now I have just used the millis() which is hardly of any use for me as it shows the time elapsed since the operation started. Could someone point me towards the right direction? The sketch I have used so far is shown below...

specifications:

Arduino Uno R3
clockspeed 16 MHz
IDE 1.6.4

#include <stdio.h>
#include <dht.h>
unsigned long clocktime;
dht DHT;

#define DHT22_PIN 2

void setup()
{
 Serial.begin(9600);
 Serial.println("DHT TEST PROGRAM ");
 Serial.print("LIBRARY VERSION: ");
 Serial.println(DHT_LIB_VERSION);
 Serial.println();
 Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (degC),\tTime");
}

void loop()
{
 // READ DATA
 Serial.print("DHT22, \t");
 int chk = DHT.read22(DHT22_PIN);
 clocktime=millis();      // time in milli seconds
 switch (chk)
 {
   case DHTLIB_OK:  
Serial.print("OK,\t"); 
break;
   case DHTLIB_ERROR_CHECKSUM: 
Serial.print("Checksum error,\t"); 
break;
   case DHTLIB_ERROR_TIMEOUT: 
Serial.print("Time out error,\t"); 
break;
   default: 
Serial.print("Unknown error,\t"); 
break;
 }
 // DISPLAY DATA
 Serial.print(DHT.humidity, 1);
 Serial.print(",\t\t");
 Serial.print(DHT.temperature, 1);
 Serial.print("\t\t\t");
 Serial.println(clocktime, 1);   
 delay(2000);

}
//
// END OF FILE
//

//

Please use code tags [code]....[/code]

To sync with a PC check the Time library - Arduino Playground - HomePage

Use a proper terminal programme, like Realterm, instead of the serial monitor. It's free and will timestamp your output using the PC clock - no extra programming required, just delete that millis stuff.

Nick_Pyner:
Use a proper terminal programme, like Realterm, instead of the serial monitor. It's free and will timestamp your output using the PC clock - no extra programming required, just delete that millis stuff.

That would be incredible...Can you please provide me a useful link for that???

Just google

realterm.

That looks good but I would rather try time stamping it using the labview interface first because I already have it installed.....if that fails I will look at Realterm.....

Anyways, thanks for providing an alternative .

Dear all, I have tweaked the code above to include a time stamp for the my sensor readouts in the serial monitor but it still requires me to manually input a value (T followed by ten digit time since Jan 1, 1970) to synchronise with the real time....but I wanted it to automatically synchronize with the PC time....I know there are the labview and Realterm alternatives but I ll be considering them at a later stage...Any suggestion in this direction would be really helpful....

#include <stdio.h>
#include <dht.h>
#include <Time.h> 
unsigned long clocktime;
dht DHT;

#define DHT22_PIN 2        // pin used for sensor input
#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);               // Messages consist of the letter T followed by ten digit time (as seconds since Jan 1 1970)
  Serial.println("Waiting for sync message");  // you can send the text on the next line using Serial Monitor to set the clock to noon Jan 1 2013 T1357041600   
  Serial.println("DHT TEST PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT_LIB_VERSION);
  Serial.println();
  Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (degC),\tTime");
}

//void digitalClockDisplay()
void loop()
{
  // READ DATA
  Serial.print("DHT22, \t");
  int chk = DHT.read22(DHT22_PIN);
  if (Serial.available())
  {
    processSyncMessage();
  }
  clocktime=millis();      // time in milli seconds
  switch (chk)
  {
    case DHTLIB_OK:  
 Serial.print("OK,\t"); 
 break;
    case DHTLIB_ERROR_CHECKSUM: 
 Serial.print("Checksum error,\t"); 
 break;
    case DHTLIB_ERROR_TIMEOUT: 
 Serial.print("Time out error,\t"); 
 break;
    default: 
 Serial.print("Unknown error,\t"); 
 break;
  }
  // DISPLAY DATA
  Serial.print(DHT.humidity, 1);
  Serial.print(",\t\t");
  Serial.print(DHT.temperature, 1);
  Serial.print("\t\t\t");
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year()); 
  Serial.println();  
  delay(2000);
}  
 

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
}

biswa263:
.I know there are the labview and Realterm alternatives but I

I'm not sure "alternatives" is the best choice of word here, but, if you insist on going down the path you are on now, you are indeed likely to find some PC programme that can continuously send the time over serial so that Arduino can read it during Setup, thereby enabling it to set the millis clock in sync. I would suggest though that, if you can find a brick wall along the way first, you should stop right there and bang your head against it for an hour, it will certainly be more productive.

There are other ways of getting the time on an Arduino but, if you are disinclined spend $2 on an RTC, I imagine you will be less inclined to spend up on those.

Since you are running Arduino connected to the PC, there should never be any need for Arduino to know what the time is - unless you are providing for the occasion when the PC connection fails, i.e. local recording, and I don't see any evidence that you plan to do that. This because Arduino is simply acting as a programmable interface between the PC and the sensors, and the PC already has a clock.

Nick_Pyner:
I'm not sure "alternatives" is the best choice of word here, but, if you insist on going down the path you are on now, you are indeed likely to find some PC programme that can continuously send the time over serial so that Arduino can read it during Setup, thereby enabling it to set the millis clock in sync. I would suggest though that, if you can find a brick wall along the way first, you should stop right there and bang your head against it for an hour, it will certainly be more productive.

There are other ways of getting the time on an Arduino but, if you are disinclined spend $2 on an RTC, I imagine you will be less inclined to spend up on those.

Since you are running Arduino connected to the PC, there should never be any need for Arduino to know what the time is - unless you are providing for the occasion when the PC connection fails, i.e. local recording, and I don't see any evidence that you plan to do that. This because Arduino is simply acting as a programmable interface between the PC and the sensors, and the PC already has a clock.

Tweaking the sketches to make the serial monitor display the real time along with the reading is exactly what I am working upon at the moment and the trouble I am faced with is that I have to send in a request through the serial input before the time stamp starts....I am trying different ways so that the arduino can use the computer clock to time stamp the sensor output, thus saving me the trouble of manual entry...

I am disinclined on RTC chip usage at the moment because I plan on using it in the later stages....right now my objective is to use the serial monitor only...After this I will be using the labview/realterm interface....

As for the need for time stamping, it is essential because all the sensor outputs changes with respect to time under varying conditions and this really helps to figure out when exactly the change occurs....u would otherwise have to take stock of it through the PC clock....saves u the trouble....isn't it??

biswa263:
Tweaking the sketches to make the serial monitor display the real time along with the reading is exactly what I am working upon at the moment

You are wasting your time. You are trying to use the serial monitor as a terminal when it isn't one, and ignoring the use of a terminal that is one. I appreciate your position - newbie - but you better off finding out that you are on the wrong tram now than later.

and the trouble I am faced with is that I have to send in a request through the serial input before the time stamp starts....I am trying different ways so that the arduino can use the computer clock to time stamp the sensor output, thus saving me the trouble of manual entry...

I submit you are looking at the aforementioned brick wall. While you are banging your head against it, you might reflect on the logic of Arduino getting the time from the PC clock, only to promptly send it back to the PC.

I am disinclined on RTC chip usage at the moment because I plan on using it in the later stages....right now my objective is to use the serial monitor only...After this I will be using the labview/realterm interface....

See above - and the last par of reply #7.

As for the need for time stamping, it is essential because all the sensor outputs changes with respect to time under varying conditions and this really helps to figure out when exactly the change occurs....u would otherwise have to take stock of it through the PC clock...

There is nothing of note here. It is normal for sensors to change their data as time changes, and it is normal to need to know when that happened but, if Arduino is feeding a PC, it doesn't need to know.

.saves u the trouble....isn't it??

No

Well, I have moved on to plotting the time vs sensor output graph in a LabVIEW interface which will be just fine for now...

Appreciate your direction Nick, it stopped me from banging too much....

Thank You Guys....

biswa263:
Appreciate your direction Nick, it stopped me from banging too much....

Good......
I know nothing about Labview, but it is a popular choice and I'm sure you have done the right thing.