Page 1 of 1
SainSmart iMatic relay board with RJ45
Posted: Thursday 02 April 2015 18:58
by Szmalu
Hi guys
Is there any way to control that board from Domoticz? (
http://www.sainsmart.com/sainsmart-imat ... d-ios.html)
I bought it hoping it will allow HTTP GET requests but id doesn't
There are some clues about that relay board (8 channel but it's almost the same):
https://codecardinal.wordpress.com/2015 ... c-windows/
https://codecardinal.wordpress.com/2015 ... x-windows/
But I'm not a programmer and it doesn't tell me anything...

Re: SainSmart iMatic relay board with RJ45
Posted: Thursday 02 April 2015 19:42
by ThinkPad
First: welcome to this forum!
I think the Python script is perfectly usable with Domoticz.
Put the script on your Pi (domoticz folder > scripts).
Then create a virtual switch (create dummy hardware at Setup > Hardware), then go to 'Switches' tab and click on 'Manual switch'. Choose the dummy hardware from dropdown. Then fill in a name and choose on/off. Protocol doesn't matter because it is virtual.
Then when you have made the switch, click the 'Edit' button and at the 'On action', fill in something like this:
Code: Select all
script://path_to_script/relay.py 8 1
For the 'Off action' you fill in almost the same, but this time the off command:
Code: Select all
script://path_to_script/relay.py 8 0
(not sure if zero means off, but it probably does)
Hope you can get it working that way
Edit: Looked at the Python script again, but i think his blog messes up the code. The '&', '<' and ' " ' are replaced by HTML alternatives.
Try this version that i corrected:
relay.py
Code: Select all
from socket import *
import sys
if len(sys.argv) != 3:
sys.exit(2)
relay = str(sys.argv[1])
state = str(sys.argv[2])
data_send = "FD0220"
if int(relay) <= 8:
data_send += "0" + relay
if int(state) == 0:
data_send += "00"
elif int(state) == 1:
data_send += "01"
elif int(relay) == 9:
data_send += "F8"
if int(state) == 0:
data_send += "80"
elif int(state) == 1:
data_send += "88"
data_send += "5D"
s = socket(AF_INET, SOCK_STREAM)
s.connect(("192.168.1.4", 30000))
s.send(data_send.decode('hex'))
data = s.recv(4)
print(data.encode('hex'))
Be sure to change the IP to that of your board.
Re: SainSmart iMatic relay board with RJ45
Posted: Thursday 02 April 2015 19:58
by Szmalu
Thx for quick response! I'll check it right away:)
AND I'm sorry I did not greeted before

Hi everyone!
Re: SainSmart iMatic relay board with RJ45
Posted: Thursday 02 April 2015 20:00
by ThinkPad
Haha it was not meant like: you didn't welcome yourself!
I saw this was your first post here, so i wanted to welcome you here
By the way i don't have this relay board, so can't help trying. Just translating/helping what i see on the (second) page you gave me.
Re: SainSmart iMatic relay board with RJ45
Posted: Thursday 02 April 2015 20:45
by Szmalu
Ok... I realized, that I didn't have python @ my raspberry (I've got it now

) but when I'm trying to run that script error appears:
Code: Select all
pi@raspberrypi ~/domoticz/scripts $ sudo python relay.py 8 1
File "relay.py", line 1
Script started on czw, 2 kwi 2015, 20:25:47
^
SyntaxError: invalid syntax
Re: SainSmart iMatic relay board with RJ45
Posted: Thursday 02 April 2015 20:54
by ThinkPad
Can't help you with that unfortunately. My coding skills are insufficient.
Maybe someone with better coding skills comes by and is able to help.
Re: SainSmart iMatic relay board with RJ45
Posted: Thursday 02 April 2015 21:16
by Szmalu
Thx a lot
Sth new appeared
Code: Select all
File "test.py", line 14
data_send += "00"
^
IndentationError: expected an indented block
Re: SainSmart iMatic relay board with RJ45
Posted: Thursday 02 April 2015 21:50
by Szmalu
Ok it's alive !

I'll send details in the moment.
Re: SainSmart iMatic relay board with RJ45
Posted: Thursday 02 April 2015 22:20
by ThinkPad
Nice, please share the script so someone else can benefit from it

Re: SainSmart iMatic relay board with RJ45
Posted: Thursday 02 April 2015 23:06
by Szmalu
Ok so mainly it was wrong some tabs and spaces...
I've added working one as attachment.
But I have another problem

When I'm using that in Domoticz it generates error:
Code: Select all
2015-04-02 22:43:59.154 Error: Error executing script command (/home/pi/domoticz/scripts/test.py). returned: 512
It was Error 32256, but afer I've changed attributes of the script file to 755 (chmod +x file.py) it changed to error 512...
It works @ console without any problem.
Re: SainSmart iMatic relay board with RJ45
Posted: Friday 03 April 2015 2:27
by Szmalu
Ok, I think, that I've found a source of the problem. I assume that Domoticz runs python scripts @ Python 3. That script works only with Python 2. For a programmer it wont be a problem to fix it

I hope that some of u can help?
When I'm trying to run that script @ Python 3, I get an error:
Code: Select all
Traceback (most recent call last):
File "/home/pi/domoticz/scripts/test.py", line 29, in <module>
s.send(data_send.decode('hex'))
AttributeError: 'str' object has no attribute 'decode'
Re: SainSmart iMatic relay board with RJ45
Posted: Friday 03 April 2015 16:50
by Szmalu
Ok, I made it

It's woring now. I'll post later to explain everything.
Re: SainSmart iMatic relay board with RJ45
Posted: Wednesday 08 April 2015 12:38
by ThinkPad
Can you please explain how you got it working, and post the script here?
I think other users who maybe have/will buy the same board are interested in it!
Re: SainSmart iMatic relay board with RJ45
Posted: Wednesday 08 April 2015 16:45
by CodeCardinal
Hi,
I'm the guy from the blog!
The code is indeed for Python =< v2.7
I have updated the blog with a new link to the code which has a more proper format, that fixes the indent and the html fuck ups.
In case someone needs this code for v3 and up, personal message me and I'll give it a try.
Re: SainSmart iMatic relay board with RJ45
Posted: Tuesday 23 February 2016 14:15
by codesnipper
I have the "error 512" problem. Can anyone help me?
Re: SainSmart iMatic relay board with RJ45
Posted: Wednesday 09 November 2016 17:56
by ikelhak
Hi,
I'm going to buy Imatic relay with 8 relay onboard.
Can you confirm it will work with Domoticz?
Thank you.
Re: SainSmart iMatic relay board with RJ45
Posted: Thursday 13 April 2017 19:39
by Totter
I was finally able to get the 16 relay module
working with Domoticz.
I am not much of a coder but I was able to cobble together some working code from other sources. For some reason the code I found worked great running from SSH but when I tried to run it as an On/Off Action it wouldn't run. After a lot of trials I figured out that for some reason I could not pass two arguments through the On/Off action.(Probably just something I was doing wrong). So I simply made two python scripts one for On and one for Off
I also couldn't decipher their code but I was able to use it to make a list of the commands for on and off so my script uses a list instead of code to generate the on/off commands.
To use these put these in your:
/home/pi/domoticz/scripts/python folder
SSH into that folder and run
sudo chmod +x relayOn.py
sudo chmod +x relayOff.py
Create a Dummy Swith
Under On/Off Action point to the files like this, changing the argument to the relay number you want to control:

- SainSmartimatic screenshot.PNG (105.12 KiB) Viewed 5743 times
Not the most elegant code, but it works for me, I hope it works for others.