Feb-08-2022, 04:30 PM
New guy here still struggling with time issues.
I'm trying to write a python3 program for my Raspberry PI 3 model B+ that will turn on a LED light every quarter of an hour for a minute and the brightness of the LED will be brighter between dawn and dusk (daylight hours).
With help from this forum, I have this program that tells me sunrise and sunset times in my time zone, but, I can't figure out how to compare current time to sunrise and sunset.
Running during the day should result in LED = 255.
Checking for quarter hour seems to work.
Please look over my code and tell me what I'm assuming or doing wrong.
I'm trying to write a python3 program for my Raspberry PI 3 model B+ that will turn on a LED light every quarter of an hour for a minute and the brightness of the LED will be brighter between dawn and dusk (daylight hours).
With help from this forum, I have this program that tells me sunrise and sunset times in my time zone, but, I can't figure out how to compare current time to sunrise and sunset.
Running during the day should result in LED = 255.
Checking for quarter hour seems to work.
Please look over my code and tell me what I'm assuming or doing wrong.
""" Turn on LED every quarter hour where LED brightness is brighter between sunrise and sunset """ import requests from datetime import datetime,timedelta from zoneinfo import ZoneInfo from array import * latitude = 28.262222 longitude = -96.747165 target_timezone = "America/Chicago" timezone_offset = ZoneInfo(target_timezone).utcoffset(datetime.now()) quarter_hour = array ('d',[00,15,30,45]) day_brightnes = 255 night_brightnes = 125 url = "https://api.sunrise-sunset.org/json" params = { "lat": latitude, "lng": longitude, "formatted": 0, "date": "today", } input_format = "%Y-%m-%dT%H:%M:%S+00:00" output_format = "%H:%M:%S" resp_json = requests.get(url, params=params).json() sunrise_date_time = resp_json["results"]["sunrise"] sunset_date_time = resp_json["results"]["sunset"] LED = night_brightnes if (sunrise_date_time < str(datetime.now()) < sunset_date_time): LED = day_brightnes print(f"LED brightness is {LED}") print(f"Current time is {datetime.now()} local") date_time = datetime.now() time = date_time.time() min = int(time.minute) print(f"Current minute is {min} local") if min in quarter_hour: print(f"It is a quarter hour {min} local")
Output:>>> %Run check_quarter_hour.py
LED brightness is 125
Current time is 2022-02-08 10:13:54.216009 local
Current minute is 13 local
>>>