【问题标题】:I can not subscribe with ESP8266-ESP32 in IBM Watson IOT我无法在 IBM Watson IOT 中订阅 ESP8266-ESP32
【发布时间】:2017-11-29 09:12:40
【问题描述】:

我想在 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]);
  }
}

【问题讨论】:

    标签: mqtt publish-subscribe esp8266 watson-iot


    【解决方案1】:

    设备(“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/type/yourDeviceType/id/yourDeviceId/evt/status/fmt/json

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

    【讨论】:

    • 嗨@idan。谢谢你的回答。我不通过说“iot-2/evt/status/fmt/json”来订阅该主题。我通过说“iot-2/evt/status/fmt/json”通过android发布它。如果您检查上述 esp8266 代码,我会通过说“iot-2/cmd/status/fmt/json”来订阅该主题。我已经在上面应用了您的建议,但结果仍然是否定的。在这个例子中,“developer.ibm.com/recipes/tutorials/…”没有按照你的建议完成。
    • 在一开始的帖子中,您说:“我想在 ibm watson IOT 上使用 esp8266 订阅“iot-2/evt/status/fmt/json”主题。”你能连接吗?如果没有,我注意到您想通过 1883 进行连接。如果您想这样做,您需要在 IoT Platform 的安全设置下设置 TLS Optional。如果您需要在“iot-2/cmd/status/fmt/json”上获取事件,则需要在该主题上发布(例如作为应用程序)在“iot-2/evt/status/fmt/json”上发布事件并不意味着您会将它们放入“iot-2/cmd/status/fmt/json”。它们是两种不同的东西。
    • 我在 IOT 平台安全设置下将 TLS 设置为可选,但我无法连接。基本上我想从我的 android 应用程序发布。并从 ESP8266 模块订阅。
    • 感谢@idan 的回答。我找到了问题的解决方案。我的错误,发生错误是因为我对两个应用程序使用了相同的 api_key。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-26
    • 2019-06-18
    • 1970-01-01
    • 1970-01-01
    • 2022-06-21
    • 1970-01-01
    相关资源
    最近更新 更多