Esempio n. 1
0
class IoTDevice(object):
    """
    IoTDevice represents a single EyeoT IoT (Arduino 101) BLE device, initialized by one of several authorized MAC
    addresses.

    Input: MAC address of the EyeoT device to control

    Output: Initialized EyeoT device containing the proper MAC address, service UUID, tx_command and rx_response
    characteristic UUIDs, tx_handle, and GATTRequester
    """
    def __init__(self, address):
        self.address = address
        self.service_uuid = arduino.service_uuid
        self.tx_command = arduino.tx_command
        self.rx_response = arduino.rx_response
        self.tx_handle = arduino.tx_handle
        self.rx_handle = arduino
        self.req = GATTRequester(address,
                                 False)  # initialize req but don't connect
        self.response = ble_consts.not_ready

    def connect(self):
        print("Connecting...\n")
        self.req.connect(True)
        print("Connection Successful! \n")

    def send_command(self, command):
        self.req.write_by_handle(arduino.tx_handle, command)
        print("Sent '{0}' command\n".format(ble_consts.commands[command]))

    def receive_response(self):
        self.response = int(
            self.req.read_by_handle(arduino.rx_handle)[0].encode('hex')) / 100
        print("Response '{0}' received!\n".format(
            ble_consts.responses[self.response]))
Esempio n. 2
0
class Cloudpets(object):
    def __init__(self, address):
        self._requester = GATTRequester(address, False)
        self.led = Led(self)
        self.speaker = Speaker(self)
        self.microphone = Microphone(self)
        self.state = None
        self.name = None
        self.setup_datetime = None

    def connect(self):
        self._requester.connect(True)
        self._retrieve_config()

    def disconnect(self):
        self._requester.disconnect()

    def _read_value(self, handle):
        return self._requester.read_by_handle(handle)

    def _send_command(self, handle, cmd):
        self._requester.write_by_handle(handle, str(bytearray(cmd)))

    def _retrieve_config(self):
        response = self._read_value(HANDLES['config'])[0]
        self.name = MODELS[int(response[:2])]
        self.setup_datetime = datetime.strptime(response[2:], "%m_%d_%H_%M_%S")
Esempio n. 3
0
class Driver(object):
    handle = 0x16
    commands = {
        'press': '\x57\x01\x00',
        'on': '\x57\x01\x01',
        'off': '\x57\x01\x02',
    }

    def __init__(self, device, bt_interface=None, timeout_secs=None):
        self.device = device
        self.bt_interface = bt_interface
        self.timeout_secs = timeout_secs if timeout_secs else 5
        self.req = None

    def connect(self):
        if self.bt_interface:
            self.req = GATTRequester(self.device, False, self.bt_interface)
        else:
            self.req = GATTRequester(self.device, False)

        self.req.connect(True, 'random')
        connect_start_time = time.time()

        while not self.req.is_connected():
            if time.time() - connect_start_time >= self.timeout_secs:
                raise RuntimeError('Connection to {} timed out after {} seconds'
                                   .format(self.device, self.timeout_secs))

    def run_command(self, command):
        self.req.write_by_handle(self.handle, self.commands[command])
        data = self.req.read_by_handle(self.handle)
        return data
Esempio n. 4
0
class Reader():
    def __init__(self, address):
        self.requester = GATTRequester(address, False)
        self.connect()
        self.request_data()
        self.turn()

    def connect(self):
        print("Connecting...", end=' ')
        sys.stdout.flush()

        self.requester.connect(True)
        print("OK!")

    def request_data(self):
        data = self.requester.read_by_handle(0x03)
        print("Device name: " + str(data))

        candle = self.requester.read_by_handle(0x23)
        print("Candle: " + str(candle))

    def turn(self):
        self.requester.write_by_handle(0x0016, str(bytearray([0, 0, 0, 0])))
Esempio n. 5
0
characteristics = grq.discover_characteristics()

#
# the UUID of the service on the BLE device.
#
sample_uuid = '59c889e0-5364-11e7-b114-b2f933d5fe66'

# find the handle for the characteristic.
vh_sample = None
for c12c in characteristics:
    if c12c['uuid'] == sample_uuid:
        vh_sample = c12c['value_handle']
        print("Characteristic value handle is %d." % vh_sample)
        break
assert (vh_sample is not None)

# use the handle to read the signal value, at a one-second interval
print("Press ctrl-c to stop.")
try:
    while True:
        data = grq.read_by_handle(vh_sample)[0]
        print("bytes received:", )
        for b in data:
            print(hex(ord(b)), )
        print("")
        time.sleep(1)
except KeyboardInterrupt:
    print("\nDisconnecting...")
    grq.disconnect()
    print("Done.")