-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfoo_server.py
52 lines (42 loc) · 1.09 KB
/
foo_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 08 19:42:15 2017
@author: John Smith
"""
import socket
import sys
import RPi.GPIO as GPIO
import foo_common as foo
address = "192.168.1.32"
port = 9999
duty_cycle = 100
# Prepare PWMs
GPIO.setmode(GPIO.BCM)
[GPIO.setup(pin, GPIO.OUT) for pin in foo.GPIOS]
PWMs = [GPIO.PWM(pin, duty_cycle) for pin in foo.GPIOS]
[pwm.start(0) for pwm in PWMs]
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the port
server_address = (address, port)
sock.bind(server_address)
# Listen for incoming connections
sock.listen(1)
while True:
# Wait for a connection
connection, client_address = sock.accept()
try:
data = connection.recv(128)
if data:
connection.sendall("1")
else:
connection.sendall("0")
data = "NULL"
vals = [int(x) for x in data.split(';')]
for i in range(6):
PWMs[i].ChangeDutyCycle(abs(vals[i]))
# print >> sys.stderr, data
finally:
# Clean up the connection
connection.close()
sock.close()