SHOW:
|
|
- or go back to the newest paste.
| 1 | #!/usr/bin/env node | |
| 2 | ||
| 3 | var net = require('net');
| |
| 4 | var fs = require('fs');
| |
| 5 | var pconfig = require('./weatherstation/bmp085-pressure');
| |
| 6 | var w1config = require('./weatherstation/w1-temperature');
| |
| 7 | var cosmconfig = require('./weatherstation/cosm');
| |
| 8 | ||
| 9 | var temperature = 20; | |
| 10 | var pressure = 1020; | |
| 11 | var w1temperature = 20; | |
| 12 | ||
| 13 | var tempArray = {0: temperature, 1: temperature, 2: temperature};
| |
| 14 | var pressureArray = {0: pressure, 1: pressure, 2: pressure};
| |
| 15 | var w1tempArray = {0: w1temperature, 1: w1temperature, 2: w1temperature};
| |
| 16 | ||
| 17 | var pscale = pconfig.pressureConfig.scale; | |
| 18 | var pfileData = pconfig.pressureConfig.file; | |
| 19 | ||
| 20 | var tscale = pconfig.tempConfig.scale; | |
| 21 | var tfileData = pconfig.tempConfig.file; | |
| 22 | ||
| 23 | var w1tscale = w1config.w1tempConfig.scale; | |
| 24 | var w1tfileData = w1config.w1tempConfig.file; | |
| 25 | ||
| 26 | var n = 0; | |
| 27 | ||
| 28 | try {
| |
| 29 | fs.writeFileSync('/sys/class/i2c-adapter/i2c-3/new_device', 'bmp085 0x77', encoding = 'ascii');
| |
| 30 | } | |
| 31 | catch (ex) {
| |
| 32 | console.log('bmp085 driver load failed.');
| |
| 33 | } | |
| 34 | ||
| 35 | function readWeather(n) {
| |
| 36 | pressureArray[n] = fs.readFileSync(pfileData) / pscale; | |
| 37 | tempArray[n] = fs.readFileSync(tfileData) / tscale; | |
| 38 | w1data = fs.readFileSync(w1tfileData); | |
| 39 | w1data = '' + w1data; | |
| 40 | w1Breakdown = w1data.split("\n");
| |
| 41 | w1tempArray[n] = w1Breakdown[1].split("t=")[1] / w1tscale;
| |
| 42 | console.log(n + ': ' + pressureArray[n] + ' ' + tempArray[n] + ' ' + w1tempArray[n]); | |
| 43 | ||
| 44 | n++; | |
| 45 | if(n>2) | |
| 46 | return | |
| 47 | - | setTimeout(readWeather(n), 3000); |
| 47 | + | setTimeout(function() { readWeather(n); }, 3000);
|
| 48 | } | |
| 49 | ||
| 50 | readWeather(n) | |
| 51 | ||
| 52 | // Average the 3 readings and round to 1 decimal | |
| 53 | temperature = Math.round((tempArray[0] + tempArray[1] + tempArray[2])/0.3)/10; | |
| 54 | pressure = Math.round((pressureArray[0] + pressureArray[1] + pressureArray[2])/0.3)/10; | |
| 55 | w1temperature = Math.round((w1tempArray[0] + w1tempArray[1] + w1tempArray[2])/0.3)/10; | |
| 56 | ||
| 57 | console.log(pressure + ' ' + temperature + ' ' + w1temperature); |