/Domoticz-Pi-USB8-relay-board-SOS-solutions

Integrate 8-channel relay board with ATMEL8 from SOS solutions or Ebay/Ali into Domoticz on Raspberry Pi

Domoticz-Pi-USB8-relay-board-SOS-solutions

Integrate 2/4/8-channel relay USB board with an ATMEL8 microcontroller from SOS solutions or Ebay/AliExpress into Domoticz on Raspberry Pi (3).

Which board is this about?

I got this board from https://www.sossolutions.nl/8xusbrelais but they are also available on Ebay and Aliexpress. In the product description there was a crude Youtube video explaining how to get this board to work.
However, the board in the video had a FTDI 245R controller chip and the board I got only had an Atmel8 microcontroller on board.
The board is recognized as vendor=0x16c0 and product=0x05df.

How to get this board to work at all

Luckily, someone already did most of the work:
http://vusb.wikidot.com/project:driver-less-usb-relays-hid-interface and
https://github.com/pavel-a/usb-relay-hid

This particular Github page is a short guide how to integrate this relay board into Domoticz running on a Raspberry Pi 3.
This is more a reminder to myself how I got it working and it may be wholefully incomplete, so your mileage may very.

If you know your Linux stuff just follow the above mentioned Github and build it yourself.
If not, you can use this description for guidance.

General control scheme and usage

What you will do in the end is control the separate relays on this board via virtual switches in Domoticz.
This board has a certain firmware on board that emulates USB and listens to a number of commands.
In the On and Off action of the virtual switch you point to a script while passing parameters.
In turn this script/utility will connect to the Atmel controller via USB and set those relays for you.

Example to turn ON relay 1 of board id ABCDE:
script://usbrelay/hidusb-relay-cmd id=ABCDE ON 1

Example to turn OFF relay 2 of board id ABCDE:
script://usbrelay/hidusb-relay-cmd id=ABCDE OFF 2

Step by Step instructions:

Step 1: plug in the board and check if it is the right one

Plug in the relay board to a free USB port on your Pi and log in to the Pi via a SSH shell (F.i. with Putty).

type: sudo lsusb -v | more
This will list all devices on USB.
With the spacebar you can check if the board there.
You should see something like this somewhere in the list:

idVendor           0x16c0
idProduct          0x05df
iManufacturer      1 www.dcttech.com
iProduct           2 USBRelay8

If not, you cannot use this method. If it is there, you can continue.

Step 2: Add the USB device to a udev rule so it can be accessed by Domoticz

To grant access to non-root users, define an udev rule for the devices:
Command f.i.: sudo nano /etc/udev/rules.d/99-usbrelay.rules
Add the following line to the file and save the file (CTRL+X):
SUBSYSTEMS=="usb", ATTRS{idVendor}=="16c0", ATTRS{idProduct}=="05df", MODE:="0666"
(For some background info on udev see this: https://hackaday.com/2009/09/18/how-to-write-udev-rules/)

Step 3 for Non Raspberry PI -> Get and build the source

F.i.:

git clone https://github.com/pavel-a/usb-relay-hid.git
cd commandline/makemake && make

(Keep in mind the actual source is not from my Github page)

Step 3 for Raspberry PI -Copy compiled source to correct folder

Copy the files from the usbrelay folder on this Github page to a new subfolder in the Domoticz scripts folder (/domoticz/scripts/usbrelay).

Step 4: Set permissions

you may need to set some file permissions so everything can execute properly.

Step 5: Find the serial number of the relay board

In the shell go to the usbrelay folder and type:
usbrelay-cmd ENUM
You will get something like this:
Board ID=[6EDX8] State: 00 (hex)
The ID is your boards' serial number. Write this down.
If you have more of these boards connected, you will see more boards in the list.

Step 6: Add the virtual switches in Domoticz

In Domoticz you need to create one virtual switch for every relay.
What I normally do is to create a hardware device per board.
'Setup->Hardware->Add Type:Dummy'
When this hardware is added, you can add virtual devices of type 'Switch'.
So one virtual switch per relay. If the board has 8 relays, you create 8 virtual switches.

Step 7: Set the ON/OFF action for every virtual switch

Now go to the 'Switches' tab in Domoticz and do this for each virtual switch;
Go to 'Edit' and fill in the following for the 'On' and 'Off action';
script://usbrelay/hidusb-relay-cmd id=[device ID] [ON or OFF action] [relay number 1-8]
So for relay 1 of device ID 6EDX8;
'On action':
script://usbrelay/hidusb-relay-cmd id=6EDX8 ON 1
And for the 'Off' action:
script://usbrelay/hidusb-relay-cmd id=6EDX8 OFF 1

When you are done, you can use these virtual switches to control the relays as you would do with any other switch in Domoticz.
Domoticz will execute the function in the background for every switch action.

Note: If you put the files somewhere else, change the location in the above commands.
Domoticz uses the domoticz scripts folder as root.

Checking relay status

With the command usbrelay-cmd STATUS you will get the status of all relays in HEX format.
So if relay 2 and 4 are ON while the rest is OFF, the response will be State: 0A (hex).
'0A' HEX is 0b00001010, You can see this corresponds with relay 2 and 4.
When Domoticz executes the script, it does not check whether the relay has actually turned on or off.
If you need this, you need to write a custom script that checks the relay status after the switch action with the 'STATUS' parameter.

Additional commands

usbrelay-cmd id=ABCDE ON ALL
usbrelay-cmd id=ABCDE OFF ALL

If you omit the device ID, the script will execute the command for the first ID it found.
So if you have more of these boards connected, you need to include the device ID.