Skip to Content
1

Not able to connect to IoT MMS- MQTT websocket

May 17, 2017 at 04:24 AM

374

avatar image
Former Member

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

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Nadine Gaertner
Jun 06, 2017 at 03:55 PM
2

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!

Share
10 |10000 characters needed characters left characters exceeded
Anton Levin
May 17, 2017 at 06:26 AM
0

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

Show 3 Share
10 |10000 characters needed characters left characters exceeded

@Shyam, have you found a solution in the meantime?

@Anton: I'm having the same issue. I tried the example program which you linked "as is" and it works fine. When I adapt it to connect to the IoT service instead of the mosquitto test I get "offline" and "closed" events.

I have tried different protocols ("mqtt" and "mqtts" like in the original mosquitto example, "wss" like mentioned above and "https" like in the documentation) and also added port 443 to the URL, but none of these variants worked.

Someone else seems to have fixed the same issue by modifying file mqtt/lib/client.js. I tried the same but without success. Note that in the example they are working with a p12 certificate which I cannot use due to working in a trial instance. Also I have not succeeded adapting their code from p12 to bearer token.

I am kind of out of ideas.

My best guess would be that maybe the options for the mqtt connect or even the websocket options might need to be set differently in order to be properly mapped to the needs of the IoT service.

Do you have any more tips or ideas?

Thanks & cheers!

0

Hello Nadine, interestingly the default npmjs example (mosquitto connectivity) didn't work for me. I think it has something to do with the mqtt library (MQTT.js) itself. For example, Python example from the IoT StarterKit [1] works perfectly fine sending messages to the SAP Cloud Platform Internet of Things. It uses Paho MQTT library.

Regards, Anton

[1] https://github.com/SAP/iot-starterkit/tree/master/src/examples/python/mqtt-over-wss

0

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

0