Delay problem with Node js for Raspberry Pi 3B+

Hi everybody,

I’m trying to build a Nodejs program for Ruuvitag but I am having trouble with an unexpected delay.

The program at this point can already identify the sensor and display the values. The values are also shared through MQTT to the network. However, after about two days the values sent and the values displayed are not the same anymore. For some reason the date of the main module resets itself while the module in charge of reading the values keeps working normally. The sensor values don´t match anymore.

I have to mention that the program will be running on a Raspberry Pi 3 B+ inside a Docker container. I did find some trouble with the continuity of the bluetooth connection for Raspberry Pi but the solution I found for this was to regularly restart the scanning. Other than that I haven’t found any other issues with Raspberry or Docker, so I believe the cause for the issue has something to do with the Noble module and/or the code.

The code is too long for the post so I tried to simplify it as much as possible, keeping only the key features. Hopefully somebody can help me with this. In advance, thank you very much.

index.js (main module)

const sensorModule = require("./ruuvitagModule");
//Here the program should start reading the sensor values
sensorModule.readData(macAddress);
setInterval(() => {
  //State Machine for the administration of sensors
  switch (registrationStatus) {
    case "notRegistered": {
      //("Not Registered State")
      registerDevice();
      break;
    }
    case "registered": {
      //("Registered State")
      //Here I read the information from the second module
      let newContent = sensorModule.measuringValues();
      let measuringValues = createMessage(newContent);
      let measuringValuesTopic = "test"
      //publishing the information through MQTT
      mqttModule.publishMessage(measuringValuesTopic, JSON.stringify(measuringValues), false);
      break;
    }
    case "iddle": {
      //No response from server
      counterIddleState++;
      if (counterIddleState > 1800000) {
        counterIddleState = 0;
        registrationStatus = "notRegistered";
        console.log("Not Registered State");
      }
      break;
    }
  }
}, 2000);

ruuvitagModule.js

const noble = require('noble');
const content = {
  Content01: 30,
  };
module.exports = {
  /**
   *Function to read the data from a specific sensor 
   */
  readData: function (macId) {
    noble.on('stateChange', state => {
      console.log(`State changed: ${state}`)
      if (state === 'poweredOn') {
        noble.startScanning([], true);
      }
    });
    noble.on('discover', function (peripheral) {
      const advertisement = peripheral.advertisement;
      const manufacturerData = advertisement.manufacturerData;
      if (peripheral.id == macId) {
        let data = decodeTempo(manufacturerData);
        publish(data);
        let moduleDate = new Date();
        console.log("Date: ", moduleDate);
        console.log("ManuData: ", advertisement.manufacturerData);
      }
    });
    function publish(data) {
      console.log("displaying data: ", data.data.temperature);
      content.Content01=data.data.temperature;
      //Once a value is received, the scanning should be stopped and restarted after 6 seconds
       try {
        noble.stopScanning();
        setTimeout(() => {
          console.log("newScan");
          noble.startScanning([], true);
        }, 6000);
      }
      catch (e) {
        console.log("The scanning could not be stopped");
      }
    }
    function decodeTempo(buf) {
      let data = {};
      let manuData = buf.toString('hex');
      let temperatureStart = 8;
      let temperatureEnd = 12;
      let temperatureString = manuData.substring(temperatureStart, temperatureEnd);
      let temperature = parseInt(temperatureString.substring(0, 2), 16); //Full degrees
      temperature += parseInt(temperatureString.substring(2, 4), 16) / 100; //Decimals
      data.temperature = temperature;
      return {
        data: data
      };
    }
  },

  /**
 *Function for retrieving the information coming from the sensor
 */
  measuringValues: function () {
    let measuringValueObject = content;
    return measuringValueObject;
  }
};