def connect(self): #UUID where temp is stored # tempc_uuid = UUID(0x0021) tempc_uuid = UUID(0x2221) self.isBTNOK = 1 #connection... while not self._stopevent.isSet() and self.isBTNOK: try: # print("Connection...") #my bluefruit address self.p = Peripheral("EE:1B:17:8F:A4:5D", "random") #EE:1B:17:8F:A4:5D # print("OK!\ngetCharacteristics") #for ch in self.p.getCharacteristics(): # print str(ch) self.cht = self.p.getCharacteristics(uuid=tempc_uuid)[0] # print("Done!") self.isBTNOK = 0 except BTLEException as e: # print "CONNECT - BTLEException : {0}".format(e) self.disconnect() except: # print 'CONNECT - Other error! ',sys.exc_info()[0] self.disconnect() #wait 3 second before try again if (not self._stopevent.isSet()): self._stopevent.wait(3)
def __init__(self,addr): Peripheral.__init__(self,addr) # self.discoverServices() self.IRtemperature = IRTemperatureSensor(self) self.accelerometer = AccelerometerSensor(self) self.humidity = HumiditySensor(self) self.magnetometer = MagnetometerSensor(self) self.barometer = BarometerSensor(self) self.gyroscope = GyroscopeSensor(self) self.keypress = KeypressSensor(self) self.luxometer = LuxometerSensor(self) self.microphone = MicrophoneSensor(self)
def __init__(self, addr): # list of channels self.nbChans = 2 self.buffer = '' # current position self.head = 0 self.samples = [] self.read_state = 0 print "connecting to device", addr Peripheral.__init__(self, addr) print "...connected"
def connect(self): try: self.peripheral = Peripheral(self.macAddress, addrType='random') except BTLEException: return False try: self.enableNotifications() self.peripheral.setDelegate(NuimoDelegate()) except BTLEException: self.peripheral.disconnect() self.peripheral = None return False return True
def runDoCycle(self): ## self.player.set_mrl(MRL) ## self.player.play() print("cerco di connettermi") per = Peripheral("d7:84:e7:e2:87:68") #per.setDelegate( MyDelegate() ) print("connessione avvenuta") GObject.timeout_add(3000, self.doCycle, per)
def ble_init(): print('Scanning...') scanner = Scanner().withDelegate(ScanDelegate()) devices = scanner.scan(10.0) for dev in devices: print("Device %s (%s), RSSI=%d dB" % (dev.addr, dev.addrType, dev.rssi)) for (adtype, desc, value) in dev.getScanData(): print(" %s = %s" % (desc, value)) print('Querying...') try: p = Peripheral(BLE1_MAC_ADDR, "random", 0) except: print( "Check device on or does it throw an BTLEException exception from BLE constructor? " ) try: c = p.getCharacteristics(uuid="BD6664C8941DCBA806E1AA75F7D44BAC")[0] #c = p.getCharacteristics(uuid="1814")[0] except: print("getCharacteristics Error")
def __init__(self, addr): print "connecting to device", addr, " in random mode" Peripheral.__init__(self, addr, addrType=ADDR_TYPE_RANDOM) print "...connected"
def startCycle(): print("cerco di connettermi") per = Peripheral("d7:84:e7:e2:87:68") per.setDelegate(MyDelegate()) print("connessione avvenuta") GObject.timeout_add(7000, doCycle, per)
import struct import time from bluepy.bluepy.btle import UUID, Peripheral temp_uuid = UUID(0x2221) p = Peripheral("xx:xx:xx:xx:xx:xx", "random") try: ch = p.getCharacteristics(uuid=temp_uuid)[0] if (ch.supportsRead()): while 1: val = binascii.b2a_hex(ch.read()) val = binascii.unhexlify(val) val = struct.unpack('f', val)[0] print str(val) + " deg C" time.sleep(1) finally: p.disconnect()
class Nuimo: def __init__(self, macAddress='FA:48:12:00:CA:AC'): self.macAddress = macAddress def connect(self): try: self.peripheral = Peripheral(self.macAddress, addrType='random') except BTLEException: return False try: self.enableNotifications() self.peripheral.setDelegate(NuimoDelegate()) except BTLEException: self.peripheral.disconnect() self.peripheral = None return False return True def enableNotifications(self): self.peripheral.writeCharacteristic(CLICK_NOTIFICATION_HANDLE, NOTIFICATION_ON) self.peripheral.writeCharacteristic(BATTERY_NOTIFICATION_HANDLE, NOTIFICATION_ON) self.peripheral.writeCharacteristic(FLY_NOTIFICATION_HANDLE, NOTIFICATION_ON) self.peripheral.writeCharacteristic(SWIPE_NOTIFICATION_HANDLE, NOTIFICATION_ON) self.peripheral.writeCharacteristic(ROTATION_NOTIFICATION_HANDLE, NOTIFICATION_ON) def waitForNotifications(self): try: self.peripheral.waitForNotifications(1.0) return True except BTLEException as e: return False def displayLedMatrix(self, matrix, brightness, timeout): self.peripheral.writeCharacteristic(LEDMATRIX_VALUE_HANDLE, struct.pack("BBBBBBBBBBBBB", 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff))
import binascii import struct import time from bluepy.bluepy.btle import UUID, Peripheral # Message constants MSG_LOCK = 0x10 MSG_UNLOCK = 0x11 MSG_STATE_REQ = 0x12 # Define read and write UUIDs read_uuid = UUID(0x2221) write_uuid = UUID(0x2222) # Create a connection to the RFduino p = Peripheral("F9:D8:C2:B9:77:E9", "random") try: # Create handles for read and write characteristics w_ch = p.getCharacteristics(uuid=write_uuid)[0] r_ch = p.getCharacteristics(uuid=read_uuid)[0] # If we can, continuously send lock and unlock requests if (r_ch.supportsRead()): while 1: # Tell the Lockitron to lock msg = struct.pack('i', MSG_LOCK) print "Writing: " + str(msg) w_ch.write(msg)
from bluepy.bluepy.btle import UUID, Peripheral p = Peripheral("28:84:fa:97:0f:f8") svc = p.getServiceByUUID(uuid="2B1DA6DE-9C29-4D6C-A930-B990EA2F12BB") ch = svc.getCharacteristics(uuid="7F855F82-9378-4508-A3D2-CD989104AF22")[0] ch.write() while 1: if p.waitForNotifications(1.0): continue print "waiting"
import binascii import struct import time from bluepy.bluepy.btle import UUID, Peripheral temp_uuid = UUID(0x2A6E) p = Peripheral("98:4F:EE:0D:05:6A", "public") try: ch = p.getCharacteristics(uuid=temp_uuid)[0] if (ch.supportsRead()): while 1: print ch.read() val = binascii.b2a_hex(ch.read()) print val val = binascii.unhexlify(val) print val #val = struct.unpack('f', val)[0] #print str(val) + " deg C" time.sleep(1) finally: p.disconnect()
import binascii import struct import time from bluepy.bluepy.btle import UUID, Peripheral # Message constants MSG_LOCK = 0x10 MSG_UNLOCK = 0x11 MSG_STATE_REQ = 0x12 # Define read and write UUIDs read_uuid = UUID(0x2221) write_uuid = UUID(0x2222) # Create a connection to the RFduino p = Peripheral("F9:D8:C2:B9:77:E9", "random") try: # Create handles for read and write characteristics w_ch = p.getCharacteristics(uuid=write_uuid)[0] r_ch = p.getCharacteristics(uuid=read_uuid)[0] # Tell the Lockitron to lock msg = struct.pack('i', MSG_UNLOCK) print "Writing: " + str(msg) w_ch.write(msg) finally: p.disconnect()
class TaskPrintWeight2(threading.Thread): def __init__(self, taskid=0, mData=readHSR.HSRData()): threading.Thread.__init__(self) self.taskid = taskid self._stopevent = threading.Event() self.mData = mData self.p = 0 self.cht = 0 self.previousT = time.time() self.interval = 0.5 self.Tlast = self.Tnow = self.Tmax = self.Tmin = 0 self.T_dif_now = self.T_dif_last = 0 self.TminLoopCount = 0 self.TmaxLoopCount = 0 self.isBTNOK = 1 self.tempo = 1 def disconnect(self): self.isBTNOK = 1 if self.p != 0: try: self.p.disconnect() self.p = 0 except: print "disconnect error" def connect(self): #UUID where temp is stored # tempc_uuid = UUID(0x0021) tempc_uuid = UUID(0x2221) self.isBTNOK = 1 #connection... while not self._stopevent.isSet() and self.isBTNOK: try: # print("Connection...") #my bluefruit address self.p = Peripheral("EE:1B:17:8F:A4:5D", "random") #EE:1B:17:8F:A4:5D # print("OK!\ngetCharacteristics") #for ch in self.p.getCharacteristics(): # print str(ch) self.cht = self.p.getCharacteristics(uuid=tempc_uuid)[0] # print("Done!") self.isBTNOK = 0 except BTLEException as e: # print "CONNECT - BTLEException : {0}".format(e) self.disconnect() except: # print 'CONNECT - Other error! ',sys.exc_info()[0] self.disconnect() #wait 3 second before try again if (not self._stopevent.isSet()): self._stopevent.wait(3) #extract float data from BLE struct def extractBLEval(self, data): val = binascii.b2a_hex(data) val = binascii.unhexlify(val) val = struct.unpack('f', val)[0] return float(val) def read(self): #read loop while not self._stopevent.isSet(): try: # print "BT READ, get value..." tc = self.extractBLEval(self.cht.read()) print "poids=", tc return tc except BTLEException as e: print "BT READ - BTLEException : {0}".format(e) self.disconnect() except: print 'BT READ - Other error! ', sys.exc_info()[0] self.disconnect() #disconnect self.disconnect() #wait 1 second before try again self._stopevent.wait(1) #reconnect self.connect() def rythmeHaut(self): #set fast tempo 100ms self.tempo = 0.1 def rythmeBas(self): #set slow tempo 1sec self.tempo = 1 #main task body def run(self): print "thread BT is readry!" retBTNOK = 1 #try to connect to BT sensor self.connect() #do a while not self._stopevent.isSet(): tc = self.read() #upgrade data in shared memory self.mData.setRange(tc) #wait a little self._stopevent.wait(self.tempo) def stop(self): print "stopping thread BT" self._stopevent.set() #give a chance do disconnect nicely time.sleep(1) #disconnect if it was not done before self.disconnect()
import binascii import struct import time from bluepy.bluepy.btle import UUID, Peripheral temp_uuid = UUID(0x2221) p = Peripheral("00:A0:50:18:1D:10", "random") try: ch = p.getCharacteristics(uuid=temp_uuid)[0] if (ch.supportsRead()): while 1: val = binascii.b2a_hex(ch.read()) val = binascii.unhexlify(val) val = struct.unpack('f', val)[0] print str(val) + " deg C" time.sleep(1) finally: p.disconnect()