Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import sys
- import json
- import RPi.GPIO as GPIO
- import time
- GPIO.setmode(GPIO.BOARD)
- # Create a TCP/IP socket
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- # Bind the socket to the port
- server_address = ('localhost', 10000)
- print >>sys.stderr, 'starting up on %s port %s' % server_address
- sock.bind(server_address)
- # Listen for incoming connections
- json_received=b''
- sock.listen(1)
- while True:
- # Wait for a connection
- print >>sys.stderr, 'waiting for a connection'
- connection, client_address = sock.accept()
- try:
- print >>sys.stderr, 'connection from', client_address
- # Receive the data in small chunks and retransmit it
- while True:
- data = connection.recv(16)
- print >>sys.stderr, 'received "%s"' % data
- if data:
- json_received+=data
- time.sleep(0.01)
- print >>sys.stderr, 'sending data back to the client'
- connection.sendall(data)
- time.sleep(0.01)
- else:
- print >>sys.stderr, 'no more data from', client_address
- break
- finally:
- time.sleep(0.01)
- parsed_json=json.loads(json_received)
- print(parsed_json[9:11])
- if(parsed_json[23:27] == 'HIGH'):
- GPIO.setup(int(parsed_json[9:11]),GPIO.OUT)
- GPIO.output(int(parsed_json[9:11]),True)
- time.sleep(5)
- print(parsed_json)
- print(json_received)
- GPIO.cleanup()
- connection.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement