Skip to content

ESP32 library to calculate the moon phase angle and illuminated fraction for a given time. Version 2.0 introduces API cleanup, stricter compilation, and improved accuracy.

License

Notifications You must be signed in to change notification settings

CelliesProjects/moonPhase-esp32

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MoonPhase

A library for esp32 to get the moon phase angle and amount of the moon that is illuminated. (as seen from Earth)

For esp8266 non-os or avr (Arduino) you can use the steve-sienk fork.

Breaking changes upgrading from 1.x to 2.0

The include path changed from #include <moonPhase.h> to #include "MoonPhase.hpp"

The class name changed from moonPhase to MoonPhase

Struct member names were clarified:

angleangleDeg

percentLitamountLit

Before (v1.x) After (v2.0)
moonPhase MoonPhase
moonData_t.angle moonData_t.angleDeg
moonData_t.percentLit moonData_t.amountLit

The library now compiles cleanly with -Wall and -Werror enabled.

Add to PlatformIO project

lib_deps = celliesprojects/moonPhase-esp32@^2.0.0

Functions

  • getPhase() Get the current moon phase.
    First set freeRTOS system time - see the example below.

  • getPhase(time_t t) Get the moon phase from time t.

Example code

#include <Arduino.h>
#include <WiFi.h>
#include <MoonPhase.hpp>

const char *wifissid = "network name";
const char *wifipsk = "network password";

MoonPhase moonPhase;

struct tm timeinfo{};

void setup()
{
    Serial.begin(115200);
    Serial.println();
    Serial.println();
    Serial.println("moonPhase esp32-sntp example.");
    Serial.print("Connecting to ");
    Serial.println(wifissid);
    WiFi.begin(wifissid, wifipsk);
    while (!WiFi.isConnected())
        delay(10);
    Serial.println();

    Serial.println("Connected. Syncing NTP...");

    // find your local timezone string at  
    // https://remotemonitoringsystems.ca/time-zone-abbreviations.php

    // timezone: Amsterdam, Netherlands
    const char timeZone[]{"CET-1CEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00"}; 

    const char countryCode[]{"nl"};

    char ntpPool[64];
    snprintf(ntpPool, sizeof(ntpPool), "%s.pool.ntp.org", countryCode);

    configTzTime(timeZone, ntpPool); 

    while (!getLocalTime(&timeinfo, 0))
        delay(10);
}

void loop()
{
    getLocalTime(&timeinfo);
    Serial.print(asctime(&timeinfo));

    moonData_t moon = moonPhase.getPhase();

    Serial.print("Moon phase angle: ");
    Serial.print(moon.angleDeg); // angleDeg is a integer between 0-360
    Serial.println("° (Where 0° is new moon and 180° is full moon)");

    Serial.print("Illuminated: ");
    Serial.print(moon.amountLit * 100); // amountlit is a real between 0-1
    Serial.println("% as seen from Earth");
    Serial.println();
    delay(1000);
}

About

ESP32 library to calculate the moon phase angle and illuminated fraction for a given time. Version 2.0 introduces API cleanup, stricter compilation, and improved accuracy.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages