nathankellenicki / node-poweredup

A Javascript module to interface with LEGO Powered Up components.
https://nathankellenicki.github.io/node-poweredup/
MIT License
478 stars 59 forks source link

Unable to connect consistently on Pi 4 #110

Closed jncraton closed 3 years ago

jncraton commented 3 years ago

I've been working on getting this running on a Pi 4, and I'm having difficulty connecting using the example code. I'm using a PUP Hub with a linear motor and train motor attached. This is the output that I see when I run the example code from the readme using sudo node ./example.js:

Scanning for Hubs...
Discovered HUB NO.4!   
Discovered HUB NO.4! 
Discovered HUB NO.4!
Discovered HUB NO.4!

There is a multiple second delay (~6 seconds) between each hub discovery. During that time, connect() is attempted and the hub LED becomes solid white, but then goes back to flashing. After many attempts, the hub will usually connect and be controllable. Once properly connected, the connection is stable. I've successfully used it for around an hour without issue.

The following have already been tried:

I've looked around for similar issues, and it looks like this may be an underlying issue with Noble:

https://github.com/abandonware/noble/issues/99

Does anyone have ideas on how to begin to address this?

More details

The output of uname -a is:

Linux raspberrypi 5.4.51-v7l+ #1333 SMP Mon Aug 10 16:51:40 BST 2020 armv7l GNU/Linux

The complete output of sudo DEBUG=* node example.js is included here. In this case, a successful connection was made after 4 attempts.

dlech commented 3 years ago

I read somewhere recently that there was issues with the Bluetooth firmware on RPi 3/4 and rolling back the firmware fixed the connectivity issues.

jncraton commented 3 years ago

Thanks for this. I see a somewhat similar firmware issue here:

https://github.com/raspberrypi/firmware/issues/1447

And here:

https://github.com/Hexxeh/rpi-firmware/issues/238

I attempted a rollback to the same firmware version specified in that thread:

rpi-update 8382ece2b30be0beb87cac7f3b36824f194d01e9

That did not resolve the issue for me. However, it may have helped. I'm now able to reliable and repeatedly connect to the hub for any amount of time, disconnect, and reconnect again using noble directly:

const noble = require('@abandonware/noble');

noble.on('stateChange', async (state) => {
  console.log("Starting...")
  if (state === 'poweredOn') {
    console.log("Scanning...")
    await noble.startScanningAsync();
  }
});

noble.on('discover', async (h) => {
  if (h['address'] == {hub address here} ) {
    console.log(h['address'])
    await noble.stopScanningAsync();
    await h.connectAsync();
    console.log("Connected")
    await new Promise(r => setTimeout(r, 8000));
    console.log("Disconnecting")
    await h.disconnectAsync();
    await noble.startScanningAsync();
  }
});

I still see the same behavior as before using node-poweredup.

jncraton commented 3 years ago

I was able to get this working by rolling back the Bluetooth firmware specifically. If others are trying to solve this, here is the solution that worked for me:

wget http://archive.raspberrypi.org/debian/pool/main/b/bluez-firmware/bluez-firmware_1.2-4+rpt2_all.deb
sudo dpkg -i bluez-firmware_1.2-4+rpt2_all.deb

I then rebooted to make sure the new (old) firmware was in use.