SainSmart iMatic relay board with RJ45

Others (MiLight, Hue, Toon etc...)

Moderator: leecollings

Post Reply
Szmalu
Posts: 13
Joined: Thursday 02 April 2015 18:50
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

SainSmart iMatic relay board with RJ45

Post 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... :)
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: SainSmart iMatic relay board with RJ45

Post 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.
Last edited by ThinkPad on Thursday 02 April 2015 19:57, edited 3 times in total.
Reason: Added fixed script
I am not active on this forum anymore.
Szmalu
Posts: 13
Joined: Thursday 02 April 2015 18:50
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: SainSmart iMatic relay board with RJ45

Post by Szmalu »

Thx for quick response! I'll check it right away:)

AND I'm sorry I did not greeted before :) Hi everyone!
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: SainSmart iMatic relay board with RJ45

Post 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.
I am not active on this forum anymore.
Szmalu
Posts: 13
Joined: Thursday 02 April 2015 18:50
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: SainSmart iMatic relay board with RJ45

Post 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
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: SainSmart iMatic relay board with RJ45

Post 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.
I am not active on this forum anymore.
Szmalu
Posts: 13
Joined: Thursday 02 April 2015 18:50
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: SainSmart iMatic relay board with RJ45

Post by Szmalu »

Thx a lot :)

Sth new appeared

Code: Select all

  File "test.py", line 14
    data_send += "00"
            ^
IndentationError: expected an indented block
Szmalu
Posts: 13
Joined: Thursday 02 April 2015 18:50
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: SainSmart iMatic relay board with RJ45

Post by Szmalu »

Ok it's alive ! :) I'll send details in the moment.
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: SainSmart iMatic relay board with RJ45

Post by ThinkPad »

Nice, please share the script so someone else can benefit from it :)
I am not active on this forum anymore.
Szmalu
Posts: 13
Joined: Thursday 02 April 2015 18:50
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: SainSmart iMatic relay board with RJ45

Post 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.
Attachments
relay.zip
(427 Bytes) Downloaded 310 times
Szmalu
Posts: 13
Joined: Thursday 02 April 2015 18:50
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: SainSmart iMatic relay board with RJ45

Post 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'
Szmalu
Posts: 13
Joined: Thursday 02 April 2015 18:50
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: SainSmart iMatic relay board with RJ45

Post by Szmalu »

Ok, I made it :) It's woring now. I'll post later to explain everything.
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: SainSmart iMatic relay board with RJ45

Post 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!
I am not active on this forum anymore.
CodeCardinal
Posts: 1
Joined: Wednesday 08 April 2015 16:33
Target OS: -
Domoticz version:
Contact:

Re: SainSmart iMatic relay board with RJ45

Post 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.
codesnipper
Posts: 16
Joined: Tuesday 23 February 2016 14:04
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: SainSmart iMatic relay board with RJ45

Post by codesnipper »

I have the "error 512" problem. Can anyone help me?
ikelhak
Posts: 1
Joined: Wednesday 09 November 2016 17:54
Target OS: -
Domoticz version:
Contact:

Re: SainSmart iMatic relay board with RJ45

Post by ikelhak »

Hi,
I'm going to buy Imatic relay with 8 relay onboard.
Can you confirm it will work with Domoticz?
Thank you.
Totter
Posts: 4
Joined: Saturday 21 November 2015 4:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.3530
Location: San Francico, U.S.
Contact:

Re: SainSmart iMatic relay board with RJ45

Post by Totter »

I was finally able to get the 16 relay module
SainSmart imatic 16 On Off codes.zip
(1.42 KiB) Downloaded 396 times
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
SainSmartimatic screenshot.PNG (105.12 KiB) Viewed 5603 times

Not the most elegant code, but it works for me, I hope it works for others.
SainSmart imatic 16 On Off codes.zip
(1.42 KiB) Downloaded 396 times
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest