Skip to content

Commit

Permalink
Fixed error with clock returning None. Resolved LED blinking issue, a…
Browse files Browse the repository at this point in the history
…dded 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.
  • Loading branch information
Tomrm96 committed Nov 3, 2024
1 parent 7f50bd1 commit a51e3bc
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 22 deletions.
22 changes: 16 additions & 6 deletions Pico_pins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
18 changes: 15 additions & 3 deletions bridge.py
Original file line number Diff line number Diff line change
@@ -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):

Expand All @@ -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:
Expand All @@ -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):
Expand Down
24 changes: 20 additions & 4 deletions clock.py
Original file line number Diff line number Diff line change
@@ -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}"
time = f"{self.hour}:{self.minute}"
return time
elif param == 'last_feed_time':
time = f"{self.hour}:{self.minute}:{self.seconds}"
return time
7 changes: 6 additions & 1 deletion dispense_food.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ def __init__(self):
self.pico_pins = PicoPins()

def dispense(self, turns):
self.pico_pins.servo_forward(turns)
self.notify(turns)
self.pico_pins.servo_forward(turns)

def notify(self, turns):
for i in range(0, 10):
self.pico_pins.blink(1)
25 changes: 19 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
8 changes: 6 additions & 2 deletions wifi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit a51e3bc

Please sign in to comment.