master
iotcat 4 years ago
commit ebb062e71d
  1. 1
      .gitignore
  2. 31
      index.js
  3. 28
      package.json
  4. 1
      waterFlow.dat

1
.gitignore vendored

@ -0,0 +1 @@
node_modules/

@ -0,0 +1,31 @@
const mqtt = require('mqtt');
const fs = require('fs');
var client = mqtt.connect('mqtt://127.0.0.1');
var record = (addNum) => {
var waterFlow = parseFloat(fs.readFileSync('waterFlow.dat'));
waterFlow += Number(addNum);
waterFlow = "" + waterFlow;
console.log(waterFlow);
fs.writeFileSync('waterFlow.dat', waterFlow);
return waterFlow;
}
client.on('connect', ()=>{
client.subscribe('hass/snsr/wc0/#', (err)=>{
if(!err){
client.publish('hass/ctl/wc0/flowmeter', '1');
console.log(new Date().toTimeString() + ' - MQTT Connected!!');
}
});
});
client.on('message', (subject, content)=>{
if(subject == "hass/snsr/wc0/flowmeter"){
client.publish('hass/snsr/wc0/flowmeterTotal', record(content));
client.publish('hass/ctl/wc0/flowmeterGotten', '1');
}
});

@ -0,0 +1,28 @@
{
"name": "flowmeter",
"version": "1.0.0",
"description": "flowmeter recoder for wc MCU",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/iotcat/sola.git"
},
"keywords": [
"iot",
"mqtt",
"arduino",
"esp8266"
],
"author": "iotcat",
"license": "MIT",
"bugs": {
"url": "https://github.com/iotcat/sola/issues"
},
"homepage": "https://github.com/iotcat/sola#readme",
"dependencies": {
"mqtt": "^3.0.0"
}
}

@ -0,0 +1 @@
239.06999999999962
Loading…
Cancel
Save