mirror of
https://github.com/DanielPollithy/bluetooth_drone.git
synced 2025-10-16 11:45:38 +00:00
activate_bluetooth_discovery on startup
This commit is contained in:
parent
485e6dadb6
commit
33b091e6ea
23
client.py
23
client.py
@ -4,6 +4,8 @@ import bluetooth
|
||||
import time
|
||||
import json
|
||||
|
||||
from subprocess import Popen, PIPE
|
||||
|
||||
import settings
|
||||
|
||||
settings.activate_bluetooth_discovery()
|
||||
@ -12,7 +14,7 @@ print('This devices bluetooth address is: {}'.format(bt_mac))
|
||||
|
||||
|
||||
def protocol(address):
|
||||
payload = json.dumps({'addr': '0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae'})
|
||||
payload = json.dumps({'addr': settings.CLIENT_ETHEREUM_ADDRESS})
|
||||
assert len(payload) < 1024
|
||||
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
|
||||
|
||||
@ -54,8 +56,23 @@ def protocol(address):
|
||||
print('Closing connection.')
|
||||
sock.close()
|
||||
|
||||
# do some ethereum logic here with
|
||||
# server_ethereum_address
|
||||
# ETHEREUM
|
||||
# now start the transaction
|
||||
p = Popen(
|
||||
['node', 'start_charging.js', settings.CLIENT_ETHEREUM_ADDRESS],
|
||||
stdin=PIPE,
|
||||
stdout=PIPE,
|
||||
stderr=PIPE
|
||||
)
|
||||
output, err = p.communicate()
|
||||
returncode = p.returncode
|
||||
|
||||
if returncode != 0:
|
||||
sock.send(json.dumps({'start_charging': False}))
|
||||
print('There was an error in the ethereum start charging logic')
|
||||
print('Closing connection.')
|
||||
sock.close()
|
||||
|
||||
sock.send(json.dumps({'start_charging': True}))
|
||||
|
||||
# the server will activate the relais now
|
||||
|
||||
4
drone.py
Normal file
4
drone.py
Normal file
@ -0,0 +1,4 @@
|
||||
# run two threads
|
||||
# 1) the bluetooth connector
|
||||
# 2) the website listener
|
||||
|
||||
7
end_charging.js
Normal file
7
end_charging.js
Normal file
@ -0,0 +1,7 @@
|
||||
// 1) get commandline arguments:
|
||||
// - the drone's ethereum address,
|
||||
// - the station's eth address,
|
||||
// - the start of the timeslot in seconds (unix time)
|
||||
// 2) interact with the booking function of the station's contract
|
||||
// 3) wait for the new booking event
|
||||
// 4) Return 0 if booking is o.k., return 1 if not
|
||||
@ -0,0 +1,8 @@
|
||||
[
|
||||
{
|
||||
"booking": "1",
|
||||
"drone_address": "0x772dcb53b59fc61410aa0514bebce8a9bb1e8ed6",
|
||||
"station_address": "0x",
|
||||
"start_time": "1314112131321"
|
||||
}
|
||||
]
|
||||
@ -0,0 +1 @@
|
||||
[]
|
||||
11
http/drone.py
Normal file
11
http/drone.py
Normal file
@ -0,0 +1,11 @@
|
||||
# this file checks in a given interval a http endpoint for booking the drone should make
|
||||
|
||||
import urllib
|
||||
import json
|
||||
import settings
|
||||
|
||||
while True:
|
||||
url = 'http://localhost:8080/drone/{}/bookings'.format(settings.CLIENT_ETHEREUM_ADDRESS)
|
||||
response = urllib.urlopen(url)
|
||||
data = json.loads(response.read())
|
||||
print data
|
||||
11
http/server.py
Normal file
11
http/server.py
Normal file
@ -0,0 +1,11 @@
|
||||
import SimpleHTTPServer
|
||||
import SocketServer
|
||||
|
||||
PORT = 8080
|
||||
|
||||
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
|
||||
|
||||
httpd = SocketServer.TCPServer(("", PORT), Handler)
|
||||
|
||||
print "serving at port", PORT
|
||||
httpd.serve_forever()
|
||||
141
make_booking.js
Normal file
141
make_booking.js
Normal file
@ -0,0 +1,141 @@
|
||||
var process = require('process');
|
||||
|
||||
if (process.argv.length < 4) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 1) get commandline argument: Is the drone's ethereum address
|
||||
var drone_eth_address = process.argv[2];
|
||||
var station_eth_address = process.argv[3];
|
||||
|
||||
|
||||
|
||||
var Web3 = require('web3');
|
||||
|
||||
var settings = require('./settings');
|
||||
|
||||
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
|
||||
|
||||
web3.personal.unlockAccount(drone_eth_address, "123", 150000);
|
||||
|
||||
|
||||
//'0x772dcb53b59fc61410aa0514bebce8a9bb1e8ed6'
|
||||
|
||||
var contractAddress = '0x3f629cee69c94b4e83b3b98ac129022a26ccc478';
|
||||
|
||||
var ABI = [{
|
||||
"constant": true,
|
||||
"inputs": [],
|
||||
"name": "chargingPrice",
|
||||
"outputs": [{"name": "", "type": "uint256"}],
|
||||
"payable": false,
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
}, {
|
||||
"constant": false,
|
||||
"inputs": [],
|
||||
"name": "register",
|
||||
"outputs": [],
|
||||
"payable": false,
|
||||
"stateMutability": "nonpayable",
|
||||
"type": "function"
|
||||
}, {
|
||||
"constant": false,
|
||||
"inputs": [],
|
||||
"name": "withdraw",
|
||||
"outputs": [],
|
||||
"payable": false,
|
||||
"stateMutability": "nonpayable",
|
||||
"type": "function"
|
||||
}, {
|
||||
"constant": false,
|
||||
"inputs": [],
|
||||
"name": "stopCharging",
|
||||
"outputs": [],
|
||||
"payable": false,
|
||||
"stateMutability": "nonpayable",
|
||||
"type": "function"
|
||||
}, {
|
||||
"constant": false,
|
||||
"inputs": [{"name": "_chargingPrice", "type": "uint256"}],
|
||||
"name": "setChargingPrice",
|
||||
"outputs": [],
|
||||
"payable": false,
|
||||
"stateMutability": "nonpayable",
|
||||
"type": "function"
|
||||
}, {
|
||||
"constant": false,
|
||||
"inputs": [],
|
||||
"name": "startCharging",
|
||||
"outputs": [],
|
||||
"payable": false,
|
||||
"stateMutability": "nonpayable",
|
||||
"type": "function"
|
||||
}, {
|
||||
"constant": true,
|
||||
"inputs": [{"name": "", "type": "address"}],
|
||||
"name": "refund",
|
||||
"outputs": [{"name": "", "type": "uint256"}],
|
||||
"payable": false,
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
}, {"inputs": [], "payable": false, "stateMutability": "nonpayable", "type": "constructor"}, {
|
||||
"anonymous": false,
|
||||
"inputs": [{"indexed": false, "name": "_drone", "type": "address"}, {
|
||||
"indexed": false,
|
||||
"name": "result",
|
||||
"type": "bool"
|
||||
}],
|
||||
"name": "Registered",
|
||||
"type": "event"
|
||||
}, {
|
||||
"anonymous": false,
|
||||
"inputs": [{"indexed": false, "name": "_station", "type": "address"}, {
|
||||
"indexed": false,
|
||||
"name": "_drone",
|
||||
"type": "address"
|
||||
}],
|
||||
"name": "ChargingStarts",
|
||||
"type": "event"
|
||||
}, {
|
||||
"anonymous": false,
|
||||
"inputs": [{"indexed": false, "name": "_station", "type": "address"}, {
|
||||
"indexed": false,
|
||||
"name": "_drone",
|
||||
"type": "address"
|
||||
}],
|
||||
"name": "ChargingStopped",
|
||||
"type": "event"
|
||||
}];
|
||||
|
||||
var contract = web3.eth.contract(ABI).at(station_eth_address);
|
||||
|
||||
contract.register({from: drone_eth_address}, (e, r) => {
|
||||
console.log(e,r);
|
||||
var registered = contract.Registered();
|
||||
registered.watch(function(error, result){
|
||||
console.log(error, result);
|
||||
var addr_drone = result.args["_drone"];
|
||||
var success = result.args["result"];
|
||||
if (addr_drone == drone_eth_address) {
|
||||
console.log("This is my booking");
|
||||
if (success) {
|
||||
console.log("Booking successful");
|
||||
process.exit(0);
|
||||
} else {
|
||||
console.log("Booking NOT successful");
|
||||
process.exit(1);
|
||||
}
|
||||
} else {
|
||||
console.log("NOT my booking");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -88,7 +88,7 @@ def run(server_sock):
|
||||
print('FINISHED charging')
|
||||
client_sock.send(json.dumps({'electricity': '10W'}))
|
||||
else:
|
||||
print('NO electricity wanted')
|
||||
print('NO electricity wanted or blockchain problem')
|
||||
|
||||
print('END: regularly closing the connection')
|
||||
client_sock.close()
|
||||
|
||||
3
settings.js
Normal file
3
settings.js
Normal file
@ -0,0 +1,3 @@
|
||||
module.exports = {
|
||||
'eth_address_': ''
|
||||
};
|
||||
13
settings.py
13
settings.py
@ -4,6 +4,12 @@ HIKEY_BT_ADDRESS = '98:7B:F3:19:FE:57'
|
||||
RASPY_BT_ADDRESS = 'B8:27:EB:76:09:0B'
|
||||
|
||||
|
||||
# for demo purpose
|
||||
DEMO = False
|
||||
|
||||
|
||||
WEBSITE_POLLING_SLEEP = 1 # seconds
|
||||
|
||||
BT_SLEEP = 0.01 # seconds
|
||||
DISTANCE_SLEEP = 0.1
|
||||
|
||||
@ -15,8 +21,11 @@ CHARGING_TIME = 10
|
||||
DRONE_REJECTED_RESTART_TIME = 10
|
||||
|
||||
# eth
|
||||
CLIENT_ETHEREUM_ADDRESS = '0xde0b2sdsdsddddddddddddddddddddddddddddde'
|
||||
SERVER_ETHEREUM_ADDRESS = '0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae'
|
||||
# this is the drone
|
||||
CLIENT_ETHEREUM_ADDRESS = '0x772dcb53b59fc61410aa0514bebce8a9bb1e8ed6'
|
||||
|
||||
# this is the station contract
|
||||
SERVER_ETHEREUM_ADDRESS = '0xc35719829b3a08211f181e22a3d93d79f4fa3ab9'
|
||||
|
||||
RSSI_DISTANCE = 20
|
||||
MAX_RSSI_TRY_COUNT = 10000
|
||||
|
||||
112
start_charging.js
Normal file
112
start_charging.js
Normal file
@ -0,0 +1,112 @@
|
||||
|
||||
// 2) interact with the startCharging contract function
|
||||
// 3) wait for the Start charging event
|
||||
// 4) Return 0 if booking is o.k., return 1 if not
|
||||
|
||||
// 1) get commandline arguments:
|
||||
// - the drone's ethereum address,
|
||||
// - the station's eth address,
|
||||
// - the start of the timeslot in seconds (unix time)
|
||||
// 2) interact with the booking function of the station's contract
|
||||
// 3) wait for the new booking event
|
||||
// 4) Return 0 if booking is o.k., return 1 if not
|
||||
|
||||
var process = require('process');
|
||||
|
||||
if (process.argv.length < 5) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 1) get commandline argument: Is the drone's ethereum address
|
||||
var drone_eth_address = process.argv[2];
|
||||
var station_eth_address = process.argv[3];
|
||||
var timeslot_start = process.argv[4];
|
||||
|
||||
console.log(drone_eth_address, station_eth_address, timeslot_start);
|
||||
|
||||
var Web3 = require('web3');
|
||||
|
||||
var settings = require('./settings');
|
||||
|
||||
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
|
||||
|
||||
web3.personal.unlockAccount(web3.eth.accounts[2], "123", 150000);
|
||||
|
||||
|
||||
//'0x772dcb53b59fc61410aa0514bebce8a9bb1e8ed6'
|
||||
|
||||
var contractAddress = '0x3f629cee69c94b4e83b3b98ac129022a26ccc478';
|
||||
|
||||
var ABI = [{
|
||||
"constant": false,
|
||||
"inputs": [],
|
||||
"name": "addDrone",
|
||||
"outputs": [],
|
||||
"payable": false,
|
||||
"stateMutability": "nonpayable",
|
||||
"type": "function"
|
||||
}, {
|
||||
"constant": true,
|
||||
"inputs": [],
|
||||
"name": "getKeys",
|
||||
"outputs": [{
|
||||
"name": "",
|
||||
"type": "address[]"
|
||||
}],
|
||||
"payable": false,
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
}, {
|
||||
"constant": true,
|
||||
"inputs": [{
|
||||
"name": "",
|
||||
"type": "address"
|
||||
}],
|
||||
"name": "register",
|
||||
"outputs": [{
|
||||
"name": "",
|
||||
"type": "uint256"
|
||||
}],
|
||||
"payable": false,
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
}, {
|
||||
"constant": false,
|
||||
"inputs": [],
|
||||
"name": "removeDrone",
|
||||
"outputs": [],
|
||||
"payable": false,
|
||||
"stateMutability": "nonpayable",
|
||||
"type": "function"
|
||||
}, {
|
||||
"inputs": [],
|
||||
"payable": false,
|
||||
"stateMutability": "nonpayable",
|
||||
"type": "constructor"
|
||||
}, {
|
||||
"anonymous": false,
|
||||
"inputs": [{
|
||||
"indexed": false,
|
||||
"name": "",
|
||||
"type": "address"
|
||||
}],
|
||||
"name": "DroneAdded",
|
||||
"type": "event"
|
||||
}, {
|
||||
"anonymous": false,
|
||||
"inputs": [{
|
||||
"indexed": false,
|
||||
"name": "",
|
||||
"type": "address"
|
||||
}],
|
||||
"name": "DroneRemoved",
|
||||
"type": "event"
|
||||
}];
|
||||
|
||||
var drs = web3.eth.contract(ABI).at(contractAddress);
|
||||
|
||||
drs.addDrone({from: web3.eth.accounts[2]}, (e, r) => {
|
||||
console.log(e,r)
|
||||
});
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user