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! :-)