ok so here is a code snippit you can paste into a python script and run.
You need to change the IP_ADDRESS variable to have the ip address of the relay board
If you have your device set up to use a password then you will need to change PASSWORD to be the password you are using
once you run the script it should display a new application window and there will be buttons for each of the eight relays. You can toggle the buttons on and off and it will turn a relay on and off.. what i am interested in is the data that gets spit out into the log when you do this. you will need to copy that data and paste it here for me to check out. we need to know what the data is that is being returned from the get command
Code: Select all
IP_ADDRESS = '192.168.1.1'
PASSWORD = 'SOME_PASSWORD'
import wx
import socket
from threading import Timer
def relay_property(func):
relay_num = int(func.__name__[-1:])
def fget(self):
response = self.send('\x24')
return response
def fset(self, state):
if state:
command = [0x20]
else:
command = [0x21]
command += [relay_num, 0x0]
command = ''.join(chr(c) for c in command)
response = self.send(command)
if response is None:
return
if ord(response):
print 'Command for relay {0} failed'.format(relay_num)
else:
print 'Command for relay {0} success'.format(relay_num)
return property(fget=fget, fset=fset)
class ETH008(object):
def __init__(self, ip):
self.sock = None
self.ip = ip
self.timer = None
self.password = None
def connect(self):
try:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((self.ip, 17494))
return True
except socket.error:
return False
def logout(self):
if self.timer is not None:
self.timer.cancel()
self.sock.send('\x7B')
self.sock.shutdown(socket.SHUT_RDWR)
self.sock.close()
@relay_property
def relay1(self):
pass
@relay_property
def relay2(self):
pass
@relay_property
def relay3(self):
pass
@relay_property
def relay4(self):
pass
@relay_property
def relay5(self):
pass
@relay_property
def relay6(self):
pass
@relay_property
def relay7(self):
pass
@relay_property
def relay8(self):
pass
def _login(self, password, delay):
try:
self.sock.sendall('\x79' + password)
except (socket.error, AttributeError):
if self.connect():
self.sock.sendall('\x79' + password)
else:
print 'Socket Connection problem'
return
self.timer = Timer(delay, self._login, args=[password, 30.0])
self.timer.start()
def login(self, password=None):
try:
self.sock.sendall('\x7A')
except (socket.error, AttributeError):
if self.connect():
self.sock.sendall('\x7A')
else:
print 'Socket Connection problem'
return False
data = self.sock.recv(4096)
if not data:
print 'Socket Connection closed'
return False
if ord(data) != 255:
if password is None:
print 'Password is needed to login'
return False
data = ord(data)
if data == 0:
data = 30
self._login(password, data)
self.password = password
else:
if self.timer is not None:
self.timer.cancel()
self.timer = None
self.password = None
return True
def send(self, command):
try:
self.sock.sendall(command)
except (socket.error, AttributeError):
if self.login(self.password):
self.sock.sendall(command)
else:
print 'Socket Connection problem'
return None
data = self.sock.recv(4096)
if not data:
print 'Socket Connection closed'
return None
print ', '.join(hex(ord(char)) for char in list(data))
return data
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, size=(300, 100))
eth008 = ETH008(IP_ADDRESS)
eth008.login(PASSWORD)
sizer = wx.BoxSizer(wx.HORIZONTAL)
from wx.lib import buttons
relay1 = buttons.GenToggleButton(self, -1, 'Relay 1')
def on_relay_1(evt):
eth008.relay1 = relay1.GetToggle()
print eth008.relay1
relay1.Bind(wx.EVT_BUTTON, on_relay_1)
sizer.Add(relay1, 0, wx.EXPAND | wx.ALL, 5)
relay2 = buttons.GenToggleButton(self, -1, 'Relay 2')
def on_relay_2(evt):
eth008.relay2 = relay2.GetToggle()
print eth008.relay2
relay2.Bind(wx.EVT_BUTTON, on_relay_2)
sizer.Add(relay2, 0, wx.EXPAND | wx.ALL, 5)
relay3 = buttons.GenToggleButton(self, -1, 'Relay 3')
def on_relay_3(evt):
eth008.relay3 = relay3.GetToggle()
print eth008.relay3
relay3.Bind(wx.EVT_BUTTON, on_relay_3)
sizer.Add(relay3, 0, wx.EXPAND | wx.ALL, 5)
relay4 = buttons.GenToggleButton(self, -1, 'Relay 4')
def on_relay_4(evt):
eth008.relay4 = relay4.GetToggle()
print eth008.relay4
relay4.Bind(wx.EVT_BUTTON, on_relay_4)
sizer.Add(relay4, 0, wx.EXPAND | wx.ALL, 5)
relay5 = buttons.GenToggleButton(self, -1, 'Relay 5')
def on_relay_5(evt):
eth008.relay5 = relay5.GetToggle()
print eth008.relay5
relay5.Bind(wx.EVT_BUTTON, on_relay_5)
sizer.Add(relay5, 0, wx.EXPAND | wx.ALL, 5)
relay6 = buttons.GenToggleButton(self, -1, 'Relay 6')
def on_relay_6(evt):
eth008.relay6 = relay6.GetToggle()
print eth008.relay6
relay6.Bind(wx.EVT_BUTTON, on_relay_6)
sizer.Add(relay6, 0, wx.EXPAND | wx.ALL, 5)
relay7 = buttons.GenToggleButton(self, -1, 'Relay 7')
def on_relay_7(evt):
eth008.relay7 = relay7.GetToggle()
print eth008.relay7
relay7.Bind(wx.EVT_BUTTON, on_relay_7)
sizer.Add(relay7, 0, wx.EXPAND | wx.ALL, 5)
relay8 = buttons.GenToggleButton(self, -1, 'Relay 8')
def on_relay_8(evt):
eth008.relay8 = relay8.GetToggle()
print eth008.relay8
relay8.Bind(wx.EVT_BUTTON, on_relay_8)
sizer.Add(relay8, 0, wx.EXPAND | wx.ALL, 5)
self.SetSizer(sizer)
def on_close(evt):
eth008.logout()
evt.Skip()
self.Bind(wx.EVT_CLOSE, on_close)
self.Show()
if hasattr(eg.globals, 'frame'):
try:
eg.globals.frame.Destroy()
except:
pass
eg.globals.frame = Frame()