Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Error: Failed to discover services and characteristics: DISCONNECTED" on Raspberry Pi 4 B #3

Closed
dnicolson opened this issue Feb 16, 2020 · 3 comments

Comments

@dnicolson
Copy link
Contributor

The following code runs without issue on macOS but on a Raspberry Pi 4 B running Raspbian Buster Lite (2020-02-05) the code usually fails after 1 or more commands:

const Switchbot = require('node-switchbot');
const switchbot = new Switchbot();

(async () => {
  try {
    const deviceList = await switchbot.discover({ model: 'H', quick: true });
    const device = deviceList[0];
    await device.turnOn();
    await switchbot.wait(5000);
    await device.turnOff();
    await switchbot.wait(5000);
    await device.turnOn();
  } catch(e) {
    console.log(e);
  }
  process.exit();
})();

The hcidump.log log shows the output from sudo hcidump -t -x when an exception was raised when running the final device.turnOn command.

The issue is perhaps related to noble as other tools like gatttool do not appear to be affected. In my testing the issue can be mitigated somewhat by replacing calls to discoverAllServicesAndCharacteristics with discoverServices.

@futomi
Copy link
Collaborator

futomi commented Feb 18, 2020

Thank you for your feedback.

I checked the source code of the discoverAllServicesAndCharacteristics() method in the noble. As you mentioned, the method seems to be unstable as far as I saw the relevant codes. Asynchronous methods seems to be called in parallel.

I replaced the discoverAllServicesAndCharacteristics() with the discoverServices() and discoverCharacteristics() methods. I hope it works well. Try the latest version.

@dnicolson
Copy link
Contributor Author

Thank you for your quick response.

I think the reliability has improved but I think the issue lies elsewhere.

Initial connection errors occur in both examples, but the Python example seems to always complete the commands:

import binascii
import time
from bluepy.btle import Peripheral

p = Peripheral('ff:ff:ff:ff:ff:ff', 'random')
hand_service = p.getServiceByUUID('cba20d00-224d-11e6-9fb8-0002a5d5c51b')
hand = hand_service.getCharacteristics('cba20002-224d-11e6-9fb8-0002a5d5c51b')[0]

hand.write(binascii.a2b_hex('570101'))
time.sleep(5)
hand.write(binascii.a2b_hex('570102'))
time.sleep(5)
hand.write(binascii.a2b_hex('570101'))
time.sleep(5)
hand.write(binascii.a2b_hex('570102'))
time.sleep(5)

p.disconnect()

This leads me to believe there might some event handling difference in noble.

I believe the major issue is with the hardware and Raspian though, the interference between Wi-Fi and Bluetooth seems to be common. Most of the errors occur while Homebridge is running using the Wi-Fi. At times Homebridge needs to be quit because the messages from gatttool alternate between "Characteristic Write Request failed: Request attribute has encountered an unlikely error" and "connect error: Function not implemented (38)".

@futomi
Copy link
Collaborator

futomi commented Feb 19, 2020

The turnOn() method of this module automatically connect the Bot in advance if no connection is established, then moves the arm, finally automatically closes the connection.

If you want to move the arm repeatedly, it is strongly recommended to call the connect() method in advance, finally call the disconnect() method.

const Switchbot = require('node-switchbot');
const switchbot = new Switchbot();

(async () => {
  try {
    const deviceList = await switchbot.discover({ model: 'H', quick: true });
    const device = deviceList[0];
    await device.connect();
    await device.turnOn();
    await switchbot.wait(5000);
    await device.turnOff();
    await switchbot.wait(5000);
    await device.turnOn();
    await switchbot.wait(5000);
    await device.disconnect();
  } catch(e) {
    console.log(e);
  }
  process.exit();
})();

See the description of the methods in the README.md for more details.
https://github.com/futomi/node-switchbot#SwitchbotDevice-connect-method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants