-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
139 lines (112 loc) · 4.55 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const moment = require('moment');
const mqtt = require('mqtt');
var Service, Characteristic;
var FakeGatoHistoryService;
module.exports = homebridge => {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
FakeGatoHistoryService = require('fakegato-history')(homebridge);
homebridge.registerAccessory('homebridge-soil-moisture', 'Soil Moisture Sensor', SoilMoistureAccessory);
}
function SoilMoistureAccessory(log, config) {
this.log = log;
this.displayName = config['name'];
this.maxAnalogReading = config['maxAnalogReading'] || 1024;
this.minAnalogReading = config['minAnalogReading'] || 0;
this.moistureThreshold = config['moistureThreshold'] || 25;
this.timeout = config['timeout'] || 5;
this.enableFakeGato = config['enableFakeGato'] || false;
this.fakeGatoStoragePath = config['fakeGatoStoragePath'];
this.mqttConfig = config['mqtt'];
this._currentMoisture = null;
let informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, 'Waveshare')
.setCharacteristic(Characteristic.Model, 'Moisture Sensor')
.setCharacteristic(Characteristic.SerialNumber, `${this.mqttConfig.soilMoistureTopic.replace("/", "-")}`)
.setCharacteristic(Characteristic.FirmwareRevision, require('./package.json').version);
let humidityService = new Service.HumiditySensor();
let leakService = new Service.LeakSensor();
this.informationService = informationService;
this.humidityService = humidityService;
this.leakService = leakService;
if (this.enableFakeGato) {
this.fakeGatoHistoryService = new FakeGatoHistoryService('weather', this, {
storage: 'fs',
filename: `SoilMoisture-${this.mqttConfig.soilMoistureTopic.replace('/', '-')}.json`,
folder: this.fakeGatoStoragePath
});
}
this.setupMQTT();
this.lastUpdated = moment();
setInterval(() => {
if (moment().diff(this.lastUpdated, 'minutes') >= this.timeout) {
this.log(`No messages received for last ${this.timeout} minute(s), assuming error!`);
this.humidityService.getCharacteristic(Characteristic.CurrentRelativeHumidity)
.updateValue(Error());
this.leakService.getCharacteristic(Characteristic.LeakDetected)
.updateValue(Error());
}
}, 60 * 1000);
}
Object.defineProperty(SoilMoistureAccessory.prototype, 'currentMoisture', {
set: function(moistureReading) {
moistureReading = Math.min(moistureReading, this.maxAnalogReading);
moistureReading = Math.max(moistureReading, this.minAnalogReading);
// For capacitive soil moisture sensors, lower value = higher moisture content
this._currentMoisture = 100 - (moistureReading - this.minAnalogReading) / (this.maxAnalogReading - this.minAnalogReading) * 100;
this.humidityService.getCharacteristic(Characteristic.CurrentRelativeHumidity)
.updateValue(this._currentMoisture);
if (this._currentMoisture < this.moistureThreshold) {
this.leakService.getCharacteristic(Characteristic.LeakDetected)
.updateValue(true);
} else {
this.leakService.getCharacteristic(Characteristic.LeakDetected)
.updateValue(false);
}
if (this.enableFakeGato) {
this.fakeGatoHistoryService.addEntry({
time: moment().unix(),
humidity: this._currentMoisture,
});
}
},
get: function() {
return this._currentMoisture;
}
});
SoilMoistureAccessory.prototype.setupMQTT = function() {
if (!this.mqttConfig) {
this.log.error('No MQTT config found');
return;
}
this.mqttURL = this.mqttConfig.url;
this.soilMoistureTopic = this.mqttConfig.soilMoistureTopic || 'soil_humidity';
this.mqttClient = mqtt.connect(this.mqttURL);
this.mqttClient.on('connect', () => {
this.log(`MQTT client connected to ${this.mqttURL}`);
this.mqttClient.subscribe(this.soilMoistureTopic, (err) => {
if (!err) {
this.log(`MQTT client subscribed to ${this.soilMoistureTopic}`);
}
});
});
this.mqttClient.on('message', (topic, message) => {
this.onMQTTMessage(topic, message)
});
this.mqttClient.on('error', (err) => {
this.log(`MQTT client error: ${err}`);
client.end();
});
}
SoilMoistureAccessory.prototype.onMQTTMessage = function(topic, message) {
this.log(`Received measurement: ${parseInt(message)}`);
this.currentMoisture = parseInt(message);
this.lastUpdated = moment();
}
SoilMoistureAccessory.prototype.getServices = function() {
return [this.informationService,
this.humidityService,
this.leakService,
this.fakeGatoHistoryService];
}