Skip to content

Instantly share code, notes, and snippets.

@roelandp
Last active March 19, 2018 23:32
Show Gist options
  • Save roelandp/f4a7d30fccd1c4df6a926ff4113acf76 to your computer and use it in GitHub Desktop.
Save roelandp/f4a7d30fccd1c4df6a926ff4113acf76 to your computer and use it in GitHub Desktop.
Translating bw_tool commands to python via dirty shell exec.
#!/usr/bin/env python
import subprocess
# This script runs bw_tool commands from the shell to talk with 10 relays connected over SPI (bitwizard.nl)
# this config is 1 small block with 4 relays (RPI SPI Relay) and extended over SPI with the "Big Relay" 6 relays block.
# I wanted to have a logical array of relays so this script is mainly to ease the talking to the 10 arrays
# the 4 + 6 relays are now in 1 List from 0 to 9 mapped out
# now you can do stuff like toggleRelay(0,1) which turns relay 0 to 1 (on) (regardless of its state). or toggleRelay(5,0) to turn relay 5 to 0 (off).
# additionally the script offers a feature to -R (read) the current state (on or off) of the relay per index(0-9) "isRelayOn(6)" as a Boolean (True = on) (False = off)
# This script does no error checking and assumes a working setup. Should be extended with more error checking to be noob/foolproof most probs :)
# bw_tool is the 'bitwizard_tool' (BW_TOOL) written in cpp but i dont know cpp to perform SPI and I2C writes & reads to connected peripherals
# "bw_tool -S" for scanning connected devices.
# This script needs mod spi loaded.
def executeProcess(proc):
#executing shell processes
try:
return (subprocess.check_output(proc,shell=True,stderr=subprocess.STDOUT).decode('utf-8'))
except subprocess.CalledProcessError as e:
raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))
def getRelayAddress(relayindex):
# ADDRESSES
# 9c: spi_bigrelay 1.1
# a6: spi_RPi spi relay 2.0
# this is assuming the config: RPI has SPI 4 relays board on top (has address a6) and then connected over SPI the spi_bigrelay (address 9c)
address = "a6"
if(relayindex > 3):
address = "9c"
return address
def getRelayNumber(relayindex):
# small relay: 0,1,2,3 => 20,21,22,23
# big relay: 4,5,6,7,8,9 => 20,21,22,23,24,25
relaymap = [20,21,22,23,20,21,22,23,24,25]
return relaymap[relayindex]
def toggleRelay(relayindex, onoff):
onoroff = str(1)
if onoff:
onoroff = str(1)
else:
onoroff = str(0)
return executeProcess("bw_tool -a "+str(getRelayAddress(relayindex))+ " -W "+str(getRelayNumber(relayindex))+":"+str(onoroff)+":b")
def isRelayOn(relayindex):
#read function to get current status (either 00 (off) or 01 (on))
ison = True
result = str(executeProcess("bw_tool -a "+str(getRelayAddress(relayindex))+ " -R "+str(getRelayNumber(relayindex)))).splitlines()[0].strip()
if result == "01":
ison = True
elif result == "00":
ison = False
return ison
def turnAllOff():
# special call to turn all off (via calling address 0)
executeProcess("bw_tool -a 9c -W 10:0:b")
executeProcess("bw_tool -a a6 -W 10:0:b")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment