diff --git a/dht.js b/dht.js index 5031852..f8b0d0e 100644 --- a/dht.js +++ b/dht.js @@ -98,6 +98,7 @@ function(req, res){ function(req, res){ res.render('chart', { user: req.user }); }); + app.get('/garden', function(req, res){ res.render('chartGarden'); }); //Authentication -- @@ -119,8 +120,8 @@ function BroadcastWS(msg){ } -app.get("/device/:field_name", function (req, res) { - db.devicemessages.findByName(req.params.field_name, function (err, data) { +app.get("/device/:field_name/:days?", function (req, res) { + db.devicemessages.findByName(req.params.field_name, req.params.days, function (err, data) { if (!err) { res.send(data); } else { console.log("error: ", err); } }); @@ -155,20 +156,20 @@ else - // # ┌────────────── second (optional) - // # │ ┌──────────── minute - // # │ │ ┌────────── hour - // # │ │ │ ┌──────── day of month - // # │ │ │ │ ┌────── month - // # │ │ │ │ │ ┌──── day of week - // # │ │ │ │ │ │ - // # │ │ │ │ │ │ - // # * * * * * * -cron.schedule(' */30 * * * *', () => {//cron.schedule('*/5 * * * * *', () => { - console.log(new Date().toISOString() + ' running a task every 30 minutes'); - //StoreSensorReadings(); - db.devicemessages.getFromDht('http://192.168.1.126/json'); -}).start(); +// // # ┌────────────── second (optional) +// // # │ ┌──────────── minute +// // # │ │ ┌────────── hour +// // # │ │ │ ┌──────── day of month +// // # │ │ │ │ ┌────── month +// // # │ │ │ │ │ ┌──── day of week +// // # │ │ │ │ │ │ +// // # │ │ │ │ │ │ +// // # * * * * * * +// cron.schedule(' */30 * * * *', () => {//cron.schedule('*/5 * * * * *', () => { +// console.log(new Date().toISOString() + ' running a task every 30 minutes'); +// //StoreSensorReadings(); +// db.devicemessages.getFromDht('http://192.168.1.126/json'); +// }).start(); //db.devicemessages.getFromDht('http://192.168.1.126/json'); @@ -204,23 +205,28 @@ mqtt_client.on('connect', function () { mqtt_client.subscribe('tele/tasmota/STATE'); mqtt_client.subscribe('tele/tasmota/RESULT');//IR mqtt_client.subscribe('tele/tasmota/INFO2'); - mqtt_client.subscribe('tele/tasmota/SENSOR');//DHT - + // mqtt_client.subscribe('tele/tasmota/SENSOR');//DHT + mqtt_client.subscribe('tele/+/SENSOR');//DHT mqtt_client.subscribe('tasmota_3FD92D'); - mqtt_client.subscribe('tele'); - mqtt_client.subscribe('dht'); - mqtt_client.subscribe('ir'); - mqtt_client.publish('tasmota', 'controller') + // mqtt_client.subscribe('tele'); + // mqtt_client.subscribe('dht'); + // mqtt_client.subscribe('ir'); + + mqtt_client.subscribe('ESP_Easy/+/+'); + mqtt_client.publish('tasmota', 'controller connected') }); + +var dht = {}; // https://github.com/pauloromeira/Sonoff-Tasmota/wiki/Commands //https://stevessmarthomeguide.com/setting-up-the-sonoff-tasmota-mqtt-switch/ mqtt_client.on('message', function (topic, message) { var context = message.toString(); console.log("MMQT> " + topic + " : " + context); + var handled = false; if(topic === "tele/tasmota/SENSOR") { var j = JSON.parse(message); - console.log("JSON> " + util.inspect(j)); + //console.log("JSON> " + util.inspect(j)); if(j.DHT11 && j.DHT11.Humidity !== null) { var msg = { @@ -238,5 +244,52 @@ mqtt_client.on('message', function (topic, message) { }else { console.log("got wrong DHT data: " + message ); } + handled = true; } -}); \ No newline at end of file + if(topic === "ESP_Easy/Water/Moisture") + { + console.log("MANGO> " + message + "% moisture"); + dht.Soil = message.toString(); + SaveDhtIf(); + // db.devicemessages.insert(1, "A23_Garden_Humidity", message, function (err, data) { + // if (!err) { console.log("success: "+ data);} + // else { console.log("error: " + err); } + // }); + handled = true; + } + if(topic === "ESP_Easy/bmp/Temperature") + { + console.log("Garden > air " + message + "C"); + dht.Temp = message.toString(); + SaveDhtIf(); + handled = true; + } + if(topic === "ESP_Easy/bmp/Humidity") + { + console.log("Garden > air " + message + "% RH"); + dht.Hum = message.toString(); + SaveDhtIf(); + handled = true; + } + if(topic === "ESP_Easy/bmp/Pressure") + { + console.log("Garden > air " + message + " hPa"); + dht.Pres = message.toString(); + SaveDhtIf(); + handled = true; + } + if(!handled){ + console.log(topic + " > " + message ); + } +}); +function SaveDhtIf(){ + if(dht.Temp && dht.Hum && dht.Pres && dht.Soil) + { + console.log("Writing to DB > " + JSON.stringify(dht)); + 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 = {}; + } +} \ No newline at end of file diff --git a/src/db/database.js b/src/db/database.js index 4830ae8..181dacc 100644 --- a/src/db/database.js +++ b/src/db/database.js @@ -33,6 +33,32 @@ var DeviceMessage = sqlz.define('devicemessage', { timestamp: { type: Sequelize.DATE, allowNull: false}, }); +DeviceMessage.GetByName = function (name, days) +{ + var User = sqlz.User; + User.find({ + where: { + field_name: name, + timestamp: {$gte: new Date()} + } + }).then(function(user) + { + if(!user) + { + error.status = 500; error.message = "ERROR_INVALID_USER"; error.code = 301; + return next(error); + } + + // Build the profile from the user object + profile = { + "firstName": user.firstName, + "lastName": user.lastName, + "emailAddress": user.emailAddress + } + response.status(200).send(profile); + }); +} + var DeviceCommand = sqlz.define("command", { device: { type: Sequelize.STRING}, command: {type: Sequelize.TEXT}, diff --git a/src/db/devicemessages.js b/src/db/devicemessages.js index ca6b6c2..37b8144 100644 --- a/src/db/devicemessages.js +++ b/src/db/devicemessages.js @@ -9,10 +9,13 @@ var con = mysql.createConnection({ const got = require('got'); -exports.findByName = function(fieldName, cb) { +exports.findByName = function(fieldName, days, cb) { process.nextTick(function() { - con.query("SELECT * FROM devicemessages WHERE field_name=? OR ? IS NULL", - [fieldName, fieldName], (err, data) => { + // console.log("findByName(" +fieldName + ",?" + days +")" ); + // days = days|365; + console.log("findByName(" +fieldName + "," + days +")" ); + con.query("SELECT * FROM devicemessages WHERE (field_name=? OR ? IS NULL) AND (timestamp >= ( CURDATE() - INTERVAL ? DAY ))", + [fieldName, fieldName, days], (err, data) => { if (!err) { cb(null, data); } else { diff --git a/src/db/index.js b/src/db/index.js index e88654c..d6d5582 100644 --- a/src/db/index.js +++ b/src/db/index.js @@ -21,3 +21,4 @@ data.init(); exports.devicemessages = require('./devicemessages'); exports.users = require('./users'); +exports.orm = require('./database'); diff --git a/views/accontrol.ejs b/views/accontrol.ejs index 089c273..bd04595 100644 --- a/views/accontrol.ejs +++ b/views/accontrol.ejs @@ -59,7 +59,7 @@ INFO: window.onload = function () { $.getJSON("/n/dht?e=now", function(data){ if(data && data.dht){ - $('#current').text( "A23 Currently is " + data.dht.temp + "°C, " + data.dht.hum +"% RH. Dew Point at:" + data.dht.dew ); + $('#current').text( "A23 Currently is " + data.dht.temp + "°C, " + data.dht.hum +"% RH. Dew point : " + data.dht.dew + "°C" ); }else { //chart.title.set("text", "A23 conditions"); } diff --git a/views/chart.ejs b/views/chart.ejs index b86d823..73cec6b 100644 --- a/views/chart.ejs +++ b/views/chart.ejs @@ -8,6 +8,7 @@ window.onload = function () { var dataPoints1 = []; var dataPoints2 = []; + var dataPoints3 = []; /*{ x: new Date(2017, 0, 3), y: 650 }, { x: new Date(2017, 0, 4), y: 700 }, */ @@ -25,6 +26,16 @@ text: "X Axis scale is Logarithmic", fontSize: 14 }],*/ + toolTip:{ + shared:true + }, + legend:{ + cursor:"pointer", + verticalAlign: "bottom", + horizontalAlign: "left", + dockInsidePlotArea: true, + itemclick: toogleDataSeries + }, axisX:{ valueFormatString: "HH:mm DD MMM", crosshair: { @@ -39,13 +50,25 @@ labelFontColor: "#C24642", crosshair: { enabled: true - } + }, + minimum:0, + maximum: 50, }, axisY2: [ { - title: "Relative hum %", + title: "Air RH %", titleFontColor: "#51CDA0", - labelFontColor: "#51CDA0" + labelFontColor: "#51CDA0", + minimum:0, + maximum: 100, + }, + { + title: "Soil RH %", + lineColor: "#99D9EA", + titleFontColor: "#99D9EA", + labelFontColor: "#99D9EA", + minimum:0, + maximum: 100, } ], //{title: "Temperature", @@ -53,19 +76,9 @@ //titleFontColor: "#C0504E", //labelFontColor: "#C0504E" //}, - toolTip:{ - shared:true - }, - legend:{ - cursor:"pointer", - verticalAlign: "bottom", - horizontalAlign: "left", - dockInsidePlotArea: true, - itemclick: toogleDataSeries - }, data: [{ name: "Temperature", - type: "spline",//line + type: "line",//line showInLegend: true, markerType: "square", xValueFormatString: "HH:mm on DD MMM, YYYY", @@ -73,13 +86,23 @@ dataPoints: dataPoints2 }, { - name: "Humidiry", + axisYType: "secondary",//at right + //axisYIndex: 1, + name: "RH Air", type: "spline", - axisYType: "secondary", - axisYIndex: 1, showInLegend: true, lineDashType: "dash", dataPoints: dataPoints1 + }, + { + axisYType: "secondary", + //axisYIndex: 1, + name: "RH Soil", + type: "spline", + showInLegend: true, + lineDashType: "dash", + color: "#99D9EA", + dataPoints: dataPoints3 }] }); @@ -95,9 +118,10 @@ function addData(data) { for (var i = 0; i < data.length; i++) { + var date = new Date(data[i]["timestamp"]); if(data[i].field_name == "A23_DHT"){ + //console.log("Got DHT:" + date); var dht = JSON.parse(data[i].field_value).dht; - var date = new Date(data[i]["timestamp"]); if(dht && dht.hum <= 100){ dataPoints1.push({ x: date, @@ -109,6 +133,15 @@ }); } } + if(data[i].field_name === "A23_Garden_Humidity") + { + //console.log("Got Soil:" + date); + var soil = parseFloat( data[i].field_value); + dataPoints3.push({ + x: date, + y: soil + }); + } } chart.render(); @@ -120,8 +153,9 @@ } }); } - - $.getJSON("/n/device/A23_DHT", addData); + + $.getJSON("/n/device/A23_DHT/7", addData); + $.getJSON("/n/device/A23_Garden_Humidity/7", addData); } diff --git a/views/chartGarden.ejs b/views/chartGarden.ejs new file mode 100644 index 0000000..ee1dc62 --- /dev/null +++ b/views/chartGarden.ejs @@ -0,0 +1,180 @@ +
+ + +<%- contentFor('head') %> + \ No newline at end of file diff --git a/views/nav.ejs b/views/nav.ejs index bb406e1..ee92125 100644 --- a/views/nav.ejs +++ b/views/nav.ejs @@ -1,4 +1,6 @@ + control log out - chart \ No newline at end of file + chart + garden \ No newline at end of file