Giter Club home page Giter Club logo

usbrelay's Introduction

USB Relay Driver For Linux

alt text

A cheap USB relay available from Ebay with 1,2,4 or 8 relay output. The double throw relay ratings are 10A 250VAC each.

The USB device is HID compatible and comes with Windows control software. This code can control the relay vi HIDAPI which is a cross platform library. This code was tested under linux both on x86 and Raspberry Pi ARM. The program is command line only as it is likely to be used by shell scripts.

The output of lsusb for the device is:

Bus 001 Device 003: ID 16c0:05df Van Ooijen Technische Informatica HID device except mice, keyboards, and joysticks

# lsusb -v -d 16c0:05df 

Bus 001 Device 003: ID 16c0:05df Van Ooijen Technische Informatica HID device except mice, keyboards, and joysticks
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               1.10
  bDeviceClass            0 (Defined at Interface level)
  bDeviceSubClass         0 
  bDeviceProtocol         0 
  bMaxPacketSize0         8
  idVendor           0x16c0 Van Ooijen Technische Informatica
  idProduct          0x05df HID device except mice, keyboards, and joysticks
  bcdDevice            1.00
  iManufacturer           1 www.dcttech.com
  iProduct                2 USBRelay2
  iSerial                 0 
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength           34
    bNumInterfaces          1
    bConfigurationValue     1
    iConfiguration          0 
    bmAttributes         0x80
      (Bus Powered)
    MaxPower               20mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           1
      bInterfaceClass         3 Human Interface Device
      bInterfaceSubClass      0 No Subclass
      bInterfaceProtocol      0 None
      iInterface              0 
        HID Device Descriptor:
          bLength                 9
          bDescriptorType        33
          bcdHID               1.01
          bCountryCode            0 Not supported
          bNumDescriptors         1
          bDescriptorType        34 Report
          wDescriptorLength      22
         Report Descriptors: 
           ** UNAVAILABLE **
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x81  EP 1 IN
        bmAttributes            3
          Transfer Type            Interrupt
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0008  1x 8 bytes
        bInterval              20
Device Status:     0x0000
  (Bus Powered)

HIDAPI

http://www.signal11.us/oss/hidapi

HIDAPI is a fairly recent addition to linux and is available as a package for Fedora 20 but not for Pidora (F18). The package was built for Pidora (Fedora 18) using the F20 hidapi source package.

Installing Debian Packages:

This code is a maintained package in Debian (and Raspian). Use normal apt-get commands:

$ sudo apt-get install usbrelay

Installing Fedora Packages:

The packages are available in Fedora36+

$ sudo dnf install usbrelay-common python3-usbrelay usbrelay-mqtt

Other Linux platforms will need to build the source, see below

Protocol:

The relay modules does not set the USB serial number but has a unique serial when the HID device is queried, the current state of the relays is also sent with the serial. The HID serial is matched and the ON/OFF command is sent to the chosen relay.

Building The Code:

The usual make, make install dance assuming the hidapi and hidapi-devel packages have been installed. Note that there are two options for the hidapi library: hidapi-hidraw or hidapi-libusb. Different distributions have better results with one or the other. YMMV.

$ sudo apt-get install libhidapi-dev libhidapi-hidraw0 git 
$ git clone https://github.com/darrylb123/usbrelay
$ cd usbrelay
$ make
$ sudo make install


### Test with a usbrelay plugged in
$ sudo usbrelay

### Build the python interface using the instructions below

To choose the alternative hidapi libraries, add the option to the make command line

### hidapi-hidraw - This is the default if no option is given
$ make HIDAPI=hidraw
### hidapi-libusb
$ make HIDAPI=libusb

Docker Build:

You can also build using Docker. Assuming you have Docker installed (only tested with version 18), execute the build script:

$ ./build.sh

The usbrelay binary, libusbrelay.so and libusbrelay_py.so libraries will be built in the root directory of the repo.

Usage:

The code needs to access the device. This can be achieved either by running the program with root privileges (so sudo is your friend) or by copying 50-usbrelay.rules to /etc/udev/rules.d, note Debian derivatives have a group 'plugdev' designed for this function. Edit 50-usbrelay.rules and substitute GROUP="plugdev" for GROUP="usbrelay"

$ sudo cp 50-usbrelay.rules /etc/udev/rules.d
$ sudo udevadm control -R

Add users that need to operate the relays to the usbrelay group

Fedora:

sudo usermod -a -G usbrelay <user name>

Debian/Ubuntu:

sudo usermod -a -G plugdev <user name>
$ sudo usbrelay --help
Usage: usbrelay [OPTION...] [ACTION...]
Control or query USB HID relays.

  -d, --debug                Produce debugging output
  -q, --quiet                Be quiet
  -?, --help                 Give this help list
      --usage                Give a short usage message
  -V, --version              Print program version

Without ACTION, the actual state of all relays is printed to stdout.
ACTION can be one of:
RELID_N=[0|1] to switch the N-th relay off or on
RELID=NEWID to change relay ID

Report bugs to https://github.com/darrylb123/usbrelay/issues.

Running the program without arguments will display each module that matches device 16c0:05df or 0519:2018. The program can be invoked with the debug (-d) or quiet (-q) flags. The debug information is sent to stderr while the state is sent to stdout for use in scripts. The only limit to the number of these relays that can be plugged in and operated at once is the number of USB ports. Using neither the -d or -q flags just prints the state of the relays to stdout.

$ sudo usbrelay
PSUIS_1=1
PSUIS_2=0
$ sudo usbrelay -d (--debug)
Device Found
  type: 16c0 05df
  path: /dev/hidraw1
  serial_number: PSUIS
  Manufacturer: www.dcttech.com
  Product:      USBRelay2
  Release:      100
  Interface:    0
PSUIS_1=1
PSUIS_2=0
$ sudo usbrelay -q (--quiet)
$

To get the relay state

$ sudo usbrelay
PSUIS_1=1
PSUIS_2=0

To use the state in a script:

$ eval $(sudo usbrelay)
$ echo $PSUIS_2
0

To set the relay state of 1 or more modules at once:

$ sudo usbrelay PSUIS_2=0
$ sudo usbrelay PSUIS_2=1 PSUIS_1=0
$ sudo usbrelay PSUIS_2=0 PSUIS_1=1 0U70M_1=0 0U70M_2=1

Operate relay 9 to set the state of all relays together

$ sudo usbrelay PSUIS_9=0
$ sudo usbrelay PSUIS_9=1 0U70M_9=1

The path to a device can be used in lieu of the serial, this can be useful for devices with corrupted serials

$ sudo usbrelay /dev/hidraw1_1=0

Alternatively if using libusb instead of hidraw you may use the usb device path

Device Found
  type: 0519 2018
  path: 0001:0015:00
  serial_number: A0001
Manufacturer: Ucreatefun.com
  Product:      HIDRelay
  Release:      1
  Interface:    0
  Number of Relays = 9
  Module_type = 2

$ sudo usbrelay 0001:0015:00_1=0

Change the serial permanently

Use the fictitious relay 0 to set the serial permanently. If you have duplicate serials, make sure only one is plugged in when you change it. Maximum of 5 character serial (A-Z0-9 only). It is probably sensible to change one module at a time to avoid serial collisions. If the serial contains characters that are not A-Z0-9 and therefore an illegal ID, you may also use the device path to set the serial as above

$ sudo usbrelay
ZXCV_1=0
ZXCV_2=0

$ sudo usbrelay ZXCV_0=ZAQ12 
( or sudo usbrelay /dev/hidraw4_0=ZAQ12 )
ZXCV_1=0
ZXCV_2=0
Setting new serial

$ sudo usbrelay
ZAQ12_1=0
ZAQ12_2=0

Python Extension:

This also optionally includes a python extension. In order to build the python extension, you must have the Python 3 development libraries installed. The docker build process will produce the python library as well.

Debian:

##Install Python3 dev package
# sudo apt install libpython3-dev python3-venv pip
# sudo pip install build

Fedora:

##Install Python3 dev package
# sudo dnf install python3-devel
# sudo dnf install python3-build

With the dependency installed, the library can be built and installed with:

##Build libusbrelay_py.so
$ cd usbrelay_py
$ make
##Install to global python
$ sudo make install

Once installed, the library can be used by any python script, assuming it is running as a user with suitable permissions per the changes to udev above.

The following is a test script included as tests/usbrelay_test.py, showing how to use the library:

import usbrelay_py
import time
# Note that a call to count() is required to enumerate the attached relays
# before attempting to operate the relays

count = usbrelay_py.board_count()
print("Count: ",count)

boards = usbrelay_py.board_details()
print("Boards: ",boards)

for board in boards:
    print("Board: ",board)
    relay = 1
    while(relay < board[1]+1):
        result = usbrelay_py.board_control(board[0],relay,1)
        print("Result: ",result)
        relay += 1
        
    relay = 1
    while(relay < board[1]+1):
        result = usbrelay_py.board_control(board[0],relay,0)
        print("Result: ",result)
        relay += 1

Once the library is installed, you can run the test script in python as follows:

$ python3 test.py

It will turn on and then off every relay attached to every board on your system.

Fine-grained UDEV permissions

When using many relays on a system, which is shared by several users and it is not desired to give all users access to all relays, one can add the following line to udev rules, e.g. /etc/udev/rules.d/50-dct-tech-usb-relay-2.rules.

SUBSYSTEMS=="usb", ATTRS{idVendor}=="16c0", ATTRS{idProduct}=="05df", IMPORT{program}="/usr/bin/usbrelay --quiet --export-id $devnode"

This ensures that subsequent rules can use relay ID stored in the ID_SERIAL environment variable to match different relays. For example giving permissions for different relays to different users can be achieved by the following rules:

SUBSYSTEMS=="usb", ATTRS{idVendor}=="16c0", ATTRS{idProduct}=="05df", ENV{ID_SERIAL}=="PSUIS", MODE="0600", OWNER="user1"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="16c0", ATTRS{idProduct}=="05df", ENV{ID_SERIAL}=="0U70M", MODE="0600", OWNER="user2"

Support for Ucreatefun USB Modules

alt text

A USB relay became available that is supported by the software but with severe limitations

  • Status of the relays is not available
  • There is no serial so there can only be one of these modules attached to a system, unless referred to by device path. The module has a USB serial number of A0001 on every module.
  • The number of relays is not available

The module has a USB device ID of 0519:2018. There are modules with 1,2,4,and 8 relays. The module accepts a request for relay 9 which turns on/off all relays. Operating the module works the same as for the DccTech modules except the serial used is A0001 Running usbrelay without arguments prints all possible relays (8) to stdout.

$ sudo usbrelay A0001_2=1 # Turns on relay 2
$ sudo usbrelay /dev/hidraw4_1=1
$ sudo usbrelay A0001_9=1 # turns on all relays

Referencing devices by physical USB port

Symbolic links can be used to devices to allow physical USB ports to be referenced. The following line in a /etc/udev/rules.d file will create a symbolic link with the name of the USB port:

KERNEL=="hidraw*",KERNELS=="*-*", SYMLINK+="usbrelay%b"

The default 50-usbrelay.rules udev file creates these links for UCREATEFUN relays.

The following example has a ucreatefun usb relay plugged into a USB port and 2 dcttech relays plugged into a USB hub attached to another port:

$ ls -l /dev/hidr*
crw-------. 1 root root 243, 0 Mar  9 15:23 /dev/hidraw0
crw-------. 1 root root 243, 1 Mar  9 15:23 /dev/hidraw1
crw-------. 1 root root 243, 2 Mar  9 15:23 /dev/hidraw2
crw-------. 1 root root 243, 3 Mar  9 15:23 /dev/hidraw3
crw-------. 1 root root 243, 4 Mar  9 17:47 /dev/hidraw4
crw-------. 1 root root 243, 5 Mar  9 17:36 /dev/hidraw5
crw-------. 1 root root 243, 6 Mar  9 17:47 /dev/hidraw6
lrwxrwxrwx. 1 root root      7 Mar  9 17:47 /dev/usbrelay3-10.1:1.0 -> hidraw4 
lrwxrwxrwx. 1 root root      7 Mar  9 17:47 /dev/usbrelay3-10.3:1.0 -> hidraw6
lrwxrwxrwx. 1 root root      7 Mar  9 15:23 /dev/usbrelay3-5:1.0 -> hidraw0
lrwxrwxrwx. 1 root root      7 Mar  9 15:23 /dev/usbrelay3-5:1.1 -> hidraw1
lrwxrwxrwx. 1 root root      7 Mar  9 15:23 /dev/usbrelay3-5:1.2 -> hidraw2
lrwxrwxrwx. 1 root root      7 Mar  9 15:23 /dev/usbrelay3-6:1.0 -> hidraw3
lrwxrwxrwx. 1 root root      7 Mar  9 17:36 /dev/usbrelay3-9:1.0 -> hidraw5

$ sudo usbrelay
OMG12_1=0
OMG12_2=0
QWERT_1=0
QWERT_2=0
A0001_1=-1
A0001_2=-1
A0001_3=-1
A0001_4=-1
A0001_5=-1
A0001_6=-1
A0001_7=-1
A0001_8=-1
A0001_9=-1

  
  
  $ sudo usbrelay -d /dev/usbrelay3-10.1:1.0_1=1 /dev/usbrelay3-10.3:1.0_2=0 /dev/usbrelay3-9:1.0_2=0
Orig: /dev/usbrelay3-10.1:1.0_1=1, Serial: /dev/usbrelay3-10.1:1.0, Relay: 1 State: ff
Orig: /dev/husbrelay3-10.3:1.0_2=0, Serial: /dev/usbrelay3-10.3:1.0, Relay: 2 State: fd
Orig: /dev/usbrelay3-9:1.0_2=0, Serial: /dev/usbrelay3-9:1.0, Relay: 2 State: fd
Found 3 devices
Device Found
  type: 16c0 05df
  path: /dev/hidraw4
  serial_number: ASDFG
  Manufacturer: www.dcttech.com
  Product:      USBRelay2
  Release:      100
  Interface:    0
  Number of Relays = 2
  Module_type = 1
Device Found
  type: 16c0 05df
  path: /dev/hidraw6
  serial_number: 48VZ7
  Manufacturer: www.dcttech.com
  Product:      USBRelay2
  Release:      100
  Interface:    0
  Number of Relays = 2
  Module_type = 1
Device Found
  type: 0519 2018
  path: /dev/hidraw5
  serial_number: A0001
  Manufacturer: Ucreatefun.com
  Product:      HIDRelay
  Release:      1
  Interface:    0
  Number of Relays = 9
  Module_type = 2
Serial: /dev/usbrelay3-10.1:1.0, Relay: 1 State: ff 
1 HID Serial: ASDFG Serial: /dev/usbrelay3-10.1:1.0, Relay: 1 State: ff

Serial: /dev/usbrelay3-10.3:1.0, Relay: 2 State: fd 
2 HID Serial: 48VZ7 Serial: /dev/usbrelay3-10.3:1.0, Relay: 2 State: fd

Serial: /dev/usbrelay3-9:1.0, Relay: 2 State: fd 
3 HID Serial: A0001 Serial: /dev/usbrelay3-9:1.0, Relay: 2 State: fd
target fd ucreate 2 f0 f0


Serial: /dev/usbrelay3-10.1:1.0, Relay: 1 State: ff --- Found
Serial: /dev/usbrelay3-10.3:1.0, Relay: 2 State: fd --- Found
Serial: /dev/husbrelay3-9:1.0, Relay: 2 State: fd --- Found


Any

MQTT support

MQTT support requires the successful installation of the python library described above. Check this first (with a module plugged in) by running:

sudo python3 test.py

MQTT support provides capability of using Home Assistant or nodered with usbrelay. The capability is made up of:

  • usbrelayd
  • usbrelay.service
  • usbrelayd.conf

usbrelayd

A python daemon using libusbrelay to connect to an MQTT server. When the daemon starts, it publishes the state of all usbrelay devices found and subscribes to command topics for each relay. To install:

Debian

sudo apt-get install python3-paho-mqtt
sudo cp usbrelayd /usr/sbin
sudo cp usbrelayd.conf /etc/usbrelayd.conf

Fedora

sudo useradd usbrelay
sudo dnf install python3-paho-mqtt
sudo cp usbrelayd /usr/sbin
sudo cp usbrelayd.conf /etc/usbrelayd.conf

Modify /etc/usbrelayd.conf to suit your circumstances.

usbrelay.service

A systemd unit for controlling and monitoring the usbrelayd daemon. The file comes configured for Fedora and needs to be modified for Debian.

Debian

Edit usbrelayd.service and change

SupplementaryGroups=usbrelay

to

SupplementaryGroups=plugdev

To install:

sudo cp usbrelayd.service /etc/systemd/system
sudo systemctl daemon-reload

50-usbrelay.rules

A udev rule file that reacts and starts/stops the usbrelayd.service when a module is pluggedin or removed. The file should be installed with the initial installation. The file comes configured for Fedora and needs to be modified for Debian.

Debian

Edit 50-usbrelay.rules and change

GROUP="usbrelay"

to

GROUP="plugdev"

To install:

sudo cp 50-usbrelay.rules /etc/udev/rules.d
sudo udevadm control -R

Operation

After installation and configuration confirm the correct operation.

systemctl status usbrelayd
usbrelayd.service - USB Relay MQTT service
     Loaded: loaded (/etc/systemd/system/usbrelayd.service; disabled; vendor preset: disabled)
     Active: active (running) since Thu 2021-06-24 15:23:01 AEST; 2s ago
   Main PID: 1151364 (python3)
      Tasks: 1 (limit: 14159)
     Memory: 14.4M
        CPU: 117ms
     CGroup: /system.slice/usbrelayd.service
             └─1151364 /usr/bin/python3 /usr/local/sbin/usbrelayd mymqttbroker

Jun 24 15:23:01 xxx.local systemd[1]: Started USB Relay MQTT service.
Jun 24 15:23:02 xxx.local python3[1151364]: Modules Connected:  1
Jun 24 15:23:02 xxx.local python3[1151364]: State:  stat/OMG12/1 OFF
Jun 24 15:23:02 xxx.local python3[1151364]: Subscribed:  cmnd/OMG12/1
Jun 24 15:23:02 xxx.local python3[1151364]: State:  stat/OMG12/2 OFF
Jun 24 15:23:02 xxx.local python3[1151364]: Subscribed:  cmnd/OMG12/2

MQTT Topics for controlling usbrelays

  • Current state: stat/SERIAL/Relay (eg stat/OMG12/1 )
  • Command: cmnd/SERIAL/Relay ON/OFF (eg cmnd/OMG12/2 )

Using mosquitto client tools

mosquitto_sub -h your_mqtt_broker -t stat/OMG12/1
mosquitto_pub -h your_mqtt_broker -t cmnd/OMG12/1 -m ON
 

Enjoy

usbrelay's People

Contributors

amv007 avatar atrump avatar darrylb123 avatar imxron avatar jandd avatar kwasd avatar mefuller avatar niecore avatar seanmollet avatar wentasah avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

usbrelay's Issues

usbrelay device detected, but not the serial

Hi,
I've got the same usbrelay as your.
It works fine in my Archlinux 64bits, but in my other ArchLinux 32bit doesn't work, it is detected Ok, but is not detecting the "serial".

Following, I paste the output of both systems:

64Bits:
$ lsusb -v -d 16c0:05df

Bus 002 Device 006: ID 16c0:05df Van Ooijen Technische Informatica HID device except mice, keyboards, and joysticks
Couldn't open device, some information will be missing
Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 1.10
bDeviceClass 0 (Defined at Interface level)
bDeviceSubClass 0
bDeviceProtocol 0
bMaxPacketSize0 8
idVendor 0x16c0 Van Ooijen Technische Informatica
idProduct 0x05df HID device except mice, keyboards, and joysticks
bcdDevice 1.00
iManufacturer 1
iProduct 2
iSerial 0
bNumConfigurations 1
Configuration Descriptor:
bLength 9
bDescriptorType 2
wTotalLength 34
bNumInterfaces 1
bConfigurationValue 1
iConfiguration 0
bmAttributes 0x80
(Bus Powered)
MaxPower 20mA
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 0
bAlternateSetting 0
bNumEndpoints 1
bInterfaceClass 3 Human Interface Device
bInterfaceSubClass 0 No Subclass
bInterfaceProtocol 0 None
iInterface 0
HID Device Descriptor:
bLength 9
bDescriptorType 33
bcdHID 1.01
bCountryCode 0 Not supported
bNumDescriptors 1
bDescriptorType 34 Report
wDescriptorLength 22
Report Descriptors:
** UNAVAILABLE **
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x81 EP 1 IN
bmAttributes 3
Transfer Type Interrupt
Synch Type None
Usage Type Data
wMaxPacketSize 0x0008 1x 8 bytes
bInterval 20

$ sudo ./usbrelay
Device Found
type: 16c0 05df
path: /dev/hidraw3
serial_number:
Manufacturer: www.dcttech.com
Product: USBRelay2
Release: 100
Interface: 0
WDXZG_1=0
WDXZG_2=0

32Bits:

sudo lsusb -v -d 16c0:05df

Bus 001 Device 002: ID 16c0:05df Van Ooijen Technische Informatica HID device except mice, keyboards, and joysticks
Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 1.10
bDeviceClass 0 (Defined at Interface level)
bDeviceSubClass 0
bDeviceProtocol 0
bMaxPacketSize0 8
idVendor 0x16c0 Van Ooijen Technische Informatica
idProduct 0x05df HID device except mice, keyboards, and joysticks
bcdDevice 1.00
iManufacturer 1 www.dcttech.com
iProduct 2 USBRelay2
iSerial 0
bNumConfigurations 1
Configuration Descriptor:
bLength 9
bDescriptorType 2
wTotalLength 34
bNumInterfaces 1
bConfigurationValue 1
iConfiguration 0
bmAttributes 0x80
(Bus Powered)
MaxPower 20mA
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 0
bAlternateSetting 0
bNumEndpoints 1
bInterfaceClass 3 Human Interface Device
bInterfaceSubClass 0 No Subclass
bInterfaceProtocol 0 None
iInterface 0
HID Device Descriptor:
bLength 9
bDescriptorType 33
bcdHID 1.01
bCountryCode 0 Not supported
bNumDescriptors 1
bDescriptorType 34 Report
wDescriptorLength 22
Report Descriptors:
** UNAVAILABLE **
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x81 EP 1 IN
bmAttributes 3
Transfer Type Interrupt
Synch Type None
Usage Type Data
wMaxPacketSize 0x0008 1x 8 bytes
bInterval 20
Device Status: 0x0000
(Bus Powered)

$ sudo usbrelay
[sudo] password for barmaley:
Device Found
type: 16c0 05df
path: /dev/hidraw0
serial_number:
Manufacturer: www.dcttech.com
Product: USBRelay2
Release: 100
Interface: 0
_1=0
_2=0

$ sudo ./usbrelay _1=1
Orig: _1, Serial: 1, Relay: 1 State: 0
Device Found
type: 16c0 05df
path: /dev/hidraw0
serial_number:
Manufacturer: www.dcttech.com
Product: USBRelay2
Release: 100
Interface: 0
_1=0
_2=0
Serial: 1, Relay: 1 State: 0

Serial: 1, Relay: 1 State: 0 --- Not Found

I have built hidapi 0.8.0rc1-2 from sources in both systems...

Do you know what may happen???

Thank you very much

change initial state?

Hi

I am not very familiar with the internals of the relay hardware but I am just curious about if
it would be possible to modify the relay somehow so that all relays switch to ON on power-up
(as default state) even without USB connected, just 12V powering. Is that somehow possible?

Module usbrelay_py has no attribute 'board_control'

Hi,

usbrelay seems to be installed in ubuntu (mint) fine. It imports ok into python but none of the attributes seem to be there. I ran make and install for python.

In the meantime going to use a workaround:
import os
os.system("usbrelay...

Wildcard Disabling Ability to Use Relay

Originally in my setup I had one 8-channel usbrelay device and there were no issues. However, I recently added a second 8-channel usbrelay to my setup, and now there are wildcards in my device ID's. Here is the result of running $ usbrelay:

$ usbrelay
Device Found
  type: 16c0 05df
  path: /dev/hidraw1
  serial_number: 
  Manufacturer: www.dcttech.com
  Product:      USBRelay8
  Release:      100
  Interface:    0
  Number of Relays = 8
?#}??_1=0
?#}??_2=0
?#}??_3=0
?#}??_4=0
?#}??_5=0
?#}??_6=0
?#}??_7=0
?#}??_8=0

Device Found
  type: 16c0 05df
  path: /dev/hidraw0
  serial_number: 
  Manufacturer: www.dcttech.com
  Product:      USBRelay8
  Release:      100
  Interface:    0
  Number of Relays = 8
?QMBS_1=0
?QMBS_2=0
?QMBS_3=0
?QMBS_4=0
?QMBS_5=0
?QMBS_6=0
?QMBS_7=0
?QMBS_8=0

The wildcards seem to be prohibiting my ability to turn on and off the relays. When I run:

$ usbrelay ?QMBS_1=1

the output is:

$ usbrelay ?QMBS_1=1
Orig: ?QMBS, Serial: ?QMBS, Relay: 1 State: ff
Device Found
  type: 16c0 05df
  path: /dev/hidraw1
  serial_number: 
  Manufacturer: www.dcttech.com
  Product:      USBRelay8
  Release:      100
  Interface:    0
  Number of Relays = 8
Serial: ?QMBS, Relay: 1 State: ff 

Device Found
  type: 16c0 05df
  path: /dev/hidraw0
  serial_number: 
  Manufacturer: www.dcttech.com
  Product:      USBRelay8
  Release:      100
  Interface:    0
  Number of Relays = 8
Serial: ?QMBS, Relay: 1 State: ff 

Serial: ?QMBS, Relay: 1 State: ff --- Not Found

I have tried following your doco to change the serial, but it am not having luck because typing the wildcard is not the same as the correct serial number.

Any suggestions on how to move forward?

error while running make python

Hi,

I'm currently installing on the Raspberry PI 4

all previous steps are done

with the command

I get the info about the relay
$sudo usbrelay

Device Found
  type: 16c0 05df
  path: /dev/hidraw4
  serial_number: 
  Manufacturer: www.dcttech.com
  Product:      USBRelay2
  Release:      100
  Interface:    0
  Number of Relays = 2
HURTM_1=0
HURTM_2=0

now i would like to install the Python extension and get no further with make pyhton:

step
##Install Python3 dev package

Packages have been installed;

pi @ raspberrypi: ~ / home/usr / usbrelay $ sudo apt install libpython3.5-dev
Package lists are being read ... Done
The dependency tree is built.
Status information is read in .... Done
libpython3.5-dev is the newest version (3.5.4-4).
0 updated, 0 reinstalled, 0 removed, and 0 not updated.

Next step ##Build libusbrelay_py.so
$ make python

pi @ raspberrypi: ~ $ make python
make: *** No rule to create "python". Enough.

python3.7 is installed on the pi.

what am I doing wrong?

How to use in script if two one channel usb relay with same ID

Your program works great on one channel relay
But I am stuck with the problem that I have two 1 channel relays attached to two usb ports of my laptop.
Following is the output of the command ./usbrelay

Device Found
type: 16c0 05df
path: /dev/hidraw2
serial_number:
Manufacturer: www.dcttech.com
Product: USBRelay1
Release: 100
Interface: 0
Number of Relays = 1
959BI_1=0

Device Found
type: 16c0 05df
path: /dev/hidraw0
serial_number:
Manufacturer: www.dcttech.com
Product: USBRelay1
Release: 100
Interface: 0
Number of Relays = 1
959BI_1=0

When I provide the command "./usbrelay 959BI_1=1" it works on both the relays simultaneously, but I want that only one must work at a time. Why both channels device showing same ID?
How can I differentiate between those two relay channel in the command
./usbrelay
??

Memory error

When I run usbrelay command , it gives following memory error,
$ sudo usbrelay
free(): invalid next size (fast)
Aborted

How to get it working in javascript under Windows environment using serialport

I apologize for this is not an issue but the only way I found I could reach you. I have the 2 channel usb relay module and have established a connection with it in javascript but don't know the commands to make it work. I have bought a similar usb relay module (a one channel module) that can be found here: https://goo.gl/a04gvL and have gotten it to work in a similar fashion: Establishing a serial port connection and writing to the port given the instructions. However I have found no such instructions for this device. Would you help me out?

usbrelay stops giving any output

I have USBrelay hosted on a Pi. It works well and has 2 relays. I have renamned my two relays as WATER_1 and WATER_2

If I type simply "sudo usbrelay" then the output is:
WATER_1=0
WATER_2=0

However, occasionally it stops providing output at all - and lsusb shows that the USB device itself is no longer a visible USB device - it is as if it is no longer plugged in.

The only fix I have so far found is to reboot.

Do you know any way to reset that particular USB device without rebooting or disturbing the USB hub itself?

Thanks...

Activate All Relays

What is the entry to enable all relays at once?
I have tried relay_9=1 and relay_0=1. (0 changes serial to 1).
I changed serial from 6QMBS to relay.
I can successfully activate each relay, but need to activate all at once.

no output on orange pi zero

Hi,
i installed usbrelay programm(SW) with apt-get. Using it make no error but also no output.
The usb relay is connected, works well on Raspberry with this SW.

I tried to compile from source... needed to install hidapi and hidapi_dev, make and, GCC.
Now, "make Makefile install" runs without error.
But the SW give still no output.
Any Idea?

libusb is not usable for 2 Ucreatefun relay boards

hi Darryl

as requested, here is the case with the libusb...
here is the output of the command usbrelay-d with libusb, on a Rpi, with 2 x 8 relay boards attached :

./usbrelay -d
enumerate_relay_boards()Found 1 devices
Device Found
  type: 0519 2018
  path: 0001:0006:00
  serial_number: A0001
Manufacturer: Ucreatefun.com
  Product:      HIDRelay
  Release:      1
  Interface:    0
  Number of Relays = 9
  Module_type = 2
A0001_1=-1
A0001_2=-1
A0001_3=-1
A0001_4=-1
A0001_5=-1
A0001_6=-1
A0001_7=-1
A0001_8=-1
A0001_9=-1
double free or corruption (out)
Aborted

Device renamed to empty name

Hi,
apparently my device has gotten an empty name, and now I cant use it nor rename it anymore.
I am on Raspian OS and just installed the package via apt-get.
Before I had renamed the device to "rel01", then I had called "usbrelay rel01", which apparently somehow set an empty device name.

pi@raspidb:~ $ usbrelay
Device Found
type: 16c0 05df
path: /dev/hidraw0
serial_number:
Manufacturer: www.dcttech.com
Product: USBRelay8
Release: 100
Interface: 0
Number of Relays = 8
rel01_1=0
rel01_2=0
rel01_3=0
rel01_4=0
rel01_5=0
rel01_6=0
rel01_7=0
rel01_8=0

pi@raspidb:~ $ usbrelay rel01
Orig: rel01, Serial: rel01, Relay: 0 State: 0
Device Found
type: 16c0 05df
path: /dev/hidraw0
serial_number:
Manufacturer: www.dcttech.com
Product: USBRelay8
Release: 100
Interface: 0
Number of Relays = 8
Serial: rel01, Relay: 0 State: 0
1 HID Serial: rel01
Serial: rel01, Relay: 0 State: 0 --- Not Found
pi@raspidb:~ $ usbrelay
Device Found
type: 16c0 05df
path: /dev/hidraw0
serial_number:
Manufacturer: www.dcttech.com
Product: USBRelay8
Release: 100
Interface: 0
Number of Relays = 8
_1=0
_2=0
_3=0
_4=0
_5=0
_6=0
_7=0
_8=0

*** buffer overflow detected ***: ./usbrelay terminated Aborted (core dumped)

I suspect that the FORTIFY SOURCE gcc compiler flag is raising a warning after adding a check code into certain calls that could cause buffer overflows.

As described here FORTIFY SOURCE works on only a few functions, including: memcpy, mempcpy, memmove, memset, strcpy, stpcpy, strncpy, strcat, strncat, sprintf, vsprintf, snprintf, vsnprintf, gets.

free(): invalid next size (fast)

Hi,

Following the relevant snippet of my Python code:

try:
proc = subprocess.run(set_cmd, capture_output=True, universal_newlines=True, check=True, shell=False, timeout=1, encoding='UTF-8')
except subprocess.CalledProcessError as e:
print(f"ERROR ({fname}): stderr={e.stderr}, stdout={e.stdout}")

(sorry - I couldn't find a way to keep the indentation alive)

set_cmd is basically an array like the following:
['/usr/bin/usbrelay', 'GPIOS_1=1', 'GPIOS_2=0', 'GPIOS_3=1', 'GPIOS_4=0']

Quite often I see a failure while looping over all permutations of any of my relays:

CMD: /usr/bin/usbrelay WAKES_1=1 WAKES_2=0 WAKES_3=1 WAKES_4=1
CMD: /usr/bin/usbrelay WAKES_1=1 WAKES_2=1 WAKES_3=0 WAKES_4=0
CMD: /usr/bin/usbrelay WAKES_1=1 WAKES_2=1 WAKES_3=0 WAKES_4=1
ERROR (read_dct_relays): stderr=free(): invalid next size (fast)
, stdout=
CMD: /usr/bin/usbrelay WAKES_1=1 WAKES_2=1 WAKES_3=1 WAKES_4=0
CMD: /usr/bin/usbrelay WAKES_1=1 WAKES_2=1 WAKES_3=1 WAKES_4=1

Where does this failure "free(): invalid next size (fast)" come from? System is running Ubuntu LTS 16.04 on an x86 PC.

Debug output:

$ /usr/bin/usbrelay --debug
Version: 0.7-50-g8f6b7fd1ea
Library Version: 0.7-50-g8f6b7fd1ea
enumerate_relay_boards()Found 3 devices
Device Found
type: 16c0 05df
path: /dev/hidraw4
serial_number: GPIOS
Manufacturer: www.dcttech.com
Product: USBRelay4
Release: 100
Interface: 0
Number of Relays = 4
Module_type = 1
GPIOS_1=0
GPIOS_2=0
GPIOS_3=0
GPIOS_4=0
Device Found
type: 16c0 05df
path: /dev/hidraw5
serial_number: WAKES
Manufacturer: www.dcttech.com
Product: USBRelay4
Release: 100
Interface: 0
Number of Relays = 4
Module_type = 1
WAKES_1=0
WAKES_2=0
WAKES_3=0
WAKES_4=0
Device Found
type: 16c0 05df
path: /dev/hidraw3
serial_number: ERTSW
Manufacturer: www.dcttech.com
Product: USBRelay4
Release: 100
Interface: 0
Number of Relays = 4
Module_type = 1
ERTSW_1=0
ERTSW_2=0
ERTSW_3=0
ERTSW_4=0

Ucreatefun.com - HIDRelay

Bought a new relay with "ID 0519:2018 Star Micronics Co., Ltd"
but does not work, it does not detect the serial of the relay.
It uses a CH551G chip, red pcb with "QYF-UR0" marking.
Aliexpress Listing

Would it be possible to make this relay work with this code?
when running usbrelay:
sudo USBID=0519:2018 usbrelay
Device Found
type: 0519 2018
path: /dev/hidraw0
serial_number: A0001
Manufacturer: Ucreatefun.com
Product: HIDRelay
Release: 1
Interface: 0
Number of Relays = 0

After searching online, I can control it with a raspberry pi and hidapitester
sudo ./hidapitester --list-detail
0519/2018: Ucreatefun.com - HIDRelay
vendorId: 0x0519
productId: 0x2018
usagePage: 0x0000
usage: 0x0000
serial_number: A0001
interface: 0
path: /dev/hidraw0

Hex Values
0xF1 to F8 turns on relays 1-8
0x01 to 08 turns off relays 1-8
0xF9 turns on all relays
0x09 turns off all relays

can be controlled by sending the hex value
sudo ./hidapitester --vidpid 0519:2018 --open --send-output 0xF1 #on
sudo ./hidapitester --vidpid 0519:2018 --open --send-output 0x01 #off

I only have a 1 channel version.
I hope this relay can be integrated to the project, if not, I hope it helps others with this relay.
Thanks for the library, the old relay works great with it.

renaming issue

Hi, I tried this amazing software because they sent me boards with similar serial numbers. I accidentally renamed one board to no name. Output is now:
_1=0
_2=0

usbrelay _0=NEWSN is not working. Is there any way to fix it?
Thanks!

Question: what about compiling/using this under Windows?

I apologize - not quite sure how else to contact you other than submitting this question as an Issue.

Can this library be used to operate these relay switches under Windows? I am specifically running Windows 7 embedded, for a target machine, and Windows 7 and 8.1 on my dev boxes.

And too -- thank you for providing this for others to share and utilize. I appreciate your work.

James Hurst

exit code always 0

the exit code is always 0, even if i try to change the state of a non existing relay. this makes scripting much harder.

AUR package of USBrelay fails to build on Manjaro 5.4

It has multiple failures. First it was the MDS for master.zip - I edited the build script.

That sorted the mds but then it failed further on - log:

GNU nano 4.9.2 g
Cloning usbrelay build files...
Checking usbrelay dependencies...
Resolving dependencies...
Checking inter-conflicts...

Building usbrelay...
==> Making package: usbrelay 20190629-1 (Fri 15 May 2020 17:05:01 BST)
==> Checking runtime dependencies...
==> Checking buildtime dependencies...
==> Retrieving sources...
-> Found master.zip
==> Validating source files with md5sums...
master.zip ... Passed
==> Removing existing $srcdir/ directory...
==> Extracting sources...
-> Extracting master.zip with bsdtar
==> Starting prepare()...
==> Starting build()...
make: *** No rule to make target '.git/HEAD', needed by 'gitversion.c'. Stop.
==> ERROR: A failure occurred in build().

Here is the build script:

Maintainer: Your Name [email protected]

pkgname=usbrelay
pkgver=20190629
pkgrel=1
pkgdesc="Control usb relay - based on hidapi"
url="https://github.com/darrylb123/usbrelay"
license=('GPL-2.0')
groups=('system')
depends=('hidapi')
arch=('i686' 'x86_64')
makedepends=()
checkdepends=()
optdepends=()
provides=()
conflicts=()
replaces=()
backup=()
options=()
install=
changelog=
source=("https://github.com/darrylb123/usbrelay/archive/master.zip")
noextract=()
md5sums=('B42B221CC51B5FEA0D22661E379A92FE')
validpgpkeys=()

prepare() {
cd "$pkgname-master"
}

build() {
cd "$pkgname-master"
make
}

package() {
cd "$pkgname-master"
make DESTDIR="$pkgdir/" install
}

No devices found

Hi guys,

Library working well for about a week and then get issue with no devices found. Reinstall, change usb port. Worked briefly and now get:
ioctl (GFEATURE): Broken pipe
hid_get_feature_report
: Broken pipe

Using Linux mint 20.2

Compile in linux undefined reference error

Thank you for creating this repository and sharing your valuable experience.

When I tried to compile repo using make, I got these errors:

cc -O2 -Wall  -lhidapi-hidraw  usbrelay.c   -o usbrelay
/tmp/ccU9yOsi.o: In function `operate_relay':
usbrelay.c:(.text+0x4d): undefined reference to `hid_write'
usbrelay.c:(.text+0x8f): undefined reference to `hid_error'
/tmp/ccU9yOsi.o: In function `main':
usbrelay.c:(.text.startup+0xc5): undefined reference to `hid_enumerate'
usbrelay.c:(.text.startup+0x1cb): undefined reference to `hid_open_path'
usbrelay.c:(.text.startup+0x1f0): undefined reference to `hid_get_feature_report'
usbrelay.c:(.text.startup+0x34f): undefined reference to `hid_close'
usbrelay.c:(.text.startup+0x53f): undefined reference to `hid_free_enumeration'
usbrelay.c:(.text.startup+0x544): undefined reference to `hid_exit'
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'usbrelay' failed
make: *** [usbrelay] Error 1

I already installed hidapi-hidraw library using apk manager.

I am a bit unexperienced on linux compilation. How can I continue.

Thanks.

Glitched serials?

Hi

I have an 8 relay board.

When I use usbrelay, it reports 8 relay states but with glitched serials:
ÿÿÿÿÿ_1=0
ÿÿÿÿÿ_2=0
ÿÿÿÿÿ_3=0
ÿÿÿÿÿ_4=0
ÿÿÿÿÿ_5=0
ÿÿÿÿÿ_6=0
ÿÿÿÿÿ_7=0
ÿÿÿÿÿ_8=0

I am running it on a raspberry pi

How do I access the relays in this situation?

Are FTDI based relays supported?

I just got an 8 channel FTDI based usb relay and I was wondering if usbrelay can work with it? It isn't listed when I run 'usbrelay' on the command line. It creates a /dev/ttyUSB0 and the relevant info is:

`# lsusb -v -d 0403:6001

Bus 004 Device 020: ID 0403:6001 Future Technology Devices International, Ltd FT232 Serial (UART) IC
Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 2.00
bDeviceClass 0
bDeviceSubClass 0
bDeviceProtocol 0
bMaxPacketSize0 8
idVendor 0x0403 Future Technology Devices International, Ltd
idProduct 0x6001 FT232 Serial (UART) IC
bcdDevice 6.00
iManufacturer 1 FTDI
iProduct 2 FT245R USB FIFO
iSerial 3 A600OJ7L
bNumConfigurations 1
Configuration Descriptor:
bLength 9
bDescriptorType 2
wTotalLength 0x0020
bNumInterfaces 1
bConfigurationValue 1
iConfiguration 0
bmAttributes 0xa0
(Bus Powered)
Remote Wakeup
MaxPower 90mA
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 0
bAlternateSetting 0
bNumEndpoints 2
bInterfaceClass 255 Vendor Specific Class
bInterfaceSubClass 255 Vendor Specific Subclass
bInterfaceProtocol 255 Vendor Specific Protocol
iInterface 2 FT245R USB FIFO
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x81 EP 1 IN
bmAttributes 2
Transfer Type Bulk
Synch Type None
Usage Type Data
wMaxPacketSize 0x0040 1x 64 bytes
bInterval 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x02 EP 2 OUT
bmAttributes 2
Transfer Type Bulk
Synch Type None
Usage Type Data
wMaxPacketSize 0x0040 1x 64 bytes
bInterval 0
can't get device qualifier: Resource temporarily unavailable
can't get debug descriptor: Resource temporarily unavailable
Device Status: 0x0000
(Bus Powered)
`

Segmentation fault

Hi,

I am using the usbrelay pretty well. However, I have a scheduled job and after a while I have noticed that the program stop working. In the dmesg, I have a bunch of messages:

[ 640.894574] usbrelay[4976]: segfault at 20 ip 00007fc5935672e0 sp 00007ffed82f1cd0 error 4 in libc-2.25.so[7fc59352f000+19d000]
[ 671.907217] usbrelay[4997]: segfault at 20 ip 00007f7e3fd8c2e0 sp 00007ffc554b7d70 error 4 in libc-2.25.so[7f7e3fd54000+19d000]
[ 702.895423] usbrelay[5009]: segfault at 20 ip 00007f9b1ebb42e0 sp 00007ffdc6fde390 error 4 in libc-2.25.so[7f9b1eb7c000+19d000]
[ 731.060219] usbrelay[5026]: segfault at 20 ip 00007fb84c0242e0 sp 00007fff2d290ef0 error 4 in libc-2.25.so[7fb84bfec000+19d000]
[ 731.370189] usbrelay[5031]: segfault at 20 ip 00007fe1ee2222e0 sp 00007fffe8029990 error 4 in libc-2.25.so[7fe1ee1ea000+19d000]
[ 733.887483] usbrelay[5047]: segfault at 20 ip 00007fa78ca842e0 sp 00007ffd306279b0 error 4 in libc-2.25.so[7fa78ca4c000+19d000]
[ 744.658830] usbrelay[5057]: segfault at 20 ip 00007ff23fb5d2e0 sp 00007ffe3f0c1770 error 4 in libc-2.25.so[7ff23fb25000+19d000]
[ 744.926952] usbrelay[5064]: segfault at 20 ip 00007feef9ef92e0 sp 00007ffcb27ed400 error 4 in libc-2.25.so[7feef9ec1000+19d000]
[ 764.895788] usbrelay[5087]: segfault at 20 ip 00007f40fc1d92e0 sp 00007ffe376b3750 error 4 in libc-2.25.so[7f40fc1a1000+19d000]
[ 795.896736] usbrelay[5098]: segfault at 20 ip 00007f30316ac2e0 sp 00007ffd4c006500 error 4 in libc-2.25.so[7f3031674000+19d000]
[ 826.901807] usbrelay[5136]: segfault at 20 ip 00007fc0dafc52e0 sp 00007ffdf7400e30 error 4 in libc-2.25.so[7fc0daf8d000+19d000]
[ 857.891152] usbrelay[5147]: segfault at 20 ip 00007f8836b9a2e0 sp 00007ffe1f527820 error 4 in libc-2.25.so[7f8836b62000+19d000]
[ 888.893886] usbrelay[5165]: segfault at 20 ip 00007f06e26342e0 sp 00007ffd7b7e2790 error 4 in libc-2.25.so[7f06e25fc000+19d000]
[ 919.896832] usbrelay[5182]: segfault at 20 ip 00007ff071be52e0 sp 00007ffc587fbd20 error 4 in libc-2.25.so[7ff071bad000+19d000]
[ 950.899590] usbrelay[5200]: segfault at 20 ip 00007fd7352ec2e0 sp 00007ffe405c1d40 error 4 in libc-2.25.so[7fd7352b4000+19d000]

If I run the usbrelay with strace, I see this:

8, TFD_TIMER_ABSTIME, {it_interval={tv_sec=0, tv_nsec=0}, it_value={tv_sec=76435, tv_nsec=590560000}}, NULL) = 0
ioctl(9, USBDEVFS_SUBMITURB, 0x798a68b100) = 0
poll([{fd=6, events=POLLIN}, {fd=8, events=POLLIN}, {fd=9, events=POLLOUT}], 3, 60000) = 1 ([{fd=9, revents=POLLOUT}])
ioctl(9, USBDEVFS_REAPURBNDELAY, 0x7fffe8206960) = 0
timerfd_settime(8, 0, {it_interval={tv_sec=0, tv_nsec=0}, it_value={tv_sec=0, tv_nsec=0}}, NULL) = 0
ioctl(9, USBDEVFS_REAPURBNDELAY, 0x7fffe8206960) = -1 EAGAIN (Resource temporarily unavailable)
write(7, "\1", 1) = 1
close(9) = 0
open("/sys/bus/usb/devices/1-1/bConfigurationValue", O_RDONLY) = 9
read(9, "1\n", 5) = 2
close(9) = 0
open("/sys/bus/usb/devices/1-1/bConfigurationValue", O_RDONLY) = 9
read(9, "1\n", 5) = 2
close(9) = 0
open("/sys/bus/usb/devices/usb1/bConfigurationValue", O_RDONLY) = 9
read(9, "1\n", 5) = 2
close(9) = 0
open("/sys/bus/usb/devices/usb1/bConfigurationValue", O_RDONLY) = 9
read(9, "1\n", 5) = 2
close(9) = 0
write(2, "Device Found\n type: 16c0 05df\n "..., 75Device Found
type: 16c0 05df
path: 0001:0007:00
serial_number: (null)) = 75
write(2, "\n", 1
) = 1
write(2, " Manufacturer: (null)\n", 23 Manufacturer: (null)
) = 23
write(2, " Product: (null)\n", 23 Product: (null)
) = 23
write(2, " Release: 100\n", 20 Release: 100
) = 20
write(2, " Interface: 0\n", 18 Interface: 0
) = 18
--- SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_MAPERR, si_addr=0x20} ---
+++ killed by SIGSEGV (core dumped) +++
[1] 17365 segmentation fault sudo strace /home/home-assistant/automation/usbrelay

I noticed a few things:

  • the output of the program is like this:
    Device Found
    type: 16c0 05df
    path: 0001:0007:00

The path is not displayed correctly. However I have noticed that the device is mounted in /dev/hidraw3

If i unplug the device and plug it again, everything works.

I am using ARCH LINUX (Linux 4.12.4-1-ARCH #1) and I have used gcc 7.1 to compile the program.

Do you have any idea why this behavior is only after a matter of time ?

usbrelay show no relay serial

Hi,

session transcript follows,

felipe@satriani:~$ sudo usbrelay
Device Found
type: 16c0 05df
path: /dev/hidraw5
serial_number:
Manufacturer: www.dcttech.com
Product: USBRelay8
Release: 100
Interface: 0
Number of Relays = 8
_1=0
_2=0
_3=0
_4=0
_5=0
_6=0
_7=0
_8=0

as you can see, no relay serial is displayed .. so no relay can be managed. I've tried this:

felipe@satriani:~$ sudo usbrelay '_1'=1
Orig: _1, Serial: 1, Relay: 1 State: 0
Device Found
type: 16c0 05df
path: /dev/hidraw5
serial_number:
Manufacturer: www.dcttech.com
Product: USBRelay8
Release: 100
Interface: 0
Number of Relays = 8
Serial: 1, Relay: 1 State: 0

Serial: 1, Relay: 1 State: 0 --- Not Found
felipe@satriani:~$ sudo usbrelay
Device Found
type: 16c0 05df
path: /dev/hidraw5
serial_number:
Manufacturer: www.dcttech.com
Product: USBRelay8
Release: 100
Interface: 0
Number of Relays = 8
_1=0
_2=0
_3=0
_4=0
_5=0
_6=0
_7=0
_8=0

tested with versions: v0.4, 0.5, 0,6 same result

version 0.7 show nothing

felipe@satriani:$ sudo usbrelay
felipe@satriani:
$

version v0.4 work with other relay, same chipset only 1 Relay on the board.

thank you.

Quiet output not working

Hi, I can not get the quiet output to work
What I tried is following, whatever I do I always get Device found output...

pi@raspberrypi:~ $ sudo usbrelay
Device Found
  type: 16c0 05df
  path: /dev/hidraw2
  serial_number: 
  Manufacturer: www.dcttech.com
  Product:      USBRelay2
  Release:      100
  Interface:    0
  Number of Relays = 2
1_1=1
1_2=1

pi@raspberrypi:~ $ sudo usbrelay -q
Orig: -q, Serial: -q, Relay: 0 State: 0
Device Found
  type: 16c0 05df
  path: /dev/hidraw2
  serial_number: 
  Manufacturer: www.dcttech.com
  Product:      USBRelay2
  Release:      100
  Interface:    0
  Number of Relays = 2
Serial: -q, Relay: 0 State: 0 

Serial: -q, Relay: 0 State: 0 --- Not Found

Typo in "Building the Code"

$ git clone https://git.com/darrylb123/usbrelay

should be:

$ git clone https://github.com/darrylb123/usbrelay

After modify the name of the board, but the name becomes empty.

After modify the name of the board, the board's name becomes empty
Then I can't change it back.


root@ubuntu:/usb-relay/usbrelay# ls -l
total 408
-rw-r--r--  1 root root    412 11月  6 16:04 build.sh
-rw-r--r--  1 root root    127 11月  6 16:04 Dockerfile
drwxr-xr-x 16 root root   4096 11月  6 16:07 hidapi
-rw-r--r--  1 root root  18026 11月  6 16:04 LICENSE.md
-rw-r--r--  1 root root    298 11月  6 16:04 Makefile
-rw-r--r--  1 root root   6604 11月  6 16:04 README.md
-rwxr-xr-x  1 root root  18096 11月  6 16:05 usbrelay
-rw-r--r--  1 root root   6901 11月  6 16:04 usbrelay.c
-rw-r--r--  1 root root   1116 11月  6 16:04 usbrelay.h
-rw-r--r--  1 root root 337602 11月  6 16:04 usbrelay.jpg


root@ubuntu:/usb-relay/usbrelay# ./usbrelay 2>/dev/null
_1=0
_2=0
_3=0
_4=0
_5=0
_6=0
_7=0
_8=0


root@ubuntu:/usb-relay/usbrelay# ./usbrelay _0=HW554
Orig: _0, Serial: 0, Relay: 0 State: 0
Device Found
  type: 16c0 05df
  path: 0001:0009:00
  serial_number: (null)
  Manufacturer: www.dcttech.com
  Product:      USBRelay8
  Release:      100
  Interface:    0
  Number of Relays = 8
Serial: 0, Relay: 0 State: 0

Serial: 0, Relay: 0 State: 0 --- Not Found


root@ubuntu:/usb-relay/usbrelay# ./usbrelay 2>/dev/null
_1=0
_2=0
_3=0
_4=0
_5=0
_6=0
_7=0
_8=0
root@ubuntu:/usb-relay/usbrelay#

Serial changes (despite sudo)

Hi,
First of all thank you for usbrelay!
I am having the Problem that the serial changes every now and then to some strange string. I couldnt see a pattern.
I am triggering Usbrelay from a home automation Software (fhem) and am using sudo always..
Any thoughts?

Here is what it looks like:

nZ�_1=0 nZ�_2=1 nZ�_3=0 nZ�_4=0 nZ�_5=0 nZ�_6=0 nZ�_7=0 nZ�_8=0

Add apt-get instructions?

I was having some trouble compiling this in my Ubuntu installation, but then noticed it's available as a Debian repo. Should the fact that can install with "apt-get install usbrelay" be added to the instructions?

And thank you so much for this fantastic utility. I'd been trying to get this relay working for a good hour before finding this repo.

Interested to contribute to crelay?

Hi,

I just got notified of your project and was wondering if you would be interested to contribute to the crelay project:
http://ondrej1024.github.io/crelay

This software aims at providing a unified way of controlling different relay types (mostly USB). I wrote the framework and the drivers for 2 USB relay cards. It would be great if you wanted to add the driver for the HIDAPI type of relay card that your usbrelay software supports.

Basically what you would need to write is the low level functions for this relay card to perform the following:

  • detection of the card and communication port
  • set relay states
  • read relay states

Please let me know what you think about this.
Thanks, Ondrej

Cross compiling for OpenWRT

Hello Darrylb,

This is not really a problem, but I couldn't find any other way to contact you...
Fist of all, I'd like to say many thanks for this program, it is really useful! I mean, how cool can it be to turn on/off stuff from the computer?? :)

I have compiled your code in ubuntu 64 and Rasperry PI and it works great!

Now I'd like to have it also in my router (based on OpenWRT), so I have to cross-compile it.
I have built the cross-compile environment and I was able to compile a simple "hello world" program (tested successfully, so I guess we can say the build environment is OK).
However, I'm not able to cross-compile your code because I'm unable to link the HIDAPI libraries. Do I need to cross-compile the HIDAPI libraries in advance or its code is enough to cross-compile usbrelay.c ?
Maybe this is a very basic question, but I'm not a programmer or anything and I'm struggling with this for a couple of months...

The error I'm getting is:
[code]
/mnt/storage/usbrelay-openwrt/openwrt/staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/bin/mips-openwrt-linux-gcc -o usbrelay usbrelay.c -lhidapi-hidraw

/mnt/storage/usbrelay-openwrt/openwrt/staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/lib/gcc/mips-openwrt-linux-uclibc/4.8.3/../../../../mips-openwrt-linux-uclibc/bin/ld: cannot find -lhidapi-hidraw
collect2: error: ld returned 1 exit status
[/code]

I have created a post in openwrt forum, but I'm afraid I couldn't get an answer I could understand...
More info on https://forum.openwrt.org/viewtopic.php?id=50682

Would you know what would the problem be and if so, would you point me in the right direction?

Thank you very much,
João Silva

I've got a USBRelay 4 but it works only on the last 2 Relays

Everything seems to work, but when I switch HW341_1/HW341_2 the relay won't click and the led doesn't work. The software recognizes the device correctly.

sudo ./usbrelay

Device Found
type: 16c0 05df
path: 0001:0005:00
serial_number: (null)
Manufacturer: www.dcttech.com
Product: USBRelay4
Release: 100
Interface: 0
Number of Relays = 4
HW341_1=0
HW341_2=0
HW341_3=0
HW341_4=0

So when I execute "# sudo ./usbrelay HW341_1=1 HW341_2=1 HW341_3=1 HW341_4=1" I get:
Orig: HW341, Serial: HW341, Relay: 1 State: ff
Orig: HW341, Serial: HW341, Relay: 2 State: ff
Orig: HW341, Serial: HW341, Relay: 3 State: ff
Orig: HW341, Serial: HW341, Relay: 4 State: ff
Device Found
type: 16c0 05df
path: 0001:0002:00
serial_number: (null)
Manufacturer: www.dcttech.com
Product: USBRelay4
Release: 100
Interface: 0
Number of Relays = 4
Serial: HW341, Relay: 1 State: ff
1 HID Serial: HW341 Serial: HW341, Relay: 1 State: ff
Serial: HW341, Relay: 2 State: ff
2 HID Serial: HW341 Serial: HW341, Relay: 2 State: ff
Serial: HW341, Relay: 3 State: ff
3 HID Serial: HW341 Serial: HW341, Relay: 3 State: ff
Serial: HW341, Relay: 4 State: ff
4 HID Serial: HW341 Serial: HW341, Relay: 4 State: ff

Serial: HW341, Relay: 1 State: ff --- Found
Serial: HW341, Relay: 2 State: ff --- Found
Serial: HW341, Relay: 3 State: ff --- Found
Serial: HW341, Relay: 4 State: ff --- Found

But only relay 3 and 4 are switched on an have a red LED. Relay 1 and 2 don't click and LED stay off.
Is this a bug in the code or is my relay defect?
Can you help me out please!

Can't switch relays, errors: "ioctl (GFEATURE): No such device" AND "Serial: PSUIS, Relay: 2 State: ff --- Not Found Serial: PSUIS, Relay: 1 State: fd --- Not Found"

Hi.

I have this usb relay board, looks identical to your picture and the output of lsusb -v -d 16c0:05df is the same as yours.

Getting the status of the device appears to be working fine (or who knows, maybe it just displays the default 0 value, if it can't get the real status), I get:

Device Found
type: 16c0 05df
path: /dev/hidraw3
serial_number:
Manufacturer: www.dcttech.com
Product: USBRelay2
Release: 100
Interface: 0
ioctl (GFEATURE): No such device
_1=0
_2=0

However, I can't get it to switch the relays on/off. With sudo ./usbrelay PSUIS_2=1 PSUIS_1=0 I get:

Orig: PSUIS, Serial: PSUIS, Relay: 2 State: ff
Orig: PSUIS, Serial: PSUIS, Relay: 1 State: fd
Device Found
type: 16c0 05df
path: /dev/hidraw3
serial_number:
Manufacturer: www.dcttech.com
Product: USBRelay2
Release: 100
Interface: 0
ioctl (GFEATURE): No such device
Serial: PSUIS, Relay: 2 State: ff
Serial: PSUIS, Relay: 1 State: fd

Serial: PSUIS, Relay: 2 State: ff --- Not Found
Serial: PSUIS, Relay: 1 State: fd --- Not Found

I'm running Ubuntu 14.04 LTS on a laptop, kernel "3.13.0-30-generic x86_64".
The relays work fine when switched from Windows using the program that came with it.

Any hints would be much appreciated!

Thanks!

Ok with pi 1 B but not anymore with pi 4 B

Hello, I realized that the relay was detected by lsusb in the raspberry pi 1 B but was no longer detected with the pi 4 B, however both are up to date, could you tell me how I can make it recognize? what libraries or manipulatio to do please

2 modules with the same ID product and ID vendor in the same linux system

Hello,
I am using a ubuntu system 16.04 with 2 modules usb-relay connected and i want to create linux usb rule to differentiate them from each other.

The python code example lets you create an object having only 2 arguments ( id vendor and id product) but those 2 are the same.
I was not able to find the serial number and i have seen that this kinds of modules have a special serial number but i do not know how to link this to the linux usb rules.
Could you help me please?

Help! Seems USBRelay's serial is missing

Hi! Ive got the 8 ch relay worked, but since yesterday it seems it lost its serial which is needed to operate.
When i try with the GUI on a windows computer it shows as serial just: ","

Any idea how to fix this?

usbrelayd crashing

I built the usbrelayd daemon according to the instructions but it fails to run. The logs are not much help but my knowledge is limited:

root@rock64:~/usbrelay# systemctl status usbrelayd

  • usbrelayd.service - USB Relay MQTT service
    Loaded: loaded (/etc/systemd/system/usbrelayd.service; enabled; vendor preset: enabled)
    Active: failed (Result: exit-code) since Mon 2021-12-20 17:52:48 MST; 2min 23s ago
    Process: 26034 ExecStart=/usr/bin/python3 /usr/local/sbin/usbrelayd ${BROKER} (code=exited, status=1/FAILURE)

Main PID: 26034 (code=exited, status=1/FAILURE)

Dec 20 17:52:47 rock64 systemd[1]: usbrelayd.service: Failed with result 'exit-code'.
Dec 20 17:52:48 rock64 systemd[1]: usbrelayd.service: Service RestartSec=1s expired, scheduling restart.
Dec 20 17:52:48 rock64 systemd[1]: usbrelayd.service: Scheduled restart job, restart counter is at 5.
Dec 20 17:52:48 rock64 systemd[1]: Stopped USB Relay MQTT service.
Dec 20 17:52:48 rock64 systemd[1]: usbrelayd.service: Start request repeated too quickly.
Dec 20 17:52:48 rock64 systemd[1]: usbrelayd.service: Failed with result 'exit-code'.
Dec 20 17:52:48 rock64 systemd[1]: Failed to start USB Relay MQTT service.

The test.py script works as it should.
Did I mess up the BROKER definition in the .conf file?
I simply inserted the IP address of the MQTT broker.
N

Raspberry pi 4 Enumerate

Hi Darryl

Thank you for the use of your usbrelay2 code for the past couple of years. In 2017 I purchased a wonderful coffee machine that unfortunately suffers from a ghastly EU ruling that forces the machine to switch off after 30 minutes of inactivity. Fair enough but to get the machine to the peak running state it needs 45 minutes at least to warm up.

I discovered that the on/off switch just needed a pulse within 30 minutes to keep the machine running and the usbrelay for ebay was the beginning of the solution. I found your code on GitHub and a friend helped me to get it running on an early Raspberry pi B. I used crontab to schedule the events and it would switch the machine on in the morning and switch it off after lunch and provided a pulse every 20 minutes to keep the timer running.

It has run well (headless) for the 2 years but now connecting to it is difficult. The ethernet port now seems to have inactivity and so I purchased a new Raspberry PI 4. Unfortunately, your code no longer seems to compile and my friend is at a loss as to the error.

usbrelay.c:(.text+0x36c): undefined reference to enumerate_relay_boards' /usr/bin/ld: usbrelay.c:(.text+0x42c): undefined reference to find_board'
/usr/bin/ld: usbrelay.c:(.text+0x4f8): undefined reference to set_serial' /usr/bin/ld: usbrelay.c:(.text+0x5dc): undefined reference to operate_relay'
collect2: error: ld returned 1 exit status

Attached is a copy of my installation procedure and also a copy of the compile run. My knowledge of C++ is limited together with my experience of the cmd line. Would you be able to look at the attached file and hopefully give some pointers to the solution.

Many thanks in advance.

KR

Roy
Raspberry Pi - USBRELAY compile commands.docx
Run gcc -v -o.docx

2 Ucreatefun.com on same Rpi => no use of second Relay board

Hi Team !

First of wall, let me congratulate you for your work on the "usbrelay" project. I use it daily, and thanks to its stability, i'm confident with the automation at home scale. So, i bought a few more relay cards (2 cards with 8 relay each)

And i get 2 "Ucreatefun.com" 8 relays cards... connected now on an Rpi B version 1.
lsusb says :

Bus 001 Device 006: ID 0519:2018 Star Micronics Co., Ltd
Bus 001 Device 005: ID 0519:2018 Star Micronics Co., Ltd

My only problem is that when i use this command :
sudo usbrelay /dev/hidraw0_1=1
or this command :
sudo usbrelay /dev/hidraw1_1=1
i've got the exact same result : the "first" board is changing is relay1 state... even if i try to change the second "hidraw" board...

here is the result of usbrelay -d :

sudo ./usbrelay -d
Found 2 devices
Device Found
  type: 0519 2018
  path: /dev/hidraw0
  serial_number: A0001
Manufacturer: Ucreatefun.com
  Product:      HIDRelay
  Release:      1
  Interface:    0
  Number of Relays = 9
  Module_type = 2
A0001_1=-1
A0001_2=-1
A0001_3=-1
A0001_4=-1
A0001_5=-1
A0001_6=-1
A0001_7=-1
A0001_8=-1
A0001_9=-1
Device Found
  type: 0519 2018
  path: /dev/hidraw1
  serial_number: A0001
Manufacturer: Ucreatefun.com
  Product:      HIDRelay
  Release:      1
  Interface:    0
  Number of Relays = 9
  Module_type = 2
A0001_1=-1
A0001_2=-1
A0001_3=-1
A0001_4=-1
A0001_5=-1
A0001_6=-1
A0001_7=-1
A0001_8=-1
A0001_9=-1

may you help me to use this command as i should or point me to changes i may try on the source code.

for information, i'm using current git code compiled today (17-04-2020).

Regards

please tag a first version

Hello again,

would you mind tagging the current HEAD as version 0.1? I think the code is good and works quite well.

Best regards
Jan

unable to change serial number

Hi,
I'm trying to change device serial number using
sudo usbrelay OLD_0=NEW but it does not get changed. Can please advise?

sudo usbrelay HURTM_0=ABCDE
Orig: HURTM, Serial: HURTM, Relay: 0 State: fd
Device Found
type: 16c0 05df
path: /dev/hidraw1
serial_number:
Manufacturer: www.dcttech.com
Product: USBRelay2
Release: 100
Interface: 0
Number of Relays = 2
Serial: HURTM, Relay: 0 State: fd
1 HID Serial: HURTM Serial: HURTM, Relay: 0 State: fd

Serial: HURTM, Relay: 0 State: fd --- Found

Update

I want to control this over Web Browser Instead of UI on IP address (Ethernet) Any Solution?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.