首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >连接PubNub到freeboard实例

连接PubNub到freeboard实例
EN

Stack Overflow用户
提问于 2018-04-02 07:10:18
回答 1查看 153关注 0票数 1

我已经在我自己的主机上安装了freeboard,但我无法将其连接到PubNub。我曾尝试使用mqtt插件通过MQTT连接到pubnub,但不起作用。如何将PubNub连接到freeboard?我自己的freeboard实例,而不是freeboard.io托管的仪表板

EN

回答 1

Stack Overflow用户

发布于 2018-04-03 02:01:32

我可以给你一些建议。虽然使用PubNub supports MQTT,但不需要使用协议。根据您使用的IoT设备,您可以将标准PubNub连接与您的设备支持的语言的任何SDK一起使用。

如果您确实想使用MQTT,您可以使用Python和Paho:

代码语言:javascript
复制
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)的语言。

代码语言:javascript
复制
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在带有仪表板的网页中的样子。

代码语言:javascript
复制
<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
        });
    }
);

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49603464

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档