-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (56 loc) · 2.3 KB
/
index.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
const five = require('johnny-five')
const notifier = require('node-notifier')
const argv = require('optimist')
.usage('Usage: $0 --baseTemp [num] --maxIncrease [num] --serialPort <PORT>')
.demand(['baseTemp', 'maxIncrease'])
.default({baseTemp: 22, maxIncrease: 7})
.argv
const baseTemperature = argv.baseTemp
const serialPort = argv.serialPort
const maxTemperatureIncreasing = argv.maxIncrease
const maxTemperature = baseTemperature + maxTemperatureIncreasing
console.log(`Base temperature: base ${baseTemperature.toFixed(2)} °C, max ${maxTemperature.toFixed(2)} °C`)
const board = new five.Board({port: serialPort, repl: false})
const ledPins = [3, 4, 5]
const alarmLedPin = 6
const ledCount = ledPins.length
const ledMaxBrightness = 255
let isAlarmOn = false
board.on('ready', function () {
// This requires OneWire support using the ConfigurableFirmata
const thermometer = new five.Thermometer({
controller: 'DS18B20',
pin: 2
})
console.log('Led pins', ledPins)
console.log('Alarm led pin', alarmLedPin)
const leds = new five.Leds([...ledPins]) // clone because five mutate/erase array
const alarmLed = new five.Led(alarmLedPin)
thermometer.on('change', function () {
const tempPercent = (this.celsius - baseTemperature) / maxTemperatureIncreasing
console.log(`${this.celsius.toFixed(2)} / ${maxTemperature.toFixed(2)} °C | ${(tempPercent * 100).toFixed(2)} %`)
for (let indx = 0; indx < ledCount; indx++) {
const led = leds[indx]
const ledPercentTo = (indx + 1) / ledCount
const ledPercentFrom = (indx) / ledCount
const brightness = Math.min(ledMaxBrightness * (tempPercent - ledPercentFrom) / ledPercentTo, ledMaxBrightness);
(tempPercent > ledPercentFrom) ? (led.on() && led.brightness(brightness)) : led.off()
}
if ((tempPercent >= 1) && (!isAlarmOn)) {
notifier.notify({
title: 'Alarm',
message: `ALARM! Temperature is ${this.celsius.toFixed(2)} / Max is ${maxTemperature.toFixed(2)} °C`
})
alarmLed.on()
isAlarmOn = true
}
if (isAlarmOn && (tempPercent < 1)) {
notifier.notify({
title: 'Info',
message: `Temperature is normal now. It is ${this.celsius.toFixed(2)} / Max is ${maxTemperature.toFixed(2)} °C`
})
alarmLed.off()
isAlarmOn = false
}
})
})