ds18x20 sensor reading fix

Fixing how the sensor functions works over time.

James Peret 7 年之前
父節點
當前提交
5f44756d25
共有 1 個文件被更改,包括 14 次插入10 次删除
  1. 14 10
      external_temperature_sensor.js

+ 14 - 10
external_temperature_sensor.js

@@ -1,31 +1,35 @@
1 1
 var logger = require('winston');
2 2
 var sensor = require("ds18x20")
3
+var devices;
4
+var channel;
3 5
 
4
-module.exports.start = function(channel){
6
+module.exports.start = function(channel_config){
7
+  channel = channel_config
5 8
   logger.info("Starting External temperature module");
6 9
   var isLoaded = sensor.isDriverLoaded();
7 10
   if(isLoaded){
8
-    console.log(isLoaded);
9
-    var devices = sensor.list();
11
+    logger.info("ds18x20 sensor drivers are loaded.")
12
+    devices = sensor.list();
10 13
     console.log(devices);
11
-    setInterval(function (devices, channel) {
12
-      var temperature = readSensor(devices, channel);
14
+    setInterval(function () {
15
+      var temperature = readSensor();
13 16
     }, 5000);
17
+  } else {
18
+    logger.info("ds18x20 sensor drivers are not loaded.")
14 19
   }
15 20
 
16 21
 
17 22
 }
18 23
 
19
-function readSensor(devices, channel) {
24
+function readSensor() {
20 25
   // Read Sensor data
26
+  if(devices == undefined){
27
+    devices = sensor.list();
28
+  }
21 29
   sensor.get(devices[devices.length - 1], function(err, temp){
22 30
   	if(err){
23 31
   		console.log(err);
24 32
   	}
25 33
   	channel.emit('temperature_external', { temperature: temp });
26 34
   });
27
-  // give random data for testing
28
-  // var high = 25;
29
-  // var low = 23;
30
-  // var temp =  Math.random() * (high - low) + low
31 35
 }