From ebb062e71d3d3c80c41e3b7a6c93149bb84290a1 Mon Sep 17 00:00:00 2001 From: iotcat Date: Fri, 14 Aug 2020 09:54:45 +0800 Subject: [PATCH] init --- .gitignore | 1 + index.js | 31 +++++++++++++++++++++++++++++++ package.json | 28 ++++++++++++++++++++++++++++ waterFlow.dat | 1 + 4 files changed, 61 insertions(+) create mode 100644 .gitignore create mode 100644 index.js create mode 100644 package.json create mode 100644 waterFlow.dat diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bca51b3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/index.js b/index.js new file mode 100644 index 0000000..9c895f0 --- /dev/null +++ b/index.js @@ -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'); + } +}); + diff --git a/package.json b/package.json new file mode 100644 index 0000000..e12bcc1 --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/waterFlow.dat b/waterFlow.dat new file mode 100644 index 0000000..ddf566e --- /dev/null +++ b/waterFlow.dat @@ -0,0 +1 @@ +239.06999999999962 \ No newline at end of file