0
votes

I followed this tutorial :

Running the CC26xx Contiki Examples

but instead of using the cc26xx-demo I used the cc26xx-web-demo and successfully manged to get everything up and running and I can access the 6lbr web page, when I access the sensorTag page I see a mqtt configuration page as shown: sensorTag page

and if I click index in the sensorTag page (pic above) I get to see the data:

enter image description here

the question is , how can I write a simple nodejs js file that uses the mqtt broker information to grab all the sensorTag sensors data and save it in an local object.

I tried to do run this example but no luck

var mqtt = require('mqtt') 
client = mqtt.createClient(1883, '192.168.1.109'); 
client.subscribe(what do I write here); 
client.on('message', function(topic, message) { console.log(message); });

I don't know what I'm doing wrong

UPDATE:

mqtt configuration page :enter image description here

javascript file : enter image description here

and I run the js with node and listen on port 1883: enter image description here

tcpdump seems to detect mqqt packets on 1883 port but I can't to seem to be able to console.log the sensor data when I run the js file with node ??

I went on the contiki wiki and came across this info "You can also subscribe to topics and receive commands, but this will only work if you use "Org ID" != 'quickstart'. Thus, if you provide a different Org ID (do not forget the auth token!), the device will subscribe to:

iot-2/cmd/+/fmt/json"

does this mean that the topic to subscribe to is quickstart but even if that's so, I used '+/#' which subrcibes to all topics and still got nothing printing on the console ?

1
There are plenty of NodeJS MQTT examples out there, what have you already tried?hardillb
Hi sorry for the late replay: I tried to do a simple example var mqtt = require('mqtt') client = mqtt.createClient(port, host); client.subscribe(what do I write here); client.on('message', function(topic, message) { console.log(message); }); but I'm a but confused as what to subscribe toLearningDev
Update the question with what you tried and why it doesn't workhardillb
will do that :DLearningDev
updated the question, tried with the code I posted above, still no luckLearningDev

1 Answers

0
votes

Hope this works for you:

var mqtt = require('mqtt');
var fs = require('fs');

var options = { port: PORT, host: HOST };/*user, password and others authentication if there.*/
var client = mqtt.connect('HOST', options);
client.on('connect', function ()
{
    client.subscribe("topic, command or data");

    client.publish("topic, command or data", data, function () {
    });
});

client.on('error', function () { });
client.on('offline', function () { });
client.on('message', function (topic, message) {
    console.log(message.toString());
});