Advertisement
alexarcan

MS_server

Mar 26th, 2016
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.57 KB | None | 0 0
  1. import socket
  2. import sys
  3. import json
  4. import RPi.GPIO as GPIO
  5. import time
  6.  
  7. GPIO.setmode(GPIO.BOARD)
  8.  
  9. # Create a TCP/IP socket
  10. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  11. # Bind the socket to the port
  12. server_address = ('localhost', 10000)
  13.  
  14. print >>sys.stderr, 'starting up on %s port %s' % server_address
  15. sock.bind(server_address)
  16. # Listen for incoming connections
  17. json_received=b''
  18. sock.listen(1)
  19.  
  20. while True:
  21.     # Wait for a connection
  22.     print >>sys.stderr, 'waiting for a connection'
  23.     connection, client_address = sock.accept()
  24.     try:
  25.         print >>sys.stderr, 'connection from', client_address
  26.  
  27.         # Receive the data in small chunks and retransmit it
  28.         while True:
  29.             data = connection.recv(16)
  30.             print >>sys.stderr, 'received "%s"' % data
  31.             if data:
  32.                 json_received+=data
  33.                 time.sleep(0.01)
  34.                 print >>sys.stderr, 'sending data back to the client'
  35.                 connection.sendall(data)
  36.                 time.sleep(0.01)
  37.             else:
  38.                 print >>sys.stderr, 'no more data from', client_address
  39.                 break
  40.  
  41.     finally:
  42.         time.sleep(0.01)
  43.         parsed_json=json.loads(json_received)
  44.         print(parsed_json[9:11])
  45.         if(parsed_json[23:27] == 'HIGH'):
  46.                 GPIO.setup(int(parsed_json[9:11]),GPIO.OUT)
  47.                 GPIO.output(int(parsed_json[9:11]),True)
  48.                 time.sleep(5)
  49.         print(parsed_json)
  50.         print(json_received)
  51.         GPIO.cleanup()
  52.         connection.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement