Reading temperature sensor

Added code to read the temperature sensor using the ds18x20 library. If the necessary drivers are not running, then the script does not try to read the temperature.

James Peret 7 years ago
parent
commit
a16f44179e
2 changed files with 22 additions and 9 deletions
  1. 21 9
      external_temperature_sensor.js
  2. 1 0
      package.json

+ 21 - 9
external_temperature_sensor.js

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

+ 1 - 0
package.json

@@ -10,6 +10,7 @@
10 10
   "license": "ISC",
11 11
   "dependencies": {
12 12
     "body-parser": "^1.17.2",
13
+    "ds18x20": "^0.2.0",
13 14
     "ejs": "^2.5.7",
14 15
     "express": "^4.15.4",
15 16
     "express-ejs-layouts": "^2.3.1",