-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
81 lines (75 loc) · 2.29 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Configuration value, from how much C° the fan should start.
const MAXHEAT = 50;
// require system modules
const spawn = require("child_process").spawn;
const os = require("os");
// require custom module
const font = require("oled-font-5x7");
const i2c = require("i2c-bus");
const oledI2cBus = require("oled-i2c-bus");
/**
* piHAT class to control the waveshare POE HAT with node.JS
*/
class piHAT {
constructor() {
}
/**
* Writes the text on the display
* @param {string} str
*/
writeToDisplay(str) {
let i2cBus = i2c.openSync(1);
let opts = {
width: 128,
height: 32,
address: 0x3C
};
let oled = new oledI2cBus(i2cBus, opts);
oled.setCursor(1, 1);
oled.writeString(font, 1, str, 1, true);
oled.update();
}
/**
* Fetches the current temperature of the Raspberry PI
*/
getTemperature() {
return new Promise((resolve, reject) => {
let vcgencmd = spawn("vcgencmd", ["measure_temp"]);
vcgencmd.stdout.on("data", buff =>
resolve(buff.toString("utf8").split("=")[1].split("'")[0])
);
vcgencmd.stderr.on("data", buff => reject(buff.toString("utf8")));
});
}
/**
* Switches the fan on or off
* @param {boolean} shouldOn
*/
writeI2c(shouldOn) {
return new Promise((resolve, reject) => {
let wbuf = null;
if (!shouldOn) {
// fan off
wbuf = Buffer.from([0x01]);
} else {
// fan on
wbuf = Buffer.from([0xFE]);
}
i2c.openPromisified(1).then(i2c1 => i2c1.i2cWrite(0x20, wbuf.length, wbuf).then(_ => { i2c1.close(); resolve(); })).catch(reject);
});
}
}
let hat = new piHAT();
// Write current temperature and status of the fan on the display.
(async () => {
let temp = await hat.getTemperature();
if (temp >= MAXHEAT) {
hat.writeI2c(true);
console.log(`:: Hotter than expected => FAN ON ${temp} C°`);
hat.writeToDisplay(`@${os.hostname()} (FAN: ON; ${temp})`);
} else {
hat.writeI2c(false);
console.log(`:: Everything fine => FAN OFF ${temp} C°`);
hat.writeToDisplay(`@${os.hostname()} (FAN: OFF; ${temp})`);
}
})();