Control ethernet relays relays with http commands

I need help finding the way to control the relays from within python.
Are there http commands to turn off and on relays
I want to use four 24 channel and one 16 channel relay

Hi,

The best way to control the relays programmatically is to use a TCP socket and send commands to the board. We have a Python library that should help: https://github.com/ncd-io/Industrial-Relay-Control

Thank you Jacob, I appreciate the reply

Hi Jacob and Siekertech!
I wanted to continue this discussion with something I found. So I used the Python library as so:
#####################################################################################
#import the pyserial module
import socket
import ncd_industrial_relay
#set up your socket with the desired settings.
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#instantiate the board object and pass it the network socket
board1 = ncd_industrial_relay.Relay_Controller(sock)
#connect the socket using desired IP and Port
IP_ADDRESS = “192.168.1.74”
PORT = 2101
sock.connect((IP_ADDRESS, PORT))
sock.settimeout(.5)
print board1.turn_on_relay_by_index(1)

sock.close()

##############################################################################

But then I get this error.

pi@raspberrypi:~/Industrial-Relay-Control $ python trynewrelay.py
Traceback (most recent call last):
File “trynewrelay.py”, line 13, in
board1.turn_on_relay_by_index(2)
File “/home/pi/Industrial-Relay-Control/ncd_industrial_relay.py”, line 21, in turn_on_relay_by_index
return self.process_control_command_return(self.send_command(command, 4))
File “/home/pi/Industrial-Relay-Control/ncd_industrial_relay.py”, line 122, in send_command
self.combus.send(command)
TypeError: send() argument 1 must be string or buffer, not list

Jacob, Have you seen this error before sir?

Please let me know if there is something I need to change to get this working.

Oh by the way. I used index 2 when I got the error. You will notice there is a different index in the code and then in the error.

Oh and I also must mention that the line that the line under the send_command function is commented out in the source code.
I wonder if I am using a different version of python where the “send” command does not accept lists.
Just thinking out loud.

Hi,

It could very well be a problem with the Python version or perhaps an issue of socket vs serial port.

What version of python are you running and I’ll troubleshoot on my end. Also if you uncomment that line does the command work no problem?

Originally that was uncommented, but people were running into issues with that line and I couldn’t remember why I put it in there.

Thank you again for your quick reply!
So this is what happened now:
#####################################################################################
pi@raspberrypi:~ $ cd Industrial-Relay-Control
pi@raspberrypi:~/Industrial-Relay-Control $ python trynewrelay.py
[170, 4, 254, 48, 1, 0, 221]
Traceback (most recent call last):
File “trynewrelay.py”, line 13, in
board1.turn_on_relay_by_index(2)
File “/home/pi/Industrial-Relay-Control/ncd_industrial_relay.py”, line 21, in turn_on_relay_by_index
return self.process_control_command_return(self.send_command(command, 4))
File “/home/pi/Industrial-Relay-Control/ncd_industrial_relay.py”, line 123, in send_command
self.combus.send(command)
TypeError: send() argument 1 must be string or buffer, not list
pi@raspberrypi:~/Industrial-Relay-Control $ sudo nano ncd_industrial_relay.py
pi@raspberrypi:~/Industrial-Relay-Control $ python trynewrelay.py
▒▒0▒
Traceback (most recent call last):
File “trynewrelay.py”, line 13, in
board1.turn_on_relay_by_index(2)
File “/home/pi/Industrial-Relay-Control/ncd_industrial_relay.py”, line 21, in turn_on_relay_by_index
return self.process_control_command_return(self.send_command(command, 4))
File “/home/pi/Industrial-Relay-Control/ncd_industrial_relay.py”, line 124, in send_command
return self.combus.recv(bytes_back)
socket.timeout: timed out

####################################################################################

So I did one try with the line commented and one without.
I also added a quick statement to print what data was being sent to the relay board.
Below is my python version.

pi@raspberrypi:~/Industrial-Relay-Control $ python --version
Python 2.7.13
pi@raspberrypi:~/Industrial-Relay-Control $ python3 --version
Python 3.5.3
pi@raspberrypi:~/Industrial-Relay-Control $

Thank you Jacob!

It looks like the first one threw an error while the socket was open and didn’t close it gracefully. So then the second iteration couldn’t open a socket because the board thought it was still connected to the first socket. Try power cycling the board and see if the second iteration of the script works.

Thanks for the new information.
I went ahead and kept the line uncommented and then ran the file again.

I got the same error, but I do see the that the raspberry pi and the relay board are talking to eachother as the green light flashes whenever I send a command.

I even tried to remove the timeout and it just runs for a few seconds and then the process ends.

pi@raspberrypi:~/Industrial-Relay-Control $ python trynewrelay.py
▒▒0▒
Traceback (most recent call last):
File “trynewrelay.py”, line 13, in
board1.turn_on_relay_by_index(2)
File “/home/pi/Industrial-Relay-Control/ncd_industrial_relay.py”, line 21, in turn_on_relay_by_index
return self.process_control_command_return(self.send_command(command, 4))
File “/home/pi/Industrial-Relay-Control/ncd_industrial_relay.py”, line 124, in send_command
return self.combus.recv(bytes_back)
socket.timeout: timed out

Let me know if there are any other things I can try.
Thank you for your patience!

Hi,

The board isn’t sending the expected command back. Can you let me know what version of Python and on what OS you’re running this?

What board are you using this with?

Hi Jacob
We are using 1 of the 16 channel and 4 of the 24 channel
https://relaypros.com/Relay/Relay/CAT_RELAY16_ETHERNET
https://relaypros.com/Relay/Relay/CAT_RELAY24_ETHERNET
The OS is Raspian which has Python 3.5.3

When we talked to the RelayPros guys, we specifically asked for relays that had the ability work with python

Yeh so far they definitely work together, but the issue is that the relays will not turn on and the socket timeout is reached.
I wonder if there is something else we aren’t thinking about here.
The raspberry pi is rasbian stretch and the raspberry pi is a 3b+

I’m going to run some tests on my end. It should work, can you try controlling that same board using Base Station (ncd.io/start) from a windows machine to see if that software can connect?

Ok all!
Glorious day.
I was using a privileged port for linux that was not the same as the relay board.
I was able to open the socket, change the state of a relay, and close the socket with the ncd provided python program.

In the process though I accidentally changed the http port and now I can no longer access the device.
I read on how to restore the device to factory settings but I do not understand how.
Does anyone know how to do this?

Okay I read the manual really closely and I finally got my program too work.
Thanks Jacob and siekertech!
Here is one more problem I found. I am controlling five relay boards at once.
The code is below:

#################################################################################

#import the pyserial module
import socket
import ncd_industrial_relay
import time
#set up your socket with the desired settings.
sock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#instantiate the board object and pass it the network socket
board1 = ncd_industrial_relay.Relay_Controller(sock1)
#connect the socket using desired IP and Port
IP_ADDRESS1 = “192.168.1.95”
PORT1 = 10000
sock1.connect((IP_ADDRESS1, PORT1))
sock1.settimeout(.5)
board1.turn_on_relay_by_index(1)
time.sleep(5)
board1.turn_off_relay_by_index(1)
sock1.close()

#set up your socket with the desired settings.
sock2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#instantiate the board object and pass it the network socket
board2 = ncd_industrial_relay.Relay_Controller(sock2)
#connect the socket using desired IP and Port
IP_ADDRESS2 = “192.168.1.93”
PORT2 = 5000
sock2.connect((IP_ADDRESS2, PORT2))
sock2.settimeout(.5)
board2.turn_on_relay_by_index(5)
time.sleep(5)
board2.turn_off_relay_by_index(5)
sock2.close()

#set up your socket with the desired settings.
sock3 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#instantiate the board object and pass it the network socket
board3 = ncd_industrial_relay.Relay_Controller(sock3)
#connect the socket using desired IP and Port
IP_ADDRESS3 = “192.168.1.96”
PORT3 = 11000
sock3.connect((IP_ADDRESS3, PORT3))
sock3.settimeout(.5)
board3.turn_on_relay_by_index(1)
time.sleep(5)
board3.turn_off_relay_by_index(1)
sock3.close()

#set up your socket with the desired settings.
sock4 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#instantiate the board object and pass it the network socket
board4 = ncd_industrial_relay.Relay_Controller(sock4)
#connect the socket using desired IP and Port
IP_ADDRESS4 = “192.168.1.92”
PORT4 = 2101
sock4.connect((IP_ADDRESS4, PORT4))
sock4.settimeout(.5)
board4.turn_on_relay_by_index(5)
time.sleep(5)
board4.turn_off_relay_by_index(5)
sock4.close()

#set up your socket with the desired settings.
sock5 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#instantiate the board object and pass it the network socket
board5 = ncd_industrial_relay.Relay_Controller(sock5)
#connect the socket using desired IP and Port
IP_ADDRESS5 = “192.168.1.94”
PORT5 = 6000
sock5.connect((IP_ADDRESS5, PORT5))
sock5.settimeout(.5)
board5.turn_on_relay_by_index(5)
time.sleep(5)
board5.turn_off_relay_by_index(5)
sock5.close()
#################################################################################
So there are times where this works and times it doesnt.
The error I get sometimes looks like this:

Traceback (most recent call last):
File “trynewrelay.py”, line 63, in
board4.turn_off_relay_by_index(5)
File “/home/pi/Industrial-Relay-Control/ncd_industrial_relay.py”, line 27, in turn_off_relay_by_index
return self.process_control_command_return(self.send_command(command, 4))
File “/home/pi/Industrial-Relay-Control/ncd_industrial_relay.py”, line 128, in process_control_command_return
bytes_back = self.check_bytes_back(data)
File “/home/pi/Industrial-Relay-Control/ncd_industrial_relay.py”, line 155, in check_bytes_back
return ord(data[1:2]) == (len(data)-3)
TypeError: ord() expected a character, but string of length 0 found

Jacob any ideas how to get rid of this?
It is a fifty fifty. Sometime my program runs all the way through and sometimes it throws this error.

Well its pretty rare to see this. A couple things I could think of to cause this issue.

One would be inductive loads. If you are switching a motor, heater, or anything that causes an inductive spike to go over the power line it can cause communication interruptions. You can mitigate this using capacitors, see this article: https://ncd.io/controlling-inductive-devices-managing-electromagnetic-interference/

The other thing I can think of is maybe the ethernet modules have a socket timeout set on them? This would tell the ethernet module to close the socket. Try closing and opening the socket before/after every command.

Hi Jacob. There are no inductive loads. These relays will be connected to elevator buttons. Only a control voltage

Is this for local network control?

Try increasing the socket timeout in the code. .5 seconds should be plenty, but sometimes the the network can be slow especially over the internet.

Do the ethernet modules have a socket timeout set on them?
If so try upping this

Sorry I thought I posted this yesterday.