簡體   English   中英

MQTT 客戶端未訂閱給定主題(或回調未按預期工作)

[英]MQTT client not subscribing to a given topic (or callback not working as intended)

我要瘋了,試圖猜測為什么我的 Arduino Nano33 無法接收 mqtt 消息。 或者可能是它接收到它們,但回調 function 沒有按我的預期工作(因為回調是我必須查看是否收到消息的唯一方法)。

我可以毫無問題地發布消息。 關於我的代碼中可能有什么問題的任何線索? (我對 C++ 的經驗為零,所以這可能是一些愚蠢的錯誤)。

我正在復制整個代碼,以防問題出在其他地方,但我猜要查看的關鍵函數是callbacksetup

#include <WiFiNINA.h> 
#include <PubSubClient.h>
#include <Arduino_LSM6DS3.h>
#include "credentials.h"

const char* ssid = SSID_WIFI;
const char* password = PW_WIFI;
const char* mqttServer = MQTT_SERVER;
const int mqttPort = MQTT_PORT;
const char* mqttUsername = MQTT_USRNM;
const char* mqttPassword = MQTT_PW;
char pubTopic[] = "sensors/imu1/values";
char subTopic[] = "sensors/imu1/mode";

WiFiSSLClient wifiClient; //SSL
PubSubClient client(wifiClient);
void setup_wifi()

//Connect to wifi network 
{
  delay(10);  
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
    Serial.print(".");
  }
  randomSeed(micros());
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
  //char* payload_chr;
  Serial.print("Message arrived [");  
  Serial.print(topic);  
  Serial.print("] ");
  for (int i=0;i<length;i++) {
    //payload_chr[i] = (char)payload[i];
    Serial.print((char)payload[i]);
  }
  Serial.println();
  //String message = payload_chr;
  //Serial.print(message);
}

void reconnect() 
{
  // Loop until we're reconnected
  while (!client.connected()) 
  {
    Serial.print("Attempting MQTT connection...");
    String clientId = "Nano33-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str(), mqttUsername, mqttPassword)) 
    {
      Serial.println("connected");
      // ... and resubscribe
      client.subscribe(subTopic);
    } else 
    {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() 
{   
  Serial.begin(9600);
  setup_wifi();
  client.setServer(mqttServer, mqttPort);
  client.setCallback(callback);  
  if (!IMU.begin()) {
    Serial.println("Failed to initialize IMU!");
    while (1);
  }
  Serial.println("IMU initialized!");  
}

void publish_imu_values(float x_acc, float y_acc, float z_acc, 
                        float x_gyr, float y_gyr, float z_gyr){
  //Publish imu readings in InfluxDB Line Protocol format
  String temp_str;
  char mensaje[100];
  temp_str  = "imu x_acc=";
  temp_str += String(x_acc);
  temp_str += ",y_acc=";
  temp_str += String(y_acc);
  temp_str += ",z_acc=";
  temp_str += String(z_acc);
  temp_str += ",x_gyr=";
  temp_str += String(x_gyr);
  temp_str += ",y_gyr=";
  temp_str += String(y_gyr);
  temp_str += ",z_gyr=";
  temp_str += String(z_gyr);        
  temp_str.toCharArray(mensaje, temp_str.length() + 1);  
  client.publish(pubTopic, mensaje);      
}

void loop() 
{
  if (!client.connected()) 
  {
    reconnect();
  }
  float x_acc, y_acc, z_acc;
  float x_gyr, y_gyr, z_gyr;  
  if (IMU.accelerationAvailable() && IMU.gyroscopeAvailable()) {
    IMU.readAcceleration(x_acc, y_acc, z_acc);
    IMU.readGyroscope(x_gyr, y_gyr, z_gyr);
    publish_imu_values(x_acc, y_acc, z_acc, x_gyr, y_gyr, z_gyr);    
  }  
  delay(1000);
}

你需要一個client.loop(); loop() main function 中的行。 如果沒有該行,您的 MQTT 代碼將永遠不會被執行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM