ESP8266未连接到MQTT代理hivemq

时间:2017-02-21 12:15:02

标签: mqtt esp8266 arduino-esp8266 hivemq

我有一个简单的代码,我试图连接到HiveMQ开放代理并订阅一个主题来收听传入的消息。

这是代码

        #include <ESP8266WiFi.h>
        #include <PubSubClient.h>

        const char *ssid =  "P9Inct";       // cannot be longer than 32 characters!
        const char *pass =  "P9inct123*";       //
        const char *mqtt_server = "broker.hivemq.com";
        const int mqtt_port = 1883;
        const char *mqtt_user = "testUser";
        const char *mqtt_pass = "abc123";
        const char *mqtt_client_name = "12312312332212";
        #define BUFFER_SIZE 100

        String incoming="";
        String did="";
        String state="";
        // Update these with values suitable for your network.
        //IPAddress server(172, 16, 0, 2);
        String DEVID="8581870006";//"6931108641";//old

        WiFiClient wclient;
        PubSubClient client(wclient, mqtt_server,mqtt_port);

        void callback(const MQTT::Publish& pub) {
          // handle message arrived
          Serial.print(pub.topic());
          Serial.print(" => ");
          if (pub.has_stream()) {
            uint8_t buf[BUFFER_SIZE];
            int read;
            while (read = pub.payload_stream()->read(buf, BUFFER_SIZE)) {
              Serial.write(buf, read);
            }
            pub.payload_stream()->stop();
            Serial.println("");
          } else
            Serial.println(pub.payload_string());

        ////////////////////////////////////////////

        incoming=String(pub.payload_string());
        Serial.println(pub.payload_string());
        Serial.println(incoming);

          }




        void setup() {
          // Setup console
          Serial.begin(115200);
          delay(10);
          Serial.println();
          Serial.println();
        }

        void loop() {
          if (WiFi.status() != WL_CONNECTED) {
            Serial.print("Connecting to ");
            Serial.print(ssid);
            Serial.println("...");
            WiFi.begin(ssid, pass);

            if (WiFi.waitForConnectResult() != WL_CONNECTED)
              return;
            Serial.println("WiFi connected");
          }

          if (WiFi.status() == WL_CONNECTED) {
            if (!client.connected()) {
              Serial.println("Connecting to MQTT server");
              if (client.connect(MQTT::Connect(mqtt_client_name)
                     .set_auth(mqtt_user, mqtt_pass))) {
                Serial.println("Connected to MQTT server");
            client.set_callback(callback);


            client.subscribe("diy/1"+DEVID);
              } else {
                Serial.println("Could not connect to MQTT server");   
              }
            }

            if (client.connected())
              client.loop();
          }

        }

WiFi连接工作正常但是,通过代理的通信无法正常工作,并且始终显示消息“无法连接到MQTT服务器”。如何使esp8266与HiveMQ代理一起使用。 borker的仪表板是 http://www.mqtt-dashboard.com/

1 个答案:

答案 0 :(得分:2)

所以,你已经联系了。循环中有不必要的打印行。试着适应这个:

/* Incoming data callback. */
void callback(char* topic, byte* payload, unsigned int length)
{
  char payloadStr[length + 1];
  memset(payloadStr, 0, length + 1);
  strncpy(payloadStr, (char*)payload, length);
  Serial.printf("Topic : [%s]\n", topic);
  Serial.printf("Payload : %s\n", payloadStr);
}

void performConnect()
{
  uint16_t connectionDelay = 5000;
  while (!client.connected())
  {
    if (client.connect(MQTT_CLIENT_ID, MQTT_USERNAME, MQTT_KEY))
    {
      Serial.printf("Connected to Broker.\n");
      client.subscribe("diy/1"+DEVID);
    }
    else
    {
      Serial.printf("MQTT Connect failed, rc = %d\n", client.state());
      Serial.printf("Trying again in %d msec.\n", connectionDelay);
      delay(connectionDelay);
    }
  }
}

void loop()
{
  if (!client.connected())
  {
    performConnect();
  }
  client.loop();
}