import datetime
from suntime import Sun, SunTimeException #This is a custom library, for more information, https://github.com/SatAgro/suntime
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BOARD)
GPIO.setup(3, GPIO.OUT)
latitude = 24.68773 #Change to your latitude (obtained from google maps)
longitude = 46.72185 #Change to your longitude (obtained from google maps)
while 1:
sun = Sun(latitude, longitude)
ss = sun.get_sunset_time() + datetime.timedelta(hours=3) #3 hours to leap from UTC to UTC+3, change to your local timezone
now = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(hours=3) #3 hours to leap from UTC to UTC+3, change to your local timezone
diff = ss - now
seconds = diff.total_seconds()
minutes = seconds / 60
hours = minutes / 60
print(seconds) #used for debug
if (-2 < seconds <= 2):
GPIO.output(3, 1)
sleep(60) #You can change this value to control duration of lantern's light time
else:
GPIO.output(3,0)
sleep(1)
GPIO.cleanup()