0

I'm making a js-based clock on a Raspberry Pi. It runs in chromium & displays the time & weather (using simpleWeather). I've connected a ds18b20 and I want to display the temperature too. The device is working, I can read the temperature from the command line but I want to read it and display it in my js script.

I'm using w1temp. I'm getting the require part wrong and not managing to load w1temp properly, but I don't understand where I'm going wrong. Could someone please point me in the right direction?

I'm using require like this:

require(['../../node_modules/w1temp'], function (W1Temp) { 
});

Then

setInterval( function () {
     W1Temp.getSensor('28-0414609d26ff').then(function (sensor) {
        // get inside temperature
        var temp = sensor.getTemperature();
        $("#inside_temp").html(temp);
     });
},1000);

In doing this I get a js error when I load the page & my temperature isn't displayed. The error is:

Not allowed to load local resource: home/pi/node_modules/w1temp.js

I guess this is because their isn't a home/pi/node_modules/w1temp.js, but what do I require to get the temp out of w1temp?

I know I'm making a simple error based on a simple misunderstanding, & I've read the w1temp docs but I can't see where I'm going wrong. I hope someone can point me in the right direction.

Bharata
  • 13,509
  • 6
  • 36
  • 50

1 Answers1

0

The npm package ds18x20 worked well. You can use either synchronous or asynchronous loading:

var sensor = require('ds18x20');

  sensor.getAll(function (err, tempObj) {
  console.log(tempObj); // will display temperature using the asynchronous method
});

var tempObj = sensor.getAll();
  console.log(tempObj); // will display temperature using the synchronous method

I tried several other npm packages, but this one was the easiest to work with and it helped tremendously.

user5040
  • 1
  • 1