diff --git a/main.py b/main.py index 6775ebb..8344a9d 100644 --- a/main.py +++ b/main.py @@ -1,73 +1,16 @@ -import time from google.oauth2.service_account import Credentials -from google.auth.transport.requests import AuthorizedSession import gspread from gpiozero import Button -from datetime import datetime, date +from datetime import datetime import requests # Typo corrected from "urequets" - print("Loaded modules") + + # Replace with your Google Sheet ID and JSON key file path sheet_id = '1u2M_qIo_XLVv5HPKYmyX_ehruxVFECf8vmXNKnErk4U' json_key_file = '/creds/service_account.json' API_URL = "http://api.henriserverack.com/get_sheet_data" -# Specify GPIO pins for buttons and LEDs - -##set 1 (left?) -button1_pin = 2 # Adjust pin numbers as needed -button2_pin = 3 -button3_pin = 4 -button4_pin = 17 -button5_pin = 27 -button6_pin = 22 -button7_pin = 10 -button8_pin = 9 -button9_pin = 11 -button10_pin = 5 -button11_pin = 6 -button12_pin = 13 -button13_pin = 19 -button14_pin = 26 - -##set 2 (right?) -button15_pin = 14 -button16_pin = 15 -button17_pin = 18 -button18_pin = 23 -button19_pin = 24 -button20_pin = 25 -button21_pin = 8 -button22_pin = 7 - - - - -# Create Button objects -button1 = Button(button1_pin) -button2 = Button(button2_pin) -button3 = Button(button3_pin) -button4 = Button(button4_pin) -button5 = Button(button5_pin) -button6 = Button(button6_pin) -button7 = Button(button7_pin) -button8 = Button(button8_pin) -button9 = Button(button9_pin) -button10 = Button(button10_pin) -button11 = Button(button11_pin) -button12 = Button(button12_pin) -button13 = Button(button13_pin) -button14 = Button(button14_pin) -button15 = Button(button15_pin) -button16 = Button(button16_pin) -button17 = Button(button17_pin) -button18 = Button(button18_pin) -button19 = Button(button19_pin) -button20 = Button(button20_pin) -button21 = Button(button21_pin) -button22 = Button(button22_pin) - - # Authenticate with Google Sheets API using google-auth SCOPES = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive'] @@ -76,193 +19,60 @@ worksheet = gc.open_by_key(sheet_id).sheet1 print("Loaded credentials") + def make_api_request(): response = requests.get(API_URL) # Use authorized session data = response.json() return data -# Function to add a row to the spreadsheet def add_row(timestamp, action, name): worksheet.append_row([timestamp, action, name], value_input_option='USER_ENTERED') -# Function to check if a person is currently clocked in -def is_clocked_in(name): - api_data = make_api_request() - if name == "Mason": - if api_data["data"][0] == "Clocked In": - return True - if api_data["data"][0] == "Not Clocked In": - return False - if name == "Cameron": - if api_data["data"][1] == "Clocked In": - return True - if api_data["data"][1] == "Not Clocked In": - return False - if name == "Jonah": - if api_data["data"][2] == "Clocked In": - return True - if api_data["data"][2] == "Not Clocked In": - return False - if name == "Jerry": - if api_data["data"][3] == "Clocked In": - return True - if api_data["data"][3] == "Not Clocked In": - return False - if name == "Henri K": - if api_data["data"][4] == "Clocked In": - return True - if api_data["data"][4] == "Not Clocked In": - return False - if name == "Liam": - if api_data["data"][5] == "Clocked In": - return True - if api_data["data"][5] == "Not Clocked In": - return False - if name == "Ki Bae": - if api_data["data"][6] == "Clocked In": - return True - if api_data["data"][6] == "Not Clocked In": - return False - if name == "Oliver": - if api_data["data"][7] == "Clocked In": - return True - if api_data["data"][7] == "Not Clocked In": - return False - if name == "Clark": - if api_data["data"][8] == "Clocked In": - return True - if api_data["data"][8] == "Not Clocked In": - return False - if name == "Rain": - if api_data["data"][9] == "Clocked In": - return True - if api_data["data"][9] == "Not Clocked In": - return False - if name == "Malcom": - if api_data["data"][10] == "Clocked In": - return True - if api_data["data"][10] == "Not Clocked In": - return False - if name == "Henry N": - if api_data["data"][11] == "Clocked In": - return True - if api_data["data"][11] == "Not Clocked In": - return False - if name == "Auggie": - if api_data["data"][12] == "Clocked In": - return True - if api_data["data"][12] == "Not Clocked In": - return False - if name == "Anders": - if api_data["data"][13] == "Clocked In": - return True - if api_data["data"][13] == "Not Clocked In": - return False - if name == "Victor": - if api_data["data"][14] == "Clocked In": - return True - if api_data["data"][14] == "Not Clocked In": - return False - if name == "Finn": - if api_data["data"][15] == "Clocked In": - return True - if api_data["data"][15] == "Not Clocked In": - return False - if name == "Sam": - if api_data["data"][16] == "Clocked In": - return True - if api_data["data"][16] == "Not Clocked In": - return False - if name == "Neah": - if api_data["data"][17] == "Clocked In": - return True - if api_data["data"][17] == "Not Clocked In": - return False - if name == "Sophia": - if api_data["data"][18] == "Clocked In": - return True - if api_data["data"][18] == "Not Clocked In": - return False - if name == "Eddie": - if api_data["data"][19] == "Clocked In": - return True - if api_data["data"][19] == "Not Clocked In": - return False - if name == "Sadie": - if api_data["data"][20] == "Clocked In": - return True - if api_data["data"][20] == "Not Clocked In": - return False - if name == "Alena": - if api_data["data"][21] == "Clocked In": - return True - if api_data["data"][21] == "Not Clocked In": - return False +def button_pressed(api_id, name): + global api_data + clocked_in = (api_data["data"][api_id] == "Clocked In") + + action = "Clock Out" if clocked_in else "Clock In" + timestamp = datetime.now().strftime("%m/%d/%Y %H:%M:%S") + add_row(timestamp, action, name) + + # Logging + print(f"[{timestamp}] {action}: {name}") + +def define_button(api_id, pin, name): + # TODO: Do these need debouncing? Default is to not debounce at all + button = Button(pin) + button.when_pressed = lambda: button_pressed(api_id, name) + +# Get initial data +api_data = make_api_request() + +# api_id, pin, name +define_button( 0, 2, "Mason") +define_button( 1, 3, "Cameron") +define_button( 2, 4, "Jonah") +define_button( 3, 17, "Jerry") +define_button( 4, 27, "Henri K") +define_button( 5, 22, "Liam") +define_button( 6, 10, "Ki Bae") +define_button( 7, 9, "Oliver") +define_button( 8, 11, "Clark") +define_button( 9, 5, "Rain") +define_button(10, 6, "Malcom") +define_button(11, 13, "Henry N") +define_button(12, 19, "Auggie") +define_button(13, 26, "Anders") +define_button(14, 14, "Victor") +define_button(15, 15, "Finn") +define_button(16, 18, "Sam") +define_button(17, 23, "Neah") +define_button(18, 24, "Sophia") +define_button(19, 25, "Eddie") +define_button(20, 8, "Sadie") +define_button(21, 7, "Alena") + +# Continuously update data from API while True: - if button1.is_pressed: - action = "Clock Out" if is_clocked_in("Mason") else "Clock In" - add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Mason") - if button2.is_pressed: - action = "Clock Out" if is_clocked_in("Cameron") else "Clock In" - add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Cameron") - if button3.is_pressed: - action = "Clock Out" if is_clocked_in("Jonah") else "Clock In" - add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Jonah") - if button4.is_pressed: - action = "Clock Out" if is_clocked_in("Jerry") else "Clock In" - add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Jerry") - if button5.is_pressed: - action = "Clock Out" if is_clocked_in("Henri K") else "Clock In" - add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Henri K") - if button6.is_pressed: - action = "Clock Out" if is_clocked_in("Liam") else "Clock In" - add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Liam") - if button7.is_pressed: - action = "Clock Out" if is_clocked_in("Ki Bae") else "Clock In" - add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Ki Bae") - if button8.is_pressed: - action = "Clock Out" if is_clocked_in("Oliver") else "Clock In" - add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Oliver") - if button9.is_pressed: - action = "Clock Out" if is_clocked_in("Clark") else "Clock In" - add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Clark") - if button10.is_pressed: - action = "Clock Out" if is_clocked_in("Rain") else "Clock In" - add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Rain") - if button11.is_pressed: - action = "Clock Out" if is_clocked_in("Malcom") else "Clock In" - add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Malcom") - if button12.is_pressed: - action = "Clock Out" if is_clocked_in("Henry N") else "Clock In" - add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Henry N") - if button13.is_pressed: - action = "Clock Out" if is_clocked_in("Auggie") else "Clock In" - add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Auggie") - if button14.is_pressed: - action = "Clock Out" if is_clocked_in("Anders") else "Clock In" - add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Anders") - if button15.is_pressed: - action = "Clock Out" if is_clocked_in("Victor") else "Clock In" - add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Victor") - if button16.is_pressed: - action = "Clock Out" if is_clocked_in("Finn") else "Clock In" - add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Finn") - if button17.is_pressed: - action = "Clock Out" if is_clocked_in("Sam") else "Clock In" - add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Sam") - if button18.is_pressed: - action = "Clock Out" if is_clocked_in("Neah") else "Clock In" - add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Neah") - if button19.is_pressed: - action = "Clock Out" if is_clocked_in("Sophia") else "Clock In" - add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Sophia") - if button20.is_pressed: - action = "Clock Out" if is_clocked_in("Eddie") else "Clock In" - add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Eddie") - if button21.is_pressed: - action = "Clock Out" if is_clocked_in("Sadie") else "Clock In" - add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Sadie") - if button22.is_pressed: - action = "Clock Out" if is_clocked_in("Alena") else "Clock In" - add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Alena") + api_data = make_api_request() + # TODO: Do we want to sleep for some time to not be annoying to the server? diff --git a/pico.py b/pico.py index f98a635..7f80e4a 100644 --- a/pico.py +++ b/pico.py @@ -2,10 +2,7 @@ import adafruit_requests import time import digitalio -import json -import os -import ipaddress import wifi import socketpool @@ -15,7 +12,7 @@ # connect to your SSID wifi.radio.connect("southwest-wireless", "southwwifi000") -#print("Connected to WiFi") +print("Connected to WiFi") pool = socketpool.SocketPool(wifi.radio) @@ -24,169 +21,43 @@ # Replace this with the URL of your API API_URL = "http://api.henriserverack.com/get_sheet_data" -#headers = {"user-agent": "blinka/1.0.0"} -# Replace this with the GPIO pin number where your LED is connected -led1 = digitalio.DigitalInOut(board.GP0) -led1.direction = digitalio.Direction.OUTPUT -led2 = digitalio.DigitalInOut(board.GP1) -led2.direction = digitalio.Direction.OUTPUT -led3 = digitalio.DigitalInOut(board.GP2) -led3.direction = digitalio.Direction.OUTPUT -led4 = digitalio.DigitalInOut(board.GP3) -led4.direction = digitalio.Direction.OUTPUT -led5 = digitalio.DigitalInOut(board.GP4) -led5.direction = digitalio.Direction.OUTPUT -led6 = digitalio.DigitalInOut(board.GP5) -led6.direction = digitalio.Direction.OUTPUT -led7 = digitalio.DigitalInOut(board.GP6) -led7.direction = digitalio.Direction.OUTPUT -led8 = digitalio.DigitalInOut(board.GP7) -led8.direction = digitalio.Direction.OUTPUT -led9 = digitalio.DigitalInOut(board.GP8) -led9.direction = digitalio.Direction.OUTPUT -led10 = digitalio.DigitalInOut(board.GP9) -led10.direction = digitalio.Direction.OUTPUT -led11 = digitalio.DigitalInOut(board.GP10) -led11.direction = digitalio.Direction.OUTPUT -led12 = digitalio.DigitalInOut(board.GP11) -led12.direction = digitalio.Direction.OUTPUT -led13 = digitalio.DigitalInOut(board.GP12) -led13.direction = digitalio.Direction.OUTPUT -led14 = digitalio.DigitalInOut(board.GP13) -led14.direction = digitalio.Direction.OUTPUT -led15 = digitalio.DigitalInOut(board.GP14) -led15.direction = digitalio.Direction.OUTPUT -led16 = digitalio.DigitalInOut(board.GP15) -led16.direction = digitalio.Direction.OUTPUT -led17 = digitalio.DigitalInOut(board.GP16) -led17.direction = digitalio.Direction.OUTPUT -led18 = digitalio.DigitalInOut(board.GP17) -led18.direction = digitalio.Direction.OUTPUT -led19 = digitalio.DigitalInOut(board.GP18) -led19.direction = digitalio.Direction.OUTPUT -led20 = digitalio.DigitalInOut(board.GP19) -led20.direction = digitalio.Direction.OUTPUT -led21 = digitalio.DigitalInOut(board.GP20) -led21.direction = digitalio.Direction.OUTPUT -led22 = digitalio.DigitalInOut(board.GP21) -led22.direction = digitalio.Direction.OUTPUT - - -def control_led(api_data): - clocked_in = api_data["data"] - if api_data["data"][0] == "Clocked In": - led1.value = True - if api_data["data"][0] == "Not Clocked In": - led1.value = False - if api_data["data"][1] == "Clocked In": - led2.value = True - if api_data["data"][1] == "Not Clocked In": - led2.value = False - if api_data["data"][2] == "Clocked In": - led3.value = True - if api_data["data"][2] == "Not Clocked In": - led3.value = False - if api_data["data"][3] == "Clocked In": - led4.value = True - if api_data["data"][3] == "Not Clocked In": - led4.value = False - if api_data["data"][4] == "Clocked In": - led5.value = True - if api_data["data"][4] == "Not Clocked In": - led5.value = False - if api_data["data"][5] == "Clocked In": - led6.value = True - if api_data["data"][5] == "Not Clocked In": - led6.value = False - if api_data["data"][6] == "Clocked In": - led7.value = True - if api_data["data"][6] == "Not Clocked In": - led7.value = False - if api_data["data"][7] == "Clocked In": - led8.value = True - if api_data["data"][7] == "Not Clocked In": - led8.value = False - if api_data["data"][8] == "Clocked In": - led9.value = True - if api_data["data"][8] == "Not Clocked In": - led9.value = False - if api_data["data"][9] == "Clocked In": - led10.value = True - if api_data["data"][9] == "Not Clocked In": - led10.value = False - if api_data["data"][10] == "Clocked In": - led11.value = True - if api_data["data"][10] == "Not Clocked In": - led11.value = False - if api_data["data"][11] == "Clocked In": - led12.value = True - if api_data["data"][11] == "Not Clocked In": - led12.value = False - if api_data["data"][12] == "Clocked In": - led13.value = True - if api_data["data"][12] == "Not Clocked In": - led13.value = False - if api_data["data"][13] == "Clocked In": - led14.value = True - if api_data["data"][13] == "Not Clocked In": - led14.value = False - if api_data["data"][14] == "Clocked In": - led15.value = True - if api_data["data"][14] == "Not Clocked In": - led15.value = False - if api_data["data"][15] == "Clocked In": - led16.value = True - if api_data["data"][15] == "Not Clocked In": - led16.value = False - if api_data["data"][16] == "Clocked In": - led17.value = True - if api_data["data"][16] == "Not Clocked In": - led17.value = False - if api_data["data"][17] == "Clocked In": - led18.value = True - if api_data["data"][17] == "Not Clocked In": - led18.value = False - if api_data["data"][18] == "Clocked In": - led19.value = True - if api_data["data"][18] == "Not Clocked In": - led19.value = False - if api_data["data"][19] == "Clocked In": - led20.value = True - if api_data["data"][19] == "Not Clocked In": - led20.value = False - if api_data["data"][20] == "Clocked In": - led21.value = True - if api_data["data"][20] == "Not Clocked In": - led21.value = False - if api_data["data"][21] == "Clocked In": - led22.value = True - if api_data["data"][21] == "Not Clocked In": - led22.value = False - - +def define_led(pin): + led = digitalio.DigitalInOut(pin) + led.direction = digitalio.Direction.OUTPUT + return led +leds = [define_led(pin) for pin in [ + board.GP0, board.GP1, board.GP2, board.GP3, board.GP4, board.GP5, + board.GP6, board.GP7, board.GP8, board.GP9, board.GP10, board.GP11, + board.GP12, board.GP13, board.GP14, board.GP15, board.GP16, board.GP17, + board.GP18, board.GP19, board.GP20, board.GP21 +]] def make_api_request(): response = requests.get(API_URL) - #print(response) data = response.json() print(data) return data +def control_leds(api_data): + for i in range(len(leds)): + clocked_in = (api_data["data"][i] == "Clocked In"); + leds[i].value = clocked_in + def main(): while True: try: # Make API request api_data = make_api_request() - # Control LED based on API data - control_led(api_data) + # Control LEDs based on API data + control_leds(api_data) # Wait for some time before the next request time.sleep(2) # Adjust the delay as needed - except Exception as e: + except Exception: print("lol") time.sleep(10) if __name__ == "__main__": - main() \ No newline at end of file + main()