Using .cpp and .h in arduino that were written for esp8266

And am having nothing but issues. All I want to do is use SunMoonCalc.h and cpp in a mega, but this is turning out to be a thorny issue.

Anybody know a good place to start.

I got it to compile and run but somehow when I call it now, the time_t variable gets messed up and returns the same wrong date everytime.

SunMoonCalc.h

Where from ?

All I want to do is use SunMoonCalc.h and cpp
Anybody know a good place to start.

Within those files, open them in an editor like notepad++ and have a look.

time_t variable gets messed up and returns the same wrong date everytime.

well maybe the struct is not defined in the same way. Without the code (within </> code-tags) and a reference to the library, this is all i can say.

It's on GitHub ESP8266_Weather_Station lots of really fun stuff you can do with weather reading in any of 40000 places on the map. Plus it has this handy SunMoonCalc you can give it your lon and lat and any time and it will tell you where the sun and moon are with respect to your position plus rise and set times so you can program your lights accordingly. I was hoping to be able to just use the calculation part in a Arduino module, but here's the gotcha, the whole thing is tightly linked to the esp board and unplugging all that is a hassle unless you know what you are doing. I got pretty far with it, but this time_t keeps causing problems of one sort or the other. And if you look at the cpp you see
/*****************************************************************************
*

the GitHub link gets you this leaving me lost because I don't know enough cpp:

timegm(3) - Linux man page

Name

timegm, timelocal - inverses of gmtime and localtime

Synopsis

#include <time.h>

time_t timelocal(struct tm *tm);

time_t timegm(struct tm *tm);

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
timelocal(), timegm(): _BSD_SOURCE || _SVID_SOURCE
Description

The functions timelocal() and timegm() are the inverses of localtime(3) and gmtime(3).

Conforming To

These functions are nonstandard GNU extensions that are also present on the BSDs. Avoid their use; see NOTES.

Notes

The timelocal() function is equivalent to the POSIX standard function mktime(3). There is no reason to ever use it.

For a portable version of timegm(), set the TZ environment variable to UTC, call mktime(3) and restore the value of TZ. Something like

#include <time.h>
#include <stdlib.h>

time_t
my_timegm(struct tm *tm)
{
time_t ret;
char *tz;

tz = getenv("TZ");
setenv("TZ", "", 1);
tzset();
ret = mktime(tm);
if (tz)
setenv("TZ", tz, 1);
else
unsetenv("TZ");
tzset();
return ret;
}

What was so strange about all of this was when I finally got it to run on the arduino mega time_t would give a date in 2050 I think and as i would change the date within the sketch the date kept coming back with the same date over and over.

Then I'd run the same sketch on the esp device and it worked.

sevenoutpinball:
What was so strange about all of this was when I finally got it to run on the arduino mega time_t would give a date in 2050 I think and as i would change the date within the sketch the date kept coming back with the same date over and over.

Apparently the ESP8266 uses UNIX time. That has a base of 1970. Arduino time has a base of 2000 so if you put a UNIX time into the Arduino time functions you get a result that is 30 years later. There is a constant named UNIX_OFFSET you can subtract from a UNIX timestamp to make and Arduino timestamp. (Similarly, the Network Time Protocol timestamp has an NTP_OFFSET)

that's exactly what happened!!

i guess that explains it. Thanks!!!

#define UNIX_OFFSET 946684800
Difference between the Y2K and the UNIX epochs, in seconds. To convert a Y2K timestamp to UNIX...
long unix;
time_t y2k;
y2k = time(NULL);
unix = y2k + UNIX_OFFSET;
Typedef Documentation
typedef uint32_t time_t
time_t represents seconds elapsed from Midnight, Jan 1 2000 UTC (the Y2K 'epoch'). Its range allows this implementation to represent time up to Tue Feb 7 06:28:15 2136 UTC.

One more time, please post code within </> code tags

Deva_Rishi:
One more time, please post code within </> code tags

Seriously @sevenoutpinball, 197 post and you don't know how to use Code Tags?