我无法在 IBM Watson IOT 中订阅 ESP8266-ESP32

I can not subscribe with ESP8266-ESP32 in IBM Watson IOT

我想在 IBM Watson IOT 上使用 ESP8266 订阅 "iot-2/evt/status/fmt/json" 主题。连接已建立,但再次断开连接。因此,将 MQTT 客户端重新连接到……并订阅 iot-2 / cmd / + / fmt / + OK。这个循环继续。为什么连接断开?

我的ESP8266代码如下

我使用了 ESP8266-12E NodeMCU,我为发布者创建了一个 Android 应用程序。

/*
 Basic ESP8266 MQTT example

 This sketch demonstrates the capabilities of the pubsub library in combination
 with the ESP8266 board/library.

 It connects to an MQTT server then:
  - publishes "hello world" to the topic "outTopic" every two seconds
  - subscribes to the topic "inTopic", printing out any messages
    it receives. NB - it assumes the received payloads are strings not binary
  - If the first character of the topic "inTopic" is an 1, switch ON the ESP Led,
    else switch it off

 It will reconnect to the server if the connection is lost using a blocking
 reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
 achieve the same result without blocking the main loop.

 To install the ESP8266 board, (using Arduino 1.6.4+):
  - Add the following 3rd party board manager under "File -> Preferences -> Additional Boards Manager URLs":
       http://arduino.esp8266.com/stable/package_esp8266com_index.json
  - Open the "Tools -> Board -> Board Manager" and click install for the ESP8266"
  - Select your ESP8266 in "Tools -> Board"

*/

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
//#include <ESP8266HTTPClient.h>

#include <SPI.h>
#include <ArduinoJson.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.
//ZYxel
#define ssid  "......."
#define password  ".................."

//GES ARGE
#define ssid2     "..............."      // WiFi SSID
#define password2  "............."  // WiFi password

#define spi_ss_pin SS

#define ORG "............"
#define DEVICE_TYPE "........."
#define DEVICE_ID "..........."
#define TOKEN "................"
//-------- Customise the above values --------
char server[] = ORG ".messaging.internetofthings.ibmcloud.com";
int mqttPort=1883;
const char topic[] = "iot-2/cmd/status/fmt/json"; //"iot-2/cmd/status/fmt/json";

char authMethod[] = "use-token-auth";
char token[] = TOKEN;
char clientId[] = "d:" ORG ":" DEVICE_TYPE ":" DEVICE_ID;

WiFiClient wifiClient;
void callback(char* topic, byte* payload, unsigned int payloadLength) ;
PubSubClient client(server, 1883, callback, wifiClient);

void setup() {
  Serial.begin(115200);
  Serial.println();
  wifiConnect();
  mqttConnect();
}

void loop() {
  if (!client.loop()) {
    mqttConnect();
  }
}

void wifiConnect() {
  Serial.print("Connecting to "); Serial.print(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.print("nWiFi connected, IP address: "); Serial.println(WiFi.localIP());
}

void mqttConnect() {
  if (!client.connected()) {
    Serial.print("Reconnecting MQTT client to "); Serial.println(server);
    while (!client.connect(clientId, authMethod, token)) {
      Serial.print(".");
      delay(500);
    }
    initManagedDevice();
    Serial.println();
  }
}

void initManagedDevice() {
  if (client.subscribe(topic)) {
    Serial.println("subscribe to cmd OK");
  } else {
    Serial.println("subscribe to cmd FAILED");
  }
}

void callback(char* topic, byte* payload, unsigned int payloadLength) {
  Serial.print("callback invoked for topic: "); Serial.println(topic);

  for (int i = 0; i < payloadLength; i++) {
    Serial.print((char)payload[i]);
  }
}

设备("use-token-auth" 身份验证类型)无法订阅类似 "iot-2/evt/status/fmt/json" 的主题,只允许 "iot-2/cmd/status/fmt/json"。

您需要做的是生成 API 密钥和令牌并作为应用程序进行身份验证:

以下示例显示了一个典型的 API 密钥:

a-orgId-a84ps90Ajs

以下示例显示了典型的身份验证令牌:

MP$08VKz!8rXwnR-Q*

当您使用 API 密钥建立 MQTT 连接时,请确保应用以下准则:

The MQTT client ID is in the format: a:orgId:appId
The MQTT user name is the API key (for example, a-orgId-a84ps90Ajs)
The MQTT password is the authentication token (for example, MPVKz!8rXwnR-Q*)

之后您可以订阅 iot-2/type/device_type/id/device_id/evt/event_id/fmt/format_string 之类的主题。所以应该是:

iot-2/type/yourDeviceType/id/yourDeviceId/evt/status/fmt/json

您可以对命令使用相同的命令:主题应该是这样的 iot-2/type/device_type/id/device_id/cmd/command_id/fmt/format_string