Temperature Logger

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!

Just completed the code that stores temperature readings with date-time stamps using an array with FIFO operations. Here is the script:

const period = 30 *60*1000; // milliseconds (every 30 minutes)
const length = 3 *24*60*60*1000 /period; // number of records (for 3 days)

var ruuvi = require("Ruuvitag");
var records = [];

function log() {
  ruuvi.setEnvOn(true);
  if (records.unshift({time:Date(),temp:ruuvi.getEnvData().temp}) > length){
    records.pop();
  }
  ruuvi.setEnvOn(false);
}

function onInit() {
  E.setTimeZone(8);
  setInterval(log, period);
  setDeepSleep(true);
}

function getData() {
  records.forEach( function(d,i) {
    console.log(i+"\t"+ d.temp +"\t"+ Date(d.time.ms).toString());
  });
}
2 Likes

Nice!

I made a few adjustments:

const period = 30*60*1000; // milliseconds (every 30 minutes)
const length = 200; // number of records (for 100 hours / 4 days)

var ruuvi = require("Ruuvitag");
var records = [];

function log() {
  ruuvi.setEnvOn(true);
  if (records.unshift({time:getTime(),temp:ruuvi.getEnvData().temp}) > length){
    records.pop();
  }
  ruuvi.setEnvOn(false);
}

function getData() {
  records.forEach( function(d,i) {
    console.log(i+"\t"+ d.temp +"\t"+ (getTime()-d.time));
  });
}

ruuvi.setAccelOn(true);
setInterval(log, period);
  • Instead of storing the whole Date object, only milliseconds since boot is stored. This saves a few bytes and allows for a few more stored elements.
  • I removed timezone, and send “time since” timestamp instead. The receiver can use this information to determine the real-world time on their end.
  • Deep sleep is not needed on nRF52. I also set the accelerometer on as counterintuitively this actually saves power. This is because sensors share SPI lines, and the chip selects float unless sensor is on. Floating chip selects may cause sensors to use up extra power.

Usage:

  • Flash Espruino 1.93 on RuuviTag
  • Send script to RuuviTag using Espruino IDE
  • Type “getData()” on Espruino Console to receive stored data. You might want to shorten the period when developing your application.
  • To use the application without Espruino Console, you can implement Nordic UART service on your application and send the getData() on Nordic UART.
  • Results are stored newest data point first. After the memory runs out, oldest element is discarded to make room for new elements.
    Power consumption is ~65 µA when Espruino Console is disconnected. A CR2477 battery should last for a year on this application.

@cw Please contact @henri (Private Message) for your shipping details so we can send your reward.

1 Like

Thanks for the edit, makes for good learning. I have trying to figure out if there is a simple way to determine when memory is running low before popping out records from the array. So that there would not be a need to pre-determine the length of the total records, i.e. use the max memory available. Any advice?

I haven’t found a reliable way to measure amount of memory available in Espruino, right now I just allocate more objects until I get error.

Please let me know if you find a method :slight_smile:

Javascript try and catch mechanism is unable to catch the memory overflow error; I tried it earlier today.

This morning, I actually did what you just suggested, here are the details:

  1. set length to a very large number, i.e. 10000
  2. set period = 1
  3. run the code until there is memory error
  4. console.log( records.length ) to obtain the max length
  5. set length to less than (4) and reset period to desire value

and we are good to go.

Im complete noob testing this on our wholesale market.
Could someone with more info tell me how many data´points can you store before unit runs out of memory.
How do you set up unit so that it doesnt overwrite data and just stops recording when it reaches full memory.
How do I set up unit so that it starts recording lets say after 4 hours when I set it up.

I have very basic knowledge about python coding I mean very basic.

I would consider setting up a gateway which would the send all of the data to a backend on the cloud. This way you can monitor the tags in real time and get alerts, as well as store as many data points as you need.

Thanks Otso. Im concidering trying to to use this to log cold chain when we send temperature sensitive products to our customers.

You might be interested in Corvus GPS application: https://lab.ruuvi.com/corvus/

Hey there. I have been messing with this code and it’s really cool. I’m wondering though. Is there a way to store smaller values so I can fit more data points in the memory? If I could cut off a bunch of digits after the decimal point I think it would save a lot of space and allow me to run the program a lot longer without starting to lose data. Any thoughts?

Good job!!!
how can I access temperature data getData() without Espruino IDE?