From a51e3bcac79f6e645d7b6d9ce6a0e7b04190dfb0 Mon Sep 17 00:00:00 2001 From: Tomrm96 Date: Sun, 3 Nov 2024 17:52:52 +0000 Subject: [PATCH] Fixed error with clock returning None. Resolved LED blinking issue, added Blinks for indication when not plugged in to PC. TODO: - Find a way of sending a Feed manually - FInd a way of getting the next feed time and using it to trigger the next feed. --- Pico_pins.py | 22 ++++++++++++++++------ bridge.py | 18 +++++++++++++++--- clock.py | 24 ++++++++++++++++++++---- dispense_food.py | 7 ++++++- main.py | 25 +++++++++++++++++++------ wifi.py | 8 ++++++-- 6 files changed, 82 insertions(+), 22 deletions(-) diff --git a/Pico_pins.py b/Pico_pins.py index c67aa5f..8bcb78d 100644 --- a/Pico_pins.py +++ b/Pico_pins.py @@ -3,15 +3,28 @@ class PicoPins: def __init__(self): - self.led = Pin(25, Pin.OUT) + self.led = Pin('LED', Pin.OUT) self.servo_pin = PWM(Pin(16)) self.servo_pin.freq(50) self.backwards_value = 6553 self.forward_value = 3276 self.stop_value = 4915 - def blink(self): - self.led.toggle() + def blink(self, num): + for i in range(0,num): + self.led.value(True) + time.sleep(0.2) + self.led.value(False) + time.sleep(0.2) + self.led.value(False) + + def error_blink(self, num): + for i in range(0,num): + self.led.value(True) + time.sleep(0.5) + self.led.value(False) + time.sleep(0.5) + self.led.value(False) def servo_forward(self, amount): for i in range(0, amount): @@ -29,8 +42,5 @@ def servo_backward(self, amount): time.sleep(1) self.servo_pin.deinit() - def dispense_food(self): - pass - def weigh_food(self): pass diff --git a/bridge.py b/bridge.py index c78f6be..9091f5f 100644 --- a/bridge.py +++ b/bridge.py @@ -1,17 +1,19 @@ import requests import wifi_config +from Pico_pins import PicoPins IP_ADDRESS = wifi_config.IP_ADDRESS ENDPOINTS = { 'Test': 'test', 'GET': 'get_feed', - 'DELETE': 'delete_feed' + 'DELETE': 'delete_feed', + 'NEXT_FEED': 'get_next_feed', } class Bridge: def __init__(self): - pass + self.pins = PicoPins() def get_schedule(self, endpoint): @@ -24,6 +26,8 @@ def get_schedule(self, endpoint): print('Response Code:', response.status_code) data = response.json() + self.pins.blink(4) + response.close() for item in data: @@ -33,10 +37,18 @@ def get_schedule(self, endpoint): self.deleted_at = item["deleted_at"] self.pet_name = item["Pet_Name"] self.date_to_feed = item["Date_to_Feed"] - self.feed_time = item["Feed_Time"] + self.feed_time = item["Feed_Time"][:5] + self.last_feed_time = item['Feed_Time'] self.feeding_amount = item["Feeding_Amount"] + self.Next_feed = item["Next_Feed"] def update_schedule(self): + # BUG + self.get_schedule('NEXT_FEED') + next_date = self.get_schedule.date_to_feed + next_time = self.get_schedule.feed_time + print(next_date, next_time) + return 1 # TODO return the next feed time by querying the web app translate the date and time into seconds and return def delete_feed(self): diff --git a/clock.py b/clock.py index 4ee8d0d..2cf2b71 100644 --- a/clock.py +++ b/clock.py @@ -1,14 +1,30 @@ import utime import ntptime +import time +from Pico_pins import PicoPins class Clock: def __init__(self): - self.current_datetime = utime.localtime() + self.update_clock() + self.pins = PicoPins() + + def update_clock(self): + ntptime.host = "ntp1.npl.co.uk" ntptime.settime() - self.year, self.month, self.day, self.hour, self.minute, self.second, _, _ = self.current_datetime + time.sleep(0.5) + self.current_datetime = utime.localtime() + time.sleep(0.5) + self.hour, self.minute = f"{self.current_datetime[3] +1 :02d}", f"{self.current_datetime[4]:02d}" + self.year, self.month, self.day, _, _, self.seconds, _, _ = self.current_datetime def current_clock(self, param): + self.update_clock() if param == 'date': - return f"{self.year}-{self.month}-{self.day}" + date = f"{self.year}-{self.month}-{self.day}" + return date elif param == 'time': - return f"{self.hour}:{self.minute}:{self.second}" \ No newline at end of file + time = f"{self.hour}:{self.minute}" + return time + elif param == 'last_feed_time': + time = f"{self.hour}:{self.minute}:{self.seconds}" + return time \ No newline at end of file diff --git a/dispense_food.py b/dispense_food.py index 41d0222..56ace42 100644 --- a/dispense_food.py +++ b/dispense_food.py @@ -5,4 +5,9 @@ def __init__(self): self.pico_pins = PicoPins() def dispense(self, turns): - self.pico_pins.servo_forward(turns) \ No newline at end of file + self.notify(turns) + self.pico_pins.servo_forward(turns) + + def notify(self, turns): + for i in range(0, 10): + self.pico_pins.blink(1) \ No newline at end of file diff --git a/main.py b/main.py index fa28262..f7f5864 100644 --- a/main.py +++ b/main.py @@ -11,20 +11,33 @@ clock = Clock() dispenser = Dispense() +# Error Blinks: + +# Two Blinks = Couldn't connect to WIFI +# Three Blinks = + +# Normal Blinks: +# One Blink = Looking for WIFI +# Five Blinks = Connected to WIFI +# Four Blinks = Recieved JSON Payload +# Ten Blinks = Dispensing food + while True: + + time.sleep(5) get_schedule.get_schedule('GET') next_feed = get_schedule.update_schedule() + print() - if get_schedule.date_to_feed == clock.current_clock('day') and get_schedule.feed_time == clock.current_clock('time'): - dispenser.dispense(get_schedule.feeding_amount) - else: + if get_schedule.date_to_feed == clock.current_clock('date') and get_schedule.feed_time == clock.current_clock('time'): dispenser.dispense(get_schedule.feeding_amount) + time.sleep(70) - time.sleep(next_feed - 60) + # wifi.disconnect_wifi() - wifi.disconnect_wifi() + time.sleep(next_feed - 60) # TODO Find if there is a row after the current row, if there is set the sleep time for the # time until the next row minus 60 seconds for a buffer. If no row, set the sleep to 10 seconds so it checks for new feeds. @@ -33,4 +46,4 @@ # TODO Send a Put request to the API to soft delete the current feed in the database indicating its been carried out. -# TODO Show that the feed has been executed on the web GUI by checking if its been soft deleted, if it has make the record green. +# TODO Show that the feed has been executed on the web GUI by checking if its been soft deleted, if it has make the record green. \ No newline at end of file diff --git a/wifi.py b/wifi.py index 91e9968..786e5c7 100644 --- a/wifi.py +++ b/wifi.py @@ -43,7 +43,7 @@ def connect_to_wifi(self): while CONNECTION_TIMEOUT >0: - self.pins.blink() + self.pins.blink(1) status = self.wlan.status() config = self.wlan.ifconfig() @@ -61,10 +61,14 @@ def connect_to_wifi(self): time.sleep(1) if status !=3: + self.pins.error_blink(2) raise RuntimeError('Failed to Make a Connection!') + else: - print(f"{STATUS_CODES[status]} Your IP Address is: {config[0]}") + print(f"{STATUS_CODES[status]} Your IP Address is: {config[0]}") + self.pins.blink(5) + time.sleep(2) def disconnect_wifi(self): self.wlan.disconnect()