mqtt 错误无效回调/在 arduino 上订阅 [英] mqtt error void callback/subscribed on arduino

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

问题描述

我正在使用我的 arduino 和 MQTT 云进行测试.为了发布一切顺利,arduino 发布了hello world"

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

但是使用 void 回调函数没有任何反应.使用我的 MQTT.fx 客户端,我订阅了主题状态"和突击队".在状态"中,我看到 arduino 是实时的.

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.

当我使用我的 MQTT.fx 客户端发布主题突击队"时.我可以看到它到达了我的客户端,但没有出现在 arduino 的串行监视器中.

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.

为什么不用void回调函数?

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;
}

推荐答案

我刚刚使用 RSMB 代理测试了您的代码,并且可以正常工作.我的计算机上没有 DHCP,所以我不得不注释掉 DHCP 处理代码 - Ethernet.begin(mac).我认为这就是你的错误所在.因为:

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. 您为以太网分配静态 IP
  2. 连接到 mqtt 代理,并订阅主题
  3. 查询 DHCP 以获取新 IP.可能此时您的 Arduino 获得了与静态配置不同的 IP,并且代理无法再访问您的 Arduino 以发布订阅的主题.

修复您的以太网处理代码.我喜欢这个公式:

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() ;  

和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天全站免登陆