Skip to content

Commit

Permalink
Refactor waveshare plugin to handle up to 16 stations.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan-in-CA committed Apr 3, 2024
1 parent 577a22f commit 3c32817
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 120 deletions.
36 changes: 22 additions & 14 deletions waveshare_relay_board/waveshare_relay_board.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
$def with (m_vals)

$var title: $_(u'SIP - Relay Board Settings')
$var title: $_('SIP - Relay Board Settings')
$var page: plugins
<script>
// Initialize behaviors
Expand All @@ -16,15 +16,15 @@
</script>

<div id="plugin">
<div class="title">$_(u'Waveshare Relay Board Settings')
<div class="title">$_('Waveshare Relay Board Settings')
</div>
<p>$_(u'A plugin for using Waveshare RPi Relay Board (B) to control sprinkler valves. See ')
<a href="https://www.waveshare.com/wiki/RPi_Relay_Board_(B)">RPi Relay Board (B)</a>
<p>$_('A plugin for using Waveshare RPi Relay Board (B) to control sprinkler valves. See ')
<a target="_blank" href="https://www.waveshare.com/rpi-relay-board-b.htm">RPi Relay Board (B)</a>
</p>
<p>$_(u'This plugin is a copy of the relay board plugin, modified to use the pi pins utilised in the Waveshare RPi Relay Board (B)')</p>
<!-- <p>$_('This plugin is a copy of the relay board plugin, modified to use the pi pins utilised in the Waveshare RPi Relay Board (B)')</p> -->
<br>
<form id="pluginForm" action="/rbu" method="get">
<div class='option' title='Number of relays in use (Waveshare relay board (B) has a total of 8 relays)'><span class='label'>$_(u'Relay channels'):</span>
<form id="pluginForm" action="/wrbu" method="get">
<div class='option' title='Number of relays in use (Waveshare relay board (B) has a total of 8 relays)'><span class='label'>$_('Relay channels'):</span>
<select name="relays">
<option value="1" ${" selected" if m_vals[u"relays"]==1 else ""}>1</option>
<option value="2" ${" selected" if m_vals[u"relays"]==2 else ""}>2</option>
Expand All @@ -34,21 +34,29 @@
<option value="6" ${" selected" if m_vals[u"relays"]==6 else ""}>6</option>
<option value="7" ${" selected" if m_vals[u"relays"]==7 else ""}>7</option>
<option value="8" ${" selected" if m_vals[u"relays"]==8 else ""}>8</option>
<option value="9" ${" selected" if m_vals[u"relays"]==9 else ""}>9</option>
<option value="10" ${" selected" if m_vals[u"relays"]==10 else ""}>10</option>
<option value="11" ${" selected" if m_vals[u"relays"]==11 else ""}>11</option>
<option value="12" ${" selected" if m_vals[u"relays"]==12 else ""}>12</option>
<option value="13" ${" selected" if m_vals[u"relays"]==13 else ""}>13</option>
<option value="14" ${" selected" if m_vals[u"relays"]==14 else ""}>14</option>
<option value="15" ${" selected" if m_vals[u"relays"]==15 else ""}>15</option>
<option value="16" ${" selected" if m_vals[u"relays"]==16 else ""}>16</option>
</select>
<span class='tooltip'>$_(u'Number of relays in use (Waveshare relay board (B) has a total of 8 relays)')</span>
<span class='tooltip'>$_('Number of relays in use (Waveshare relay board (B) has a total of 8 relays)')</span>
</div>
<div class='option' title='Active low means the relay turns on when the input is low and turns off when the input is high. Active high does the opposite.'><span class='label'>$_(u'Active'):</span>
<!-- <div class='option' title='Active low means the relay turns on when the input is low and turns off when the input is high. Active high does the opposite.'><span class='label'>$_('Active'):</span>
<input type="radio" name="active" value="low" ${" checked" if m_vals[u"active"]==u"low" else u""}>$_('Low')
<input type="radio" name="active" value="high" ${" checked" if m_vals[u"active"]==u"high" else u""}>$_('High')
<span class='tooltip'>$_('Active low means the relay turns on when the input is low and turns off when the input is high. Active high does the opposite.')</span>
</div>
<br>
<p><b>$_(u'IMPORTANT'):</b> $_(u'If') <b>$_(u'Active')</b> $_(u'is changed'), <b>$_(u'Relay channels')</b> $_(u'will be set to') <b>1</b> $_(u'to prevent all the relays being switched on, and possibly damaging the power supply.')
$_(u'After verifying that the first relay is off, return to this page and set') <b>$_(u'Relay channels')</b> $_(u'to the correct number.')
<p><b>$_('IMPORTANT'):</b> $_('If') <b>$_('Active')</b> $_('is changed'), <b>$_('Relay channels')</b> $_('will be set to') <b>1</b> $_('to prevent all the relays being switched on, and possibly damaging the power supply.')
$_('After verifying that the first relay is off, return to this page and set') <b>$_('Relay channels')</b> $_('to the correct number.')
</p>
</form>
</form> -->
</div>
<div class="controls">
<button id="cSubmit" class="submit"><b>$_(u'Submit')</b></button>
<button id="cCancel" class="cancel danger">$_(u'Cancel')</button>
<button id="cSubmit" class="submit"><b>$_('Submit')</b></button>
<button id="cCancel" class="cancel danger">$_('Cancel')</button>
</div>
151 changes: 45 additions & 106 deletions waveshare_relay_board/waveshare_relay_board.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#!/usr/bin/env python

# Python 2/3 compatibility imports
from __future__ import print_function
from six.moves import range

# standard library imports
import json
import time
Expand Down Expand Up @@ -33,96 +29,79 @@
# fmt: off
urls.extend(
[
u"/wrb", u"plugins.waveshare_relay_board.settings",
u"/wrbj", u"plugins.waveshare_relay_board.settings_json",
u"/wrbu", u"plugins.waveshare_relay_board.update",
"/wrb", "plugins.waveshare_relay_board.settings",
"/wrbu", "plugins.waveshare_relay_board.update",
]
)
# fmt: on

# Add this plugin to the home page plugins menu
gv.plugin_menu.append([_(u"Waveshare Relay Board"), u"/wrb"])
gv.plugin_menu.append([_("Waveshare Relay Board"), "/wrb"])

params = {}

# Read in the parameters for this plugin from it's JSON file
def load_params():
global params
try:
with open(u"./data/waveshare_relay_board.json", u"r") as f: # Read the settings from file
with open("./data/waveshare_relay_board.json", "r") as f: # Read the settings from file
params = json.load(f)
except IOError: # If file does not exist create file with defaults.
params = {u"relays": 8, u"active": u"low"}
with open(u"./data/waveshare_relay_board.json", u"w") as f:
params = {"relays": 8, "active": "low"}
with open("./data/waveshare_relay_board.json", "w") as f:
json.dump(params, f, indent=4, sort_keys=True)
return params


load_params()

#### define the GPIO pins that will be used ####
try:
if gv.platform == u"pi": # If this will run on Raspberry Pi:
if gv.platform == "pi": # If this will run on Raspberry Pi:
if not gv.use_pigpio:
GPIO.setmode(
GPIO.BOARD
) # IO channels are identified by header connector pin numbers. Pin numbers are
relay_pins = [
29,
31,
33,
36,
35,
38,
40,
37,
3,
5,
24,
26,
11,
12,
13,
15,
16,
18,
22,
7,
29, # GPIO 5 relay 1
31, # GPIO 6 relay 2
33, # GPIO 13 relay 3
36, # GPIO 16 relay 4
35, # GPIO 19 relay 5
38, # GPIO 20 relay 6
40, # GPIO 21 relay 7
37, # GPIO 26 relay 8
11, # GPIO 17 relay 9
12, # GPIO 18 relay 10
13, # GPIO 27 relay 11
15, # GPIO 22 relay 12
16, # GPIO 23 relay 13
18, # GPIO 24 relayl 14
22, # GPIO 25 relay 15
26, # GPIO 7 relay 16
]
for i in range(len(relay_pins)):
try:
relay_pins[i] = gv.pin_map[relay_pins[i]]
except:
relay_pins[i] = 0
pin_rain_sense = gv.pin_map[8]
pin_relay = gv.pin_map[10]
else:
print(u"relay board plugin only supported on pi.")
print("relay board plugin only supported on pi.")
except:
print(u"Relay board: GPIO pins not set")
print("Relay board: GPIO pins not set")
pass


#### setup GPIO pins as output and either high or low ####
#### setup GPIO pins as output and high ####
def init_pins():
global pi

try:
for i in range(params[u"relays"]):
for i in range(params["relays"]):
if gv.use_pigpio:
pi.set_mode(relay_pins[i], pigpio.OUTPUT)
pi.write(relay_pins[i], 1)
else:
GPIO.setup(relay_pins[i], GPIO.OUT)
if params[u"active"] == u"low":
if gv.use_pigpio:
pi.write(relay_pins[i], 1)
else:
GPIO.output(relay_pins[i], GPIO.HIGH)
else:
if gv.use_pigpio:
pi.write(relay_pins[i], 0)
else:
GPIO.output(relay_pins[i], GPIO.LOW)
GPIO.output(relay_pins[i], GPIO.HIGH)
time.sleep(0.1)
except:
pass
Expand All @@ -131,97 +110,57 @@ def init_pins():
#### change outputs when blinker signal received ####
def on_zone_change(arg): # arg is just a necessary placeholder.
""" Switch relays when core program signals a change in zone state."""

global pi

with gv.output_srvals_lock:
for i in range(params[u"relays"]):
for i in range(params["relays"]):
try:
if gv.output_srvals[i]: # if station is set to on
if (
params[u"active"] == u"low"
): # if the relay type is active low, set the output low
if gv.use_pigpio:
pi.write(relay_pins[i], 0)
else:
GPIO.output(relay_pins[i], GPIO.LOW)
else: # otherwise set it high
if gv.use_pigpio:
pi.write(relay_pins[i], 1)
else:
GPIO.output(relay_pins[i], GPIO.HIGH)
if gv.use_pigpio:
pi.write(relay_pins[i], 0)
else:
GPIO.output(relay_pins[i], GPIO.LOW)
else: # station is set to off
if (
params[u"active"] == u"low"
): # if the relay type is active low, set the output high
if gv.use_pigpio:
pi.write(relay_pins[i], 1)
else:
GPIO.output(relay_pins[i], GPIO.HIGH)
else: # otherwise set it low
if gv.use_pigpio:
pi.write(relay_pins[i], 0)
else:
GPIO.output(relay_pins[i], GPIO.LOW)
# print 'relay switched off', i + 1, "pin", relay_pins[i] # for testing ############
if gv.use_pigpio:
pi.write(relay_pins[i], 1)
else:
GPIO.output(relay_pins[i], GPIO.HIGH)
except Exception as e:
print(u"Problem switching relays", e, relay_pins[i])
print("Problem switching relays", e, relay_pins[i])
pass


init_pins()

zones = signal(u"zone_change")
zones = signal("zone_change")
zones.connect(on_zone_change)

################################################################################
# Web pages: #
################################################################################


class settings(ProtectedPage):
"""Load an html page for entering relay board adjustments"""

def GET(self):
with open(u"./data/waveshare_relay_board.json", u"r") as f: # Read the settings from file
with open("./data/waveshare_relay_board.json", "r") as f: # Read the settings from file
params = json.load(f)
return template_render.waveshare_relay_board(params)


class settings_json(ProtectedPage):
"""Returns plugin settings in JSON format"""

def GET(self):
web.header(u"Access-Control-Allow-Origin", u"*")
web.header(u"Content-Type", u"application/json")
return json.dumps(params)


class update(ProtectedPage):
"""Save user input to waveshare_relay_board.json file"""

def GET(self):
qdict = web.input()
changed = False
if params[u"relays"] != int(
qdict[u"relays"]
if params["relays"] != int(qdict["relays"]
): # if the number of relay channels changed, update the params
params[u"relays"] = int(qdict[u"relays"])
changed = True
if params[u"active"] != str(
qdict[u"active"]
): # if active low/high changed, update the params
params[u"active"] = str(qdict[u"active"])
params[
u"relays"
] = (
1
) # since changing active could turn all the relays on, reduce the relay channels to 1
params["relays"] = int(qdict["relays"])
changed = True
if changed:
init_pins()
with open(
u"./data/waveshare_relay_board.json", u"w"
"./data/waveshare_relay_board.json", "w"
) as f: # write the settings to file
json.dump(params, f, indent=4, sort_keys=True)
raise web.seeother(u"/")
raise web.seeother("/")

0 comments on commit 3c32817

Please sign in to comment.