Code: Select all
#!/usr/bin/python
# Xaver's TCP server. Runs on the Raspberry Pi.
import socket # Import socket module
s = socket.socket() # Create a socket object
host = '75.144.181.xxx' # Get local machine name
port = 109xx # Reserve a port for your service.
import serial
DEVICE = '/dev/ttyUSB0' # the arduino serial interface (use dmesg when connecting)
BAUD = 38400
ser = serial.Serial(DEVICE, BAUD)
print 'Server started!'
print 'Waiting for clients...'
s.bind((host, port)) # Bind to the port
s.listen(5) # Now wait for client connection.
c, addr = s.accept() # Establish connection with client.
print 'Got connection from', addr
while True:
msg = c.recv(2) # get 2 bytes from the TCP connection
ser.write(msg) # write the 2 bytes to the serial interfaceCode: Select all
pi@raspberrypi ~/Desktop $ sudo python tcpserv
Server started!
Waiting for clients...
Traceback (most recent call last):
File "tcpserv", line 18, in <module>
s.bind((host, port)) # Bind to the port
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 99] Cannot assign requested address
