Coder Social home page Coder Social logo

janickr / kaspersmicrobit Goto Github PK

View Code? Open in Web Editor NEW
13.0 13.0 0.0 20.62 MB

A python package to connect to the Bluetooth LE GATT services of BBC micro:bit devices. Use your micro:bit as a wireless game controller!

Home Page: https://kaspersmicrobit.readthedocs.io/en/stable/

License: Mozilla Public License 2.0

Python 100.00%
bbc-microbit ble bluetooth bluetooth-low-energy microbit python

kaspersmicrobit's Issues

Python program freeze when using microbit.uart.send_string()

/It seems like whenever the microbit.uart.send_string() gets run in my script it just stops runnig. I tried debugging it but with no result.

My code:

from kaspersmicrobit import KaspersMicrobit
from kaspersmicrobit.bluetoothprofile.services import Service

uart_available = False
loop = False

with KaspersMicrobit.find_one_microbit(microbit_name='patat') as microbit:
    print(f'Bluetooth address: {microbit.address()}')
    print(f"Device name: {microbit.generic_access.read_device_name()}")

    if microbit.uart.is_available():
        print(f'{Service.UART} available')
        uart_available = True
    else:
        print(f'{Service.UART} not found')
        

    if uart_available:
        loop = True
        
        def verwerk_string(string: str):
            print(f"Received from microbit: '{string}'") 
            
            if string == "Test pc":
                print("Test pc ontvangen")
                microbit.uart.send_string("\nTest microbit\n")
                
            if string == "ontvangen":
                print("gelukt het werkt")
        

        while loop:
            microbit.uart.receive_string(verwerk_string)
            print("looping")

Error importing kasper

Hi,
I installed kaspersmicrobit using pip on my python 3.9.1 system running on Windows/10 and got no errors, see below:

C:\Users\Raspberry\AppData\Local\Programs\Python\Python39\Scripts>pip install kaspersmicrobit
Collecting kaspersmicrobit
Downloading kaspersmicrobit-0.3.3-py3-none-any.whl (44 kB)
---------------------------------------- 44.6/44.6 kB 2.3 MB/s eta 0:00:00
Collecting bleak
Downloading bleak-0.15.1-py2.py3-none-any.whl (123 kB)
---------------------------------------- 124.0/124.0 kB 7.1 MB/s eta 0:00:00
Collecting typing-extensions>=4.2.0
Downloading typing_extensions-4.3.0-py3-none-any.whl (25 kB)
Collecting bleak-winrt>=1.1.1
Downloading bleak_winrt-1.1.1-cp39-cp39-win_amd64.whl (345 kB)
---------------------------------------- 345.7/345.7 kB 10.5 MB/s eta 0:00:00
Installing collected packages: bleak-winrt, typing-extensions, bleak, kaspersmicrobit
WARNING: The script bleak-lescan.exe is installed in 'c:\users\raspberry\appdata\local\programs\python\python39\Scripts' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed bleak-0.15.1 bleak-winrt-1.1.1 kaspersmicrobit-0.3.3 typing-extensions-4.3.0

After this I started python 3.9.1 and executed this one line:
from kaspersmicrobit import KaspersMicrobit

but got the following errors:

Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

from kaspersmicrobit import KaspersMicrobit
Traceback (most recent call last):
File "", line 1, in
File "C:\Users\Raspberry\AppData\Local\Programs\Python\Python39\lib\site-packages\kaspersmicrobit_init_.py", line 5, in
from .kaspersmicrobit import KaspersMicrobit # noqa: F401
File "C:\Users\Raspberry\AppData\Local\Programs\Python\Python39\lib\site-packages\kaspersmicrobit\kaspersmicrobit.py", line 14, in
from .services.io_pin import IOPinService
File "C:\Users\Raspberry\AppData\Local\Programs\Python\Python39\lib\site-packages\kaspersmicrobit\services\io_pin.py", line 181, in
class IOPinService:
File "C:\Users\Raspberry\AppData\Local\Programs\Python\Python39\lib\site-packages\kaspersmicrobit\services\io_pin.py", line 198, in IOPinService
def notify_data(self, callback: Callable[[[PinValue]], None]):
File "C:\Users\Raspberry\AppData\Local\Programs\Python\Python39\lib\typing.py", line 869, in getitem
return self.getitem_inner(params)
File "C:\Users\Raspberry\AppData\Local\Programs\Python\Python39\lib\typing.py", line 262, in inner
return func(*args, **kwds)
File "C:\Users\Raspberry\AppData\Local\Programs\Python\Python39\lib\typing.py", line 879, in getitem_inner
args = tuple(_type_check(arg, msg) for arg in args)
File "C:\Users\Raspberry\AppData\Local\Programs\Python\Python39\lib\typing.py", line 879, in
args = tuple(_type_check(arg, msg) for arg in args)
File "C:\Users\Raspberry\AppData\Local\Programs\Python\Python39\lib\typing.py", line 151, in _type_check
raise TypeError(f"{msg} Got {arg!r:.100}.")
TypeError: Callable[[arg, ...], result]: each arg must be a type. Got [<class 'kaspersmicrobit.services.io_pin.PinValue'>].

Am I doing something wrong?

bleak.exc.BleakError: Device with address D6:66:C1:A7:CF:0F was not found.

I have installed Python 3.10.7 and pip installed kaspersmicrobit

I am running this code:

 import time
 from kaspersmicrobit import KaspersMicrobit
 CHANGE_THIS_TO_YOUR_MICROBIT_BLUETOOTH_ADDRESS = 'D6:66:C1:A7:CF:0F'
 def pressed(button):
     print(f"button {button} pressed")  
 with KaspersMicrobit(CHANGE_THIS_TO_YOUR_MICROBIT_BLUETOOTH_ADDRESS) as microbit:
     microbit.buttons.on_button_a(press=pressed)
     time.sleep(10)

and getting this error:

Traceback (most recent call last):
File "Y:\Source\Python_Python3_MyTools\Microbits\kaspers_microbit_test.py", line 14, in
with KaspersMicrobit(CHANGE_THIS_TO_YOUR_MICROBIT_BLUETOOTH_ADDRESS) as microbit:
File "C:\Users\Raspberry\AppData\Local\Programs\Python\Python310\lib\site-packages\kaspersmicrobit\kaspersmicrobit.py", line 103, in enter
self.connect()
File "C:\Users\Raspberry\AppData\Local\Programs\Python\Python310\lib\site-packages\kaspersmicrobit\kaspersmicrobit.py", line 120, in connect
self._device.connect()
File "C:\Users\Raspberry\AppData\Local\Programs\Python\Python310\lib\site-packages\kaspersmicrobit\bluetoothdevice.py", line 68, in connect
self.loop.run_async(self.client.connect()).result()
File "C:\Users\Raspberry\AppData\Local\Programs\Python\Python310\lib\concurrent\futures_base.py", line 458, in result
return self.__get_result()
File "C:\Users\Raspberry\AppData\Local\Programs\Python\Python310\lib\concurrent\futures_base.py", line 403, in __get_result
raise self._exception
File "C:\Users\Raspberry\AppData\Local\Programs\Python\Python310\lib\site-packages\bleak\backends\winrt\client.py", line 213, in connect
raise BleakError(
bleak.exc.BleakError: Device with address D6:66:C1:A7:CF:0F was not found.

I have tried to add a screen-shot below which should show that:
I can see in Windows Settings \ Bluetooth that my microbit (vatig) is paired but not connected.
and in More Bluetooth Options, in the Hardware tab / Details / Association Endpoint Address = d6: 66:c1:a7:cf:0f

I have downloaded the simple script to my V1 microbits, which looks like this in Javascript:

bluetooth.onBluetoothConnected(function () {
    basic.showString("C")
})
bluetooth.onBluetoothDisconnected(function () {
    basic.showString("X")
})
bluetooth.startButtonService()
bluetooth.startAccelerometerService()
bluetooth.startIOPinService()
bluetooth.startLEDService()
bluetooth.startTemperatureService()
basic.showLeds(`
    # . . # #
    # . . . #
    # # # . .
    # . # . .
    # # # . .
    `)

What else can I do to try to diagnose this problem - I have tried with 3 different v1 microbits ?

Regards,
Martyn
vatig_ethernet_addr

[bug] bleak.exc.BleakError: Characteristic (...) was not found!

With line: print(microbit.accelerometer.read())

I get error:

Traceback (most recent call last):
  File "C:\Users\Miniontoby\Documents\python\controllers\microbit_bluetooth.py", line 17, in <module>
    print(microbit.accelerometer.read())
  File "C:\Users\Miniontoby\AppData\Local\Programs\Python\Python310\lib\site-packages\kaspersmicrobit\services\accelerometer.py", line 105, in read
    return AccelerometerData.from_bytes(self._device.read(Characteristic.ACCELEROMETER_DATA))
  File "C:\Users\Miniontoby\AppData\Local\Programs\Python\Python310\lib\site-packages\kaspersmicrobit\bluetoothdevice.py", line 78, in read
    result = self.loop.run_async(self.client.read_gatt_char(characteristic.value)).result()
  File "C:\Users\Miniontoby\AppData\Local\Programs\Python\Python310\lib\concurrent\futures\_base.py", line 446, in result
    return self.__get_result()
  File "C:\Users\Miniontoby\AppData\Local\Programs\Python\Python310\lib\concurrent\futures\_base.py", line 391, in __get_result
    raise self._exception
  File "C:\Users\Miniontoby\AppData\Local\Programs\Python\Python310\lib\site-packages\bleak\__init__.py", line 571, in read_gatt_char
    return await self._backend.read_gatt_char(char_specifier, **kwargs)
  File "C:\Users\Miniontoby\AppData\Local\Programs\Python\Python310\lib\site-packages\bleak\backends\winrt\client.py", line 720, in read_gatt_char
    raise BleakError(f"Characteristic {char_specifier} was not found!")
bleak.exc.BleakError: Characteristic E95DCA4B-251D-470A-A062-FA1922DFA9A8 was not found!

Sorry, but I have to get dinner now, so more details will follow up, but for now this is all I have

Update: This is on a Version 1 microbit

UPDATE: It was the hex file missing the accelerometer service, but yeah it now just keeps on giving me the 020 error even with the fix thingy that I use...

io_pin : can you explain how to read/write to 1 particular pin?

The example microbit-io_pin.py works fine, but reads all defined input ports. How can I read the value of 1 port i.e: Pin1?

Same for writing data to an output port. Say I have a led on pin0, which I configured as a digital output port. How would I write exactly to this pin0?

I am a total noob in python, so might be the reason why I can't figure this out ;-)

Let me know if kaspersmicrobit works on macOS!

I don't have a mac to test kaspersmicrobit on. Let me know in a comment if it works!
First upgrade to the latest version:

$  pip install --upgrade kaspersmicrobit  

Please mention:

  • the version of your micro:bit
  • the python version you used
  • if you used "No pairing required" or "JustWorks pairing"

Cannot detect microbit

When I try running the given program (in this link: https://kaspersmicrobit.readthedocs.io/en/stable/), I get an error like this:
"kaspersmicrobit.errors.BluetoothServiceNotFound: Could not find the service Service.BUTTON (uuid=e95d9882-251d-470a-a062-fa1922dfa9a8)
The available services on this micro:bit are:

  • Generic Access Profile (Service.GENERIC_ACCESS uuid=00001800-0000-1000-8000-00805f9b34fb)
  • Generic Attribute Profile (Service.GENERIC_ATTRIBUTE uuid=00001801-0000-1000-8000-00805f9b34fb)
  • Nordic Semiconductor ASA (None uuid=0000fe59-0000-1000-8000-00805f9b34fb)
  • Unknown (None uuid=e97dd91d-251d-470a-a062-fa1922dfa9a8)
  • Device Information (Service.DEVICE_INFORMATION uuid=0000180a-0000-1000-8000-00805f9b34fb)
  • MicroBit Event Service (Service.EVENT uuid=e95d93af-251d-470a-a062-fa1922dfa9a8)

Is this micro:bit loaded with the correct ".hex" file?"
I have the correct hex file and my microbit does get paired but, for some reason, it's not able to detect any services. Can anyone help me?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.