You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Feb 11, 2020. It is now read-only.
Please help. I am new to mqtt.
When I tried to change message payload, it automatically connect/reconnect my client. But if I change the message payload = data in index.js it works perfectly fined. But I want to add timestamp.
index.js
const bodyParser = require('body-parser');
const morgan = require('morgan');
const serialPort = require('serialport');
const readLine = serialPort.parsers.Readline;
// const socketIo = require('socket.io');
//SERIAL COMMUNICATION
const sensorPort = new serialPort('COM3', {
baudRate: 9600,
});
const parser = sensorPort.pipe(new readLine({ delimiter: '\r\n'}));
parser.on('open', onOpen);
parser.on('data', (data) => {
// Sending data from mosca to clients
var message = {
topic: 'hello',
payload: {
datum: data,
timestamp: JSON.stringify(Math.floor(new Date() / 1000)),
}, // or a Buffer
qos: 1, // 0, 1, or 2
retain: true // or true
};
mosca.publish(message, function() {
// console.log(message.payload.datum);
});
});
function onOpen() {
console.log('Arduino connected!');
}
sensorPort.on('error', (err) => {
});
//SERIAL COMMUNICATION
const app = express();
app.use(morgan('tiny'));
app.use(cors());
app.use(bodyParser.json());
const mosca = require('./moscaServer/index');
mosca/index.js
const mosca = require('mosca')
const port = process.env.PORT || 5000;
var moscaSettings = {
http: {
port: port,
bundle : true,
// static : './client/layouts/default.vue',
},
};
// start mosca
const moscaServer = new mosca.Server(moscaSettings)
moscaServer.on('ready', setup)
// fired when the mqtt server is ready
function setup() {
console.log('Mosca server is up and running in port 1883!')
console.log(`Using port ${port} for MQTT over Web-Sockets!`)
}
// fired when a client is connected
moscaServer.on('clientConnected', function(client) {
console.log('client connected', client.id)
})
// fired when a client subscribes to a topic
moscaServer.on('subscribed', function(topic, client) {
console.log('subscribed : ', topic)
})
// fired when a client unsubscribes to a topic
moscaServer.on('unsubscribed', function(topic, client) {
console.log('unsubscribed : ', topic)
})
// fired when a client is disconnecting
moscaServer.on('clientDisconnecting', function(client) {
console.log('clientDisconnecting : ', client.id)
})
// fired when a client is disconnected
moscaServer.on('clientDisconnected', function(client) {
console.log('clientDisconnected : ', client.id)
})
module.exports = moscaServer
default.vue (nuxtjs)
import Nav from '@/components/NavBar.vue';
import mqtt from 'mqtt'
export default {
components: {
Nav
},
data() {
return {
client : mqtt.connect('ws:127.0.0.1:5000')
}
},
mounted() {
this.client.on('connect', function () {
//subscribe to listen to the channels in which arduinos will be publishing
console.log("succesfully connected")
})
this.client.subscribe('hello', { qos: 1 , retain : true }, () => {
console.log('succesfully subscribe!')
});
this.client.on('message', (topic, payload, packet) => {
console.log('topic: ', topic);
// console.log('payload: ', payload.toString('utf-8'));
// console.log('payload: ', packet.toString('utf-8'));
})
},
}
The text was updated successfully, but these errors were encountered:
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Please help. I am new to mqtt.
When I tried to change message payload, it automatically connect/reconnect my client. But if I change the message
payload
=data
inindex.js
it works perfectly fined. But I want to add timestamp.index.js
mosca/index.js
default.vue (nuxtjs)
The text was updated successfully, but these errors were encountered: