arduino - PubNub Arduino 订阅

标签 arduino pubnub

您好,我正在使用 https://github.com/pubnub/arduino 中的 PubNubsubscriber 示例 我能够接收消息,只要我收到消息,一切都运行正常,如果一段时间过去了,比如20秒没有新消息,arduino似乎卡住在“等待消息(订阅)” 并且无法接收新传入消息

谁知道为什么会发生这种情况?

  /*
  PubNub sample subscribe client

  This sample client will subscribe to and handle raw PubNub messages
  (not doing any JSON decoding).  It does so with a randomly generated
  UUID.

  Circuit:
  * Ethernet shield attached to pins 10, 11, 12, 13
  * (Optional.) LED on pin 8 for reception indication.
  * Pin A4 unconnected (noise source for random number generator)

  created 23 October 2012
  by Petr Baudis

  https://github.com/pubnub/pubnub-api/tree/master/arduino
  This code is in the public domain.
  */

  #include <SPI.h>
  #include <Ethernet.h>
  #include <PubNub.h>

// Some Ethernet shields have a MAC address printed on a sticker on the shield;
// fill in that address here, or choose your own at random:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

const int subLedPin = 8;

char pubkey[] = 
char subkey[] = 
char channel[] = "hello_world";
char uuid[] = "xxxxxxxx-xxxx-4444-9999-xxxxxxxxxxxx";

void random_uuid() {
    randomSeed(analogRead(4) + millis() * 1024);
    snprintf(uuid, sizeof(uuid), "%04lx%04lx-%04lx-4444-9999-%04lx%04lx%04lx",
        random(0x10000), random(0x10000), random(0x10000),
        random(0x10000), random(0x10000), random(0x10000));
}

void setup()
{
    pinMode(subLedPin, OUTPUT);
    digitalWrite(subLedPin, LOW);

    Serial.begin(9600);
    Serial.println("Serial set up");

    while (!Ethernet.begin(mac)) {
        Serial.println("Ethernet setup error");
        delay(1000);
    }
    Serial.println("Ethernet set up");

    PubNub.begin(pubkey, subkey);
    random_uuid();
    PubNub.set_uuid(uuid);
    Serial.println("PubNub set up");
}

void flash(int ledPin)
{
    /* Flash LED three times. */
    for (int i = 0; i < 3; i++) {
        digitalWrite(ledPin, HIGH);
        delay(100);
        digitalWrite(ledPin, LOW);
        delay(100);
    }
}

void loop()
{
    Ethernet.maintain();

    PubSubClient *client;

    Serial.println("waiting for a message (subscribe)");
    client = PubNub.subscribe(channel);
    if (!client) {
        Serial.println("subscription error");
        delay(1000);
        return;
    }
    Serial.print("Received: ");
    while (client->wait_for_data()) {
        char c = client->read();
        Serial.print(c);
    }
    client->stop();
    Serial.println();
    flash(subLedPin);

    delay(200);
   }

最佳答案

我花了一些时间,在尝试获取时间 token 时发现问题出在 void PubSubClient::stop() 函数中。现在我不太确定这里到底发生了什么以及为什么它被卡住,尽管看起来如果你终止这里的连接并跳过 timetoken 捕获,它就像一个魅力。

这是PubNub.cpp中的原始代码:

void PubSubClient::stop()
{
    if ((!available() && !connected()) || !json_enabled) {
        PubNub_BASE_CLIENT::stop();
        return;
    }
    /* We are still connected. Read the rest of the stream so that
     * we catch the timetoken. */
    while (wait_for_data()) {
        char ch = read();
        this->_state_input(ch, NULL, 0);
    }
    json_enabled = false;
}

我(暂时)替换为:

void PubSubClient::stop()
{
    PubNub_BASE_CLIENT::stop();
    return;
}

目前这是一个解决方法(没有时间 token ),我希望这会有所帮助,直到找到正确的解决方案。

关于arduino - PubNub Arduino 订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25432132/

相关文章:

macos - 如何在终端中设置 Mac 的波特率

javascript - 如何通过串口将数据从网站发送到处理器?

python - 如何使用python获取arduino端口

php - Pubnub 用于在 iOS 中聊天

c++ - Arduino - 在 sketch 中使用的 C 库中包含 SD.h

encryption - Arduino 上的 HTTPS 替代方案

ios - 即使应用程序被杀死,如何通过 PubNub 发送消息

javascript - 无法使用 pubnub 在 map 中设置信息窗口

javascript - 正确检索用户名和有用的值(网站标题、版权等)

javascript - 从 pubnub javascript v3 升级到 v4,在哪里以及如何添加监听器?