Connect to mdb42q from pi and control it ?

Posted on
  • Hello,
    I want to connect to the mdb42q from my pi and be able to send him 2 types of messages to light up 2 ciruict (let's make it simple - want to control 2 gpio "1"\"0" from the pi)

    is there a guide \ example I can follow ?

    also can I make a secure connection, meaning allow to connect only from known MAC address only?
    so only my Pi will be able to connect to the device ?

    Thanks,


    I have try this example

    var gatt;
    NRF.requestDevice({ filters: [{ name: 'PI4' }] }).then(function(device) {
      console.log("found device");
      return device.gatt.connect();
    }).then(function(g) {
      gatt = g;
      console.log("connected");
      return gatt.startBonding();
    }).then(function() {
      console.log("bonded", gatt.getSecurityStatus());
      gatt.disconnect();
    }).catch(function(e) {
      console.log("ERROR",e);
    });
    

    My pi name is PI4 - I can see it in bluetooth and ble scan from my phone
    so what is wrong? missing?

    have try namePrefix: 'PI4'
    have also try to use th service I used which is '1322' - get the same resualt

    NRF.requestDevice({ filters: [{services: ['1322'] }] }).then(function(device) {
    

    ERROR No device found matching filters

  • I want to connect to the mdb42q from my pi

    your code example is doing it the other way - from Espruino to the Pi, so which one you want and which device has GPIO connected to something?

    On Espruino side you can hook into incoming connection event and refuse any connection so you can filter by mac address even without secure connection if you wish.

    I can see it in bluetooth and ble scan from my phone
    so what is wrong?

    Try also connection over BLE from the phone (e.g. via nrfConnect), does it work? What are you next steps if connection works? How do you plan to receive GPIO toggle command on Pi side (if this is the right direction)?

  • maybe I didn't explain correct
    I have connected to the mdb42q a LED in GPIO D15 (it this OK?)
    I want to control the LED from the PI
    I have the MAC address of the PI - I want it to connect only to it
    DC::32::9A:**

    I have found this - and I can see it connect to the pi

    var gatt;
    NRF.connect(" DC:**:32:**:9A:**").then(function(g) {
      gatt = g;
    }).then(function(service) {
      console.log("connected");
      return gatt.startBonding();
    }).then(function() {
      gatt.disconnect();
     console.log("Done!");
    });
    

    what else do I need to write in order to make hi m"wait" for my command ON\Off?

    and do you have an exapmle what need to do on the pi side?

  • connecting the opposite way does not help you in any way, you need example for the Pi which connects the other way, not this piece of JS code for MDBT42. Then you would just send piece of javascript to toggle GPIO. As a start you can use web page in Chromium on the PI as per https://www.espruino.com/Web+Bluetooth - this is the easiest thing and web bluetooth works on the Pi

    otherwise bluetooth in linux is a mess, there are many ways, maybe search raspberry forum or check this https://elinux.org/RPi_Bluetooth_LE#Usin­g_Bluetooth_LE_with_Python or node.js via https://github.com/noble/noble or dierctly via https://www.npmjs.com/package/espruino which is using noble

  • so maybe I'm not it the right place
    can Espruino be used as client? meaning can I send data to it?
    or they are ony used as "server" and send data to other devcies?
    ** maybe I need another solution

    Thanks,

  • can Espruino be used as client? meaning can I send data to it?
    or they are ony used as "server" and send data to other devcies?

    Espruino can be used on both sides quite easily. It is the linux on the Pi that is more complicated. Just found tutorial that may help a bit and describes various options , check https://www.espruino.com/BLE+Advertising­

  • I think we misunderstanding each other.... :-)

    can you show an example code to put on the mdb42q
    all I want it to do it to be connactable from 1 knonwn MAC address and wait for message
    I don't need it to send\advertise\publish nothing

    just to be connected and wait for message

    this is what I have done :

    console.log("Startup");
    NRF.setAdvertising([
     
      {//here I put the HEX data -https://www.bluetooth.com/specification­s/gatt/characteristics/
     
      } 
    ], { //here I put the defualt data ONLY!
       name : "Devcie TEST",
       showName: true,
       discoverable : true,
       connectable : true,
       scannable : true,
       interval:100
    });
    

    I can see the device in scanning
    I mange to connect to him - because I can see this in the IDE :

    -> Bluetooth
    

    and on my PI I can see I'm connected

    now what I need to add in order to read the data I'm sending from the PI (sending json {'open'})

    Thank you ,

  • can you show an example code to put on the mdb42q

    You don't need any code on mdb42q at all to do this. You also don't need to 'wait' on MDBT42 side for anything.

    You just need to connect from the pi to the MDBT42 and send JS code to set the GPIO as shown in example https://www.espruino.com/Web+Bluetooth that is the easiest way to do this. There are other ways with custom service on MDBT42 - see e.g. this https://www.espruino.com/BLE+Communicati­ons but this is more complicated. Since you are struggling even with the basics there is no point in doing that as step 1.

    As I see it, the biggest issue for you is how to write code on Pi side to connect and send string like 'LED1.set();\n' to Nordic UART service on MDBT42 (or any custom service if you go for the more complicated solution) . In both cases the Pi side will be similar - connect, write data to some characteristics. If you want reply back then also subscribe to notifications and get output back.

    As for refusing connection from unknown devices check https://www.espruino.com/BLE+Security but I wouldn't worry about that in step 1.

    I think we misunderstanding each other.... :-)

    Yes, there seems to be a lot of misunderstanding on your side in both how BLE works and also what you exactly want to do and how :-)

    And BTW here is example of getting battery via (already suggested) espruino cli command
    http://forum.espruino.com/conversations/­297357/#13369180

  • I know the problem is on my side ....
    so maybe you can guide me to understanding
    all I want to do is connect from the PI to the device and turn on\off led1

    this is what I'm missing :
    1.up until now when I wanted to turn LED1 on I wrote
    LED1.write(true)
    and now you say I need to write 'LED1.set()\n' - do I need to add true\false ?

    1. after I have connected to the device , why can't I see the data I'm sending to him , is there any way to write something to log in order to see where is the problem ?
      all I can see is -> Bluetooth
      I'm sending sample data once every 10 seconds , and I can't see anything - why?

    I read somewhere I can use Node-RED for creating a project and connecting with the MDB42Q
    I mange to create a connection and I'm sending data from it - but again, I don't see anything on the device - the led is not tuen on\off
    do you have an example\guide for it?

  • you say I need to write 'LED1.set()\n' - do I need to add true\false ?

    no, https://www.espruino.com/Reference#Pin

    after I have connected to the device , why can't I see the data I'm sending to him , is there any way to write something to log in order to see where is the problem ?
    all I can see is -> Bluetooth
    I'm sending sample data once every 10 seconds , and I can't see anything - why?

    -> Bluetooth is printed when output console is switched to bluetooth, this indeed happens by default when you connect. When you disconnect you will probably see -> 'name your previous console'. Often this is what you want when you connect to the device via https://www.espruino.com/ide/ or espruino cli command. You can override this behaviour as per https://www.espruino.com/Reference#l_E_s­etConsole but if you keep console as it was you cannot send commands like the LED1.set() to console via bluetooth connection.

    and I can't see anything - why?

    output goes to bluetooth console but you probably don't read it at all

    do you have an example\guide for it?

    I think I linked more than enough guides already and it doesn't look like it helped

  • I think @fanoush has provided loads of info already.

    But there is also https://www.espruino.com/Interfacing#blu­etooth-le which provides you a bunch of examples to turn the LED on and off using Web Bluetooth, Node.js, Python, even the Bash shell - all of which should work on the Pi.

    For instance this single command when run in the shell on Raspberry Pi should do it once you use the Mac address of your MDBT42Q:

    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 " "`
    

    Worth noting that a few of the examples use LED.write, LED.set, LED.toggle - but as @fanoush noted above they are all just different ways of controlling a pin: https://www.espruino.com/Reference#Pin

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

Connect to mdb42q from pi and control it ?

Posted by Avatar for David1234321 @David1234321

Actions