changed device (toppic) name from ESP_Easy to Garden

This commit is contained in:
Dobromir Popov
2020-09-12 22:11:30 +03:00
parent 67271d1c3b
commit fd499560b1

50
dht.js
View File

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