0

「MQTT_interface」というクラスを書いています。このクラスのメソッド「再接続」で「コールバック」関数を使用する必要があります。開発者は自分自身のために関数「コールバック」を書くと思います。そうでない場合は、コンソールにエラーを送信する同じ名前(「コールバック」)の弱い関数のようなものがあるでしょう。

それが可能かどうかはわかりません。

私はこれらのファイルを持っています:

MQTT_interface.h

class MQTT_interface : public PubSubClient
{
    public:
    MQTT_interface(Client& c, String hostname, uint16_t port = 1883);
    void reconnect(void);

    private:
    uint8_t buf[UART_MAX_TRANSFER_SIZE];
};

MQTT_interface.cpp

void MQTT_interface::reconnect() {

    while (!connected()) {
  set_server(MQTT_auth_data.mqtt0_server, MQTT_auth_data.mqtt_port);
    if (connect(MQTT::Connect(MQTT_auth_data.mqtt_uid)
                           .set_auth(MQTT_auth_data.mqtt_uid, MQTT_auth_data.mqtt_pass))) {
      Serial.println("Connected to MQTT server");
      set_callback(callback);   //here my function must be used
        subscribe(topics.mqtt_subscribe_topic); // this is our receive filter. We able to receive only these topics
        subscribe(topics.mqtt_topic_1);

      } else {
        delay(timeout);
    }
  }
}

main.cpp

// Callback function
//  Receive data from MQTT and send it to serial
uint8_t buf[UART_MAX_TRANSFER_SIZE];
void callback(const MQTT::Publish& pub) {
// Copy the payload to a new message
  //MQTT::Publish newpub("outTopic", pub.payload(), pub.payload_len());
  //client.publish(newpub);
  String start_symbol = "";
  char end_symbol = '\n';
  String div_symbol = "&";
  //Serial.println(pub.topic());
  if (pub.has_stream()) {
    int read;
    while (read = pub.payload_stream()->read(buf, UART_MAX_TRANSFER_SIZE)) {
      Serial.write(buf, read);    
    }
    pub.payload_stream()->stop();
  }

  else{
    char symbol = 1;

    String t = pub.topic();
    String m = pub.payload_string();
    String message = start_symbol + t + div_symbol + m + end_symbol;
    int str_len ;
      str_len = message.length();
      Serial.print(message);
  }
}
4

1 に答える 1