Skip to content

Add SunMoonCalc port #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 7, 2018
Merged

Add SunMoonCalc port #121

merged 2 commits into from
Jul 7, 2018

Conversation

marcelstoer
Copy link
Member

@marcelstoer marcelstoer commented Jun 21, 2018

My Arduino test bed was as follows:

#include <SunMoonCalc.h>
#include <time.h>
#include <math.h>
#include <Arduino.h>

void output(SunMoonCalc::Result result) {
  Serial.println("Sun");
  Serial.println("\tRise: " + formatTime(result.sun.rise));
  Serial.println("\tNoon: " + formatTime(result.sun.transit));
  Serial.println("\tSet: " + formatTime(result.sun.set));
  Serial.println("\tAzimuth: " + String(result.sun.azimuth) + "°");
  Serial.println("\tElevation: " + String(result.sun.elevation) + "°");
  Serial.println("\tDistance: " + String(result.sun.distance) + "km");
  Serial.println("Moon");
  Serial.println("\tRise: " + formatTime(result.moon.rise));
  Serial.println("\tNoon: " + formatTime(result.moon.transit));
  Serial.println("\tSet: " + formatTime(result.moon.set));
  Serial.println("\tAzimuth: " + String(result.moon.azimuth) + "°");
  Serial.println("\tElevation: " + String(result.moon.elevation) + "°");
  Serial.println("\tDistance: " + String(result.moon.distance) + "km");
  Serial.println("\tAge: " + String(result.moon.age) + " days");
  Serial.println("\tIllumination: " + String(result.moon.illumination * 100) + "%");
  Serial.println("\tPhase: " + result.moon.phase.name);
}

String padWithZeroBelowTen(int d) {
  return d < 10 ? "0" + String(d) : String(d);
}

String formatTime(time_t timestamp) {
  tm *date = gmtime(&timestamp);
  String year = "" + String(date->tm_year + 1900);
  String month = padWithZeroBelowTen(date->tm_mon + 1);
  String day = padWithZeroBelowTen(date->tm_mday);
  return year + "-" + month + "-" + day + " " + padWithZeroBelowTen(date->tm_hour) + ":" +
         padWithZeroBelowTen(date->tm_min) + ":" + padWithZeroBelowTen(date->tm_sec) + " UTC";
}

void setup() {
  Serial.begin(115200);
  Serial.println();
//  struct tm tm{};
//  tm.tm_year = 2018 - 1900;
//  tm.tm_mon = 6 - 1;
//  tm.tm_mday = 8;
//  tm.tm_hour = 20;
//  tm.tm_min = 55;
//  tm.tm_sec = 34;
//  SunMoonCalc smCalc = SunMoonCalc(timegm(&tm), 47.46375, 8.95489);
//  <- Arduino doesn't have timegm => use epoch instant directly
  SunMoonCalc smCalc = SunMoonCalc(1528491334, 47.46375, 8.95489);
  const SunMoonCalc::Result result = smCalc.calculateSunAndMoonData();
  output(result);
}

void loop() {
  // put your main code here, to run repeatedly:
}

It produces

Sun
	Rise: 2018-06-09 03:27:34 UTC
	Noon: 2018-06-08 11:23:13 UTC
	Set: 2018-06-08 19:18:53 UTC
	Azimuth: 325.51°
	Elevation: -12.19°
	Distance: 151851716.82km
Moon
	Rise: 2018-06-09 01:11:21 UTC
	Noon: 2018-06-09 07:29:04 UTC
	Set: 2018-06-08 12:49:30 UTC
	Azimuth: 33.89°
	Elevation: -37.32°
	Distance: 389017.22km
	Age: 24.17 days
	Illumination: 29.18%
	Phase: Waning Crescent

which is extremely close (<1min deviation) to what https://www.timeanddate.com/moon/@6294748 produces.

@marcelstoer marcelstoer requested a review from squix78 June 21, 2018 21:17
Add new example to CI build

Clean-up whitespace
@squix78 squix78 merged commit e5e5af6 into master Jul 7, 2018
@marcelstoer marcelstoer deleted the feat/sunmoon-calc branch July 7, 2018 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants