cummulative changes

This commit is contained in:
d-popov
2020-06-09 00:38:35 +03:00
parent 8a68a07112
commit 6a8a895f69
5 changed files with 211 additions and 83 deletions

53
dht.js
View File

@@ -212,6 +212,7 @@ 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')
});
@@ -246,11 +247,36 @@ mqtt_client.on('message', function (topic, message) {
}
handled = true;
}
if(topic === "ESP_Easy/Water/Moisture")
if(topic === "ESP_Easy/status/LWT")
{
console.log("MANGO> " + message + "% moisture");
console.log("ESP_Easy >'" + message + "'");
var stat = {};
var pairs = message.toString().split(';');
stat.event = pairs[0];
for (var i = 0; i < pairs.length; i++)
{
var pair = pairs[i].split(':');
if(pair.length == 2){
stat[pair[0].trim()] = pair[1].trim() || '';
//console.log( "'"+pair[0] +"' >'" + pair[1] + "'");
}
}
dht.ssid=stat["SSID"];
dht.rssi=stat["RSSI"];
if(stat.event ==='ON'){console.log("Device is ONLINE! <" + dht.ssid + "> " + dht.rssi + "dB" );}
if(stat.event ==='OFF'){console.log("Device went ofline!");}
handled = true;
}
if(topic === "ESP_Easy/Soil/RH")
{
console.log("Garden > soil " + message + "% moisture");
dht.Soil = message.toString();
SaveDhtIf();
if(dht.Soil > 10){
SaveDhtIf();
} else {
console.log("Got suspicious soil RH value:" + dht.Soil);
dht.Soil = null;
}
// db.devicemessages.insert(1, "A23_Garden_Humidity", message, function (err, data) {
// if (!err) { console.log("success: "+ data);}
// else { console.log("error: " + err); }
@@ -278,6 +304,15 @@ mqtt_client.on('message', function (topic, message) {
SaveDhtIf();
handled = true;
}
if(topic === "ESP_Easy/Water/start" || topic === "ESP_Easy/Water/stop")
{
var j = JSON.parse(message);
console.log("Garden > Finished watering with " + j.soil + "% soil RH");
dht.Soil = j.soil;
SaveDhtIf();
handled = true;
}
if(!handled){
console.log(topic + " > " + message );
}
@@ -285,11 +320,19 @@ mqtt_client.on('message', function (topic, message) {
function SaveDhtIf(){
if(dht.Temp && dht.Hum && dht.Pres && dht.Soil)
{
console.log("Writing to DB > " + JSON.stringify(dht));
if(dht.Soil < 5)
{
console.log("Soil reported below 5%! Probable loose sensor wire. Record discarded" );
dht = {};
return;
}
console.log("Writing to DB > " + JSON.stringify(dht)+"; SSID: '"+dht.ssid+"' RSSI:"+ dht.rssi + "dB");
db.devicemessages.insert(1, "A23_Garden_dht", JSON.stringify(dht), function (err, data) {
if (!err) { console.log("success: "+ data);}
else { console.log("error: " + err); }
});
dht = {};
}else{
console.log("Missing all data to write to DB !");
}
}
}