on 09-13-2016 3:51 AM
Hi,
I am trying to use MQTT Websockets (now available in HCP IoT Service) to push data from my device to HCP IoT Services. I am able to test this using the MQTT Websockets Test client. I am after a sample code (nodejs/python) which can show me how to publish a message to the device which is registered in IoT services. I cant get my code to work.
//Program testmqtt.js
var mqtt | = require('mqtt'); |
var host = 'iotmmsc59c97a90.ap1.hana.ondemand.com';
var device = '0ccc712a-eac4-42ba-af2e-02528938a3ae';
var oAuthToken = 'e862e126d2f28ab0cff167224aecf83';
var messageType = '1a96769b7fda1b7c5433';
var path = '/com.sap.iotservices.mms/v1/api/ws/mqtt';
var options = {
extraHeaders: {
'Authorization': 'Bearer ' + oAuthToken, | |
'Content-Type': 'application/json;charset=utf-8' |
}
};
var client = mqtt.connect('wss://' + host + path, options);
client.on('connect', function () {
console.log("about to connect")
var jsonData = {
"mode": "async", | |
"messageType": messageType, | |
"messages": [{"temp":99,"light":85}] |
};
var strData = JSON.stringify(jsonData);
client.publish('iot/data/0ccc712a-eac4-42ba-af2e-02528938a3ae', strData);
});
client.on('message', function (topic, message) {
// message is Buffer
console.log(message.toString());
client.end();
});
Thanks,
Murali.
Hi Murali,
from a description I didn't get what exactly issue are you facing with. Did you also try the Python (MQTT Paho lib) example from StarterKit [1] ? Maybe that one will clarify it.
Regards,
Anton
[1] iot-starterkit/src/examples/python/mqtt-over-wss at master · SAP/iot-starterkit · GitHub
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ok, Here is sample code snippet in nodejs which I obtained from a colleague. This works perfect.
// ========================================================================
// CONFIGURATION
// ========================================================================
var my_account_id='c59c97a90';
var my_device_id='0ccc712a-eac4-42ba-af2e-02528938a3ae';
var my_oauth_token='e862e126d2f28ab0cff167224aecf83';
// ========================================================================
var endpoint = 'wss://iotmms'+my_account_id+'.ap1.hana.ondemand.com:443/com.sap.iotservices.mms/v1/api/ws/mqtt';
var options = {
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);
// ========================================================================
var mqtt = require('mqtt');
var client = mqtt.connect(endpoint, options);
client.on('connect', function () {
console.log('connected!');
var data = {"mode":"async","messageType":"1a96769b7fda1b7c5433","messages":[{"temp":11,"light":88}]};
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();
});
User | Count |
---|---|
87 | |
10 | |
10 | |
9 | |
7 | |
6 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.