def read_Sg(): try: sock = bluez.hci_open_dev(dev_id) except: print("error accessing bluetooth device...") sys.exit(1) blescan.hci_le_set_scan_parameters(sock) blescan.hci_enable_le_scan(sock) #gotData = 0 #while (gotData == 0): returnedList = blescan.parse_events(sock, 10) for beacon in returnedList: #returnedList is a list datatype of string datatypes seperated by commas (,) output = beacon.split( ',') #split the list into individual strings in an array if output[1] == green: #Change this to the colour of you tilt #gotData = 1 tiltSG = int(output[3], 16) / 1000 print("testing") print(tiltSG) print("-----") blescan.hci_disable_le_scan(sock) return tiltSG
def getdata(): try: sock = bluez.hci_open_dev(dev_id) except: print "error accessing bluetooth device..." sys.exit(1) blescan.hci_le_set_scan_parameters(sock) blescan.hci_enable_le_scan(sock) gotData = 0 while (gotData == 0): returnedList = blescan.parse_events(sock, 10) for beacon in returnedList: #returnedList is a list datatype of string datatypes seperated by commas (,) output = beacon.split(',') #split the list into individual strings in an array if output[1] == blue: #Change this to the colour of you tilt tempf = float(output[2]) #convert the string for the temperature to a float type gotData = 1 tiltSG = float(output[3]) tiltTemp = tempf #assign values to a dictionary variable for the http POST to google sheet data= { "Time": datetime.now(), 'SG': tiltSG, 'Temp': tiltTemp, } blescan.hci_disable_le_scan(sock) return data
def scan(self): #TODO - make this a "with" style context manager datalist = [] # Setup sock = bluez.hci_open_dev(self.devid) blescan.hci_le_set_scan_parameters(sock) blescan.hci_enable_le_scan(sock) #returns a list of Beacons scan_results = blescan.parse_events(sock, 10) #pprint.pprint( scan_results ) for beacon in scan_results: try: color = self.uuidmap[beacon.uuid] except (KeyError) as e: continue #print( 'Found Tilt color: {}'.format( color ) ) data = { 'datetime': datetime.datetime.now(), 'SG': float(beacon.minor) / 1000, 'Temp': float(beacon.major), 'Color': color, } #pprint.pprint( data ) datalist.append(data) # Teardown blescan.hci_disable_le_scan(sock) return datalist
def _funcRun(self): try: sock = bluez.hci_open_dev(self.dev_id) except: print("error accessing bluetooth device...") sys.exit(1) blescan.hci_le_set_scan_parameters(sock) blescan.hci_enable_le_scan(sock) while (self.running == True): returnedList = blescan.parse_events(sock, 10) for beacon in returnedList: #returnedList is a list datatype of string datatypes seperated by commas (,) output = beacon.split( ',') #split the list into individual strings in an array if output[0] == 'd4:54:01:07:45:60': #print('device found') self.IOdata.temp = ( float(output[2]) - 32 ) * 5 / 9 #convert the string for the temperature to a float type self.IOdata.sg = float(output[3]) / 1000 blescan.hci_disable_le_scan(sock)
def getdata(): try: sock = bluez.hci_open_dev(dev_id) except: print "error accessing bluetooth device..." draw.rectangle((0, 0, width, height), outline=0, fill=0) draw.text((x, top), "error accessing bluetooth device...", font=font, fill=255) disp.image(image) disp.display() sys.exit(1) blescan.hci_le_set_scan_parameters(sock) blescan.hci_enable_le_scan(sock) gotData = 0 while (gotData == 0): returnedList = blescan.parse_events(sock, 10) #lcd.message(returnedList) #print returnedList ### for beacon in returnedList: #returnedList is a list datatype of string datatypes seperated by commas (,) output = beacon.split( ',') #split the list into individual strings in an array if output[1] == purple: #Change this to the colour of you tilt tempf = float( output[2] ) #convert the string for the temperature to a float type gotData = 1 tiltTime = sheetsDate(datetime.datetime.now()) tiltSG = float(output[3]) / 1000 tiltTemp = tempf tiltColour = 'PURPLE' tiltBeer = 'test' #Change to an identifier of a particular brew #assign values to a dictionary variable for the http POST to google sheet data = { 'Timepoint': tiltTime, 'SG': tiltSG, 'Temp': tiltTemp, 'Color': tiltColour, 'Beer': tiltBeer, 'Comment': "Pi" } blescan.hci_disable_le_scan(sock) return data while True: data = getdata() #tempf = (float(data["Temp"]) #temp in f tempc = ( float(data["Temp"]) - 32 ) * 5 / 9 #convert from string to float and then farenheit to celcius just for the display tempc = round(tempc) #Round of the value to 2 decimal places tempf = tempc * 9 / 5 + 32 tiltSG = data['SG']
def scan_a_minute(self): pkts = [] blescan.hci_le_set_scan_parameters(sock) blescan.hci_enable_le_scan(sock) t1 = time.time() while True: pkts.extend(blescan.parse_events(sock, 10)) #parse needed packets in blescan if time.time - t1 > 60: break blescan.hci_disable_le_scan(sock) return pkts
def getdata(): try: sock = bluez.hci_open_dev(dev_id) except: print "error accessing bluetooth device..." sys.exit(1) blescan.hci_le_set_scan_parameters(sock) blescan.hci_enable_le_scan(sock) gotData = 0 while (gotData == 0): returnedList = blescan.parse_events(sock, 10) for beacon in returnedList: #returnedList is a list datatype of string datatypes seperated by commas (,) output = beacon.split( ',') #split the list into individual strings in an array if output[1] == black: #Change this to the colour of you tilt tempf = float( output[2] ) #convert the string for the temperature to a float type #tempc = (float(output[2]) - 32)*5/9 #tempc = round(tempc) gotData = 1 tiltTime = sheetsDate(datetime.datetime.now()) tiltSG = float(output[3]) / 1000 tiltTemp = tempf tiltColour = 'BLACK' tiltBeer = 'test' #Change to an identifier of a particular brew #assign values to a dictionary variable for the http POST to google sheet data = { 'Time': tiltTime, 'SG': tiltSG, 'Temp': tiltTemp, 'Color': tiltColour, 'Beer': tiltBeer, 'Comment': "" } blescan.hci_disable_le_scan(sock) return data
def getdata(): try: sock = bluez.hci_open_dev(dev_id) except: print "error accessing bluetooth device..." sys.exit(1) blescan.hci_le_set_scan_parameters(sock) blescan.hci_enable_le_scan(sock) gotData = 0 while (gotData == 0): returnedList = blescan.parse_events(sock, 10) for beacon in returnedList: #returnedList is a list datatype of string datatypes seperated by commas (,) output = beacon.split( ',') #split the list into individual strings in an array if output[1] == green: #Change this to the colour of you tilt tempf = float( output[2] ) #convert the string for the temperature to a float type gotData = 1 tiltTime = 1 tiltSG = float(output[3]) / 1000 tiltTemp = tempf tiltColour = 'GREEN' tiltBeer = 'TestBrew' #Change to an identifier of a particular brew #assign values to a dictionary variable for the http POST to google sheet data = dict() data['Time'] = tiltTime data['Colour'] = tiltColour data['Beer'] = tiltBeer data['Temp'] = tiltTemp data['SG'] = tiltSG jsonObj = json.dumps(data) #print jsonObj blescan.hci_disable_le_scan(sock) return jsonObj
def getdata(): try: sock = bluez.hci_open_dev(dev_id) except: print("error accessing bluetooth device...") sys.exit(1) blescan.hci_le_set_scan_parameters(sock) blescan.hci_enable_le_scan(sock) gotData = 0 while (gotData == 0): returnedList = blescan.parse_events(sock, 10) for beacon in returnedList: #returnedList is a list datatype of string datatypes seperated by commas (,) output = beacon.split(',') #split the list into individual strings in an array if output[1] == black: #Change this to the colour of you tilt tempf = float(output[2]) #convert the string for the temperature to a float type tempc = (float(output[2]) - 32)*5/9 #tempc = round(tempc) gotData = 1 tiltTime = sheetsDate(datetime.datetime.now()) tiltSG = float(output[3])/1000 tiltTemp = tempf tiltColour = 'BLACK' tiltBeer = 'test' #Change to an identifier of a particular brew #assign values to a dictionary variable for the http POST to google sheet data= { 'Timepoint': tiltTime, 'SG': tiltSG, 'Temp': tiltTemp, 'Color': tiltColour, 'Beer': tiltBeer, 'Comment': "" } blescan.hci_disable_le_scan(sock) return data
def _readdata(self): curTime = datetime.now() timeout = curTime + timedelta( minutes=1) # scan for 1 minute looking for data from tilt logger.debug("Connecting to Bluetooth...") try: sock = bluez.hci_open_dev(self.bluetoothDeviceId) except: logger.exception("Error accessing bluetooth device...") return False blescan.hci_le_set_scan_parameters(sock) blescan.hci_enable_le_scan(sock) gotData = 0 while (gotData == 0 and curTime < timeout): returnedList = blescan.parse_events(sock, 10) for beacon in returnedList: # returnedList is a list datatype of string datatypes seperated by commas (,) output = beacon.split( ',') # split the list into individual strings in an array uuid = output[1] if uuid in Tilt.color.keys(): logger.debug("Found Tilt hydrometer:" + uuid) gotData = 1 foundColor = Tilt.color.get(uuid) foundTemp = float(int(output[2], 16) - 32) * 5 / 9 foundSG = float(int(output[3], 16) / 1000) logger.debug("Found " + foundColor + " Tilt: T/" + str(foundTemp) + ", SG/" + str(foundSG)) if foundColor != self.color: logger.debug( "Skipping data; wrong color Tilt. Looking for: " + self.color) gotData = 0 # wrong tilt elif foundTemp > 200: # first reading always showed temperature of 537.2C and SG 1.0 logger.debug( "Skipping initial datasets as temp and SG are always wrong" ) timeout = curTime + timedelta( minutes=1 ) # extend timeout looking for valid data from tilt gotData = 0 # forces to continue reading from tilt looking for valid data else: self._storeData(foundTemp, foundSG, curTime) logger.info(self.color + " - " + curTime.strftime(DATETIME_FORMAT) + " - T:" + str(round(float(foundTemp), 1)) + " - SG:" + "{:5.3f}".format(round(float(foundSG), 3))) curTime = datetime.now() # Did not collect data before timeout; set data as invalid if gotData == 0: logger.debug("Timed out collecting data from Tilt") blescan.hci_disable_le_scan(sock) return True
def getdata(): try: sock = bluez.hci_open_dev(dev_id) except: print "error accessing bluetooth device..." sys.exit(1) blescan.hci_le_set_scan_parameters(sock) blescan.hci_enable_le_scan(sock) data = [] gotData = 0 while (gotData == 0): returnedList = blescan.parse_events(sock, 10) for beacon in returnedList: # returnedList is a list datatype of string datatypes seperated by commas (,) gotDataCount = gotData output = beacon.split( ',') # split the list into individual strings in an array if output[1] == red: tiltColour = 'Red' tiltBeer = 'Test' tiltId = 1 gotData += 1 if output[1] == green: tiltColour = 'Green' tiltBeer = 'Test' tiltId = 2 gotData += 1 if output[1] == black: tiltColour = 'Black' tiltBeer = 'Test' tiltId = 3 gotData += 1 if output[1] == purple: tiltColour = 'Purple' tiltBeer = 'Test' tiltId = 4 gotData += 1 if output[1] == orange: tiltColour = 'Orange' tiltBeer = 'Test' tiltId = 5 gotData += 1 if output[1] == blue: tiltColour = 'Blue' tiltBeer = 'Test' tiltId = 6 gotData += 1 if output[1] == yellow: tiltColour = 'Yellow' tiltBeer = 'Test' tiltId = 7 gotData += 1 if output[1] == pink: tiltColour = 'Pink' tiltBeer = 'Test' tiltId = 8 gotData += 1 if gotData > gotDataCount: tempF = float( output[2] ) # convert the string for the temperature to a float type tempC = (tempF - 32) / 1.8 intGravSG = int(output[3]) gravSG = float(intGravSG) / 1000 gravP = (-1 * 616.868) + (1111.14 * gravSG) - ( 630.272 * gravSG**2) + (135.997 * gravSG**3) if METRIC == 1: tiltGrav = gravP tiltTemp = tempC else: tiltGrav = gravSG tiltTemp = tempF # tiltTXPower = int(output[4]) # not needed tiltRSSI = int(output[5]) # check if readings are within reasonable range (avoid spike artefacts) if (intGravSG >= 985) and (intGravSG <= 1125): newdata = { 'ID': tiltId, 'name': 'Tilt ' + tiltColour, #'angle': ((gravSG - 0.99) * 1000) / 1.6, # rough estimate 'angle': 25 + (gravP * 1.6), 'temperature': tiltTemp, 'battery': 4.0, 'gravity': tiltGrav, 'interval': INTERVAL, 'rssi': tiltRSSI, 'token': '* ' + tiltBeer } data.append(newdata) blescan.hci_disable_le_scan(sock) return data
tiltTemp = tempf tiltColour = 'PURPLE' tiltBeer = 'test' #Change to an identifier of a particular brew #assign values to a dictionary variable for the http POST to google sheet data= { 'Timepoint': tiltTime, 'SG': tiltSG, 'Temp': tiltTemp, 'Color': tiltColour, 'Beer': tiltBeer, 'Comment': "Pi" } blescan.hci_disable_le_scan(sock) return data def main(): global screen updateSecs = delay #time in seconds between updating the google sheet screenSecs = 60 timestamp = time.time() #Set time for beginning of loop updateTime = timestamp + updateSecs #Set the time for the next update to google sheets screenTime = timestamp + screenSecs #Set the time to put the screen to sleep while True: data = getdata() #tempf = (float(data["Temp"]) #temp in f tempc = (float(data["Temp"])-32)*5/9 #convert from string to float and then farenheit to celcius just for the display
def signal_handler(signal, frame): print("You pressed Ctrl+C") blescan.hci_disable_le_scan(sock) sys.exit(0)