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!