Trying to control an 8 channel USB relay....

1,244 views
Skip to first unread message

TAllen-t

unread,
Sep 20, 2017, 5:17:05 AM9/20/17
to Node-RED
Trying to control an 8 channel USB relay in Node-Red on raspberry Pi 3, but not sure what to do with the serial node..



SMAKN®  8-channel USB serial control switch / 8-channel 30A Relay Module

I can control it on a PC using FT245RL utility from www.researchDesignLab.com,
and I can see and connect to it with the serial out Node but cant figure out how to communicate with it through node red.


I found this on their site as well.

It didn't come with any documentation, and I couldn't find much on controlling the board from USB.
Any guidance will be greatly appreciated!



onbyte=0x00

Relay 1 On

onbyte=onbyte or 0x01

serialport.write(onbyte)

 

Relay 1 off

onbyte=onbyte And 0XFE

serialport.write(onbyte)

 

relay 2 On
OnByte = OnByte Or 0x02
serialport.write(onbyte)

 

relay 2 off
OnByte = OnByte And 0xFD
serialport.write(onbyte)

 

relay3 on
OnByte = OnByte Or 0x04
serialport.write(onbyte)

 

relay3 off
OnByte = OnByte And 0xFB
serialport.write(onbyte)

 

relay 4 On

OnByte= OnByte Or 0x08
serialport.write(onbyte)

Dave C-J

unread,
Sep 20, 2017, 4:18:03 PM9/20/17
to node...@googlegroups.com
well not a lot to go on - it's not obvious that it is serial controlled (as opposed to a USB HID device or something else) but assuming it is then first you need to know the baud rate to use to talk to it... then
you "just" need to send those bytes as per the spec...

Simplest way (to start with) is to use an inject node to inject a payload of type buffer with 0x01 for relay 1 on and another with 0x00 for relay 1 off 
Inline images 1
and send that into the serial node. It's not clear if you need another byte to follow (like line end \n) to make the command be sent - you will have to play.

Though that will also turn off all the others as it is one byte to control all 8 - one bit per relay - so 0x03 will turn on relay 1 and 2 etc... will be up to you to mix combine the relevant bits to turn on those you wish etc
Message has been deleted

TAllen-t

unread,
Sep 20, 2017, 9:15:37 PM9/20/17
to Node-RED

OK. Making some headway!

I found a few amazon reviews

:ByGregory R. Collinson July 26, 2013
I purchased this to switch contacts in a data acquisition setup. It works well, all relays function correctly. The 'Relay Manager' program that you can download from the link (and their website) works perfectly.

I am having only one issue. I am using LabVIEW to control the board and I cannot get the given (from the download link) library files to open the board. That is, I can run the Open file and get an OK error status in return (it will return various error statuses, depending on the result) but the board will not actually open. I can get the device description, location and handle, but no communication connection. I tried a USB sniffer to check the data going over the port but couldn't seem to find what I was looking for.

I contacted the company and was given all the information that I already had. They were very prompt and courteous, but no real help. I never spoke with a programmer, only a secretary or some such.

The problem may be with LabVIEW but seems to be some command that I don't know since I can send other commands correctly. I am running Windows 7 and that may be an issue since the library files were written several years ago, although they have stated that they are compatible with this version of Windows and I am not having any other problems.

Anyway, my solution (until I can figure out what to do) is to open the board with the 'Relay Manager' and then send commands to the board as I like using my LabVIEW program. I only have to open the board once until I restart my computer and can close the Relay Manager (the initial handshake, whatever it is, remains) and open and close LabVIEW as often as I like with no problems.

A Second post Stated:

procsci9 months agoIn reply toan earlier postReport abuse

Thank you very much for fast response! I finally solved the problem: In FTDI's 'Write-Read Byte Array Demo' example the FT_Set_Bit_Mode was missing. Setting it to async bit band and bit mask = 255 the program now works fine in LabView. No need for 'relay manager' any more.

So.. to solve my problem, I might need to set the bit band and mask...
 but not sure how to do that (especially in Node-Red). Any Idea's?

Dave C-J

unread,
Sep 21, 2017, 4:15:20 AM9/21/17
to node...@googlegroups.com

I think the Labview stuff may be a diversion unless  you happen to use Labview. I think the best way forward right now would be to explore the python commands they seem to offer so that you could control it via a command line - then use the exec node to call that command line.

see the manual

TAllen-t

unread,
Sep 26, 2017, 10:35:56 PM9/26/17
to Node-RED
sending "0x01" as a binary buffer,  either doesnt work, or im using it wrong. 

However, I did get the drivers installed, and finally got the relay board working with the program the website above provided.
 and could even control the relays with port listening software by sending these bit strings.

00000001 Relay one on
00000011 Relay one and two on
11111111   All relays on etc....

Each bit representing a relay, 0 for off, 1 for on.
However, I still could not find a solution for sending the byte string to my serial port directly from Node-Red Serial Node in the bit format above.

The new kernel's "echo" commands:
stty -F /dev/ttyUSB0 9600
echo -e '\xff\x01\x01' > /dev/ttyUSB0 // the relay 1 go ON.
stty -F /dev/ttyUSB0 9600
echo -e '\xff\x01\x00' > /dev/ttyUSB0 // the relay 1 go OFF.

Sample VB6 code :
Private Sub cmdOff_Click()
With MSComm1
'make sure the serial port is open
If .PortOpen = False Then .PortOpen = True
'send the data
.Output = Chr$(255)
.Output = Chr$(1)
.Output = Chr$(0)
End With 'MSComm1
End Sub
Private Sub cmdOn_Click()
With MSComm1
'make sure the serial port is open
If .PortOpen = False Then .PortOpen = True
'send the data
.Output = Chr$(255)
.Output = Chr$(1)
.Output = Chr$(1)
End With 'MSComm1
End Sub
Sample C# code :
private void button1_ON_Click(object sender, EventArgs e)
{
serialPort1.Write(new byte[] { 0xFF, 0x01, 0x01 }, 0, 3);
}
private void button1_OFF_Click(object sender, EventArgs e)
{
serialPort1.Write(new byte[] { 0xFF, 0x01, 0x00 }, 0, 3);
}


Any other Ideas?  I am not sure where to go from here....


Dave C-J

unread,
Sep 27, 2017, 3:34:20 AM9/27/17
to node...@googlegroups.com
aha - right so those snippets show two key things...
1) it's running at 9600 baud - so set the serialport to that speed.
2) the format needed is actually 3 bytes... 0xFF  0xXX  0xYY
so you need a function that creates a buffer like that...

   msg.payload = Buffer.from([0xff, 0x01, 0x01]);
  return msg

etc

TAllen-t

unread,
Oct 3, 2017, 6:25:43 PM10/3/17
to Node-RED
I Finally got it working by installing crelay. 

then adding an an "exec" node and passing:

crelay [-s <serial number>] -i | [<relay number>] [ON|OFF] as msg.payload

so if I want relay 5 on I just have to pass....

msg.payload = "crelay5 on"


Dave C-J

unread,
Oct 3, 2017, 6:29:39 PM10/3/17
to node...@googlegroups.com
great - thanks for providing the answer.
Reply all
Reply to author
Forward
0 new messages