// solarTimeRTC
// Arduino example sketch for SolarPosition library
//
// Calculate solar position from time and location information
// using an RTC as a time source.
// 2017 Ken Willmott
// Arduino library based on the program "Arduino Uno and Solar Position Calculations"
// (c) David R. Brooks, which can be found at http://www.instesre.org/ArduinoDocuments.htm
// and issued under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License:
// https://creativecommons.org/licenses/by-nc-nd/4.0/
// Simulation at https://wokwi.com/projects/362382128060882945
#include "SolarPosition.h" // https://github.com/KenWillmott/SolarPosition
// choose your RTC library (DS1307 lib can also read from the DS3231 IC):
//#include <DS3232RTC.h>
#include <DS1307RTC.h> // https://github.com/PaulStoffregen/DS1307RTC
// Wokwi sets the default RTC to match the local computer's system time,
// if your local computer's time is not UTC, you need an offset
const int localUtcOffsetHours = -2;// Adjust RTC from default Wokwi local time setting to UTC:
// number of decimal digits to print
const uint8_t digits = 3;
// some test positions:
SolarPosition Lund(55.71, 13.20); // Lund
SolarPosition Berlin(52.52, 13.41); // Berlin
SolarPosition Milan(45.46, 9.19); // Milan
// program begins
void setup()
{
Serial.begin(9600);
Serial.println(F("\tSolar Position Demo"));
if(localUtcOffsetHours != 0 ){ // make local/UTC adjustments
unsigned long newTime = RTC.get() + 3600L*localUtcOffsetHours;
RTC.set(newTime);
}
// set the Time service as the time provider
SolarPosition::setTimeProvider(RTC.get);
}
void loop()
{
// now test the real time methods:
Serial.println();
printTime(RTC.get());
Serial.print(F("Lund:\t"));
printSolarPosition(Lund.getSolarPosition(), digits);
Serial.print(F("Berlin:\t"));
printSolarPosition(Berlin.getSolarPosition(), digits);
Serial.print(F("Milan:\t"));
printSolarPosition(Milan.getSolarPosition(), digits);
delay(15000);
}
// Print a solar position to serial
void printSolarPosition(SolarPosition_t pos, int numDigits)
{
Serial.print(F("el: "));
Serial.print(pos.elevation, numDigits);
Serial.print(F(" deg\t"));
Serial.print(F("az: "));
Serial.print(pos.azimuth, numDigits);
Serial.println(F(" deg"));
}
// Print a time to serial
void printTime(time_t t)
{
tmElements_t someTime;
breakTime(t, someTime);
if(someTime.Hour<10)Serial.print('0');
Serial.print(someTime.Hour);
Serial.print(F(":"));
if(someTime.Minute<10)Serial.print('0');
Serial.print(someTime.Minute);
Serial.print(F(":"));
if(someTime.Second<10)Serial.print('0');
Serial.print(someTime.Second);
Serial.print(F(" UTC on "));
Serial.print(dayStr(someTime.Wday));
Serial.print(F(", "));
Serial.print(monthStr(someTime.Month));
Serial.print(F(" "));
Serial.print(someTime.Day);
Serial.print(F(", "));
Serial.println(tmYearToCalendar(someTime.Year));
}