2015-12-05 16:07:12 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
import bluetooth
|
|
|
|
import sys
|
|
|
|
import time
|
2015-12-05 22:58:10 +01:00
|
|
|
import datetime
|
2015-12-05 16:07:12 +01:00
|
|
|
|
2015-12-05 22:58:10 +01:00
|
|
|
bt_add = "98:D3:31:B2:35:33" #termostato
|
|
|
|
br_add = "30:14:06:26:04:18" #rele
|
2015-12-05 16:07:12 +01:00
|
|
|
|
|
|
|
|
2015-12-05 22:58:10 +01:00
|
|
|
#connessione ai dispositivi
|
|
|
|
portRele = 1
|
|
|
|
portTerm = 1
|
2015-12-05 16:07:12 +01:00
|
|
|
sockTerm=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
|
2015-12-05 22:58:10 +01:00
|
|
|
sockTerm.connect((bt_add, portTerm))
|
|
|
|
print 'Connesso al termostato'
|
|
|
|
sockRele=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
|
|
|
|
sockRele.connect((br_add, portRele))
|
|
|
|
print 'Connesso al rele'
|
|
|
|
|
2015-12-05 16:07:12 +01:00
|
|
|
sockTerm.settimeout(1.0)
|
2015-12-05 22:58:10 +01:00
|
|
|
sockRele.settimeout(1.0)
|
|
|
|
|
|
|
|
print 'parametro: ' + sys.argv[1]
|
2015-12-05 16:07:12 +01:00
|
|
|
|
2015-12-05 22:58:10 +01:00
|
|
|
releValue = "s"
|
|
|
|
|
|
|
|
#orario attuale
|
|
|
|
now = datetime.datetime.now()
|
2015-12-05 16:07:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
while 1:
|
|
|
|
try:
|
2015-12-05 22:58:10 +01:00
|
|
|
data = ""
|
|
|
|
sockTerm.send(sys.argv[1])
|
|
|
|
sockRele.send(releValue)
|
|
|
|
print 'Sent data: ' + releValue
|
|
|
|
time.sleep(5) # delays for 5 seconds
|
|
|
|
data = sockTerm.recv(1024)
|
|
|
|
if(len(data)==5):
|
|
|
|
temp = data.replace("t","")
|
|
|
|
temp = int(temp)
|
|
|
|
if (temp < 1570):
|
|
|
|
releValue = "f"
|
|
|
|
else:
|
|
|
|
releValue = "n"
|
|
|
|
print 'received t [%s]' % data
|
|
|
|
data = ""
|
|
|
|
time.sleep(5) # delays for 5 seconds
|
|
|
|
data += sockRele.recv(1024)
|
|
|
|
print 'received r: [%s]' % data
|
|
|
|
# data_end = data.find('\n')
|
|
|
|
# if data_end != -1:
|
|
|
|
# rec = data[:data_end]
|
|
|
|
# print data
|
|
|
|
# data = data[data_end+1:]
|
|
|
|
# break
|
2015-12-05 16:07:12 +01:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
break
|
|
|
|
except bluetooth.btcommon.BluetoothError as error:
|
|
|
|
continue
|
2015-12-05 22:58:10 +01:00
|
|
|
|
|
|
|
print 'socket chiuso'
|
|
|
|
sockRele.close()
|
|
|
|
sockTerm.close()
|
2015-12-05 16:07:12 +01:00
|
|
|
|
|
|
|
|