Ejemplo n.º 1
0
    def get_name(self, i):
        print('--------------------------------------------------')
        logging.debug('Type: {} - Address: {}', self.type, utils.decode_mac(self.address))
        if self.connect():
            time.sleep(1)
            if self.read_data(0x0003):
                try:
                    self.name = self.char_data.decode("utf-8")
                    self.name = self.name[:self.name.find('\x00')]  # drop trailing zeroes
                    logging.debug('Name: {} - Length: {}', self.name, len(self.name))
                except Exception as e:
                    utils.log_error_to_file('ERROR: setup ' + utils.decode_mac(self.address) + ' - ' + str(e))

            self.disconnect()
Ejemplo n.º 2
0
 def connect(self, mswait=2000, type=0):
     # Connect to the device at self.address
     count = 0
     while not self.connected and count < 60000:
         logging.info('Trying to connect to {}...', utils.decode_mac(self.address))
         try:
             self.bt.gap_connect(type, self.address)
         except Exception as e:
             utils.log_error_to_file('ERROR: connect to ' + utils.decode_mac(self.address) + ' - ' + str(e))
         now = time.ticks_ms()
         while time.ticks_diff(time.ticks_ms(), now) < mswait:
             if self.connected:
                 break
         count += mswait
     return self.connected
Ejemplo n.º 3
0
    def disconnect(self):
        logging.info('Disconnecting...')
        try:
            conn = self.bt.gap_disconnect(self.conn_handle)
        except Exception as e:
            utils.log_error_to_file('ERROR: disconnect from ' + utils.decode_mac(self.address) + ' - ' + str(e))

        # Returns false on timeout
        timer = 0
        while self.connected:
            # print('.', end='')
            time.sleep(1)
            timer += 1
            if timer > 60:
                return False
        return True
Ejemplo n.º 4
0
 def scan_devices(self):
     self.scan_complete = False
     logging.info('Starting scan...')
     # Run a scan operation lasting for the specified duration (in milliseconds).
     # Use interval_us and window_us to optionally configure the duty cycle.
     # The scanner will run for window_us microseconds every interval_us microseconds for a total of duration_ms milliseconds.
     # The default interval and window are 1.28 seconds and 11.25 milliseconds respectively (background scanning).
     #
     # Scan for 60s (at 100% duty cycle)
     duration_ms = 60000 # milliseconds
     interval_us = 30000 # microseconds
     window_us   = 30000 # microseconds
     try:
         self.bt.gap_scan(duration_ms, interval_us, window_us)
     except Exception as e:
         utils.log_error_to_file('ERROR: scan - ' + str(e))
         
     while not self.scan_complete:
         pass
Ejemplo n.º 5
0
    def read_data(self, value_handle):
        self.read_flag = False

        logging.info('Reading data...')
        try:
            self.bt.gattc_read(self.conn_handle, value_handle)
        except Exception as e:
            utils.log_error_to_file('ERROR: read from ' + utils.decode_mac(self.address) + ' - ' + str(e))
            return False

        # Returns false on timeout
        timer = 0
        while not self.read_flag:
            # print('.', end='')
            time.sleep(1)
            timer += 1
            if timer > 60:
                return False
        return True
Ejemplo n.º 6
0
    def write_data(self, value_handle, data):
        self.write_flag = False
        self.write_status = -1

        # Checking for connection before write
        self.connect()
        logging.debug('Writing data...')
        try:
            self.bt.gattc_write(self.conn_handle, value_handle, data, 1)
        except Exception as e:
            utils.log_error_to_file('ERROR: write to ' + utils.decode_mac(self.address) + ' - ' + str(e))
            return False

        # Returns false on timeout
        timer = 0
        while not self.write_flag:
            # print('.', end='')
            time.sleep(1)
            timer += 1
            if timer > 60:
                return False
        return self.write_status == 0
Ejemplo n.º 7
0
def update_time():
    try:
        ntptime.settime()
        logging.info('Time updated: {}', utils.timestamp())
    except Exception as e:
        utils.log_error_to_file('ERROR: ntptime settime - ' + str(e))
Ejemplo n.º 8
0
            if myBLE.get_reading():
                message = '{"temperature": "' + str(myBLE.temperature) + '", '
                message = message + '"humidity": "' + str(
                    myBLE.humidity) + '", '
                message = message + '"batteryLevel": "' + str(
                    myBLE.battery_level) + '", '
                message = message + '"batteryVoltage": "' + str(
                    myBLE.battery_voltage) + '"}'
                logging.debug('Message: {}', message)
                topic = topic_pub + '/' + ''.join('{:02x}'.format(b)
                                                  for b in myBLE.address)
                logging.debug('Topic: {}', topic)
                try:
                    mqtt_client.publish(topic, message)
                except Exception as e:
                    utils.log_error_to_file('ERROR: publish to MQTT - ' +
                                            str(e))
                    try:
                        mqtt_client.disconnect()
                        mqtt_client = connect_mqtt()
                    except OSError as e:
                        restart_and_reconnect()

        if oldest_read > myBLE.last_read:
            oldest_read = myBLE.last_read

    # Wait for the next cycle
    now = time.time()
    if oldest_read < now:
        delay = read_interval - now + oldest_read
        if delay > 0:
            print('--------------------------------------------------')