Guest User

Sainsmart relay 8 python

a guest
Apr 3rd, 2015
1,021
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from socket import *
  2. import sys
  3.  
  4. # Sainsmart relay board 8 channels with remote networking shield, python code. https://codecardinal.wordpress.com/2015/03/31/sainsmart-8-channel-relay-controller-python-linux-windows/
  5. if len(sys.argv) != 3:
  6.    sys.exit(2)
  7.  
  8. relay =  str(sys.argv[1])
  9. state =  str(sys.argv[2])
  10. data_send = "FD0220"
  11.  
  12. if int(relay) <= 8:
  13.    data_send += "0" + relay
  14.    if int(state) == 0:
  15.       data_send += "00"
  16.    elif int(state) == 1:
  17.       data_send += "01"
  18.      
  19. elif int(relay) == 9:
  20.    data_send += "F8"
  21.    if int(state) == 0:
  22.       data_send += "80"
  23.    elif int(state) == 1:
  24.       data_send += "88"
  25.  
  26. data_send += "5D"
  27.  
  28. s = socket(AF_INET, SOCK_STREAM)
  29. s.connect(("192.168.1.4", 30000))
  30. s.send(data_send.decode('hex'))
  31. data = s.recv(4)
  32. print(data.encode('hex'))
RAW Paste Data