|
| 1 | +/**The MIT License (MIT) |
| 2 | +
|
| 3 | +Copyright (c) 2018 by ThingPulse Ltd., https://thingpulse.com |
| 4 | +
|
| 5 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +of this software and associated documentation files (the "Software"), to deal |
| 7 | +in the Software without restriction, including without limitation the rights |
| 8 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +copies of the Software, and to permit persons to whom the Software is |
| 10 | +furnished to do so, subject to the following conditions: |
| 11 | +
|
| 12 | +The above copyright notice and this permission notice shall be included in all |
| 13 | +copies or substantial portions of the Software. |
| 14 | +
|
| 15 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +SOFTWARE. |
| 22 | +*/ |
| 23 | + |
| 24 | +#include <Arduino.h> |
| 25 | +#include <ESP8266WiFi.h> |
| 26 | +#include <ESPHTTPClient.h> |
| 27 | +#include <time.h> |
| 28 | +#include "SunMoonCalc.h" |
| 29 | + |
| 30 | +/** |
| 31 | + * WiFi Settings |
| 32 | + */ |
| 33 | +const char* WIFI_SSID = "yourssid"; |
| 34 | +const char* WIFI_PASSWORD = "yourpassw0rd"; |
| 35 | + |
| 36 | +// initiate the WifiClient |
| 37 | +WiFiClient wifiClient; |
| 38 | + |
| 39 | +/** |
| 40 | + * Helper funtions |
| 41 | + */ |
| 42 | +void connectWifi() { |
| 43 | + Serial.print("Connecting to "); |
| 44 | + Serial.println(WIFI_SSID); |
| 45 | + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); |
| 46 | + while (WiFi.status() != WL_CONNECTED) { |
| 47 | + delay(500); |
| 48 | + Serial.print("."); |
| 49 | + } |
| 50 | + Serial.println(""); |
| 51 | + Serial.println("WiFi connected!"); |
| 52 | + Serial.println(WiFi.localIP()); |
| 53 | +} |
| 54 | + |
| 55 | +void printResult(SunMoonCalc::Result result) { |
| 56 | + Serial.println("Sun"); |
| 57 | + Serial.println("\tRise: " + formatTime(result.sun.rise)); |
| 58 | + Serial.println("\tNoon: " + formatTime(result.sun.transit)); |
| 59 | + Serial.println("\tSet: " + formatTime(result.sun.set)); |
| 60 | + Serial.println("\tAzimuth: " + String(result.sun.azimuth) + "°"); |
| 61 | + Serial.println("\tElevation: " + String(result.sun.elevation) + "°"); |
| 62 | + Serial.println("\tDistance: " + String(result.sun.distance) + "km"); |
| 63 | + Serial.println("Moon"); |
| 64 | + Serial.println("\tRise: " + formatTime(result.moon.rise)); |
| 65 | + Serial.println("\tNoon: " + formatTime(result.moon.transit)); |
| 66 | + Serial.println("\tSet: " + formatTime(result.moon.set)); |
| 67 | + Serial.println("\tAzimuth: " + String(result.moon.azimuth) + "°"); |
| 68 | + Serial.println("\tElevation: " + String(result.moon.elevation) + "°"); |
| 69 | + Serial.println("\tDistance: " + String(result.moon.distance) + "km"); |
| 70 | + Serial.println("\tAge: " + String(result.moon.age) + " days"); |
| 71 | + Serial.println("\tIllumination: " + String(result.moon.illumination * 100) + "%"); |
| 72 | + Serial.println("\tPhase: " + result.moon.phase.name); |
| 73 | +} |
| 74 | + |
| 75 | +String padWithZeroBelowTen(int d) { |
| 76 | + return d < 10 ? "0" + String(d) : String(d); |
| 77 | +} |
| 78 | + |
| 79 | +String formatTime(time_t timestamp) { |
| 80 | + tm *date = gmtime(×tamp); |
| 81 | + String year = "" + String(date->tm_year + 1900); |
| 82 | + String month = padWithZeroBelowTen(date->tm_mon + 1); |
| 83 | + String day = padWithZeroBelowTen(date->tm_mday); |
| 84 | + return year + "-" + month + "-" + day + " " + padWithZeroBelowTen(date->tm_hour) + ":" + |
| 85 | + padWithZeroBelowTen(date->tm_min) + ":" + padWithZeroBelowTen(date->tm_sec) + " UTC"; |
| 86 | +} |
| 87 | +// END helper functions |
| 88 | + |
| 89 | +void setup() { |
| 90 | + Serial.begin(115200); |
| 91 | + Serial.println(); |
| 92 | + delay(500); |
| 93 | + connectWifi(); |
| 94 | + Serial.println(); |
| 95 | + |
| 96 | + Serial.println("Syncing time..."); |
| 97 | + configTime(0, 0, "pool.ntp.org"); |
| 98 | + // some explanations about this POSIX format: https://www.ibm.com/developerworks/aix/library/au-aix-posix/ |
| 99 | + // -> represents a central European timezone identifier such as Europe/Berlin |
| 100 | + setenv("TZ", "CET-1CEST,M3.5.0,M10.5.0/3", 0); |
| 101 | + while(time(nullptr) <= 100000) { |
| 102 | + Serial.print("."); |
| 103 | + delay(100); |
| 104 | + } |
| 105 | + Serial.println("Time sync'ed"); |
| 106 | + |
| 107 | + // prepare the time input value |
| 108 | + time_t tnow = time(nullptr); |
| 109 | + Serial.println(String(ctime(&tnow))); |
| 110 | + |
| 111 | + // 'now' has to be UTC, lat/lng in degrees not raadians |
| 112 | + SunMoonCalc smCalc = SunMoonCalc(tnow, 52.520008, 13.404954); |
| 113 | + const SunMoonCalc::Result result = smCalc.calculateSunAndMoonData(); |
| 114 | + |
| 115 | + // for reference you may want to compare results (remember: they're in UTC!) to https://www.timeanddate.com/moon/ |
| 116 | + printResult(result); |
| 117 | +} |
| 118 | + |
| 119 | +void loop() { |
| 120 | + // no-op, only run the code once |
| 121 | +} |
0 commit comments