I'm putting together some bits for occupancy detection. This is my first Python project and it's turning out a lot more involved than I'm probably capable of. This is the code I have:
This is my code:
Code: Select all
from bluepy.btle import Scanner, DefaultDelegate
import bluetooth
import MySQLdb
import mysql.connector
import sshtunnel
import time
import datetime
class ScanDelegate(DefaultDelegate):
def __init__(self):
DefaultDelegate.__init__(self)
def handleDiscovery(self, dev, isNewDev, isNewData):
ts = time.time()
score = 0
timestamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
if isNewDev:
if "c1:ed:da:df:55:46" in dev.addr:
sshtunnel.SSH_TIMEOUT = 30.0
sshtunnel.TUNNEL_TIMEOUT = 30.0
with sshtunnel.SSHTunnelForwarder(
('ssh.pythonanywhere.com'),
ssh_username='xxx', ssh_password='xxx',
remote_bind_address=('xxx.mysql.pythonanywhere-services.com', 3306)
) as tunnel:
connection = mysql.connector.connect(
user='xxx', password='xxx',
host='127.0.0.1', port=tunnel.local_bind_port,
database='xxx$hivedashboard',
)
cursor = connection.cursor()
status = "In"
score = score + 5
cursor.execute("UPDATE occupants SET status = %s, lastin = %s WHERE occupants.id = '1'", (status, timestamp))
connection.commit()
else:
sshtunnel.SSH_TIMEOUT = 30.0
sshtunnel.TUNNEL_TIMEOUT = 30.0
with sshtunnel.SSHTunnelForwarder(
('ssh.pythonanywhere.com'),
ssh_username='xxx', ssh_password='xxx',
remote_bind_address=('xxx.mysql.pythonanywhere-services.com', 3306)
) as tunnel:
connection = mysql.connector.connect(
user='xxx', password='xxx',
host='127.0.0.1', port=tunnel.local_bind_port,
database='xxx$hivedashboard',
)
cursor = connection.cursor()
status = "Out"
cursor.execute("UPDATE occupants SET status = %s, lastout = %s WHERE occupants.id = '1'", (status, timestamp))
connection.commit()
elif isNewData:
print "Received new data from", dev.addr
scanner = Scanner().withDelegate(ScanDelegate())
devices = scanner.scan(10.0)
def others():
sharonResult = bluetooth.lookup_name('8C:25:05:83:53:13', timeout=5)
ts = time.time()
timestamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
if (sharonResult != None):
sshtunnel.SSH_TIMEOUT = 30.0
sshtunnel.TUNNEL_TIMEOUT = 30.0
with sshtunnel.SSHTunnelForwarder(
('ssh.pythonanywhere.com'),
ssh_username='xxx', ssh_password='xxx',
remote_bind_address=('xxx.mysql.pythonanywhere-services.com', 3306)
) as tunnel:
connection = mysql.connector.connect(
user='xxx', password='xxx',
host='127.0.0.1', port=tunnel.local_bind_port,
database='xxx$hivedashboard',
)
cursor = connection.cursor()
status = "In"
cursor.execute("UPDATE occupants SET status = %s, lastin = %s WHERE occupants.id = '2'", (status, timestamp))
connection.commit()
score = score + 5
else:
sshtunnel.SSH_TIMEOUT = 30.0
sshtunnel.TUNNEL_TIMEOUT = 30.0
with sshtunnel.SSHTunnelForwarder(
('ssh.pythonanywhere.com'),
ssh_username='xxx', ssh_password='xxx',
remote_bind_address=('xxx.mysql.pythonanywhere-services.com', 3306)
) as tunnel:
connection = mysql.connector.connect(
user='xxx', password='xxx',
host='127.0.0.1', port=tunnel.local_bind_port,
database='xxx$hivedashboard',
)
cursor = connection.cursor()
status = "Out"
cursor.execute("UPDATE occupants SET status = %s, lastout = %s WHERE occupants.id = '2'", (status, timestamp))
connection.commit()
morganResult = bluetooth.lookup_name('9C:65:B0:8B:16:A0', timeout=5)
ts = time.time()
timestamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
if (morganResult != None):
sshtunnel.SSH_TIMEOUT = 30.0
sshtunnel.TUNNEL_TIMEOUT = 30.0
with sshtunnel.SSHTunnelForwarder(
('ssh.pythonanywhere.com'),
ssh_username='xxx', ssh_password='xxx',
remote_bind_address=('xxx.mysql.pythonanywhere-services.com', 3306)
) as tunnel:
connection = mysql.connector.connect(
user='xxx', password='xxx',
host='127.0.0.1', port=tunnel.local_bind_port,
database='xxx$hivedashboard',
)
cursor = connection.cursor()
status = "In"
cursor.execute("UPDATE occupants SET status = %s, lastin = %s WHERE occupants.id = '3'", (status, timestamp))
connection.commit()
score = score + 1
else:
sshtunnel.SSH_TIMEOUT = 30.0
sshtunnel.TUNNEL_TIMEOUT = 30.0
with sshtunnel.SSHTunnelForwarder(
('ssh.pythonanywhere.com'),
ssh_username='xxx', ssh_password='xxx',
remote_bind_address=('xxx.mysql.pythonanywhere-services.com', 3306)
) as tunnel:
connection = mysql.connector.connect(
user='xxx', password='xxx',
host='127.0.0.1', port=tunnel.local_bind_port,
database='xxx$hivedashboard',
)
cursor = connection.cursor()
status = "Out"
cursor.execute("UPDATE occupants SET status = %s, lastout = %s WHERE occupants.id = '3'", (status, timestamp))
connection.commit()
connorResult = bluetooth.lookup_name('18:83:31:8D:01:04', timeout=5)
ts = time.time()
timestamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
if (connorResult != None):
sshtunnel.SSH_TIMEOUT = 30.0
sshtunnel.TUNNEL_TIMEOUT = 30.0
with sshtunnel.SSHTunnelForwarder(
('ssh.pythonanywhere.com'),
ssh_username='xxx', ssh_password='xxx',
remote_bind_address=('xxx.mysql.pythonanywhere-services.com', 3306)
) as tunnel:
connection = mysql.connector.connect(
user='xxx', password='xxx',
host='127.0.0.1', port=tunnel.local_bind_port,
database='xxx$hivedashboard',
)
cursor = connection.cursor()
status = "In"
cursor.execute("UPDATE occupants SET status = %s, lastin = %s WHERE occupants.id = '4'", (status, timestamp))
connection.commit()
score = score + 1
else:
sshtunnel.SSH_TIMEOUT = 30.0
sshtunnel.TUNNEL_TIMEOUT = 30.0
with sshtunnel.SSHTunnelForwarder(
('ssh.pythonanywhere.com'),
ssh_username='xxx', ssh_password='xxx',
remote_bind_address=('xxx.mysql.pythonanywhere-services.com', 3306)
) as tunnel:
connection = mysql.connector.connect(
user='xxx', password='xxx',
host='127.0.0.1', port=tunnel.local_bind_port,
database='xxx$hivedashboard',
)
cursor = connection.cursor()
status = "Out"
cursor.execute("UPDATE occupants SET status = %s, lastout = %s WHERE occupants.id = '4'", (status, timestamp))
connection.commit()
sshtunnel.SSH_TIMEOUT = 30.0
sshtunnel.TUNNEL_TIMEOUT = 30.0
with sshtunnel.SSHTunnelForwarder(
('ssh.pythonanywhere.com'),
ssh_username='xxx', ssh_password='xxx',
remote_bind_address=('xxx.mysql.pythonanywhere-services.com', 3306)
) as tunnel:
connection = mysql.connector.connect(
user='xxx', password='xxx',
host='127.0.0.1', port=tunnel.local_bind_port,
database='xxx$hivedashboard',
)
cursor1 = connection.cursor()
cursor1.execute("UPDATE Heating SET Score = %s WHERE Heating.id = '1'", (score,))
connection.commit()
#scanner = Scanner().withDelegate(ScanDelegate())
#devices = scanner.scan(10.0)
#return 'OK'
The 'others' bit doesn't get run.
The problem is that I need the 2 'scanner' lines to run before the 'others' bit or I end up with an error:
Code: Select all
bluepy.btle.BTLEDisconnectError: Device disconnected
Cheers
Andy