Skip to Content
avatar image
Former Member

Not able to connect to IoT MMS- MQTT websocket

I've been trying to connect to the WSS MMQT endpoint created for the device i added in HCP IOT service .

Everything works fine when i test it in the MMS testing client - MQTT WebSocket API.

But when i try to do it through from Node.JS program using the MQTT module , it always shows that the client is offline.

Here is the code I'm using ,which i got from : https://archive.sap.com/discussions/thread/3955899

Please let me know what is going wrong . And if possible share a sample program for posting MQTT messages from SAPUI5 / or using node.JS.

Nodejs code :


// CONFIGURATION
// ========================================================================
va rmy_account_id='p1082370trial';
var my_device_id='418d17a4-ea20-4087-bc4d-f58335cf0af3';
var my_oauth_token='ebd38cbe3438faea2ab0721b8b54a';
// ========================================================================
var endpoint='wss://iotmms'+my_account_id+'.hanatrial.ondemand.com/com.sap.iotservices.mms/v1/api/ws/mqtt';
va roptions= {
keepalive:60,
username:my_device_id,
password:my_oauth_token,
connectTimeout:5*1000,
clientId:my_device_id,
protocolId:'MQIsdp',
protocolVersion:3,
clean:true,
will:null
};
console.log(options);
// ========================================================================
varmqtt=require('mqtt');
varclient=mqtt.connect(endpoint, options);
client.on('connect', function () {
console.log('connected!');
vardata= {"mode":"async","messageType":"c04d602114e9df66a2b5","messages":[{"text":"this is sample text","count":"11","timestamp":1413191650}]};
client.publish('iot/data/'+my_device_id, JSON.stringify(data));
console.log(JSON.stringify(data));
});
client.on('error', function () {
console.log('error!');
});
client.on('offline', function () {
console.log('offline!');
});
client.on('close', function () {
console.log('close!');
});
client.on('message', function (topic, message) {
// message is Buffer
console.log(message.toString());
//client.end();
});

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Jun 06, 2017 at 03:55 PM

    Finally found a working version!

    I managed to successfully send messages via the IoT service after modifying the sendPacket method.

    Also, in the options it is necessary to increment the protocol version and use MQTT as protocol ID:

    protocolId: 'MQTT',

    protocolVersion: 4,

    Enjoy!

    Add comment
    10|10000 characters needed characters exceeded

  • May 17, 2017 at 06:26 AM

    Hello Shyam, just a question - are you able to run the simplistic example provided on mqtt npm package web site [1]? You may extend it with "error", "offline" and "close" handlers before trying. Regards, Anton

    [1] https://www.npmjs.com/package/mqtt

    Add comment
    10|10000 characters needed characters exceeded

    • Hi Anton,

      I could get the example with the mqtt library to run when I modified the sendPacket method.

      Now I tried adjusting my JavaScript code to make use of the Paho mqtt, but had to learn fairly quickly that Paho is not working with node.js.

      So back to square one and looking into mqtt and seeing if I can fix anything via parameter settings.

      Could you do me the favour and check if you can get the example with the plain mqtt library to work when you adjust the sendPacket from the link I gave above?

      Many thanks

      Nadine