diff --git a/imports/api/mqttApi.js b/imports/api/mqttApi.js new file mode 100644 index 0000000..43491b3 --- /dev/null +++ b/imports/api/mqttApi.js @@ -0,0 +1,15 @@ +import { connect } from 'mqtt'; +import _ from 'lodash'; +// var mqtt = require('mqtt'); + + +var client = connect('mqtt://mqtt.timovolkmann.de') + +function publish(topic, message) { + if (!client.connected) { + client.reconnect(); + } + client.publish(topic, message, () => console.log('message sent...')) +} + +module.exports = { publish } \ No newline at end of file diff --git a/imports/api/mqttPublish.js b/imports/api/mqttPublish.js deleted file mode 100644 index 6724cf1..0000000 --- a/imports/api/mqttPublish.js +++ /dev/null @@ -1,32 +0,0 @@ -var mqtt = require('mqtt') -var client = mqtt.connect('mqtt://timovolkmann.de') - -const PUBLISH_PATH = "smartgarden/commands/"; -const MQTT_DEVICE_ID = "esp-sebastian"; - -publishMessage("smartgarden/commands/esp-sebastian/light", "on"); - -/*client.on('connect', function () { - client.subscribe('presence', function (err) { - if (!err) { - //client.publish('presence', 'Hello mqtt') - publishMessage("smartgarden/commands/esp-sebastian/light", "on") - } - }) -}) - -client.on('message', function (topic, message) { - // message is Buffer - console.log(message.toString()) - client.end() -})*/ - -function publishMessage(topic, message) { - client.publish(topic, message); - console.log("publish message got called: " + message); -} - -module.exports = { - publishMessage -} - diff --git a/imports/ui/Home.jsx b/imports/ui/Home.jsx index e57d60e..352a858 100644 --- a/imports/ui/Home.jsx +++ b/imports/ui/Home.jsx @@ -3,7 +3,8 @@ import {CartesianGrid, Legend, Line, LineChart, ResponsiveContainer, Tooltip, XA import SensorCardDeck from './SensorCardDeck' import {SensorDataCollection, ActiveDeviceCollection} from "../../client/main"; import {useTracker} from 'meteor/react-meteor-data'; -import {Col, Form, Row, Card, CardDeck} from "react-bootstrap"; +import { Col, Form, Row, Card, CardDeck, Button } from "react-bootstrap"; +import { Meteor } from 'meteor/meteor'; export default function Home() { @@ -37,6 +38,20 @@ export default function Home() { } } + const handleTest = (e) => { + console.log("test") + Meteor.call('mqttPublish', { + topic: 'smartgarden/commands/TEST', + payload: '{"payload": 1234}' + }, (err, res) => { + if (err) { + alert(err); + } else { + alert('success') + } + }) + } + if ((sensorData.length <= 0) || (deviceName.length <= 0)) { return ( @@ -73,6 +88,9 @@ export default function Home() {
active device: {deviceName.deviceName}
+ + + diff --git a/package.json b/package.json index 8b4d904..3f9d8b6 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,8 @@ "react-dom": "^16.13.1", "react-router-dom": "^5.2.0", "recharts": "^1.8.5", - "utf-8-validate": "^5.0.2" + "utf-8-validate": "^5.0.2", + "mqtt": "^4.1.0" }, "meteor": { "mainModule": { @@ -30,7 +31,6 @@ "testModule": "tests/main.js" }, "devDependencies": { - "mongodb": "^3.5.9", - "mqtt": "^4.1.0" + "mongodb": "^3.5.9" } } diff --git a/server/main.js b/server/main.js index b836f96..7f9ac00 100644 --- a/server/main.js +++ b/server/main.js @@ -1,4 +1,5 @@ import { Meteor } from 'meteor/meteor'; +import { publish } from '../imports/api/mqttApi.js' var PlantTypesCollection = new Meteor.Collection('plantTypes'); var SensorDataCollection = new Meteor.Collection('sensorData'); @@ -24,4 +25,11 @@ Meteor.startup(() => { Meteor.subscribe('sensorDataCollection'); Meteor.subscribe('activeDeviceCollection'); } + + Meteor.methods({ + 'mqttPublish'({ topic, payload }) { + publish(topic, payload) + } + }) + }); \ No newline at end of file