Coder Social home page Coder Social logo

Comments (5)

tristanzwart avatar tristanzwart commented on July 18, 2024 2

Ok it works now I changed it to just works pairing and also now using the bluethooth adress instead of its name.

The final code:

`import logging
import time

logging.basicConfig(level=logging.INFO)

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

uart_available = False

with KaspersMicrobit('EC:CA:9C:CC:49:7E') 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:

    class Ontvanger:
        def __init__(self):
            self.gelukt = False

        def verwerk_string(self, string: str):
            print(f"Received from microbit: '{string}'")

            if string == "Test pc":
                print("Test pc ontvangen")
                microbit.uart.send_string("Test microbit\n")

            if string == "ontvangen":
                print("gelukt het werkt")
                self.gelukt = True  # stop the program 


    ontvanger = Ontvanger()
    microbit.uart.receive_string(ontvanger.verwerk_string)  # don't do this in a loop

    while not ontvanger.gelukt:
        print("wachten op gegevens van de microbit...")
        #microbit.uart.receive_string(ontvanger.verwerk_string)
        time.sleep(1) # wait a little to not spam the console with text

`
image

from kaspersmicrobit.

janickr avatar janickr commented on July 18, 2024 2

You can use pairing and the connection method with the address if you want, but it should also work with the "no pairing required" makecode project setting if your microbit is not paired + the find_one_microbit method (with or without a name). Have fun!

from kaspersmicrobit.

tristanzwart avatar tristanzwart commented on July 18, 2024 1

Ok thanks for responding so fast to my issue. I will test it today. And will come back if there is anything else. If not I will close the issue.

Also I found it especially strange that it did not work with the function because it was listed as an example on the site. Anyway thanks to your bugfix that should now work. Also I did eventually want to add something to break the loop I just made this script fast and forgot about it.

My friend is maybe going to get a mack. So I will update you on that if I do not forget.

from kaspersmicrobit.

janickr avatar janickr commented on July 18, 2024

Hi @tristanzwart ,

Indeed, there was a bug in kaspersmicrobit: when calling a method on kaspersmicrobit from within a callback, verwerk_string in this case, the execution of the program could block. I fixed it in the latest release. You can upgrade to the latest version with

pip install --upgrade kaspersmicrobit  

But there are also some issues with your example:

  • you only need to call microbit.uart.receive_string(verwerk_string) once, before the loop: it registers a callback, any time your program receives data from the microbit verwerk_string will be called
  • To stop the program, loop should be set to false: to be able to do that I added verwerk_string and loop renamed to gelukt to a class and set gelukt to True when the received text is "ontvangen"
  • In your makecode project wait a few seconds after bluetooth connected, before you send "Test pc", so that the verwerk_string callback is registered before the "Test pc" string is sent (else you may miss it) see link to Makecode project below.

this is a working version:

import logging
import time


logging.basicConfig(level=logging.INFO)


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

uart_available = False

with KaspersMicrobit.find_one_microbit() 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:

        class Ontvanger:
            def __init__(self):
                self.gelukt = False

            def verwerk_string(self, string: str):
                print(f"Received from microbit: '{string}'")
                microbit.uart.send_string("meh\n")

                if string == "Test pc":
                    print("Test pc ontvangen")
                    microbit.uart.send_string("Test microbit\n")

                if string == "ontvangen":
                    print("gelukt het werkt")
                    self.gelukt = True  # stop the program 


        ontvanger = Ontvanger()
        microbit.uart.receive_string(ontvanger.verwerk_string)  # don't do this in a loop

        while not ontvanger.gelukt:
            print("wachten op gegevens van de microbit...")
            time.sleep(1) # wait a little to not spam the console with text

with a hex file created from this makecode project (I assumed this is more or less what you had):
https://makecode.microbit.org/_LLPWVbe7vgxx

from kaspersmicrobit.

tristanzwart avatar tristanzwart commented on July 18, 2024

I used the code you gave me but it did not work untill I put it in the loop again. This time my microbit does recieve it so that seems to work now. Lastly the conection between the program and my microbit seem to be less stable and just disconects after a while. And I do have the project setting as they should be.

Update this time it did work but it did not recive the ontvangen on the python side. So maybe I should add a dalay.

Update 2 now it seems like my microbit is haning as in it froze. it recieves it the if statement gets triggerd then it just stops executing (I am using a v2)

Update 3 Now my microbit does send the the tekst and does not hang any more but the python script does not detect it

Update 4 It seems like the python side is having issues staying connected or even conecting in the first place. It sometimes gives me OSError WinError -2147023609

from kaspersmicrobit.

Related Issues (7)

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.