MQTT错误的回调/订阅上的Arduino [英] mqtt error void callback/subscribed on arduino

查看:2541
本文介绍了MQTT错误的回调/订阅上的Arduino的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和我的Arduino和MQTT云测试。
对于发布一切顺利,Arduino的出版的Hello World

但与无效的回调函数没有任何反应。
随着我MQTT.fx的客户,我订阅的主题身份和突击队。
在身份我看到Arduino是活。

当我与我的客户MQTT.fx的主题突击队发布。
我可以看到它抵我的客户,而不是在Arduino的的连续监测。

为什么不使用的空白回调函数?

\r
\r

的#include<&spi.h中GT;\r
#包括LT&;&PubSubClient.h GT;\r
#包括LT&;&Ethernet.h GT;\r
\r
#定义服务器m20.cloudmqtt.com\r
INT端口= 13365;\r
\r
//使用适合您的网络的值更新这些。\r
字节的MAC [] = {写0xDE,0xED,0xBA,0xFE时,0xFE时,0xED};\r
字节的IP [] = {192,168,0,120};\r
\r
无符号长的时间;\r
烧焦message_buff [100];\r
\r
EthernetClient ethClient;\r
PubSubClient客户端(服务器,端口回调,ethClient);\r
\r
无效设置()\r
{\r
  //初始化串行链路进行调试\r
  Serial.begin(115200);\r
  \r
  Ethernet.begin(MAC,IP);\r
  如果(client.connect(Arduino的MQTT,测试,测试)){\r
    client.publish(/ Arduino的/状态/,世界你好);\r
    client.subscribe(/ Arduino的/突击队/);\r
    Serial.println(已连接);\r
  }\r
  \r
  如果(Ethernet.begin(MAC)== 0)\r
  {\r
      Serial.println(无法使用DHCP来配置以太网);\r
      返回;\r
  }\r
}\r
\r
无效循环()\r
{\r
  // MQTT客户循环处理\r
  client.loop();\r
}\r
\r
\r
无效回调(字符*话题,BYTE *负载,无符号整型长度){\r
 \r
  如果(STRCMP(专题,​​/ Arduino的/突击队/)== 0){\r
    弦乐味精=的toString(有效载荷长度);\r
    Serial.println(MSG);\r
  }其他{\r
    Serial.println(Arduino的话题没有找到);\r
  }\r
}\r
\r
//\r
//功能的toString\r
//\r
字符串的ToString(BYTE *负载,无符号整型长度){\r
  INT I = 0;\r
  焦炭的buff [长度+ 1];\r
  对于(i = 0; I<长度;我++){\r
    BUFF [I] =负载[I]\r
  }\r
  BUFF [I] ='\\ 0';\r
  弦乐味精=字符串(BUFF);\r
  返回味精;\r
}

\r

\r
\r


解决方案

我刚才测试了code。与RSMB经纪人和它的作品。我没有DHCP我的电脑上,所以我不得不注释掉DHCP处理code - Ethernet.begin(MAC)。我想这就是你的错误是。这是因为:


  1. 您指定静态IP到以太网

  2. 连接到MQTT经纪人,并订阅主题

  3. 查询DHCP新的IP。也许在这一点上你的Arduino得到一个不同的IP比静态配置和经纪人不能达到你的Arduino更多发布订阅话题。

解决您的以太网处理code。我喜欢这样的公式:

  //启动以太网连接:
Serial.println(F(查询DHCP));
如果(Ethernet.begin(MAC)== 0){
    Serial.println(F(DHCP失败,退回到静态IP));
    //当DHCP故障时,后备静态配置;
    Ethernet.begin(MAC,IP);
}
printIp();

和printIp功能:

 无效printIp(){
  //打印本地IP地址
  Serial.print(F(我的IP));
  为(字节thisByte = 0; thisByte 4;; thisByte ++){
    //打印的IP地址的每个字节的值:
    Serial.print(Ethernet.localIP()[thisByte] DEC);
    Serial.print('。');
  }
}

I'm testing with my arduino and MQTT cloud. For the publish everything goes well, the arduino publishes "hello world"

But with the void callback function nothing happens. With my MQTT.fx client, I'm subscribed to the topics "status" and "commando". At the "status" I see that the arduino is a live.

When I publish with my MQTT.fx client to the topic "commando". I can see it arrived in my client, but not in the serial monitor of the arduino.

Why is the void callback function not used?

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

#define server "m20.cloudmqtt.com"
int port = 13365;

// Update these with values suitable for your network.
byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
byte ip[]     = { 192, 168, 0, 120 };

unsigned long time;
char message_buff[100];

EthernetClient ethClient;
PubSubClient client(server, port, callback, ethClient);

void setup()
{ 
  // init serial link for debugging
  Serial.begin(115200);
  
  Ethernet.begin(mac, ip);
  if (client.connect("arduino-MQTT","test","test")) {
    client.publish("/arduino/status/","hello world");
    client.subscribe("/arduino/commando/");
    Serial.println("Connected");
  }
  
  if (Ethernet.begin(mac) == 0)
  {
      Serial.println("Failed to configure Ethernet using DHCP");
      return;
  }
}

void loop()
{
  // MQTT client loop processing
  client.loop();
}


void callback(char* topic, byte* payload, unsigned int length) {
 
  if (strcmp(topic, "/arduino/commando/") == 0) {
    String msg = toString(payload, length);
    Serial.println(msg);
  }else{
    Serial.println("arduino topic not found");
  }  
}

//
// toString function
//
String toString(byte* payload, unsigned int length) {
  int i = 0;
  char buff[length + 1];
  for (i = 0; i < length; i++) {
    buff[i] = payload[i];
  }
  buff[i] = '\0';
  String msg = String(buff);
  return msg;
}

解决方案

I have just tested your code with RSMB broker and it works. I do not have DHCP on my computer so I had to comment out DHCP handling code - Ethernet.begin(mac). I think that's where your bug is. Because:

  1. You assign static IP to your Ethernet
  2. Connect to mqtt broker, and subscribe to a topic
  3. Query DHCP for a new IP. Probably at this point your Arduino gets a different IP than statically configured and the broker cannot reach your Arduino any more to publish subscribed topic.

Fix your Ethernet handling code. I like this formula:

// Start the Ethernet connection:
Serial.println(F("Querying DHCP"));
if ( Ethernet.begin( mac ) == 0 ) {
    Serial.println(F("DHCP failed, fallback to static IP"));
    // When DHCP fails, fallback to static configuration ;
    Ethernet.begin( mac, ip ) ;
}
printIp() ;  

And printIp function:

void printIp() {
  // Print local IP address
  Serial.print(F("My IP "));
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print('.');
  } 
}

这篇关于MQTT错误的回调/订阅上的Arduino的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆