Server
The server works on the Raspberry Pi and can transmit camera data, ultrasonic data, etc. to the client, and it
can also receive commands from the client.
In the Server folder, there is a server.py file which contains main server code.
get_interface_ip() is used to get IP address of the native Raspberry Pi wlan0, without manually modifying the
code to set IP parameters.
StartTcpServer() is used to start the TCP service. The channel of port 5000 is mainly used to send and receive
commands between the client and the server. The channel of port 8000 is used for the server to transmit the
collected camera data to the client.
StopTcpServer() is used to stop the TCP service.
sendvideo() is used to sends the camera data.
Part of server code is as follows:
def get_interface_ip(self):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s',
"wlan0"[:15]))[20:24])
def StartTcpServer(self):
HOST=str(self.get_interface_ip())
self.server_socket1 = socket.socket()
self.server_socket1.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEPORT,1)
self.server_socket1.bind((HOST, 5000))
self.server_socket1.listen(1)
self.server_socket = socket.socket()
self.server_socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEPORT,1)
self.server_socket.bind((HOST, 8000))
self.server_socket.listen(1)
print('Server address: '+HOST)
def StopTcpServer(self):
try:
self.connection.close()
self.connection1.close()
except Exception , e:
print "No client connection"
def sendvideo(self):
try:
self.connection,self.client_address = self.server_socket.accept()
self.connection=self.connection.makefile('rb')
except:
pass