Connection with Espruino

Posted on
  • Hi Guys,

    I was having issues to connect with the espruino, but I was using the wrong code. So, I got how it works create my code, but now I'm receiving a return that my bluetooth is disconnected after connect. It is too weird. Do you know why?
    Follow below my code and the response in chrome

    Code:

    $scope.btnstatus = function(action) {
            var connection;
            if(action == 'on') {
                
                Puck.write('LED1.set();\n',function(call­back) {
                    console.log(callback);
                    Puck.write('Puck.getBatteryPercentage();­\n',function(baterry) {
                        console.log(callback);
                        initMap();
                    })
                });
            }
            else {
                Puck.write('LED1.reset();\n',function() {
                    Puck.write('Puck.magOff();\n',function()­ {
    
                    })
                });
            }
        };
    

    Response:

    Web Bluetooth is experimental on this platform. See https://github.com/WebBluetoothCG/web-bl­uetooth/blob/gh-pages/implementation-sta­tus.md
    puck.js:318 <BLE> Device Name:       Puck.js f0b5
    puck.js:318 <BLE> Device ID:         vtZGyU35qoD8K6UJmjRSEg==
    puck.js:318 <BLE> Connected
    puck.js:318 <BLE> Disconnected (gattserverdisconnected)
    
  • This is a web Bluetooth page yes? Double check you are not still connected to your computer via Bluetooth, say if you have uploaded code also via the Espruino IDE. That could account for the connect disconnect logs. Puck supports one connection on the BLE UART service

  • Hi Ollie, thanks for you answer. Yes, my note was connected to the puck. However, I still not figure out how to use the functions to handle the puck features.

    I know that I have to use Puck.write and describe the function that I wanna use in the puck and than the callback. However, I cant for example call 'Puck.on('mag',function(xyv) {' using write. Can I? I saw in examples on internet connecting through IDE that the pages call the functions of puck directly. Like

    var  on = false;
    setInterval(function() {
      on = !on;
      LED1.write(on);
    }, 500);
    

    How I can use javascript like that in my code? Can I?

    Thanks Again

  • With complex procedures to call on Puck I find wrapping them as functions and saving them on puck first, is best. Then you call the function from inside puck.write(). You will probably need to call load() to get them from flash first.

    Also check out Bluetooth.println(). I seem to think that is a better function for sending your return data over BLE UART.

    Edited: Bluetooth.write() to correct Bluetooth.println()

  • I think there might be a bit of confusion because you can run code in two places - on the Puck itself or on the PC. Unfortunately both options allow you to use functions from the Puck object, which means it's not immediately obvious what you can do in each place.

    But on the PC, pretty much all you can do is Puck.write and Puck.eval.

    • Puck.write is one-way - you're just sending to the Puck and not waiting for a response
    • Puck.eval allows you to get a response back - in your Puck.getBatteryPercentage() you should really be using eval and not write

    You can run Puck.eval in an interval, but I'd say that doing it every second is about the most you'd want.

    Take a look at the page on Web Bluetooth - it should explain most of this: https://www.espruino.com/Web+Bluetooth

    Specifically if you want to send a bunch of information from the Puck (like magnetometer readings) then it'd be worth looking at the last section on two-way comms: https://www.espruino.com/Web+Bluetooth#t­wo-way-communications

    Instead of the light example there, using code that runs something like this on the Puck should work nicely for you:

    Puck.magOn(); Puck.on('mag', function(xyz) { Bluetooth.println(xyz); });
    

    edit: That still doesn't explain your disconnection issues though - is it possible that you actually just have a flat battery? That can often be a cause of stuff like this - there's enough charge for Puck.js to advertise, but when you connect it draws a bit more power, the voltage drops, and it resets.

  • Ah yes, Bluetooth.println() is what I was reaching for in my mind... On a mobile and a bit rusty :) Edited my post above to avoid any future misdirection.

  • Mon 2019.06.10

    re: 'I was having issues to connect with the espruino'

    Hello @user100558, another item to consider, what version of Puck.js has been flashed?
    Type process.env in L-Hand console side of the WebIDE. Please post results here.


    re: 'How I can use javascript like that in my code? Can I?'

    Do these resources and examples aid in structuring your code?

    http://www.espruino.com/Puck.js
    http://www.espruino.com/Puck.js+SMS
    http://www.espruino.com/Web+Bluetooth

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

Connection with Espruino

Posted by Avatar for user100558 @user100558

Actions