#include <Arduino.h>
#include <time.h>
#include "SunMoonCalc.h"
// >>> USTAW SWOJĄ LOKALIZACJĘ <<<
const double LATITUDE = 45.6; // szerokość geograficzna (stopnie, N+ / S-)
const double LONGITUDE = 10.3; // długość geograficzna (stopnie, E+ / W-)
const double HEIGHT_M = 300.0; // wysokość n.p.m. w metrach
// Obiekt klasy głównej
SunMoonCalc astro(LATITUDE, LONGITUDE, HEIGHT_M);
// Prosta konwersja radiany -> stopnie, do ładnego wypisu
const double RAD2DEG_LOCAL = 180.0 / 3.14159265358979323846;
// Pomocnicza funkcja: wypisz czas Unix jako datę UTC
void printUnixTimeUTC(const char *label, double unixSec) {
if (unixSec <= 0) {
Serial.print(label);
Serial.println(": (brak / <= 0)");
return;
}
// obcinamy do time_t
time_t t = (time_t)unixSec;
// czas w UTC
struct tm *tm_utc = gmtime(&t);
Serial.print(label);
Serial.print(": ");
if (tm_utc) {
Serial.printf(
"%04d-%02d-%02d %02d:%02d:%02d UTC (%.0f)\n",
tm_utc->tm_year + 1900,
tm_utc->tm_mon + 1,
tm_utc->tm_mday,
tm_utc->tm_hour,
tm_utc->tm_min,
tm_utc->tm_sec,
unixSec
);
} else {
Serial.printf("unix=%.0f (gmtime() zwróciło NULL)\n", unixSec);
}
}
void setup() {
Serial.begin(115200);
while (!Serial) {
; // dla niektórych płytek
}
Serial.println();
Serial.println(F("==== SunMoonCalc - DEMO wszystkich danych klasy ===="));
Serial.printf("Lokalizacja: lat=%.4f°, lon=%.4f°, h=%.1f m\n", LATITUDE, LONGITUDE, HEIGHT_M);
Serial.println();
// ------------------------------------------------------------
// 1) Wybieramy przykładowy czas
// - time(nullptr) jeśli masz ustawiony czas przez NTP/RTC
// - jakikolwiek timestamp "na sztywno"
// ------------------------------------------------------------
// Przykład: 2025-01-01 12:00:00 UTC = 1735732800
double unixTime = 1735732800.0;
time_t t = (time_t)unixTime;
Serial.print(F("Obliczenia dla czasu UTC: "));
printUnixTimeUTC("", unixTime);
Serial.println();
// ------------------------------------------------------------
// 2) Słońce – pozycja
// ------------------------------------------------------------
SunMoonCalc::SunPosition sunPos = astro.getSunPosition(unixTime);
Serial.println(F("=== Pozycja Słońca ==="));
Serial.printf("Azymut : %.3f rad (%.2f°)\n", sunPos.azimuth, sunPos.azimuth * RAD2DEG_LOCAL);
Serial.printf("Wysokosc : %.3f rad (%.2f°)\n", sunPos.altitude, sunPos.altitude * RAD2DEG_LOCAL);
Serial.println();
// ------------------------------------------------------------
// 3) Słońce – czasy (wschody, zachody, golden hour, itd.)
// ------------------------------------------------------------
SunMoonCalc::SunTimes st = astro.getSunTimes(unixTime);
Serial.println(F("=== Czasy związane ze Słońcem ==="));
printUnixTimeUTC("Solar noon", st.solarNoon);
printUnixTimeUTC("Nadir", st.nadir);
Serial.println();
printUnixTimeUTC("Sunrise", st.sunrise);
printUnixTimeUTC("Sunrise end", st.sunriseEnd);
printUnixTimeUTC("Sunset start", st.sunsetStart);
printUnixTimeUTC("Sunset", st.sunset);
Serial.println();
printUnixTimeUTC("Civil dawn", st.dawn);
printUnixTimeUTC("Civil dusk", st.dusk);
Serial.println();
printUnixTimeUTC("Nautical dawn", st.nauticalDawn);
printUnixTimeUTC("Nautical dusk", st.nauticalDusk);
Serial.println();
printUnixTimeUTC("Astronomical night end", st.nightEnd);
printUnixTimeUTC("Astronomical night", st.night);
Serial.println();
printUnixTimeUTC("Golden hour end (rano)", st.goldenHourEnd);
printUnixTimeUTC("Golden hour (wieczor)", st.goldenHour);
Serial.println();
// ------------------------------------------------------------
// 4) Księżyc – pozycja
// ------------------------------------------------------------
SunMoonCalc::MoonPosition moonPos = astro.getMoonPosition(unixTime);
Serial.println(F("=== Pozycja Księżyca ==="));
Serial.printf("Azymut : %.3f rad (%.2f°)\n", moonPos.azimuth, moonPos.azimuth * RAD2DEG_LOCAL);
Serial.printf("Wysokosc (alt) : %.3f rad (%.2f°)\n", moonPos.altitude, moonPos.altitude * RAD2DEG_LOCAL);
Serial.printf("Odleglosc : %.2f km\n", moonPos.distance);
Serial.printf("Kat paralaktyczny : %.3f rad (%.2f°)\n", moonPos.parallacticAngle, moonPos.parallacticAngle * RAD2DEG_LOCAL);
Serial.println();
// ------------------------------------------------------------
// 5) Księżyc – iluminacja (jak w SunCalc)
// ------------------------------------------------------------
SunMoonCalc::MoonIllumination mi = astro.getMoonIllumination(unixTime);
Serial.println(F("=== Iluminacja Księżyca (SunCalc-style) ==="));
Serial.printf("Fraction (oswietlenie tarczy) : %.4f (%.1f %%)\n", mi.fraction, mi.fraction * 100.0);
Serial.printf("Phase (0=New, 0.5=Full) : %.4f\n", mi.phase);
Serial.printf("Kat jasnej krawedzi (angle) : %.3f rad (%.2f°)\n", mi.angle, mi.angle * RAD2DEG_LOCAL);
Serial.println();
// ------------------------------------------------------------
// 6) Księżyc – faza + zodiak (dawny MoonPhase)
// ------------------------------------------------------------
SunMoonCalc::MoonPhaseData mp = astro.getMoonPhase(t);
Serial.println(F("=== Faza Księżyca (MoonPhase-style) ==="));
Serial.printf("Julian Date : %.5f\n", mp.jDate);
Serial.printf("Phase (0..1) : %.4f\n", mp.phase);
Serial.printf("Age (dni) : %.4f\n", mp.age);
Serial.printf("Fraction : %.4f (%.1f %%)\n", mp.fraction, mp.fraction * 100.0);
Serial.printf("Distance (R_Earth): %.4f\n", mp.distance);
Serial.printf("Latitude (deg) : %.4f\n", mp.latitude);
Serial.printf("Longitude (deg) : %.4f\n", mp.longitude);
Serial.printf("Phase name : %s\n", mp.phaseName);
Serial.printf("Zodiac name : %s\n", mp.zodiacName);
Serial.println();
Serial.println(F("==== KONIEC DEMA – dane policzone jednorazowo w setup() ===="));
}
void loop() {
}