Skip to content
This repository has been archived by the owner on Feb 11, 2020. It is now read-only.

Trying to change the message payload it automatically connect/disconnect, subscribe/unsubscribe #789

Open
iamqinglong opened this issue Mar 10, 2019 · 0 comments

Comments

@iamqinglong
Copy link

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'));
      })

    },
  }

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant