You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.0 KiB
Python

3 years ago
#!python
#
# Bluetooth server thread
# Monitor the Bluetooth connection and receive commands
#
# 2021 - PRESENT Zhengyu Peng
3 years ago
# Website: https://zpeng.me
#
# ` `
# -:. -#:
# -//:. -###:
# -////:. -#####:
# -/:.://:. -###++##:
# .. `://:- -###+. :##:
# `:/+####+. :##:
# .::::::::/+###. :##:
# .////-----+##: `:###:
# `-//:. :##: `:###/.
# `-//:. :##:`:###/.
# `-//:+######/.
# `-/+####/.
# `+##+.
# :##:
# :##:
# :##:
# :##:
# :##:
# .+:
import socket
from threading import Thread
import json
import os
3 years ago
class BluetoothServer(Thread):
ERROR = -1
LISTEN = 1
CONNECTED = 2
STOP = 3
SIG_NORMAL = 0
SIG_STOP = 1
SIG_DISCONNECT = 2
def __init__(self, out_cmd_queue):
Thread.__init__(self)
self.cmd_queue = out_cmd_queue
with open('/home/pi/hexapod/software/raspberry pi/config.json', 'r') as read_file:
3 years ago
self.config = json.load(read_file)
stream = os.popen('hciconfig hci0')
output = stream.read()
device_id = "hci0"
bt_mac = output.split("{}:".format(device_id))[1].split(
"BD Address: ")[1].split(" ")[0].strip()
self.mac = bt_mac
self.port = 10
3 years ago
self.bt_socket = socket.socket(
socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
self.signal = self.SIG_NORMAL
def run(self):
try:
self.bt_socket.bind((self.mac, self.port))
self.bt_socket.listen(1)
print('TCP listening')
except OSError as err:
# print('emit tcp server error')
# self.status.emit(self.STOP, '')
pass
else:
while True:
self.cmd_queue.put('standby:')
3 years ago
# Wait for a connection
# print('wait for a connection')
# self.status.emit(self.LISTEN, '')
try:
self.connection, addr = self.bt_socket.accept()
# self.connection.setblocking(False)
# self.connection.settimeout(1)
print('New connection')
except socket.timeout as t_out:
pass
else:
while True:
# print('waiting for data')
# if self.signal == self.SIG_NORMAL:
try:
data = self.connection.recv(4096)
except socket.error as e:
print(e)
break
else:
if data:
print(data.decode())
3 years ago
self.cmd_queue.put(data.decode())
else:
break
finally:
self.bt_socket.close()
self.cmd_queue.put('standby:')
3 years ago
print('exit')