我不能在IBM Watson IOT中订阅ESP8266-ESP32

问题描述 投票:0回答:1

我想在IBM Watson IOT上订阅ESP8266的“iot-2 / evt / status / fmt / json”主题。建立连接但它再次断开连接。因此,将MQTT客户端重新连接到...并订阅iot-2 / cmd / + / fmt / +确定。这个循环继续。为什么连接断开了?

我的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]);
  }
}
mqtt publish-subscribe esp8266 watson-iot
1个回答
0
投票

设备(“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, MP$08VKz!8rXwnR-Q*)

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

IOT-2 /类型/ yourDeviceType / ID / yourDeviceId / EVT /状态/ FMT / JSON

你可以对命令使用相同的东西:这个主题看起来应该像iot-2 / type / device_type / id / device_id / cmd / command_id / fmt / format_string

© www.soinside.com 2019 - 2024. All rights reserved.