Python Forum
Needing to Check Every Quarter Hour
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Needing to Check Every Quarter Hour
#1
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.

""" 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 >>>
Reply


Messages In This Thread
Needing to Check Every Quarter Hour - by bill_z - Feb-08-2022, 04:30 PM
RE: Needing to Check Every Quarter Hour - by bill_z - Feb-08-2022, 05:01 PM
RE: Needing to Check Every Quarter Hour - by bill_z - Feb-08-2022, 07:19 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  needing some help to write some code for a game calculator rymdaksel 1 476 Jan-02-2024, 09:56 AM
Last Post: deanhystad
  code running for more than an hour now, yet didn't get any result, what should I do? aiden 2 1,558 Apr-06-2022, 03:41 PM
Last Post: Gribouillis
  Struggling for the past hour to define function and call it back godlyredwall 2 2,264 Oct-29-2020, 02:45 PM
Last Post: deanhystad
  Noob needing guidance.... bako 0 1,881 Mar-29-2020, 06:55 PM
Last Post: bako
  Global Variables - Some Points Needing Clarification. adt 4 3,008 Nov-30-2019, 01:23 PM
Last Post: adt
  Beginner needing advice with data files JFI2019 2 2,246 Nov-06-2019, 04:56 PM
Last Post: JFI2019
  Typical beginner needing some help foxter00 1 2,703 May-08-2019, 11:46 PM
Last Post: michalmonday
  Different prices per hour kemper 1 2,061 Jan-29-2019, 11:06 AM
Last Post: Larz60+
  New user/beginner needing help oldDog 3 3,262 Apr-17-2018, 02:31 PM
Last Post: oldDog
  Beginner needing help! Franco 2 3,293 Jul-29-2017, 12:56 PM
Last Post: Franco

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020