mqtt works
This commit is contained in:
parent
8a7aac6625
commit
0fcc6f558e
15
imports/api/mqttApi.js
Normal file
15
imports/api/mqttApi.js
Normal file
@ -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 }
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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() {
|
||||
<Col>
|
||||
<h6>active device: {deviceName.deviceName}</h6>
|
||||
</Col>
|
||||
<Col>
|
||||
<Button type={"test"} onClick={this.handleTest} >Send something test</Button>
|
||||
</Col>
|
||||
</Row>
|
||||
|
||||
<SensorCardDeck/>
|
||||
|
||||
@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
@ -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)
|
||||
}
|
||||
})
|
||||
|
||||
});
|
||||
Loading…
Reference in New Issue
Block a user