How to use time library in Arduino? - c++

I am trying to upload this simple program to my Arduino that gets to clock time:
#include <Time.h>
time_t nowTime;
void setup() {
nowTime = now();
}
However, its failing to compile:
exit status 1
'now' was not declared in this scope
Why is now() not declared in this scope? The Time.h file was included. So why wouldn't now() be declared? How can I get around this?

The compilation fails because the Time.h file that the compiler finds has nothing to do with time libraries such as that by Paul Stoffregen (https://github.com/PaulStoffregen/Time).
I tried your Sketch, compiled for an Arduino Uno, and saw the same error you see: that Time.h resolves (the file exists somewhere), yet now() is not defined by that Time.h
After searching my Windows PC for a while, I finally found what I think is the file that #include includes on my installation: C:\Users\Brad\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.2\firmwares\wifishield\wifiHD\src\time.h or perhaps C:\Users\Brad\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino5\avr\include\time.h
Neither of those files defines the now() function.
If you want to use Paul Stoffregen's Time library, download and install it from https://github.com/PaulStoffregen/Time. If instead you wish to use Michael Margolis' Time library, you can find and install it in the Arduino IDE, under Tools / Manage Libraries... and entering "Time" (without quotes) in the search term.
As others have pointed out, the Arduino environment doesn't always know the current date and time. The functions mills() and micros(), return the number of milliseconds or microseconds, respectively, since the Arduino booted. For just looking at the passage of time, most people use millis() or micros() instead of a more complex library.

Related

exit status 1 expected primary-expression before '.' token

I'm trying to get the temperature and humidity every 60 seconds, but my code won't work. Whenever I compile it, there's this error "expected primary-expression before '.' token"
This line gets highlighted.
Serial.print(DHT.humidity,0);//prints humidity in serial
Here's my entire code:
#include <DHT_U.h>
#define DHT12PIN 7// sets pin 7 for DHT11 signal connection
void setup(){
Serial.begin(9600);//opens serial
}
void loop()
{
int chk = DHT.read12(DHT12PIN);//reads DHT12
Serial.print(DHT.temperature,0);//prints temp in serial
Serial.print(",");//pints comma in serial
Serial.print(DHT.humidity,0);//prints humidity in serial
Serial.println(); //carraiage return
delay(2000);//wait 2 seconsds
}
Wow there was a lot to unpack in this question.
I have used the DHT library by Adafruit a lot of times and thought that you just ended up using the class name instead of instantiating a variable and that's why you were facing the issue.
But then I realized that you are using the following function which is not a part of the Adafruit library:
int chk = dht.read12(DHT12PIN);//reads DHT12
So, I did a bit of digging on the internet and realized that someone has made a library for DHT with that specific function. (A lot of libraries to be specific)
Based on my analysis of this library, and the example code that was given, you have 3 issues.
You have included the wrong header file. The header file dht.h from RobTillaart's library should replace the header file DHT_U.h of Adafruit's library, in your code.
You have to instantiate the variable named DHT of the dht class. This can be done as follows, above your setup function
dht DHT;
Given that you had the code and the header files mixed up from TWO DIFFERENT LIBRARIES, I am guessing that you have installed the Adafruit library, in the place of RobTillaart's library. To fix this, you will have to remove the DHT-sensor-library-1.3.4 from your Documents/Arduino/libraries, create a new folder named DHTStable in it's place, and place all of the files listed here in the new folder.
Some words of experience-based wisdom, don't use RobTillaart's library, I can tell from a glance of it's folder structure that you will face more problems then you can solve if you use it. Instead use Adafruit's library, with their example. You will also have to install Adafruit's sensor library, but it is definitely worth it.

Executing Code on a Certain Date?

Basically, I want to create a program that will check the month, the day and the year and will execute code if both the month and the day criteria is met.
For example, let's say the date was July 8th, 2016.
Let's say I had some code that simply wanted the program to output "Hello world!" on this date.
I would want this code to execute on July 8, 2016 and no other date. How would I go about this?
To run your program at a certain time, you have to rely on external tools such as cron or the Windows task scheduler. A program cannot run itself if it's not already running :-)
If your code is running and you just want it to delay action until some specific time, that's what all the stuff in the ctime header is for.
You can use time() and localtime() to get your local time into a struct tm, then examine the fields to check if some specific time is current. If so, do your action. If not, loop around and try again (with a suitable delay if needed).
By way of example, here's a program that outputs the time but only on five-second boundaries:
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
int main() {
time_t now;
struct tm *tstr;
// Ensure first one is printed.
int lastSec = -99;
// Loop until time call fails, hopefully forever.
while ((now = time(0)) != (time_t)-1) {
// Get the local time into a structire.
tstr = localtime(&now);
// Print, store seconds if changed and multiple of five.
if ((lastSec != tstr->tm_sec) && ((tstr->tm_sec % 5) == 0)) {
cout << asctime(tstr);
lastSec = tstr->tm_sec;
}
}
return 0;
}
I would use std::this_thread::sleep_until(time_to_execute); where time_to_execute is a std::chrono::system_clock::time_point.
Now the question becomes: How do you set the system_clock::time_point to the correct value?
Here is a free, open-source library for easily setting a system_clock::time_point to a specific date. Using it would look like:
using namespace date;
std::this_thread::sleep_until(sys_days{jul/8/2016});
This would trigger at 2016-07-08 00:00:00 UTC. If you would rather trigger based on your local time, or some arbitrary time zone, here is a companion library to accomplish that.
You can also drop down to the C API and set a std::tm's field values, convert that to a time_t and then convert that to a system_clock::time_point. It is uglier, more error prone, and doesn't require a 3rd party library.

Getting incorrect file modification time using stat APIs

I see a strange behavior while fetching the modification time of a file.
we have been calling _stat64 method to fetch the file modification in our project as following.
int my_win_stat( const char *path, struct _stati64 *buf)
{
if(_stati64( path, buf) == 0)
{
std::cout<<buf->st_mtime << std::endl; //I added to ensure if value is not changing elsewhere in the function.
}
...........
...........
}
When I convert the epoch time returned by st_mtime variable using epoch convertor, it shows 2:30 hrs ahead of current time set on my system.
When I call same API as following from different test project, I see the correct mtime (i.e. according to mtime of file shown by my system).
if (_stat64("D:\\engine_cost.frm", &buffer) == 0)
std::cout << buffer.st_mtime << std::endl;
Even I called GetFileTime() and converted FILETIME to epoch time with the help of this post. I get the correct time according to time set the system.
if (GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite))
{
ULARGE_INTEGER ull;
ull.LowPart = ftWrite.dwLowDateTime;
ull.HighPart = ftWrite.dwHighDateTime;
std::cout << (ull.QuadPart / 10000000ULL - 11644473600ULL);
}
What I am not able to figure out is why does the time mtime differ when called through my existing project?
What are the parameters that could affect the output of mtime ?
What else I could try to debug the problem further ?
Note
In VS2013, _stati64 is a macro which is replaced replaced by _stat64.
File system is NTFS on windows 7.
Unix time is really easy to deal with. It's the number of seconds since Jan 1, 1970 (i.e. 0 represents that specific date).
Now, what you are saying is that you are testing your time (mtime) with a 3rd party tool in your browser and expects that to give you the right answer. So... let's do a test, the following number is Sept 1, 2015 at 00:00:00 GMT.
1441065600
If you go to Epoch Converter and use that very value, does it give you the correct GMT? (if not, something is really wrong.) Then look at the local time value and see whether you get what you would expect for GMT midnight. (Note that I use GMT since Epoch Converter uses that abbreviation, but the correct abbreviation is UTC.)
It seems to me that it is very likely that your code extracts the correct time, but the epoch convertor fails on your computer for local time.
Note that you could just test in your C++ program with something like this:
std::cerr << ctime(&buf->st_mtime) << std::endl;
(just watch out as ctime() is not thread safe)
That will give you the data according to your locale on your computer at runtime.
To better control the date format, use strftime(). That function makes use of a tm structure so you have to first call gmtime or localtime.
An year later I ran into the similar problem but scenario is little different. But this time I understand why there was a +2:30Hrs of gap. I execute the C++ program through a perl script which intern sets the timezone 'GMT-3' and my machine had been in timezone 'GMT+5:30'. As a result there was a difference of '2:30Hrs'.
Why ? As Harry mentioned in this post
changing the timezone in Perl is probably causing the TZ environment variable to be set, which affects the C runtime library as per the documentation for _tzset.

Conversion of timespec for Windows 7 VS 2010

I am trying to build OpenVDB viewer for Windows 7 and bumped into this line of code:
secs = fabs(secs);
int isecs = int(secs);
struct timespec sleepTime = { isecs /*sec*/, int(1.0e9 * (secs - isecs)) /*nsec*/ };
nanosleep(&sleepTime, /*remainingTime=*/NULL);
Unfortunately, i dont know what exactly is the meaning of this code as i need to make it VS2010 compiler compatible in order to build it.
So, can i know what is the equivalent of this code or some other library that i can use to edit it easily??
Assuming secs is a float value giving the time the thread shall sleep in seconds, such as e.g.
float secs = 0.8312f;
You can replace that for the windows version with:
float secs = 0.8312f;
DWORD delay = static_cast<DWORD>(fabs(secs * 1000.0f));
Sleep(delay);
Possibly you could add some checks to this (such as if secs is not negative...).
In order to keep the main code base portable, you could create an extra module where you define your own portable sleep function, maybe with the signature void PortableSleep(float seconds);. Then, place in one .cpp file the Unix implementation, in another the win32 implementation and link accordingly.
What you also can do is using std::this_thread::sleep_for() if you like to waste time on figuring out how the <chrono> stuff works (VS lacks one feature, which makes it a bit harder to use).

Is there a way to get a Unix timestamp in Stata?

I searched online and found several sources that talk about converting Unix timestamps to various workable formats, but none that allow me to actually get such a timestamp from within Stata. As of now, I use variations on
local curr_date = c(current_date)
local curr_time = c(current_time)
to apply timestamps to logs, data sets, etc. but I'd like to just use the Unix timestamp in seconds, if possible.
Are you familiar with help datetime? My understanding is that the Unix time stamp would be something like
display %12.0g clock("`c(current_date)' `c(current_time)'", "DMY hms" )/1000 - clock("1 Jan 1970", "DMY" )/1000
which of course you can use in other circumstances as well. (I am not a C programmer, I am a Stata user, but I do understand that it is easier for most people on this site to write a snippet of C code that would go into the guts of Stata than to RTFM... which is admirable in its own ways from where I sit, of course.)
One way to achieve this is to write a simple plugin. Compile this code, in a file called unixtimestamp.c
#include "stplugin.h"
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
STDLL stata_call(int argc, char *argv[])
{
time_t seconds;
seconds = time(NULL);
char buf[33];
sprintf(buf, "%d", seconds);
SF_display(buf);
return(0);
}
with stplugin.h and stplugin.c in the same directory using this command (for a Linux system):
gcc -O3 -shared -DSYSTEM=OPUNIX -fPIC stplugin.c unix_timestamp.c -o unixtimestamp.plugin
The guide to creating plugins uses this command:
gcc -shared -DSYSTEM=OPUNIX stplugin.c unixtimestamp.c -o unixtimestamp.plugin
but on some systems, this gives an error instructing you to use the -fPIC flag, which is why I include it in my command. Also, optimizations aren't really necessary for such a simple plugin, but I included them regardless.
unixtimestamp.plugin should be placed in the ado/personal/ directory. Run Stata's sysdir function to find its location. On my system, it's HOME/ado/personal/ so I copied the plugin there. Then, from Stata, load the plugin:
program unixtimestamp, plugin
If no error message is displayed, run the plugin with:
plugin call unixtimestamp
As with any Stata command, you can also use a macro to simplify this if you plan to use this command frequently:
local unixtime plugin call unixtimestamp
`unixtime'
I use the following to grab a date/time stamp:
di "DateTime: $S_DATE $S_TIME"
Updated:
You may use Unix shell commands directly inside Stata by prefixing them an exclamation mark. To echo Unix time try:
!date +%s
1344341160