I have installed freeboard on my own hosting and I'm unable to connect it to PubNub. I have tried using the mqtt plug-in to connect to pubnub through MQTT but it didn't work.. How can I connect PubNub to freeboard ? My own instance of freeboard not a dashboard hosted by freeboard.io
-
I was going to post an answer but the one posted below is much nicer +1 – Stephen Blum Apr 02 '18 at 20:18
1 Answers
I can give you some pointers. While PubNub supports MQTT it is not necessary to use the protocol. Depending on the IoT device you are using, you can use the standard PubNub connection with any of the SDKs for a language your device supports.
If you do want to use MQTT, you can use Python and Paho:
import paho.mqtt.client as mqtt
publish_key = "<your publish key>"
subscribe_key = "<your subscribe key>"
client_id = "<your unique client identifier>"
client = mqtt.Client(client_id=publish_key + "/" + subscribe_key + "/" + client_id)
client.connect("mqtt.pndsn.com", 1883, 60)
client.publish("<topic to publish>", json.dumps({ "hi": 10 }))
What this code does is it publishes JSON data to an MQTT topic (channel in PubNub lingo).
Instead of "hi = 10" you can publish data that is suitable for your dashboard. I insist on including a Unix timestamp so you know when the data was submitted.
You can also use the PubNub standard publish with Python or any other language that there is an SDK for (there's more than 70 SDKs).
import time
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
pnconfig = PNConfiguration()
pnconfig.publish_key = '<your publish key>'
pubnub = PubNub(pnconfig)
## makes a timetoken that is easily converted to
## a JavaScript date object in a web browser
javascript_timetoken = int(time.time() * 1000)
pubnub.publish().channel("my_channel").message({
'tt': javascript_timetoken,
'foo': 'bar'
}).sync()
Now that this message has been published it can be received in realtime in your dashboard that is open in a web browser. If the dashboard is not open at the time a message is published, it can be retrieved later using PubNub storage and playback.
You can enable message retention in the PubNub Admin Dashboard under the key info tab.
This is what the JavaScript code for subscribe would look like in the web page with the dashboard.
<script src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.20.2.js"></script>
<script>
var pubnub = new PubNub({
subscribeKey: "mySubscribeKey",
ssl: true
});
var time;
pubnub.addListener({
message: function(message) {
time = new Date(message.tt);
// do more stuff
// write data to the dashboard
},
});
pubnub.subscribe({
channels: ['my_channel']
});
// Get messages from the channel history in case there were
// updates that happened while the browser was not yet opened
pubnub.history(
{
channel: 'my_channel',
reverse: true, // Setting to true will traverse the time line in reverse starting with the oldest message first.
count: 100, // how many items to fetch
stringifiedTimeToken: true, // false is the default
start: '123123123123', // start time token to fetch
end: '123123123133' // end timetoken to fetch
},
function (status, response) {
response.messages.forEach(function (message) {
time = new Date(message.tt);
// do more stuff
// write data to the dashboard
});
}
);

- 659
- 4
- 13