USB HID relay

From SundanceWiki
Revision as of 11:12, 13 May 2024 by Christopherh (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

The USB HID relay box contains one of these:

It is a simple device that converts USB to a HID device so doesn't tie up a comport.

When special commands are sent to the device it either energises or de-engeriges the relay, switching between NC (Normally Connected) and NO (Normally Open).

So if a wire is routed through NO -> COM, when the relay is switched on the contacts close and the circuit completes. When switched off again it opens and breaks the circuit.

NC is the other way round.

Windows

In windows the easiest way I found of controlling it was to use a program called "LCHID.exe". I've saved a copy of this to our FTP server HERE.

Open the program check that the VID and PID numbers are as shown (VID: 5131, PID: 2007) - shown in GREEN

Click the "Relay1" checkbox to switch it on, and again to switch it off- shown in Blue

You should hear the relay click and see a second LED come on through the slot.

Linux

In Linux you don't need any additional programs.

To find out which USB device it has been allocated run this command:

ls $(find /sys/devices -iname '*:5131:2007.*')/hidraw

This will tell you the "hidraw" number of the last thing that was connected to the PC.

This may well change each and every time you run it.

To control the relay enter the following commands in terminal (changing the hidraw number to the one reported):

You will need to change the permissions for this hid device.

Run this command:

sudo chmod 0666 /dev/hidraw1

To switch on: echo -e '\xA0\x01\01\xA2' > /dev/hidraw1

To switch off: echo -e '\xA0\x01\00\xA1' > /dev/hidraw1



This script below is probably overkill, but will switch the device on and off as many times as you tell it to.


It starts by asking you to unplug and reattach the box so that it can get the device ID of the HID dongle. It saves this ID to a file, and then reads the file to a variable (Probably can be done better?)


The script then uses this as the device to switch the relay on and off.


 #!/bin/bash
 
 echo "Please unlpug and reconnect the USB HID relay box so we can get the device ID"

 read -p $'\e[32m\e[107m Press any key to continue...\e[0m'
 
 touch /temp/usbHIDport.txt
 port=/temp/usbHIDport.txt
 
ls $(find /sys/devices -iname '*:5131:2007.*')/hidraw > "$port"
 
 port2usb=$(<"$port")
 
 echo "It found that it's on: $port2usb"

sudo chmod 0666 /dev/$port2usb
 
 counter=1
 
 read -p "Enter number of loops required: " user_max_counter
 
 while [ $counter -le $user_max_counter ]
 do
 echo $'\e[32m\e[107m Loop number' $counter $'of ' $user_max_counter $'\e[0m'
 ((counter++)) 
 
 echo -e '\xA0\x01\01\xA2' > /dev/$port2usb
 echo "Relay On!"
 
 sleep 3s 
 
 echo -e '\xA0\x01\00\xA1' > /dev/$port2usb
 echo "Relay Off!"
 
 sleep 3s 
 
 done
 echo All done
 

The box itself

On the box there is a USB-B connection on the rear. Connect this to your PC.


The two black terminals are connected together. Nothing is switched.


The two red terminals are switched via the relay ONLY when the master control switch in on. If the switch is as per the photo, the relay is bypassed.


The relay is set to be off by default.