ruuvitag GATT services

Posted on
  • Hi guys,
    I'm trying to connect a Ruuvitag supporting Espruino to my laptop through Noble.js. I need to collect some data through a sensor soldered on the Ruuvitag and to send them to the laptop.
    I flashed this code through the Espruino Web IDE to make the Ruuvi notify the central module as soon as it collects data, but I guess I have misunderstood something since the Ruuvi is not connecting nor Bluetooth flashing anymore. There is the screenshot of nRF Connect connected to the Ruuvi in bootloading below, it seems there is no more the Nordic UART Service... oops.

    const serviceUUID = '6e400001b5a3f393e0a9e50e24dcca9e';
    const characteristicUUID = '6e400003b5a3f393e0a9e50e24dcca9e';
    
    NRF.setServices({
        serviceUUID : {
          characteristicUUID : {
            value : 0,
            readable : true,
            notify : true
          }
        }
      }, { advertise : [serviceUUID] });
    
    

    Luckily I have one more Ruuvi and I really need to make it communicate with the laptop.
    How can I make it?
    Do I have to create my own service and my own notify characteristic?
    Or do I have to use the UART characteristic for notification as above?


    1 Attachment

    • WhatsApp Image 2020-12-03 at 10.44.37.jpeg
  • What you're doing looks fine - you just need to call NRF.setServices once and then call NRF.updateServices when you need to change data.

    Calling NRF.setServices more than once requires a softdevice reboot. It's not normally an issue but if you're calling it often maybe the continuous reboots just stop it from connecting.

    All you need to do is pull the battery and re-insert it with the button held down for ~10 seconds and the ruuvitag will boot without loading your code and will be back to normal for connections

  • Also, what you're seeing above in the screenshot is because I guess you maybe put it into DFU mode when powering up?

    If you power up normally you should be fine.

  • You're right, it was power up in DFU mode.
    I tried re-inserting the battery with the button held down, but it's not working. The behavior is extremely strange: when I insert the battery nRF connect can see the Ruuvi, if I press to connect it tries to connect but it fails. After that, if I scan again, the app can't see the Ruuvi. The log says "Error 133 (0x85): GATT ERROR".
    However, do you confirm I have to use the Nordic UART Service to communicate with the laptop? I'm a bit scared it can happen again and in that case, I'd run out of Ruuvitag.
    Plus, I've read that using 128 bit UUIDs to advertise could lead to some connection problems via Bluetooth. Could it be the cause of the problem?

  • Have you tried just reflashing using the firmware update mode (maybe to a Ruuvitag firmware) and then flashing back to Espruino? I'd have thought that should fix it.

    The issue might be that on Espruino devices I the bootloader is smart enough to time out after around 5 seconds if the device is powered with the button held down. If RuuviTag's doesn't do that then the only way of booting into Espruino with the button held down may be trial and error - powering on while pressing BTN1 at exactly the right point.

    Have you tried turning bluetooth off and on on your PC? GATT ERROR might be due to the computer itself getting confused.

    However, do you confirm I have to use the Nordic UART Service to communicate with the laptop?

    Actually you can attach a USB-TTL converter to D4 and D5 on the Ruuvitag, power it on, and communicate with it that way at 9600 baud.

  • I've read that using 128 bit UUIDs to advertise could lead to some connection problems via Bluetooth. Could it be the cause of the problem?

    Shouldn't on Espruino - 128 bit UUIDs go in a scan response packet so they don't interfere with the normal device.

  • Have you tried just reflashing using the firmware update mode (maybe to a Ruuvitag firmware) and then flashing back to Espruino?

    Yes, I tried flashing both Ruuvitag firmware and Espruino but it fails in both cases.
    I also have the Ruuvi DevKit but I can't use that because a pin is missing because of some soldering... the only way to flash it is through Bluetooth.

    Have you tried turning bluetooth off and on on your PC? GATT ERROR might be due to the computer itself getting confused.

    I tried, but my laptop can't see the Ruuvi. I get the same error when I try to connect with the nRF app and when I try to flash in bootloader mode. Seems like something's broken in the GATT protocol because the Ruuvi Bluetooth is not working anymore.

  • So, to change data on the notify characteristics of the UART service do I have to write something like that?

    const serviceUUID = '6e400001b5a3f393e0a9e50e24dcca9e';
    const characteristicUUID = '6e400003b5a3f393e0a9e50e24dcca9e';
    
    NRF.updateServices({
        serviceUUID : {
          characteristicUUID : {
            value : 12345,
            notify : true
          }
        }
      });
    

    And then, from the script on the laptop, I have to read the data value of the notify descriptor, isn't that right?

  • Yes - you just enable notifications for that characteristic on the PC and you get a callback whenever you call updateServices on the Ruuvitag

  • Sorry for insisting, but I tried again in the second Ruuvi I have and now it's not working anymore, as the former.
    Here the steps I've taken:

    • I flash the Ruuvi with Espruino, everything works and the webapp I wrote can detect it
    • I disconnect the Ruuvi from everything
    • I connect the Ruuvi through the Espruino Web IDE and I flash the following script

      const serviceUUID = '6e400001b5a3f393e0a9e50e24dcca9e';
      const characteristicUUID = '6e400003b5a3f393e0a9e50e24dcca9e';
      var Ruuvitag = require("Ruuvitag");
      
      NRF.setServices({
      serviceUUID : {
        characteristicUUID : {
          value : 0,
          readable : true,
          notify : true
        }
      }
      }, { advertise : [serviceUUID] });
      
      
      function startAdv(){
      var idInterval = setInterval(function(){
      var val = Math.random();
      console.log(val);
      
      NRF.updateServices({
      serviceUUID : {
        characteristicUUID : {
          value : val,
          notify : true
        }
      }
      });
      
      }, 200);
      
      setWatch(function() {
      clearInterval(idInterval);
      }, BTN1, {repeat: true});
      }
      
      • The console of the Web IDE says "BLE Connected, queueing BLE restart for later"
      • I disconnect the Ruuvi from the Web IDE (I read on another topic it's necessary if I read "queueing BLE restart for later")
      • I try to connect from the webapp I wrote, but now the app can't detect the Ruuvi anymore

    The Ruuvi is not visible on Bluetooth anymore, nRF Connect can see it only in bootloader mode.
    I noticed that if I pull the battery and re-insert it, both webapp and nRF Connect can see the Ruuvi, but if I scan again, then the Ruuvi is not visible anymore. I can't connect to it.

    Tomorrow I'll flash the Ruuvi through the Ruuvitag DevKit, but I'd like to understand how to make it works.

    Thanks a lot for helping, I really appreciate that.

  • nRF Connect can see it only in bootloader mode.

    I'm not sure how you're getting into bootloader mode... and that's without physically touching the beacon?

    but if I scan again, then the Ruuvi is not visible anymore. I can't connect to it.

    Is it possible that it's just that your PC/Mac/something is auto-connecting to the Ruuvitag? It'll stop advertising or being visible once it is connected to another device.

    As I'd mentioned above you can get serial communications to a Ruuvitag running Espruino, so that could be a good way to debug it.

    I'm not sure how much extra help I can be to be honest. While Ruuvitag were supporting Espruino (hence the builds) for quite a while, they stopped at the end of 2019. I've maintained a few builds for them for free since then, but it's now a year since they stopped and if you've got issues you should probably get in touch with them.

    Who knows, if they see there's value in getting me to support Espruino they might start supporting the project again :)

  • I'm sorry they stopped supporting Espruino, they both are great projects with great potential.
    I think I figured out where the bug was. After some debugging, this error was thrown by the web IDE

    Uncaught Error: Invalid Service UUID: UUID string should only contain hex characters and dashes
    at line 25 col 2
    });

    I found the related following issue on github https://github.com/espruino/Espruino/iss­ues/1723.
    Do you think I could solve my problem using 4 integers UUID instead of a 128 bit UUID?
    I tried to look on the web how to convert it but I couldn't find an answer. How does UUID work? How do I convert from "ABCDABCD-ABCD-ABCD-ABCD-ABCDABCDABCD" to 0xABCD?

  • Mon 2020.12.07

    Hi @titaniumwhite I'm responding as I noticed your post arrived indicating a Time Zone near myself, and most that are likely to respond, wont be available until tomorrow. I'm not a wiz using GATT, but I did remember a forum post a while back that asked a similar question.

    @Gordon provided a specific example in post #13

    GATT service on RuuviTag

    and

    Handling of 32 and 128 bit uuids for BLE



    How to: 'Generate a UUID compliant with RFC 4122'

    https://www.cryptosys.net/pki/uuid-rfc41­22.html


    I Googled using the 'site' qualifier: 128 bit UUID site:forum.espruino.com

    which returns a bunch of references which may assist also. These may not answer your question directly, but I didn't want you spinning your wheels just waiting until sunrise. Hope you find some insight there. . . .

  • Actually that error you pointed to is for ESP32 - I don't believe it was ever an issue on official boards.

    Thanks @Robin - there's a good example of using UUIDs at the link you posted on http://forum.espruino.com/conversations/­323285/

    And actually looking again at your code it's obvious - sorry I didn't spot it before! You're actually trying to use the text 'serviceUUID' itself as the UUID, not '6e400001b5a3f393e0a9e50e24dcca9e' :)

    // for instance these two are identical:
    print({
        serviceUUID : {
          characteristicUUID : {
         }
      }
    });
    print({
        "serviceUUID" : {
          "characteristicUUID" : {
         }
      }
    });
    // what you want is
    print({
        '6e400001b5a3f393e0a9e50e24dcca9e' : {
          '6e400003b5a3f393e0a9e50e24dcca9e' : {
         }
      }
    });
    

    If you want to use the UUIDs as constants at the start of the code you'd have to manually set it in code:

    var services = {};
    services[serviceUUID] = {};
    services[serviceUUID][characteristicUUID­] = { ... };
    NRF.setServices(services);
    
  • @Robin thanks for the links, they're very useful to me, the one about UUID particularly.

    @Gordon thanks for the correction.
    After some days trying on it, I could make it work just deleting the advertising.
    setServices is now

    NRF.setServices({
        '6e400001b5a3f393e0a9e50e24dcca9e' : {
          '6e400003b5a3f393e0a9e50e24dcca9e' : {
            value : 0,
            readable : true,
            notify : true
          }
        }
    });
    

    Just removing the advertise, the Ruuvi doesn't crash anymore and now my webapp can see and connect to it.
    Altought I read the documentation and other forum posts about the same topic, I couldn't really understand what advertise does. Is it necessary for my purpose?

  • I couldn't really understand what advertise does. Is it necessary for my purpose?

    No. Literally all it does is add that UUID to the bundle of advertising data it sends out when it's not connected to anything.

    The only reason you'd do that is if you wanted a way to 'find' your devices (eg, "scan for all devices with service 6e400001b5a3f393e0a9e50e24dcca9e"). Generally you'd know the address or it or would have some customised name, so that's not an issue though

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

ruuvitag GATT services

Posted by Avatar for titaniumwhite @titaniumwhite

Actions