Reading measurements with Raspberry Pi 3 and Node.js (Noble)

Hello,

I’m trying to make a RuuviTag to emit env data to my Raspberry every X seconds. The point is to make the battery last as long as possible. I only want to emit data like temperature and humidity every 1 hour for example.

I have installed the newest Espruino 1.94 to one of my RuuviTags.

I then uploaded this code to the RuuviTag and it correctly console.logs the temperature:

var Ruuvitag = require("Ruuvitag");
Ruuvitag.setEnvOn(true);
setInterval(function() {
  var temp = Ruuvitag.getEnvData().temp.toFixed(2);
  console.log(temp);
  NRF.setAdvertising({
    0x1809: temp
  });
}, 5000);

How could I now read this data on my Raspberry every time the value is emitted? I created the following code, but it reads the value only once when connected to the RuuviTag:

const noble = require('noble');
const _ = require('lodash');

const names = {
    '1809': 'Temperature'
};

const myDevices = ['xx:xx:xx:xx:xx:xx'];
let found_Devices = [];

let inRange = [];

noble.on('stateChange', (state) => {
    if (state === 'poweredOn') {
        noble.startScanning([], true);
    } else {
        noble.stopScanning();
    }
});

noble.on('discover', (peripheral) => {
    let addr = peripheral.address;
    if (!found_Devices.includes(addr)) {
        found_Devices.push(addr);
        console.log('---------------------');
        console.log(`Found - ${addr} - ${peripheral.advertisement.localName}`);
    }
    if (myDevices.indexOf(addr) > -1) {
        let id = addr;
        let entered = true;

        _.each(inRange, (item) => {
            if (item.id === id) {
                entered = false;
            }
        });

        if (entered) {
            let current_peripheral = {
                id: id,
                address: addr,
                peripheral: peripheral,
                name: peripheral.advertisement.localName,
                data: {},
                lastSeen: Date.now(),
                rssi: peripheral.rssi
            };
            inRange.push(current_peripheral);

            console.log(peripheral.advertisement.localName + ' - Found!');

            let serviceData = peripheral.advertisement.serviceData;

            if (serviceData && serviceData.length) {

                _.each(serviceData, (item) => {
                    console.log(`${peripheral.advertisement.localName} => ${names[item.uuid.toString()]} - ${item.data.toString()}`);
                });
                console.log('---------------------');
            }
        }
    }
});

Thanks!

1 Like

Here is an example of the output from this Node.js code on Raspberry.

espruino

How do you start the Noble scan? Are duplicates allowed?

I use this line from noble documentation:

noble.startScanning([], true); // any service UUID, allow duplicates

Ok. Incidentally I’ll be working with NodeJS during the Junction Hackathon this weekend, I’ll look into it while I’m working. Please ping me if I haven’t followed up by Monday

I found a workaround fix to get this to work, but not sure if this is the best way…

I added an interval around the peripheral console logging like this and the value is updated every console.log =>

    setInterval(() => {

        let serviceData = peripheral.advertisement.serviceData;

        if (serviceData && serviceData.length) {

            _.each(serviceData, (item) => {
                console.log(`${peripheral.advertisement.localName} => ${names[item.uuid.toString()]} - ${item.data.toString()}`);
            });
            console.log('---------------------');
        }
    }, 5000);