This commit is contained in:
Daniel Pollithy 2017-08-29 17:03:05 +02:00
parent d1eb91474a
commit 2e515b4c3b
4 changed files with 126 additions and 35 deletions

View File

@ -11,7 +11,8 @@ print('This devices bluetooth address is: {}'.format(bt_mac))
def protocol(address):
payload = json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
payload = json.dumps({'addr': '0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae'})
assert len(payload) < 1024
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
bt_addr = address
@ -21,25 +22,65 @@ def protocol(address):
sock.connect((bt_addr, port))
print("connected. type stuff")
data = 'hello from drone!'
sock.send(data)
print("connected. Sending: ", payload)
sock.send(payload)
# THE SERVER WILL ONLY REPLY WHEN THE BLUETOOTH DISTANCE IS CLOSE ENOUGH
# BUT THE SOCKET STAYS OPEN
data = sock.recv(1024)
print("Data received:", str(data))
try:
data = json.loads(data)
assert 'accepted' in data
connection_accepted = data['accepted']
if connection_accepted:
assert 'addr' in data
server_ethereum_address = data['addr']
else:
server_ethereum_address = False
except AssertionError:
print('Json is missing data')
sock.close()
raise StandardError
except:
print('Error reading json from the data')
sock.close()
raise StandardError
if not connection_accepted:
print('The server has not accepted the drone')
print('Closing connection.')
sock.close()
# do some ethereum logic here with
# server_ethereum_address
sock.send(json.dumps({'start_charging': True}))
# the server will activate the relais now
# the loading will take time and there could be some information exchanged here in the future
data = sock.recv(1024)
print("Data received:", str(data))
try:
data = json.loads(data)
assert 'electricity' in data
electricity = data['electricity']
except AssertionError:
print('Json is missing data')
sock.close()
raise StandardError
except:
print('Error reading json from the data')
sock.close()
raise StandardError
print('Energy consumption as hangar says: {}'.format(electricity))
print('Transaction done.')
print('Sending: "Ok... understood"')
sock.send('Ok... understood')
sock.close()
if __name__ == '__main__':
while True:
nearby_devices = bluetooth.discover_devices(lookup_names=True)
print("found %d devices" % len(nearby_devices))

0
data.py Normal file
View File

View File

@ -1,3 +1,5 @@
import json
import bluetooth
import time
from bt_proximity import BluetoothRSSI
@ -13,35 +15,77 @@ port = 0x1001
server_sock.bind(("", port))
server_sock.listen(1)
client_sock, address = server_sock.accept()
print("Accepted connection from ", address)
while True:
client_sock, address = server_sock.accept()
data = client_sock.recv(1024)
print("Data received: ", str(data))
# only accept the peer
if address[0] != settings.PEER_BT_ADDRESS:
client_sock.close()
distance = 0
try_counter = 0
print('Setting up BluetoothRSSI')
btrssi = BluetoothRSSI(addr=address[0])
while distance < settings.RSSI_DISTANCE and try_counter < settings.MAX_RSSI_TRY_COUNT:
try_counter += 1
distance = btrssi.get_rssi()
print('Distance to {} at try #{}\trssi={}'.format(address, try_counter, distance))
time.sleep(settings.DISTANCE_SLEEP)
print("Accepted connection from ", address)
data = client_sock.recv(1024)
print("Data received: ", str(data))
try:
data = json.loads(data)
assert 'addr' in data
client_ethereum_address = data['addr']
except AssertionError:
print('Json is missing data')
client_sock.close()
raise StandardError
except:
print('Error reading json from the data')
client_sock.close()
raise StandardError
distance = 0
try_counter = 0
print('Setting up BluetoothRSSI')
btrssi = BluetoothRSSI(addr=address[0])
while distance < settings.RSSI_DISTANCE and try_counter < settings.MAX_RSSI_TRY_COUNT:
try_counter += 1
distance = btrssi.get_rssi()
print('Distance to {} at try #{}\trssi={}'.format(address, try_counter, distance))
time.sleep(settings.DISTANCE_SLEEP)
if distance < settings.RSSI_DISTANCE:
print('The distance was too big to connect')
client_sock.send(json.dumps({'accepted': False}))
client_sock.close()
raise KeyboardInterrupt
# everything is o.k. The drone is close enough, so now we return the proceedings
payload = json.dumps({'accepted': True, 'addr': settings.SERVER_ETHEREUM_ADDRESS})
assert len(payload) < 1024
client_sock.send(payload)
# receive the last interaction
data = client_sock.recv(1024)
print("Data received:", str(data))
try:
data = json.loads(data)
assert 'start_charging' in data
start_charging = data['start_charging']
except AssertionError:
print('Json is missing data')
client_sock.close()
raise StandardError
except:
print('Error reading json from the data')
client_sock.close()
raise StandardError
if start_charging:
print('START THE CHARGING NOW')
# wait the charging time
time.sleep(settings.CHARGING_TIME)
client_sock.send(json.dumps({'electricity': '10W'}))
client_sock.close()
else:
print('NO electricity wanted')
client_sock.close()
if distance < settings.RSSI_DISTANCE:
print('The distance was too big to connect')
client_sock.send('Bad distance')
client_sock.close()
raise KeyboardInterrupt
# everything is o.k. The drone is close enough, so now we return the proceedings
client_sock.send('You are here my friend')
# receive the last interaction
data = client_sock.recv(1024)
print("Data received:", str(data))
client_sock.close()
server_sock.close()

View File

@ -6,6 +6,12 @@ PEER_BT_ADDRESS = HIKEY_BT_ADDRESS
BT_SLEEP = 0.01 # seconds
DISTANCE_SLEEP = 0.1
CHARGING_TIME = 10
# eth
CLIENT_ETHEREUM_ADDRESS = '0xde0b2sdsdsddddddddddddddddddddddddddddde'
SERVER_ETHEREUM_ADDRESS = '0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae'
RSSI_DISTANCE = 20
MAX_RSSI_TRY_COUNT = 10000