import binascii
import pexpect
import sys
import time
import Adafruit_BBIO.GPIO as GPIO
def floatfromhex(h):
t = float.fromhex(h)
if t > float.fromhex('7FFF'):
t = -(float.fromhex('FFFF') - t)
pass
return t
GPIO.setup("P8_18", GPIO.OUT) #BLUE LED
GPIO.setup("P8_14", GPIO.OUT) #RED LED
GPIO.setup("P8_16", GPIO.OUT) #GREEN LED
GPIO.output("P8_18", GPIO.LOW)
GPIO.output("P8_14", GPIO.HIGH) #RED LED ON
GPIO.output("P8_16", GPIO.LOW)
bluetooth_adr = 'C4:BE:84:71:0D:80'
tool = pexpect.spawn('gatttool -b ' + bluetooth_adr + ' -I')
tool.expect('\[LE\]>')
print "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nPreparing to connect...\n\n\n\n\n\n"
tool.sendline('connect')
time.sleep(1)
# test for success of connect
tool.expect('\[CON\].*>')
print "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nConnected!\n\n\n\n\n\n"
GPIO.output("P8_18", GPIO.HIGH) #BLUE LED ON SINCE BLE IS CONNECTED
time.sleep(1)
ready_temp = 40
print "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nPick a Profile (by number):\n1-Tough Guy, 2-Hot Head, 3-Medium Man, 4-Lukewarm Larry, or 5-Ice-Cold Izzy\n\n\n\n\n"
name = raw_input()
if(name == '1'):
ready_temp = 50
else:
if(name == '2'):
ready_temp = 48
else:
if(name == '3'):
ready_temp = 46
else:
if(name == '4'):
ready_temp = 44
else:
ready_temp = 42
print "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nBeverage enjoyment temperature set at: ", ready_temp, " C, ", ((float)(1.8*ready_temp) + 32), " F\n\n\n\n\n\n"
tool.sendline('char-write-cmd 0x24 01')
tool.expect('\[LE\]>')
time.sleep(2)
ready_flag = 0
while True:
time.sleep(1)
tool.sendline('char-read-hnd 0x21')
tool.expect('descriptor: .*')
rval = tool.after.split()
objT = floatfromhex((rval[2] + rval[1]))
objT_shift = ((objT/4))
output_obj = ((float)(objT_shift)) * 0.03125
ambT = floatfromhex(rval[4] + rval[3])
ambT_shift = ((ambT/4))
output_amb = ((float)(ambT_shift)) * 0.03125
print "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nToo Hot To Handle\nTemperature Dashboard:\n\nIR Temp: ", output_obj, " C, ", (1.8*output_obj + 32), " F"
print "Ambient Temp: ", output_amb, " C, ", (1.8*output_amb + 32), " F\n\n\n\n\n\n"
if(output_obj > (ready_temp + 2)): #real use 55
ready_flag = ready_flag + 1
if((ready_flag > 3) and (output_obj < ready_temp)): #real use 53
#CHANGE GPIO pins for LED
GPIO.output("P8_14", GPIO.LOW)
GPIO.output("P8_16", GPIO.HIGH)
#PRINT TO MONITOR
print "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nYour beverage is no longer Too Hot To Handle!\n\t\t Enjoy!\n\n\n\n\n\n"
#BUZZER 3 BEEPS
tool.sendline('char-write-cmd 0x50 01')
time.sleep(0.5)
tool.sendline('char-write-cmd 0x50 00')
time.sleep(0.5)
tool.sendline('char-write-cmd 0x50 01')
time.sleep(0.5)
tool.sendline('char-write-cmd 0x50 00')
time.sleep(0.5)
tool.sendline('char-write-cmd 0x50 01')
time.sleep(0.5)
tool.sendline('char-write-cmd 0x50 00')
time.sleep(5)
GPIO.output("P8_18", GPIO.LOW)
GPIO.output("P8_14", GPIO.LOW)
GPIO.output("P8_16", GPIO.LOW)
break
GPIO.output("P8_18", GPIO.LOW)
GPIO.output("P8_14", GPIO.LOW)
GPIO.output("P8_16", GPIO.LOW)
Comments