0

I have a paho based sub running on raspberry pi that listens a cloudmqtt broker with username and password. I forward the message to the bluetooth socket.

Using a cordova app I publish messages "0" or "1". It works fine. But the problem arises when I use the same app on a different mobile only the last opened app works.

Simplifying things..keeping aside the bluetooth thing.

Scenario goes like this, I am having an MQTT broker on cloudmqtt:

url : m12.cloudmqtt.com port: 11395 username : user password: pass

When I run a mosquitto_sub client on raspberry pi with this command:

mosquitto_sub -h m12.cloudmqtt.com -p 11395 -u user -P pass -t sample/test

It do run and then I run two mosquitto_pub in two different termianls with same command as below:

mosquitto_pub -h m12.cloudmqtt.com -p 11395 -u user -P pass -t sample/test -m "Hi"

Works like a charm!

But now when I use these MQTT credentials from cordova app using Paho MQTT plugin, with the below code:

index.js

var connect = false;
var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
        onButton.addEventListener('touchend', app.sendOnSignal, false);
        offButton.addEventListener('touchend', app.sendOffSignal, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicitly call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        //alert("ondeviceready called");
        app.connectFreeBroker();
    },
    // Update DOM on a Received Event

    connectFreeBroker:function (id,s) {
      //alert("connectFreeBroker called");
      cordova.plugins.CordovaMqTTPlugin.connect({
    url:"tcp://m12.cloudmqtt.com", //a public broker used for testing purposes only. Try using a self hosted broker for production. 
    port:11395, 
    connectionTimeout:3000,
    keepAlive:6000,
    isCleanSession:true,
    clientid:"client-2",
    username:"user",
    password:"pass",
    success:function(s){
        //alert("connect success callback");
    },
    error:function(e){
        //alert("connect error callback");
    },
    onConnectionLost:function (){
        console.log("disconnect");
    }
    })
    },
    sendOnSignal:function (id,s) {
      //alert("sendOnSignal called");
      cordova.plugins.CordovaMqTTPlugin.publish({
       topic:"sample/test",
       payload:"1",
      success:function(s){
        //alert("send success callback");
        document.getElementById("bulb_image").src = "images/bulb-glow.png";
      },
      error:function(e){
        //alert("send error callback");
      }
    })
    },
    sendOffSignal:function (id,s) {
      //alert("sendOffSignal called");
      cordova.plugins.CordovaMqTTPlugin.publish({
       topic:"sample/test",
       payload:"0",
      success:function(s){
        //alert("send success callback");
        document.getElementById("bulb_image").src = "images/bulb-unglow.png";
      },
      error:function(e){
        //alert("send error callback");
      }
    })
    }
};

app.initialize();

//code ends

On deploying this app on first android phone(Galaxy S5) it works and the sub running on raspberry pi receives the message. Now when deploying the same app with different client id on second android phone(HTC desire 820), it runs as same but running same app simultaneously on the two phones and sending the message wont work. Only the message from the phone on which the app was opened last will send the message.

I tried using different brokers but same problem. I tried using the app on 4 android phone still no luck. May be a small mistake I am doing, but will solve a great problem for me, any kind of help appreciated! :-)

4
  • You are going to have to provide a LOT more information before anybody can help. e.g. What type of phone is failing on? What OS is running on the phones? Also what relevance is the Raspberry Pi/Bluetooth, it seams unrelated to the problem with Cordova?
    – hardillb
    Jul 3, 2016 at 20:24
  • What do you mean by 'last opened app'? Give all the details. Pretend i don't know anything about the problem you are having and what you have already tried. Jul 3, 2016 at 20:38
  • 1
    This sounds like the behaviour on bluemix where when multiple devices login using the same ID, when a new device logs in the previous device on that ID is disconnected. Does your app login using an ID? Does each mobile use a different ID? Jul 4, 2016 at 6:27
  • Anyone going to help? I am still facing the same issue. Tried with QOS 2 as well not no luck!
    – Anuj
    Jul 6, 2016 at 18:15

1 Answer 1

0

Mqtt requires an unique id for each device, so change the line:

clientid:"client-2",

to something like:

clientid:"client-" + parseInt(Math.random() * 100, 10)
1
  • I tried that in the first step only but no luck, at last I had to shift to some other library.
    – Anuj
    Sep 6, 2016 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.