Getting a "timestamp" of when data is collected is entirely down to you.
Most Arduinos don't have any concept of the current time, only the time since the program started running. To know what the time "now" is you have to have some mechanism to tell the Arduino what the time is, along with a method of keeping track of that time.
There are devices called Real-Time Clock (RTC) modules which keep track of the time for you. They don't magically know the time - you still have to tell them at least once.
You could tell it the time through the serial port to set the clock - from then on (assuming the RTC has power) the RTC will know what the time is.
Another option to get the time into the RTC is to use an internet connection (ESP8266, WiFi shield, Ethernet shield, etc) to perform a Network Time Protocol (NTP) query to a time server on the internet (such as pool.ntp.org) to get the current time and update the RTC. This should be done regularly to correct any drift in the RTC.
Once you have an RTC and a method of setting the time you can query the time whenever you sample some data and store that time along with the data in whatever way is most suitable for your situation.
Answer from Majenko on Stack ExchangeTime stamping the sensor output
Creating an timestamp ?
Uno using timestamp
Millis vs Timestamp
Videos
Getting a "timestamp" of when data is collected is entirely down to you.
Most Arduinos don't have any concept of the current time, only the time since the program started running. To know what the time "now" is you have to have some mechanism to tell the Arduino what the time is, along with a method of keeping track of that time.
There are devices called Real-Time Clock (RTC) modules which keep track of the time for you. They don't magically know the time - you still have to tell them at least once.
You could tell it the time through the serial port to set the clock - from then on (assuming the RTC has power) the RTC will know what the time is.
Another option to get the time into the RTC is to use an internet connection (ESP8266, WiFi shield, Ethernet shield, etc) to perform a Network Time Protocol (NTP) query to a time server on the internet (such as pool.ntp.org) to get the current time and update the RTC. This should be done regularly to correct any drift in the RTC.
Once you have an RTC and a method of setting the time you can query the time whenever you sample some data and store that time along with the data in whatever way is most suitable for your situation.
It depends on how you define the time stamp. A Unix timestamp is the number of seconds elapsed since Unix epoch time, i.e. January 1 1970 00:00 UTC, this is a very common time stamp. You can setup with NTP via the internet, or you can use a RTC on your board. How you do it depends on how accurate you want it and what you have available. Try this link it may help: https://currentmillis.com/
You can use this python script as your receiver code. Change 'port' to your Arduino port.
To find your Arduino port, before you plug it in, in the shell terminal use
ls /dev/tty*
Use it again after you have plugged your Arduino in and compare the lists to find the new connection. (For me, it ended up being ACM0)
Do not forget to download serial library if it is missing: https://pypi.python.org/pypi/pyserial
import serial
import time
import datetime
ser = serial.Serial(
port='COM5',\
baudrate=9600,\
parity=serial.PARITY_NONE,\
stopbits=serial.STOPBITS_ONE,\
bytesize=serial.EIGHTBITS,\
timeout=0)
print("connected to: " + ser.portstr)
while True:
line = ser.readline()
timestamp = str(time.time())
#timestamp = str(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S'))
with open('output.txt', 'a') as pyfile:
pyfile.write(line + ' ' + timestamp +'\n')
ser.close()
In case you are on Windows, you can Try this Free console application: https://hiterminallogger.sourceforge.io/ Just download and run the exe in a bash console emulator
$./HiTerminalLogger.exe COM<portnumber> 9600
you will get it Timestamped log as well as html report. you can as well as highlight the Text to make it more conspicuous.
