Script to activate relays via MCP23008 chip (UniPi)

All kinds of 'OS' scripts
Post Reply
DHPi
Posts: 1
Joined: Monday 27 February 2017 21:16
Target OS: Raspberry Pi
Domoticz version:
Contact:

Script to activate relays via MCP23008 chip (UniPi)

Post by DHPi » Monday 27 February 2017 21:43

I made a script to activate relays via MCP23008 chip.
This chip is inter alia integrated into UniPi and functioning properly.

Script filename: i2csetrelay
Use it as follows:
script: // i2csetrelay A BB C D
A = bus
BB = module address
C = Relay
D = 1 (on) or 0 (off)

Example relay 1 on
script: // i2csetrelay 1 20 1 1
Example relay 1 off
script: // i2csetrelay 1 20 1 0

Code: Select all

#!/bin/sh
READ_HEX=$(i2cget -y $1 0x$2 0x09 | cut -c 3-4 | tr '[:lower:]' '[:upper:]')
READ_BIN=$(printf "%08d" $(echo "obase=2; ibase=16; $READ_HEX" | bc))
CHANGE_BIN=$(echo $READ_BIN | sed s/./$4/$3)
CHANGE_HEX=$(echo "obase=16; ibase=2; $CHANGE_BIN" | bc)
i2cset -y 1 0x20 0x00 0x00
i2cset -y $1 0x$2 0x09 0x$CHANGE_HEX

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest