A small USB Relay example package
Project description
Python API for Numato USB Relays
This Python API can be used to control Numato USB USB Relay expanders
- Control the Relays "On" and "Off" state.
- Write to output ports
- Read from input ports
- Read integer values from ADC input ports
numato-usb-relay allows to control relay modules (ON or OFF states). This module is compatible with all Numato USB Relay series.
Requirement
The module requires the minimum configuration :
- Windows-10(64 bits) and later
- Linux 64-bits
- Python3.6 and later
License
This API is licensed under the GNU Lesser General Public License v3.0. See LICENSE for full license text.
Installation
Install the latest release:
pip install numato_usb_relay
Usage CLI
Linux :
Test whether your devices can be found running the command-line interface like python3 -m numato_usb_relay.relay. Remember to have your user in the dialout group,
since the devices are registered as /dev/ttyACMx (i.e. modem devices). The default Baudrate is 921600.
Expected output:
❯ python3 -m numato_usb_relay.relay
Discovered devices:
dev: /dev/ttyACM0 | id: 00000000 | ver: 00000008 | ports: 32 | eol: '\r'
dev: /dev/ttyACM1 | id: 12345678 | ver: A0M09.01 | ports: 16 | eol: '\r'
Windows :
Test whether your devices can be found running the command-line interface like python -m numato_usb_relay.relay. The default Baudrate is 921600.
Expected output:
❯ python -m numato_usb_relay.relay
Discovered devices:
dev: COM27 | id: 00000000 | ver: 00000008 | ports: 32 | eol: '\r'
dev: COM5 | id: 12345678 | ver: A0M09.01 | ports: 16 | eol: '\r'
How to use it
The command line to drive relays are :
Windows :
python -m numato_usb_relay.relay --cmd=<string to drive relays> --relays=2 --port=<port> --baudrate=<baudrate>
Linux :
python3 -m numato_usb_relay.relay --cmd=<string to drive relays> --relays=2 --port=<port> --baudrate=<baudrate>
Note : For all CLI commands use python3 when using in Linux Systems and python in Windows Systems.
Parameters are :
--cmd : the command to drive individual relays (default is "x")
--relays : the number of relays to drive (default is 1)
--port : the port (default is /dev/ttyUSB1 or "COM1")
--baudrate : the baudrate (default is 921600)
--name : the type of Numato Usb relay module name (default is "usb", change the name as "powered" or "ssr" or "USBR" for Powered or SSR or NL-USBR-C usb relay modules respectively).
A relay can receive two types of orders (OFF or ON). The order in the module is encoding by an ASCII character such as :
"c" : close the relay (OFF state)
"o" : open the relay (ON state)
"x" : do not change the current state of the relay
For example consider a 4 channel USB Powered Relay module :
- 4 available relays : 0, 1, 2 and 3
- baudrate is 921600
- port is COM27
- name is powered
Single Modules:
When only a Numato USb Relay module is connected, The package automatically detects the port and number of relays connected hence there is no requirement of giving these parameters in the command pattern, follow the command pattern as shown below to control the relays through CLI:
command pattern examples for 4 channel usb relay module is given here,
To turn on all relays, string command is oooo and the command line is :
python -m numato_usb_relay.relay --cmd="oooo"
To turn off all relays, string command is cccc and the command line is :
python -m numato_usb_relay.relay --cmd="cccc"
To turn on the relay 0, and it does not matter about other ones, string command is oxxx and the command line is :
python -m numato_usb_relay.relay --cmd="oxxx"
To turn on the relays 1 and 3, and it does not matter about other ones, string command is xoxo and the command line is :
python -m numato_usb_relay.relay --cmd="xoxo"
Multiple Modules:
When many Numato usb modules are connected, provide the port and relays parameter to control the right module, use the command pattern as shown below to control a 4 channel usb relay module:
python -m numato_usb_relay.relay --cmd="oooo" --relays=4 --port="COM27"
List of Commands
- ver()
- id_get()
- id_set("x")
- info()
- relay_on("x")
- relay_off("x")
- relay_read("x")
- relay_writeall("x")
- relay_readall()
- reset()
- gpio_set("x")
- gpio_clear("x")
- gpio_read("x")
- adc_read("x")
- relay_poweron("x")
- gpio_poweron("x","x")(*3)
Note:
- "x" is the parameter to be passed in the commands, please refer the "sending commands" section in the user manual of the connected module to send the right arguement and to understand the functioning of each command.
- Please pass the values/arguements of commands within the double qoutes (eg. relay_read("A")).
- Follow the commnad pattern gpio_poweron("x") for NL-USBR-C-001 module, where dedicated outputs are present, only the Gpio value is passed as the arguement.
Usage API
The API can be used like:
from numato_usb_relay.relay import Relay
# Please change the number of Relays and COM Port accordingly.
# Please enter the name as "powered" or "ssr" or "USBR" if the module consists of Powered or SSR or NL-USBR-C Relays respectively.
Device = Relay(numberRelays=4, port="COM27", name="usb")
# To get the version of the module
Device.ver()
# To get the Device Id
Device.id_get()
# To turn on all the relays
Device.relay_writeall("0F")
# To get the Power-On Status of the module
Device.info()
# To turn on a single relay & read the status
Device.relay_on("0")
Device.relay_read("0")
#Device.relay_off("000") # NL-USBR-C-001 command pattern
# Setting the Relay Power-On Value
Device.relay_poweron("0F")
# Setting the Gpio Power-On Direction & Value
Device.gpio_poweron("00","0F")
#Device.gpio_poweron("05") # only for NL-USBR-C-001
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file numato_usb_relay-0.0.1.tar.gz.
File metadata
- Download URL: numato_usb_relay-0.0.1.tar.gz
- Upload date:
- Size: 9.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b3af674059c52e7d8eb800cf0a4e6c9b8d307aaedb1869756d950c713cd1fc8b
|
|
| MD5 |
7fcb8d16fc9b82cea26529a7345daa51
|
|
| BLAKE2b-256 |
bc90f8c52f5a7dd828f8e59a558ff3e36fff0faabe323ba49fca3f0725203f75
|
File details
Details for the file numato_usb_relay-0.0.1-py3-none-any.whl.
File metadata
- Download URL: numato_usb_relay-0.0.1-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
05532585f5f4eb2eeb5f156b5cb07476f34db40f474a082f2cde84691a8624bc
|
|
| MD5 |
94cb96fba33541d5d7ea591fd017afee
|
|
| BLAKE2b-256 |
b6bfd6fbfbb3b3c4256cb3ce2acafae4af819e3fa6e46d09175846362c8c37c8
|