changes from remote
This commit is contained in:
156
dht.js
156
dht.js
@@ -161,7 +161,7 @@ var wws;
|
|||||||
if(credentials){
|
if(credentials){
|
||||||
var httpsServer = https.createServer(credentials, app);
|
var httpsServer = https.createServer(credentials, app);
|
||||||
httpsServer.listen(8443, () => {
|
httpsServer.listen(8443, () => {
|
||||||
console.log('HTTPS server listening on port 8443');
|
console.log('\r\n\r\nHTTPS server listening on port 8443');
|
||||||
});
|
});
|
||||||
wss = new WebSocket.Server({ server: httpsServer });
|
wss = new WebSocket.Server({ server: httpsServer });
|
||||||
}
|
}
|
||||||
@@ -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("MMQT> " + topic + " : " + context);
|
//console.log("MQTT> " + topic + " : " + context);
|
||||||
var handled = false;
|
var handled = false;
|
||||||
if(topic === "tele/tasmota/SENSOR")
|
if(topic === "tele/tasmota/SENSOR")
|
||||||
{
|
{
|
||||||
@@ -269,7 +269,7 @@ mqtt_client.on('message', function (topic, message) {
|
|||||||
}
|
}
|
||||||
if(topic === "ESP_Easy/status/LWT")
|
if(topic === "ESP_Easy/status/LWT")
|
||||||
{
|
{
|
||||||
console.log("ESP_Easy >'" + message + "'");
|
//!console.log("ESP_Easy >'" + message + "'");
|
||||||
var stat = {};
|
var stat = {};
|
||||||
var pairs = message.toString().split(';');
|
var pairs = message.toString().split(';');
|
||||||
stat.event = pairs[0];
|
stat.event = pairs[0];
|
||||||
@@ -283,13 +283,13 @@ mqtt_client.on('message', function (topic, message) {
|
|||||||
}
|
}
|
||||||
dht.ssid=stat["SSID"];
|
dht.ssid=stat["SSID"];
|
||||||
dht.rssi=stat["RSSI"];
|
dht.rssi=stat["RSSI"];
|
||||||
if(stat.event ==='ON'){console.log("Device is ONLINE! <" + dht.ssid + "> " + dht.rssi + "dB" );}
|
//if(stat.event ==='ON'){console.log(formatDate(new Date()) + "Device is ONLINE! <" + dht.ssid + "> " + dht.rssi + "dB" );}
|
||||||
if(stat.event ==='OFF'){console.log("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 === "ESP_Easy/Soil/RH")
|
||||||
{
|
{
|
||||||
console.log("Garden > soil " + message + "% moisture");
|
//console.log("Garden > soil " + message + "% moisture");
|
||||||
dht.Soil = message.toString();
|
dht.Soil = message.toString();
|
||||||
if(dht.Soil > 10){
|
if(dht.Soil > 10){
|
||||||
SaveDhtIf();
|
SaveDhtIf();
|
||||||
@@ -297,10 +297,6 @@ mqtt_client.on('message', function (topic, message) {
|
|||||||
console.log("Got suspicious soil RH value:" + dht.Soil);
|
console.log("Got suspicious soil RH value:" + dht.Soil);
|
||||||
dht.Soil = null;
|
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); }
|
|
||||||
// });
|
|
||||||
handled = true;
|
handled = true;
|
||||||
}
|
}
|
||||||
if(topic === "ESP_Easy/bmp/Temperature")
|
if(topic === "ESP_Easy/bmp/Temperature")
|
||||||
@@ -330,7 +326,7 @@ mqtt_client.on('message', function (topic, message) {
|
|||||||
dht.Soil = j.soil;
|
dht.Soil = j.soil;
|
||||||
SaveDhtIf();
|
SaveDhtIf();
|
||||||
handled = true;
|
handled = true;
|
||||||
console.log("Garden > Started watering 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 === "ESP_Easy/Water/stop")
|
||||||
{
|
{
|
||||||
@@ -338,7 +334,7 @@ mqtt_client.on('message', function (topic, message) {
|
|||||||
dht.Soil = j.soil;
|
dht.Soil = j.soil;
|
||||||
SaveDhtIf();
|
SaveDhtIf();
|
||||||
handled = true;
|
handled = true;
|
||||||
console.log("Garden > Finished watering with " + j.soil + "% soil RH");
|
console.log(formatDate(new Date()) + "Garden > Finished watering at "+j.time+"(GMT) with " + j.soil + "% soil RH");
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!handled){
|
if(!handled){
|
||||||
@@ -348,83 +344,103 @@ 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 < 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;
|
||||||
}
|
}
|
||||||
console.log("Writing to DB > " + JSON.stringify(dht)+"; SSID: '"+dht.ssid+"' RSSI:"+ dht.rssi + "dB");
|
var msg = JSON.stringify(dht);
|
||||||
db.devicemessages.insert(1, "A23_Garden_dht", JSON.stringify(dht), function (err, data) {
|
db.devicemessages.insert(1, "A23_Garden_dht", msg, function (err, data) {
|
||||||
if (!err) { console.log("success: "+ data);}
|
if (!err) {
|
||||||
|
console.log("Saved to DB > ["+ data.id +"]:" + msg);//+"; SSID: '"+dht.ssid+"' RSSI:"+ dht.rssi + "dB");
|
||||||
|
}
|
||||||
else { console.log("error: " + err); }
|
else { console.log("error: " + err); }
|
||||||
});
|
});
|
||||||
dht = {};
|
dht = {};
|
||||||
}else{
|
}else{
|
||||||
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 !");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//(function() {
|
(function() {
|
||||||
// var exLog = console.log;
|
var exLog = console.log;
|
||||||
// console.log = function(msg) {
|
console.log = function(msg) {
|
||||||
// exLog.apply(this, arguments);
|
exLog.apply(this, arguments);
|
||||||
// fs.writeFileSync('./log.txt', msg);
|
fs.appendFileSync('/var/www/dht.log', msg +"\r\n");
|
||||||
// }
|
}
|
||||||
//})();
|
})();
|
||||||
|
|
||||||
const {keys} = Object;
|
// const {keys} = Object;
|
||||||
const {Console} = console;
|
// const {Console} = console;
|
||||||
|
|
||||||
/**
|
// /**
|
||||||
* Redirect console to a file. Call without path or with false-y
|
// * Redirect console to a file. Call without path or with false-y
|
||||||
* value to restore original behavior.
|
// * value to restore original behavior.
|
||||||
* @param {string} [path]
|
// * @param {string} [path]
|
||||||
*/
|
// */
|
||||||
function file(path) {
|
// function file(path) {
|
||||||
const con = path ? new Console(fs.createWriteStream(path)) : null;
|
// const con = path ? new Console(fs.createWriteStream(path)) : null;
|
||||||
|
|
||||||
keys(Console.prototype).forEach(key => {
|
// keys(Console.prototype).forEach(key => {
|
||||||
if (path) {
|
// if (path) {
|
||||||
this[key] = (...args) => con[key](...args);
|
// this[key] = (...args) => con[key](...args);
|
||||||
} else {
|
// } else {
|
||||||
delete this[key];
|
// delete this[key];
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
};
|
// };
|
||||||
|
|
||||||
// ///---patch global console object and export
|
// // ///---patch global console object and export
|
||||||
module.exports = console.file = file;
|
// module.exports = console.file = file;
|
||||||
|
|
||||||
console.file(__dirname + '/../log.txt');//__dirname + '/debug.log' log.txt
|
// console.file(__dirname + '/../log.txt');//__dirname + '/debug.log' log.txt
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var log4js = require("log4js");
|
// var log4js = require("log4js");
|
||||||
log4js.configure({
|
// log4js.configure({
|
||||||
appenders: {
|
// appenders: {
|
||||||
file: {
|
// file: {
|
||||||
type: 'file',
|
// type: 'file',
|
||||||
filename: '/../log4.txt',
|
// filename: '/var/www/log4.txt',
|
||||||
maxLogSize: 10 * 1024 * 1024, // = 10Mb
|
// maxLogSize: 10 * 1024 * 1024, // = 10Mb
|
||||||
backups: 5, // keep five backup files
|
// backups: 5, // keep five backup files
|
||||||
compress: true, // compress the backups
|
// compress: true, // compress the backups
|
||||||
encoding: 'utf-8',
|
// encoding: 'utf-8',
|
||||||
mode: 0o0640,
|
// mode: 0o0640,
|
||||||
flags: 'w+'
|
// flags: 'w+'
|
||||||
},
|
// },
|
||||||
console: { type: 'console' }
|
// console: { type: 'console' }
|
||||||
},categories: {
|
// },categories: {
|
||||||
default: { appenders: ['console','file'], level: 'trace' }
|
// default: { appenders: ['console','file'], level: 'trace' }
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
|
|
||||||
var logger = log4js.getLogger();
|
// var logger = log4js.getLogger();
|
||||||
logger.level = "debug";
|
// logger.level = "debug";
|
||||||
logger.debug("Some debug messages");
|
// console.log("Some debug messages");
|
||||||
console.log = (msg) => logger.trace(msg);
|
// //console.log = (msg) => console.log("C>" + msg);
|
||||||
console.log("Console MSG");
|
// console.log("Console MSG");
|
||||||
|
|
||||||
|
|
||||||
|
function formatDate (date) {
|
||||||
|
var hour = date.getHours();
|
||||||
|
var minutes = date.getMinutes();
|
||||||
|
var seconds = date.getSeconds();
|
||||||
|
var milliseconds = date.getMilliseconds();
|
||||||
|
|
||||||
|
return '[' +
|
||||||
|
((hour < 10) ? '0' + hour: hour) +
|
||||||
|
':' +
|
||||||
|
((minutes < 10) ? '0' + minutes: minutes) +
|
||||||
|
':' +
|
||||||
|
((seconds < 10) ? '0' + seconds: seconds) +
|
||||||
|
'.' +
|
||||||
|
('00' + milliseconds).slice(-3) +
|
||||||
|
'] ';
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user