当前位置: 首页 > 知识库问答 >
问题:

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

朱欣荣
2023-03-14

我使用了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]);
  }
}

共有1个答案

桑坚
2023-03-14

设备(“use-token-auth”身份验证类型)不能订阅像“iot-2/evt/status/fmt/json”这样的主题,只允许“iot-2/cmd/status/fmt/json”。

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

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

当您使用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

 类似资料:
  • 我正在尝试升级贝宝订阅使用Rest API的计划。 https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_remission

  • 当我更新到RxJS 6时,我的应用程序坏了。我得到了它的大部分工作,但这一个方法难倒了我。 之前,我们有一组被平面映射的观测值,然后使用CombineTest,如下所示: 我可以订阅新观察,并从所有其他网站获得一系列最新的输出。 现在我试着做这样的事情: 这给了我一个很长很复杂的 类型为“(res:Observable)的参数 坦率地说,我的阅读障碍妨碍了我进行语法分析。 如果我只返回上面的res

  • 在ngOnDestory中,我取消了两个订阅,但仍然得到前面的错误。 现在我几乎可以肯定问题出在这行:即使我在注销之前取消了proposalSubscription和chatSubscription的订阅,但仍然会出现错误。有没有解决这个问题的方法?而且,我对RXJ和操作符没有太多的经验。有没有操作符可以用来避免这种嵌套订阅? 提前道谢。

  • 我写了一个逻辑,使用Spring反应器库来获取所有运算符,然后在异步模式下为每个运算符(分页)获取所有设备。 创建了一个通量来获取所有运算符,然后订阅它。 现在,对于每个运算符,我正在获取需要多个订阅才能获得设备mono的设备,该设备通过订阅MONO获得所有页面异步。 此代码工作正常。但我的问题是,从内部订阅mono是一个好主意吗?

  •  说明 调用方法: $.f2eAct.yyue(el,options); 函数说明: Y阅功能,包含查询用户是否订阅,用户订阅,用户取消订阅 参数说明: 参数名 类型 说明 备注 el string DOM元素对象 必要 pcode int 订阅媒体的id 必要 gcode int 媒体用户分组id 必要 success function 查询是否订阅回调方法 无 confirm functio

  • 嗨,我不能收到确认订阅请求从亚马逊,尝试了一切。我的endpointurl是:。我尝试了以下代码片段来接收响应:1)-> 2)-> 请帮助我如何接收来自amazon的响应,也请指出我是否在代码中做错了什么或者我遗漏了任何一步。多谢了。