diff --git a/dht.js b/dht.js index 1cce07e..15435bc 100644 --- a/dht.js +++ b/dht.js @@ -219,7 +219,7 @@ mqtt.on('clientConnected', function(){ }); var mqtt = require('mqtt') -var mqtt_client = mqtt.connect('mqtt://192.168.1.131') +var mqtt_client = mqtt.connect('mqtt://192.168.1.131'); mqtt_client.on('connect', function () { console.log("MQTT connected. subscribing to topics"); mqtt_client.subscribe('tele/tasmota/STATE'); @@ -232,9 +232,9 @@ mqtt_client.on('connect', function () { // mqtt_client.subscribe('dht'); // mqtt_client.subscribe('ir'); - mqtt_client.subscribe('ESP_Easy/+'); - mqtt_client.subscribe('ESP_Easy/+/+'); - mqtt_client.publish('tasmota', 'controller connected') + mqtt_client.subscribe('Garden/+'); + mqtt_client.subscribe('Garden/+/+'); + mqtt_client.publish('tasmota', 'controller connected'); }); var dht = {}; @@ -242,7 +242,7 @@ var dht = {}; //https://stevessmarthomeguide.com/setting-up-the-sonoff-tasmota-mqtt-switch/ mqtt_client.on('message', function (topic, message) { var context = message.toString(); - //console.log("MQTT> " + topic + " : " + context); + console.log("MQTT> " + topic + " : " + context); var handled = false; if(topic === "tele/tasmota/SENSOR") { @@ -267,9 +267,9 @@ mqtt_client.on('message', function (topic, message) { } handled = true; } - if(topic === "ESP_Easy/status/LWT") + if(topic === "Garden/status/LWT") { - //!console.log("ESP_Easy >'" + message + "'"); + //!console.log("Garden >'" + message + "'"); var stat = {}; var pairs = message.toString().split(';'); stat.event = pairs[0]; @@ -287,7 +287,7 @@ mqtt_client.on('message', function (topic, message) { //if(stat.event ==='OFF'){console.log(formatDate(new Date()) + "Device went ofline!");} handled = true; } - if(topic === "ESP_Easy/Soil/RH") + if(topic === "Garden/Soil/RH") { //console.log("Garden > soil " + message + "% moisture"); dht.Soil = message.toString(); @@ -295,32 +295,33 @@ mqtt_client.on('message', function (topic, message) { SaveDhtIf(); } else { console.log("Got suspicious soil RH value:" + dht.Soil); - dht.Soil = null; + //dht.Soil = 0; } + SaveDhtIf(); handled = true; } - if(topic === "ESP_Easy/bmp/Temperature") + if(topic === "Garden/bmp/Temperature") { - console.log("Garden > air " + message + "C"); dht.Temp = message.toString(); + console.log("Garden > air " + dht.Temp + " C"); SaveDhtIf(); handled = true; } - if(topic === "ESP_Easy/bmp/Humidity") + if(topic === "Garden/bmp/Humidity") { - console.log("Garden > air " + message + "% RH"); dht.Hum = message.toString(); + console.log("Garden > air " + dht.Hum + "% RH"); SaveDhtIf(); handled = true; } - if(topic === "ESP_Easy/bmp/Pressure") + if(topic === "Garden/bmp/Pressure") { - console.log("Garden > air " + message + " hPa"); dht.Pres = message.toString(); + console.log("Garden > air " + dht.Pres + " hPa"); SaveDhtIf(); handled = true; } - if(topic === "ESP_Easy/Water/start") + if(topic === "Garden/Water/start") { var j = JSON.parse(message); dht.Soil = j.soil; @@ -328,7 +329,7 @@ mqtt_client.on('message', function (topic, message) { handled = true; console.log(formatDate(new Date()) + "Garden > Started watering at "+j.time+"(GMT) with " + j.soil + "% soil RH"); } - if(topic === "ESP_Easy/Water/stop") + if(topic === "Garden/Water/stop") { var j = JSON.parse(message); dht.Soil = j.soil; @@ -344,12 +345,12 @@ mqtt_client.on('message', function (topic, message) { function SaveDhtIf(){ if(dht.Temp && dht.Hum && dht.Pres)// && dht.Soil { - if(dht.Soil && dht.Soil < 5) - { - console.log("Soil reported below 5%! Probable loose sensor wire. Record discarded" ); - dht = {}; - return; - } + // if(dht.Soil && dht.Soil < 5) + // { + // console.log("Soil reported below 5%! Probable loose sensor wire. Record discarded" ); + // dht = {}; + // return; + // } var msg = JSON.stringify(dht); db.devicemessages.insert(1, "A23_Garden_dht", msg, function (err, data) { if (!err) { @@ -359,10 +360,11 @@ function SaveDhtIf(){ }); dht = {}; }else{ + console.log("T: %s; H: %s; P: $s", dht.Temp, dht.Hum, dht.Pres); // if(dht.Temp ){console.log("have Temp");} // if(dht.Hum ){console.log("have Hum");} // if(dht.Pres ){console.log("have Pres");} - // console.log("Missing all data to write to DB !"); + console.log("Missing all data to write to DB !"); } }