Using Bluez with MDBT42Q.

Posted on
  • Hi Espruino Community,
    I am trying to use Bluez(gatttool) with MDBT42Q. I don't have much experience with neither of them but for another micro-controller called simblee (not sold anymore), I was able to write on pins through bluez gatttool. With espruino, I had little bit of luck in the steps so far as I can connect to espruino from terminal and write something(which I don't know what is it.

    It is what happened with espruino. Usually when I do write with simblee it writes to 0 pin of that micro-controller but there is no handle it says. So I generated 100 handle and I found some handle that works.

    and when I wrote on those handle it worked properly. But it none of the pins are giving me any voltage.

    Can anyone please tell me what should I do that will let me write on the pins through terminal? Thanks.

  • Hello @user101743

    Is the intention to include images in that post? If so, are they being deployed to a remote server with a fully qualified URL so that they may be picked up during rendering?

    'Formatting help' below implies this, but is the solution to get images to render.


    'what should I do that will let me write on the pins through terminal'
    https://www.espruino.com/Reference#l__gl­obal_digitalWrite
    https://www.espruino.com/Reference#t_l__­global_getPinMode
    https://www.espruino.com/Reference#t_l__­global_pinMode

    There are examples just below that reference topic. That should get you started. Then for future issues, post code here for others to assist.

  • @Robin to see the image you have to do new tab by right clicking. And I am trying to write on pins through the terminal.

  • Google asks for a login. I assume something is not properly set up for sharing.

    And if you have the MDBT42, just

    LED1.toggle()
    LED2.toggle()
    

    should toggle the two LEDs. Or Dxx.set() / Dxx.reset().

  • The image sharing was a mistake from my end but you can see it now with new tab. And I want to turn on and off through terminal of linux os lets say.

  • 'to see the image you have to do new tab by right clicking'

    A bit unconventional, but adding that text below the image should suffice on future posts. Just wasn't intuitive as no rollover text. It is most likely not rendering as the image links do not end with a valid image extention.

    Although I have no idea with the tool that is being used, you are aware that a valid Javascript instruction must be submitted? It is possible that tool isn't forwarding Javascript?

    Any luck trying this example?

    https://www.espruino.com/Flashing+Lights­#line=11,15,45,56,64

    I would take five minutes and understand the code submit/interpretation process:

    https://www.espruino.com/Quick+Start+Cod­e#line=92

  • I have used this code before but I am interested in commanding through terminal as bluez was created to communicate to BLE. I have worked with other BLE and it worked but I don't know why I can't find voltage on the pins of espruino.

  • Mon 2019.08.05

    'I have used this code before'

    What code is being referred to as there isn't any provided in this thread?

    Has the WebIDE been used as a double check?
    How are the Javascript commands being sent to the MDBT42Q?

    There really isn't enough detail to make a definitive response. Not familiar with bluez.

    'I don't know why I can't find voltage on the pins of espruino'

    Let's try this way.

    Q1: Are we working with the module or the breakout board?

    http://www.espruino.com/MDBT42Q#pinout

    Q2: What voltage is being applied to power the MDBT42Q?
    Q3: . . . and to which pins, both power and ground?

    Q4: Which tutorial(s) have been attempted and what were the results?
    Q5: What was the result of digitalWrite() and pinMode()from post #2 above?
    Q6: What was the result of both examples from post #6 above?
    Q7: If breakout board, were the LED's able to be illuminated?
    Q8: If breakout board, what is the voltage at the pin adjacent to D14 labeled 3.3?
    Q9: If breakout board, what is the voltage at the pin adjacent to D7 labeled Vin?

    The responses to the questions above will assist in determining if we are dealing with a hardware or software issue.

  • My hardware properly as far as I have tested and also software.

    What code is being referred to as there isn't any provided in this thread?

    No, I am just using bluez to connect to the Bluetooth and commanding from their which works with other companies BLE. BlueZ is official Linux Bluetooth protocol stack. It is an Open Source project distributed under GNU General Public License (GPL). BlueZ kernel is part of the official Linux kernel since version 2.4.6. This is their official website-http://www.bluez.org/faq/common/

    Has the WebIDE been used as a double check?
    How are the Javascript commands being sent to the MDBT42Q?

    I have used the webIDE was capable of using usual commands and turning light on and off.

    Q1: Are we working with the module or the breakout board?
    What voltage is being applied to power the MDBT42Q?
    Q3: . . . and to which pins, both power and ground?

    Yes, the MDBT4Q breakout board. I have applied 5v through usb and checked all pins after I ran gatttool command.

    Q4: Which tutorial(s) have been attempted and what were the results?

    None. I am trying to implement these for a research as simblee is out of market.

    Q5: What was the result of digitalWrite() and pinMode()from post #2 above?
    Q6: What was the result of both examples from post #6 above?

    It worked properly with formatting and blinking. But for gattool it wrote something according to the terminal but there was no voltage output in any pin.

    Q7: If breakout board, were the LED's able to be illuminated?

    Yes.

    Q8: If breakout board, what is the voltage at the pin adjacent to D14 labeled 3.3?
    Q9: If breakout board, what is the voltage at the pin adjacent to D7 labeled Vin?

    They are according to the manual as 3.3v and 5V.

    This is the real problem. After writing I think I was supposed to get a voltage in some pin which I am not.


    1 Attachment

    • 3rd_espruino.png
  • After writing I think I was supposed to get a voltage in some pin which I am not.

    Why do you think you are supposed to get some voltage in some pin?

  • Hi! As I understand it, simblee had characteristics predefined that would change the state of pins when you wrote to them.

    Espruino is a bit more flexible - you can either write JavaScript code directly to the UART characteristic or you can make Espruino itself behave like simblee:

    JavaScript

    $ gatttool --device=fc:cc:b8:22:b0:42 --addr-type=random -I
    [fc:cc:b8:22:b0:42][LE]> connect
    Attempting to connect to fc:cc:b8:22:b0:42
    Connection successful
    [fc:cc:b8:22:b0:42][LE]> char-desc
    handle: 0x0001, uuid: 00002800-0000-1000-8000-00805f9b34fb
    handle: 0x0002, uuid: 00002803-0000-1000-8000-00805f9b34fb
    handle: 0x0003, uuid: 00002a00-0000-1000-8000-00805f9b34fb
    handle: 0x0004, uuid: 00002803-0000-1000-8000-00805f9b34fb
    handle: 0x0005, uuid: 00002a01-0000-1000-8000-00805f9b34fb
    handle: 0x0006, uuid: 00002803-0000-1000-8000-00805f9b34fb
    handle: 0x0007, uuid: 00002a04-0000-1000-8000-00805f9b34fb
    handle: 0x0008, uuid: 00002803-0000-1000-8000-00805f9b34fb
    handle: 0x0009, uuid: 00002aa6-0000-1000-8000-00805f9b34fb
    handle: 0x000a, uuid: 00002800-0000-1000-8000-00805f9b34fb
    handle: 0x000b, uuid: 00002800-0000-1000-8000-00805f9b34fb
    handle: 0x000c, uuid: 00002803-0000-1000-8000-00805f9b34fb
    handle: 0x000d, uuid: 6e400003-b5a3-f393-e0a9-e50e24dcca9e
    handle: 0x000e, uuid: 00002902-0000-1000-8000-00805f9b34fb
    handle: 0x000f, uuid: 00002803-0000-1000-8000-00805f9b34fb
    handle: 0x0010, uuid: 6e400002-b5a3-f393-e0a9-e50e24dcca9e
    [fc:cc:b8:22:b0:42][LE]> char-write-req 0x0010 4c45442e746f67676c6528290a
    Characteristic value was written successfully
    [fc:cc:b8:22:b0:42][LE]> char-write-req 0x0010 4c45442e746f67676c6528290a
    Characteristic value was written successfully
    

    Here you use 0x0010 because that's the handle for uuid 6e400002-b5a3-f393-e0a9-e50e24dcca9e which is the Nordic UART transmit characteristic.

    4c45442e746f67676c6528290a is the hex value of "LED.toggle()\n" which you get using the JavaScript code:

    "LED.toggle()\n".split("").map(x=>(256+x­.charCodeAt()).toString(16).substr(-2)).­join("")
    

    So you could change this to D3.write(1)\n for instance to change the state of pin D3.

    Simblee clone

    • Connect with the Web IDE
    • Upload this code:

      NRF.setServices({
      "35ac0001-18b0-e8b7-3feb-62cec301da00" : {
      "35ac0002-18b0-e8b7-3feb-62cec301da00" : {
        value : [0,0,0],
        maxLen : 3,
        writable : true,
        onWrite : function(evt) {
          digitalWrite([D25,D26,D27,D28,D29,D30,D3­1,D3], evt.data[0]);
          digitalWrite([D4,D5,D22,D29,D19,D18,D17,­D16], evt.data[1]);
          digitalWrite([D15,D14,LED2,LED1], evt.data[2]);
        }
      }
      }
      });
      

    This will create a characteristic with uuid 35ac0002-18b0-e8b7-3feb-62cec301da00 that will write to all the pins of the MDBT42 module based on the 3 bytes you send to it.

    • disconnect and use gatttool:

      $ gatttool --device=fc:cc:b8:22:b0:42 --addr-type=random -I
      [fc:cc:b8:22:b0:42][LE]> connect
      Attempting to connect to fc:cc:b8:22:b0:42
      Connection successful
      [fc:cc:b8:22:b0:42][LE]> char-desc 
      handle: 0x0001, uuid: 00002800-0000-1000-8000-00805f9b34fb
      handle: 0x0002, uuid: 00002803-0000-1000-8000-00805f9b34fb
      handle: 0x0003, uuid: 00002a00-0000-1000-8000-00805f9b34fb
      handle: 0x0004, uuid: 00002803-0000-1000-8000-00805f9b34fb
      handle: 0x0005, uuid: 00002a01-0000-1000-8000-00805f9b34fb
      handle: 0x0006, uuid: 00002803-0000-1000-8000-00805f9b34fb
      handle: 0x0007, uuid: 00002a04-0000-1000-8000-00805f9b34fb
      handle: 0x0008, uuid: 00002803-0000-1000-8000-00805f9b34fb
      handle: 0x0009, uuid: 00002aa6-0000-1000-8000-00805f9b34fb
      handle: 0x000a, uuid: 00002800-0000-1000-8000-00805f9b34fb
      handle: 0x000b, uuid: 00002800-0000-1000-8000-00805f9b34fb
      handle: 0x000c, uuid: 00002803-0000-1000-8000-00805f9b34fb
      handle: 0x000d, uuid: 6e400003-b5a3-f393-e0a9-e50e24dcca9e
      handle: 0x000e, uuid: 00002902-0000-1000-8000-00805f9b34fb
      handle: 0x000f, uuid: 00002803-0000-1000-8000-00805f9b34fb
      handle: 0x0010, uuid: 6e400002-b5a3-f393-e0a9-e50e24dcca9e
      handle: 0x0011, uuid: 00002800-0000-1000-8000-00805f9b34fb
      handle: 0x0012, uuid: 00002803-0000-1000-8000-00805f9b34fb
      handle: 0x0013, uuid: 35ac0002-18b0-e8b7-3feb-62cec301da00
      [fc:cc:b8:22:b0:42][LE]> char-write-req 0x0013 000001  <----------- LED1 turns on
      Characteristic value was written successfully
      [fc:cc:b8:22:b0:42][LE]> char-write-req 0x0013 000002  <----------- LED2 turns on
      Characteristic value was written successfully
      [fc:cc:b8:22:b0:42][LE]> char-write-req 0x0013 000002  <----------- both LEDS turns on
      Characteristic value was written successfully
      [fc:cc:b8:22:b0:42][LE]> char-write-req 0x0013 000000 <----------- both LEDS turns off
      Characteristic value was written successfully
      

    So you could make this work in a very similar way to simblee - however chances are the characteristic handles would be different even if you made the UUIDs the same.

    Hope that helps!

  • Just a note that you can send raw JS in one line using this command:

    gatttool --addr-type=random --device=F5:3E:A0:62:C7:74 --char-write-req --handle=0x0010 \
     --value=`echo "LED.toggle()\n" | od -A n -t x1 | tr -d " "`
    

    You just have to ensure that you split writes of >20 characters up into multiple writes

  • @Gordon thanks for the reply. this exactly what I needed!

    This-

    char-write-req 0x0010 4c45442e746f67676c6528290a

    worked properly and turned the led on and again doing it turned it off. But when I write

    "LED.toggle()\n".split("").map(x=>(256+x­.charCodeAt()).toString(16).substr(-2)).­join("")

    In WebIDE it doesn't give me any hexadecimal character

    And I also followed the simblee clone instruction

    char-write-req 0x0013 000001

    this writes but the led doesn't turn on.


    1 Attachment

    • 3.png
  • when I write ... In WebIDE it doesn't give me any hexadecimal character

    What does it give you? Maybe try running in the left-hand side of the IDE as that prints the result of evaluating an expression.

    You could also use the Linux shell:

    echo "LED.toggle()\n" | od -A n -t x1 | tr -d " "
    

    this writes but the led doesn't turn on.

    That's odd - I did that it worked for me. Are you definitely using the MDBT42Q, with an up to date firmware? I seem to recall that the original firmware didn't have LED2 defined, so the code might have created an error when it was run.

    You could always just do:

    NRF.setServices({
      "35ac0001-18b0-e8b7-3feb-62cec301da00" : {
        "35ac0002-18b0-e8b7-3feb-62cec301da00" : {
          value : [0],
          maxLen : 1,
          writable : true,
          onWrite : function(evt) {
            digitalWrite(LED1, evt.data[0]);
          }
        }
      }
    });
    // and then
     char-write-req 0x0013 01
    

    because we know that the pin LED1 does exist.

  • Writing on the left side of the viewport works with this code-

    "LED.toggle()\n".split("").map(x=>(256+x­.charCodeAt()).toString(16).substr(-2)).­join("")
    

    And also the light system works with the new code. For PWM should I just make new handle that will have 256 value for power? Thanks a lot for the help!

  • Thr 2019.08.08

    @user101743 just responding to inform rather than waiting and wondering. . . .

    Did you catch this post?

    Out of office, 9th to 26th Aug

  • It's up to you, but the secure way of doing it is by creating your own handle - because then you can disable the bluetooth UART for production and you know that all anyone will be able to do via bluetooth is to change the LED brightness

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

Using Bluez with MDBT42Q.

Posted by Avatar for user101743 @user101743

Actions