Cat Activity Monitor

This topic is dedicated to comments and conversations about the following RuuviLab tutorial:

If you don’t see any comments yet, don’t be afraid to be the first one to contribute!

1 Like

Hello there,

here is my solution for the cat activity monitor. To lower the energy consumption the code checks every 5 seconds for activity and logs if the cat moved in the last 10 minutes. The “live” 24h movement is broadcasted. To obtain all data call the getData() function. I got some inspiration from the acceleration logger solution from js.

var Ruuvitag = require("Ruuvitag");

var movement = [];
var movementLog = [];
var lastInterval = 0;
var lastLog = 0;
var activeAt = 1.1; //g value at which the cat is active

function onInit() {
  lastLogInterval = Math.floor(getTime());
  Ruuvitag.setAccelOn(true);
  checkAccel();
}

function checkAccel() {
  var xyz = Ruuvitag.getAccelData();
  var g = Math.sqrt(xyz.x * xyz.x + xyz.y * xyz.y + xyz.z * xyz.z);
  g = g / 1000;

  var interval = Math.floor(getTime() / 600) % 144; //10 minute intervals

  if (interval != lastInterval) {
    calculateMovement(true);
    if(movement.length >= 144) {
      movement.shift();
    }
    movement.push(0);
    lastInterval = interval;
  }

  if (g > movement[movement.length - 1]) {
    movement.pop();
    movement.push(g);
  }
  
  setTimeout(checkAccel, 5000); //every 5 seconds to lower energy consumption
}

function calculateMovement(publish) {
  var active = 0;
  for(i = 0; i < movement.length; i++) {
    if(movement[i] >= activeAt) {
      active += 1;
    }
  }
  var percentage = active / movement.length;
  if(!isNaN(percentage)) {
    if(publish) {
      NRF.setAdvertising(require("ble_eddystone").get(Math.round(percentage*100) + "%_CATNAP"), {interval:1000});
      checkLog(percentage);
    }
    return percentage;
  }
  else {
    return 0;
  }
}

function calculateTotalMovement() {
  var totalMovement = 0;
  for(i = 0; i < movementLog.length; i++) {
    totalMovement += movementLog[i];
  }
  totalMovement = totalMovement / movementLog.length;
  return totalMovement;
}

function checkLog(percentage) {
  if((lastLog + 86400) < getTime()) { // 24h (= 86400 seconds) logs
    lastLog = Math.floor(getTime());
    movementLog.push(percentage);
  }
}

function getData() {
  console.log("Last 24 hours: " + Math.round(calculateMovement(false)*100) + "%");
  if(movementLog.length < 1) {
    console.log("Last 24h period: Not enough Data");
    console.log("Total Movement: Not enough Data");
  }
  else {
    console.log("Last 24h period: " + Math.round(movementLog[movementLog.length - 1]*100) + "%");
    console.log("Total Movement: " + Math.round(calculateTotalMovement()*100) + "%");
  }
}

Best regards,
Lenny

Seem’s that it would less data intense to see how many hours your cat is NOT sleeping! :rofl:

Depends on the cat I guess :smiley:

Cool! Any plans to add Eddystone advertisements to make it easier to check the readings?

Check this line, it’s already broadcasting using Eddystone.

Ah, that’s true! Missed that line. Contact sales@ruuvi.com to get your reward shipped :muscle:

Hii, where did you run this code btw ?

With Espruino