NorthernMan54 / homebridge-dht

Homebridge plugin for DHT22 Temperature / Humidity Sensors
73 stars 14 forks source link

FeatureRequest: add MQTT publish #30

Closed yz88 closed 4 years ago

yz88 commented 5 years ago

Hi, great work. I'm using homebridge-dht and publish the values for temp and humi to a MQTT broker via exec() function. What do you think to build in the feature into your code?

Enclosed my modification to your code:

pi@schuppen:/usr/local/lib/node_modules/homebridge-dht $ diff index.js index.js_original
46,47c41
< var execFile = require('child_process').execFile;
< var exec = require('child_process').exec;
---
> var exec = require('child_process').execFile;
79,82d72
<  // ADD MQTT
<   this.mqttbroker = config.mqtt_broker || "localhost";
<   this.mqtttopic = config.mqtt_topic || "sensor/dht";
<
96,117d85
<
<
<         // publish sensor value via MQTT
<         var PublishTemperature = 'mosquitto_pub -h ' + this.mqttbroker + ' -q 2 -t ' + this.mqtttopic + 'temperature -m ' + roundInt(temperature);
<         exec(PublishTemperature, function (error, stdout, stderr) {
<           if (error !== null) {
<             this.log('MQTT publish failed: ' + error);
<           } else {
<             this.log('MQTT publish: ' + PublishTemperature);
<           }
<         }.bind(this));
<
<         var PublishHumidity = 'mosquitto_pub -h ' + this.mqttbroker + ' -q 2 -t ' + this.mqtttopic + 'humidity -m ' + roundInt(humidity);
<         exec(PublishHumidity, function (error, stdout, stderr) {
<           if (error !== null) {
<             this.log('MQTT publish failed: ' + error);
<           } else {
<             this.log('MQTT publish: ' + PublishHumidity);
<           }
<         }.bind(this));
<
<
143c111
<     execFile(cputemp, function(error, responseBody, stderr) {
---
>     exec(cputemp, function(error, responseBody, stderr) {
NorthernMan54 commented 5 years ago

I’m thinking that adding this doesn’t really make sense for my plugin as there are a lot of other better ways to MQTT enable a DHT22, including my personal favourite these days, node-red. But if you want to maintain your own branch, please go ahead.

On Jun 27, 2019, at 4:48 PM, yz88 notifications@github.com wrote:

Hi, great work. I'm using homebridge-dht and publish the values for temp and humi to a MQTT broker via exec() function. What do you think to build in the feature into your code?

Enclosed you modification to your code: pi@schuppen:/usr/local/lib/node_modules/homebridge-dht $ diff index.js index.js_original 5,7d4 < // See http://abyz.me.uk/rpi/pigpio/examples.html http://abyz.me.uk/rpi/pigpio/examples.html < // < // Path to this file: /usr/local/lib/node_modules/homebridge-dht/index.js 17,18d13 < // "mqtt_broker": "rp-display.fritz.box", < // "mqtt_topic": "sensors/schuppen/" 46,47c41 < var execFile = require('child_process').execFile; < var exec = require('child_process').exec;

var exec = require('child_process').execFile; 79,82d72 < // ADDON MQTT < this.mqttbroker = config.mqtt_broker || "localhost"; < this.mqtttopic = config.mqtt_topic || "sensor/dht"; < 96,117d85 < < < // publish sensor value via MQTT < var PublishTemperature = 'mosquitto_pub -h ' + this.mqttbroker + ' -q 2 -t ' + this.mqtttopic + 'temperature -m ' + roundInt(temperature); < exec(PublishTemperature, function (error, stdout, stderr) { < if (error !== null) { < this.log('MQTT publish failed: ' + error); < } else { < this.log('MQTT publish: ' + PublishTemperature); < } < }.bind(this)); < < var PublishHumidity = 'mosquitto_pub -h ' + this.mqttbroker + ' -q 2 -t ' + this.mqtttopic + 'humidity -m ' + roundInt(humidity); < exec(PublishHumidity, function (error, stdout, stderr) { < if (error !== null) { < this.log('MQTT publish failed: ' + error); < } else { < this.log('MQTT publish: ' + PublishHumidity); < } < }.bind(this)); < < 143c111 < execFile(cputemp, function(error, responseBody, stderr) {

exec(cputemp, function(error, responseBody, stderr) { — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/NorthernMan54/homebridge-dht/issues/30?email_source=notifications&email_token=AEXEFGDXQ5D2DHMZOMIJC4DP4URRVA5CNFSM4H4AB3Z2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4G4FUBBA, or mute the thread https://github.com/notifications/unsubscribe-auth/AEXEFGGZVX4TOOHAITV64ETP4URRVANCNFSM4H4AB3ZQ.