Esempio n. 1
0
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")
Esempio n. 2
0
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()
Esempio n. 3
0
# 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)
            time.sleep(3)

            # Tell the Lockitron to unlock
            msg = struct.pack('i', MSG_UNLOCK)
            print "Writing: " + str(msg)
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 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()

Esempio n. 6
0
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()