schodet / nxt-python

NXT-Python is a python driver/interface for the Lego Mindstorms NXT robot based on NXT_python.
https://ni.srht.site/nxt-python/latest/
GNU General Public License v3.0
233 stars 72 forks source link

Bluetooth Error #193

Open Josh-Hout opened 1 year ago

Josh-Hout commented 1 year ago

Im Trying to run code over Bluetooth

By the way Im Completely new to nxt-python

this is my simple program found in the nxt doc

import nxt.locator
from nxt.motor import *

#00:16:53:15:DC:F2 is my NXTs bluetooth MAC adress

with nxt.locator.find(host="00:16:53:15:DC:F2") as b:
    print(b.get_device_info()[0:2])

I ran the program and got a result of

failed to connect to device Bluetooth (00:16:53:15:DC:F2)
Traceback (most recent call last):
  File "/home/joshua/Mindstorms/Log.py", line 6, in <module>
    with nxt.locator.find(host="00:16:53:15:DC:F2") as b:
  File "/home/joshua/.local/lib/python3.9/site-packages/nxt/locator.py", line 215, in find
    raise BrickNotFoundError("no brick found")
nxt.locator.BrickNotFoundError: no brick found

And i Do have python3-bluez

Am i doing something wrong?

Josh-Hout commented 1 year ago

Running with Debug i got more info

DEBUG:nxt.locator:configuration files=['.nxt-python.conf', '/home/joshua/.nxt-python.conf']
DEBUG:nxt.locator:configuration read from []
DEBUG:nxt.locator:no section default, using DEFAULT
INFO:nxt.locator:using backend from nxt.backend.usb
INFO:nxt.locator:using backend from nxt.backend.bluetooth
INFO:nxt.backend.bluetooth:connecting via Bluetooth (00:16:53:15:DC:F2)
WARNING:nxt.backend.bluetooth:failed to connect to device Bluetooth (00:16:53:15:DC:F2)
DEBUG:nxt.backend.bluetooth:error from connect
Traceback (most recent call last):
  File "<string>", line 3, in connect
_bluetooth.error: (111, 'Connection refused')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/joshua/.local/lib/python3.9/site-packages/nxt/backend/bluetooth.py", line 127, in find
    brick = sock.connect()
  File "/home/joshua/.local/lib/python3.9/site-packages/nxt/backend/bluetooth.py", line 52, in connect
    sock.connect((self._host, PORT))
  File "<string>", line 5, in connect
bluetooth.btcommon.BluetoothError: [Errno 111] Connection refused
schodet commented 1 year ago

Is your NXT paired with your computer?

How did you get the device address?

I should add something in the manual for a step by step procedure to connect for each platform.

Josh-Hout commented 1 year ago

I got the device address by using usb but then I realized that won’t work because my robot would move

Josh-Hout commented 1 year ago

Im also getting better errors im only getting A device is busy error

schodet commented 1 year ago

Is your NXT paired with your computer?

Josh-Hout commented 1 year ago

I can only connect with serial port

Josh-Hout commented 1 year ago

But it is connected

dmue-vbg commented 1 year ago

got similar issue (on macos) :

DEBUG:nxt.locator:configuration read from [] DEBUG:nxt.locator:no section default, using DEFAULT INFO:nxt.locator:using backend from nxt.backend.usb

INFO:nxt.backend.bluetooth:no bluetooth module

Traceback (most recent call last): File "/Users/dennis/Desktop/nxt_test.py", line 16, in with nxt.locator.find(host="00:16:53:1B:AC:97") as b: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/nxt/locator.py", line 215, in find raise BrickNotFoundError("no brick found") nxt.locator.BrickNotFoundError: no brick found

schodet commented 1 year ago

@Josh-Hout Here is what I do to make it work:

% bluetoothctl
Agent registered
[bluetooth]# devices
[bluetooth]# scan on
Discovery started
[NEW] Device 00:16:53:AA:BB:CC 00-16-53-AA-BB-CC
[CHG] Device 00:16:53:AA:BB:CC LegacyPairing: no
[CHG] Device 00:16:53:AA:BB:CC Name: NXT1
[CHG] Device 00:16:53:AA:BB:CC Alias: NXT1
[CHG] Device 00:16:53:AA:BB:CC LegacyPairing: yes
[bluetooth]# pair 00:16:53:AA:BB:CC
Attempting to pair with 00:16:53:AA:BB:CC
Request PIN code
(here, enter code 1234 on NXT screen)
[agent] Enter PIN code: 1234
[CHG] Device 00:16:53:AA:BB:CC Connected: yes
[CHG] Device 00:16:53:AA:BB:CC UUIDs: 00001101-0000-1000-8000-00805f9b34fb
[CHG] Device 00:16:53:AA:BB:CC ServicesResolved: yes
[CHG] Device 00:16:53:AA:BB:CC Paired: yes
Pairing successful
[CHG] Device 00:16:53:AA:BB:CC ServicesResolved: no
[CHG] Device 00:16:53:AA:BB:CC Connected: no
[bluetooth]# scan off
Discovery stopped
[bluetooth]# devices
Device 00:16:53:AA:BB:CC NXT1
[bluetooth]#

Now I can run a nxt-python script using the 00:16:53:AA:BB:CC address.

If I keep bluetoothctl open, during the script run, I can see:

[CHG] Device 00:16:53:AA:BB:CC Connected: yes
[CHG] Device 00:16:53:AA:BB:CC Connected: no

If it does not work, try to remove the NXT from known devices and start again.

schodet commented 1 year ago

@dmue-vbg

INFO:nxt.backend.bluetooth:no bluetooth module

That’s not the same problem, you do not have a python bluetooth module installed.

See: https://ni.srht.site/nxt-python/latest/installation.html#installation-using-pip

That’s not easy because the current state of pybluez is not good.

dmue-vbg commented 1 year ago

@schodet unfortunately i already tried all this installation procedures with a bunch of variations but without any success :( i don't give up hope that i will find a way. thank you for your reply.

schodet commented 1 year ago

@dmue-vbg look at nxt/backend/bluetooth.py, it gives this error message when it is not able to import bluetooth. You can try to import bluetooth in a python script, you may have a more useful feedback.