我在Raspberry Pi上做了一个基于js的时钟。它运行在铬和显示时间和天气(使用simpleWeather
)。我已经连接了ds18b20
,我也想显示温度。设备工作正常,我可以从命令行读取温度,但我想读取它并在我的js脚本中显示它。
我使用的是w1temp
。我把require部分弄错了,也没能正确地加载w1temp,但我不知道哪里出错了。有人能给我指个方向吗?
我像这样使用了require:
require(['../../node_modules/w1temp'], function (W1Temp) {
});
然后
setInterval( function () {
W1Temp.getSensor('28-0414609d26ff').then(function (sensor) {
// get inside temperature
var temp = sensor.getTemperature();
$("#inside_temp").html(temp);
});
},1000);
在这样做的时候,当我加载页面时,我得到了一个js错误&我的温度没有显示。错误是:
不允许
加载本地资源:
home/pi/node_modules/w1temp.js
我猜这是因为他们不是home/pi/node_modules/w1temp.js
,但是我需要什么才能从w1temp中取出temp呢?
我知道我因为一个简单的误解而犯了一个简单的错误&我已经阅读了w1temp文档,但我看不出哪里错了。我希望有人能给我指明正确的方向。
发布于 2018-08-10 01:08:39
npm包运行良好。您可以使用同步或异步加载:
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
我尝试了其他几个npm包,但这个包是最容易使用的,它非常有用。
https://stackoverflow.com/questions/48254753
复制相似问题