我已经在我自己的主机上安装了freeboard,但我无法将其连接到PubNub。我曾尝试使用mqtt插件通过MQTT连接到pubnub,但不起作用。如何将PubNub连接到freeboard?我自己的freeboard实例,而不是freeboard.io托管的仪表板
发布于 2018-04-03 02:01:32
我可以给你一些建议。虽然使用PubNub supports MQTT,但不需要使用协议。根据您使用的IoT设备,您可以将标准PubNub连接与您的设备支持的语言的任何SDK一起使用。
如果您确实想使用MQTT,您可以使用Python和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 }))这段代码的作用是将JSON数据发布到MQTT主题( PubNub行话中的通道)。
您可以发布适合您的仪表板的数据,而不是"hi = 10“。我坚持要包含一个Unix时间戳,这样您就可以知道数据是什么时候提交的。
您也可以使用PubNub standard publish with Python或任何其他有开发工具包(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()现在,此消息已发布,您可以在web浏览器中打开的仪表板中实时接收此消息。如果发布消息时仪表板未打开,则稍后可以使用PubNub storage and playback检索它。

您可以在PubNub Admin Dashboard的key info选项卡下启用邮件保留。

这是订阅的JavaScript code在带有仪表板的网页中的样子。
<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
});
}
);
https://stackoverflow.com/questions/49603464
复制相似问题