Relay Expansion Python Module
The Onion PWM Expansion Python module, relayExp
is based on the C Relay Expansion Library. Using this module, you will be able to control the Relay Expansion from within your Python program.
Programming Flow
After each power-cycle, the chip that controls the Relay Expansion must be programmed with an initialization sequence. After the initialization, the relays can be turned on and off at will.
The Python Module
The relayExp
Python Module in the OmegaExpansion
package provides a wrapper around the C library functions. The functions are largely the same as their C counterparts, including the arguments and return values. Any differences from the C library will be explicitly mentioned below.
Source Code
The source code can be found in the Onion i2c-exp-driver
GitHub Repo.
Installing the Module
To install the Python module, run the following commands:
opkg update
opkg install python-light pyRelayExp
This will install the module to /usr/lib/python2.7/OmegaExpansion/
This only needs to be done once.
To add the Onion Relay Expansion Module to your Python program, include the following in your code:
The functions are largely the same as their C counterparts, including the arguments and return values. Any differences from the C library will be explicitly mentioned below.
Example Code
Example code that uses the relayExp
module can be found here, in the i2c-exp-driver
Onion GitHub Repo.
The Channels
The Relay Expansion has two channels, the image below describes how they are enumerated:
Return Values
All functions follow the same pattern with return values:
If the function operation is successful, the return value will be 0
.
If the function operation is not successful, the function will return 1
.
Functions
Function | Prototype |
---|---|
Initialization Function | driverInit(addr) |
Check for Initialization | checkInit(addr) |
Set Relay State | setChannel(addr, channel, state) |
Set State for both Relays | setAllChannels(addr, state) |
Read Relay State | readChannel(addr, channel) |
Initialization Function - driverInit()
To perform the initialization sequence on the Relay Expansion:
After this step is completed, the functions to set the relay states can be used with success.
Arguments
The addr
argument is described above in the I2C Device Address section.
Examples
Initialize a Relay Expansion with all switches set to 0, meaning the I2C device address will be 0x27:
Initialize with switches set to on-on-off (device address: 0x24):
Check for Initialization - checkInit()
Performs several reads to determine if the Relay Expansion requires the initialization sequence to be programmed before the relay states can be changed:
The return value of the function indicates the Initialization Status:
Return Value | Initialization Status |
---|---|
0 | Not Initialized |
1 | Initialized |
Arguments
The addr
argument is described above in the I2C Device Address section.
Example
Check if a Relay Expansion (with switches set to Off-On-On) is initialized:
Set Relay State - setChannel()
Use this function to change the state of the relay:
Arguments
The addr
argument is described above in the I2C Device Address section.
The channel
argument selects the relay in question. See the diagram above for info on which channel corresponds to which relay.
The state
argument allows the user to select if the relay will be turned on or off:
* 0 turn the relay OFF
* 1 turn the relay ON
Set State for both Relays - setAllChannels()
In the event that both relays need to be turned on or off at the same time:
This is performed with a single register write so both relays should react at the same time.
Arguments
The addr
argument is described above in the I2C Device Address section.
The state
argument allows the user to select if the relays will be turned on or off:
* 0 turn the relays OFF
* 1 turn the relays ON
Example
Turn both channels off (all switches are On):
Turn both channels on (all switches are On):
Read Relay State - readChannel()
Use this function to read the state of a specific relay:
Arguments
The addr
argument is described above in the I2C Device Address section.
The channel
argument selects the relay in question. See the diagram above for info on which channel corresponds to which relay.
Return Value
The return value indicates the state of the relay channel in question:
* 0
indicating the relay is OFF
* 1
indicating the relay is ON
Example
Read the state of channel 0 (all switches are Off):