我尝试设置一个Arduino,它将一些消息发布到NodeJaS服务器,但我无法从中获得发布者名称。我搜索了文档,没有发现任何真正有用的东西。我在PubNub以PubNub.set_uuid(uuid);开头之前设置了一个UUID,但没有效果。应用程序只返回一个未定义的。我该如何设置它?
#include <ESP8266WiFi.h>
#define PubNub_BASE_CLIENT WiFiClient
#include <PubNub.h>
// Replace these with your WiFi network settings
const char* ssid = "SSID"; //replace this with your WiFi network name
const char* password = "PW"; //replace this with your WiFi network password
const static char pubkey[] = "KEY"; // your publish key
const static char subkey[] = "KEY"; // your subscribe key
const static char channel[] = "test"; // channel to use
const static char uuid[] = "temp-sens"; // Unique Device UUID
void setup()
{
delay(1000);
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println();
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
PubNub.set_uuid(uuid);
PubNub.begin(pubkey, subkey);
Serial.println("PubNub set up");
Serial.println("success!");
Serial.print("IP Address is: ");
Serial.println(WiFi.localIP());
}
void loop() {
WiFiClient *client;
char msg[] = "\"Yo!\"";
client = PubNub.publish(channel, msg);
if (!client) {
Serial.println("publishing error");
delay(1000);
return;
}
if (PubNub.get_last_http_status_code_class() != PubNub::http_scc_success) {
Serial.print("Got HTTP status code error from PubNub, class: ");
Serial.print(PubNub.get_last_http_status_code_class(), DEC);
}
while (client->available()) {
Serial.write(client->read());
}
client->stop();
Serial.println("---");
}发布于 2018-03-21 02:09:35
收到消息中的PubNub发布者UUID
我相信您的要求是在订阅者接收的消息中接收发布者的UUID。这只有在最新的4.x版本的PubNub软件开发工具包中才有可能,因为它们实现了一个新的发布/订阅应用程序接口,该接口将发布者的UUID作为额外的data in the delivered message提供,而不是在实际的消息内容中。
例如,如果要使用PubNub JavaScript SDK,则可以从publisher事件中获取message键的值。
下面是一个示例message event JSON有效负载(不要将message event中的message密钥与message event混淆):
{
"channel": "ch1",
"subscription": null,
"actualChannel": null,
"subscribedChannel": "ch1",
"timetoken": "15215690220119777",
"publisher": "cv1",
"message": {
"msg": "hello"
}
}该timetoken是在4.x PubNub SDK中首次亮相的实际发布时间表。
Arduino SDK是一个较旧的SDK,没有实现包含此信息的最新发布/订阅API。作为一种解决办法,您可以在发布的消息内容中包含发布者的UUID。
https://stackoverflow.com/questions/48912657
复制相似问题