• I've found the python3 example code by now -but somehow this does not work on my pi:

    pi@millow:~ $ python3 bluepi_uart.py c4:f0:3e:4d:c2:9d
    Traceback (most recent call last):
      File "bluepi_uart.py", line 34, in <module>
      File "/home/pi/.local/lib/python3.5/site-pack­ages/bluepy/btle.py", line 200, in write
        return self.peripheral.writeCharacteristic(self­.valHandle, val, withResponse)
      File "/home/pi/.local/lib/python3.5/site-pack­ages/bluepy/btle.py", line 542, in writeCharacteristic
        self._writeCmd("%s %X %s\n" % (cmd, handle, binascii.b2a_hex(val).decode('utf-8')))
    TypeError: a bytes-like object is required, not 'str'

    This is on a pi with python 3.5, bluepi itself seems to work and run. Also I would like to use python3 - but I am really not an expert here. Any ideas?

  • Mon 2019.08.12

    Hello @hansamann, a bit puzzled by your post, you do realize this forum provides support for boards running Espruino Javascript?

    I don't see how your post applies?

  • @Robin, @hansamann is aware of that... I confidently assume, because he has puck specific forum posts since quite a while ago. In this conversation he asks about an issue connecting to MDBT42 (on puck, pixl or plain MDBT42) from a raspi using python 2.7 / 3.5.

    @hansamann, I had some heavy exposure to python in the apple heart study in 2018, but that was 2.7. Did not venture into 3.5. From the error it looks you pass a string (command[0:20]). A string looks to me usually as a byte (array) object, but with uni-code other than 8-bit, it may be a word (16-bit word) object. May be you have to pass a 8-bit byte array? ...the 20 first chars as 8-bit of this command string as a byte object? - ...my take from this stack trace...

  • Just to be sure, you're using this code? https://www.espruino.com/Interfacing#pyt­hon

    As-is, or did you tweak command? It's possible that there's some edge case if command was exactly a multiple of 20 characters (or something like that)?

  • Visit google. Type in TypeError: a bytes-like object is required, not 'str', pthyon encode
    and python decode.
    I'd say replacing decode by encode might fix the error.

  • Thx all, trying this out next time I am close to my pixl.js, might have a chance to try on a puck earlier.

  • Hi Gordon,
    that's the code I used - initially. I added some () for the prints to make them python3 compatible, the version I have on the pi is 3.5 (not the latest, also complicated, as string substitution with these fStrings - yeah - is not possible for example...).

    I'll give the python3 recommendations a try back in the office (wed), for now I tried to go with useing node.js - to start with I wanted to use my macbook with mojave. So the whole noble lib seems a mess - as the docs suggest I went with noble-mac now.

    Interestingly, I was able to use my new pixl.js with the example - the code example found the device, connected, sent the command and disconnected. I did need to switch from : to - in the device address -somehow the devices addresses have - for me...

    Now at home tried it with a puck, latest version.

    pro:pixltests i511648$ node puckjs.js 
    Noble: stateChange -> poweredOn
    Found device:  80-82-23-2d-de-d5
    Found device:  80-82-23-2d-de-d5
    Found device:  ef-22-d8-d6-2e-71
    BT> Connecting
    Found device:  ef-22-d8-d6-2e-71
    BT> Connecting
    BT> Connected
    BT> Connected

    All I changed in my code, see below, is the device address. I connected via the web ide, NRF.getAddress() and replace the : with - so the device is found.

    Really the same code (https://www.espruino.com/Interfacing#nod­e-js) except the address line changed to:

    var ADDRESS = "ef:22:d8:d6:2e:71".replace(/:/g, "-");

    BTW find it super amazing that the browser with web bluetooth now seems to be the most stable BLE api you can find out there :-)

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

Example code for connecting from a rpi with node.js or python3

Posted by Avatar for hansamann @hansamann