Skip to content

Commit

Permalink
Merge pull request #22 from sjefferson99/20-switch-and-toggle-are-not…
Browse files Browse the repository at this point in the history
…-independent

20 switch and toggle are not independent
  • Loading branch information
sjefferson99 authored Nov 20, 2022
2 parents 5b8a961 + ebd1697 commit 0b559ab
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
8 changes: 7 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ def start_wifi() -> bool:
print("Enabling relay hardware")
relays = relay_module()
hardware = relays.hardware
print("Running hardware demo")
# Set initial relay states
hardware.relay_switch(1, 1)
hardware.relay_switch(2, 1)
hardware.relay_switch(3, 0)
hardware.relay_switch(4, 0)

#print("Running hardware demo")
#hardware.demo()

if enable_webserver:
Expand Down
5 changes: 4 additions & 1 deletion relays/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,19 @@ def put(self, data, relayid):
"""Switch relay"""
value = data["value"]
type = data["type"]
print("Received API call - relayid {}, type: {}, value: {}".format(relayid, type, value))
hardware = relay_board()
if type == "switch":
print("API call to switch")
hardware.relay_switch(int(relayid), int(value))
# Return message AND set HTTP response code to "200"
return {'message': 'Switched'}, 200
elif type == "toggle":
print("API call to toggle")
hardware.relay_toggle(int(relayid), 500, int(value))
# Return message AND set HTTP response code to "200"
return {'message': 'Toggled'}, 200
else:
print("Incorrect data provided")
print("Incorrect data provided to relays API")
# Return message AND set HTTP response code to "200"
return {'message': 'Error'}, 500
14 changes: 11 additions & 3 deletions relays/hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ def __init__(self) -> None:
self.pin_mapping = {1: 18, 2: 19, 3: 20, 4: 21}
self.relays = {}
self.states = {0: "off", 1: "on"}
#Build pin objects and set known state (off)
#Build pin objects
x = 1
p = 18
while x <= 4:
self.relays[x] = Pin(p, Pin.OUT)
self.relays[x].value(0) #set NO to off
x += 1
p += 1

Expand Down Expand Up @@ -51,9 +50,18 @@ def list_relays(self) -> list:
def demo(self) -> None:
"""Cycles quickly through toggling each relay"""
x = 1
while x <= 4:
self.relay_switch(x, 1)
sleep_ms(200)
x += 1
x = 1
while x <= 4:
self.relay_switch(x, 0)
sleep_ms(200)
x += 1
x = 1
while x <= 4:
self.relay_toggle(x, 100, 1)
print("Toggling relay ")
sleep_ms(200)
x += 1

Expand Down

0 comments on commit 0b559ab

Please sign in to comment.