无法将esp32 MQTT连接到azure IOT Hub

iovurdzv  于 2023-06-30  发布在  其他
关注(0)|答案(1)|浏览(133)
#include <WiFi.h>
#include <PubSubClient.h>
#include <WiFiClientSecure.h>

// Wi-Fi settings
const char* ssid = "T-HUB2.0";
const char* password = "Innovation";

// Azure IoT Hub settings
const char* mqttServer = "PASSDEMO1.azure-devices.net";
const int mqttPort = 8883;
const char* deviceId = "iotdevice01";
const char* connectionString = "SharedAccessSignature sr=PASSDEMO1.azure-devices.net%2Fdevices%2Fiotdevice01&sig=XXXXX&se=1687851087";

// TLS/SSL client setup
WiFiClientSecure wifiClient;
PubSubClient mqttClient(wifiClient);

// MQTT topics
const char* telemetryTopic = "devices/iotdevice01/messages/events/";

// MQTT callback function
void mqttCallback(char* topic, byte* payload, unsigned int length) {
  // Handle incoming MQTT messages here if needed
}

void setup() {
  Serial.begin(9600);

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  // Set MQTT server and port
  mqttClient.setServer(mqttServer, mqttPort);

  // Set MQTT callback function
  mqttClient.setCallback(mqttCallback);

  // Connect to MQTT broker
  connectToMqttBroker();
}

void loop() {
  if (!mqttClient.connected()) {
    // Reconnect to MQTT broker if the connection is lost
    connectToMqttBroker();
  }
  mqttClient.loop();

  // Send telemetry data to Azure IoT Hub
  float temperature = readTemperature();
  char telemetryMessage[100];
  snprintf(telemetryMessage, sizeof(telemetryMessage), "{\"temperature\": %.2f}", temperature);
  mqttClient.publish(telemetryTopic, telemetryMessage);

  delay(5000);
}

void connectToMqttBroker() {
  while (!mqttClient.connected()) {
    Serial.println("Connecting to MQTT broker...");

    if (mqttClient.connect(deviceId, connectionString, "")) {
      Serial.println("Connected to MQTT broker");
      //mqttClient.subscribe("devices/your-device-id/messages/devicebound/#");
    } else {
      Serial.print("Failed, rc=");
      Serial.print(mqttClient.state());
      Serial.println(" Retrying in 5 seconds...");
      delay(5000);
    }
  }
}

float readTemperature() {
  // Code to read temperature from sensor
  // Replace this with your own code
  return 25.0;
}

我尝试使用上面的代码连接到Azure IoT Hub。它给了我这个错误。

Failed, rc=-2 Retrying in 5 seconds...
Connecting to MQTT broker...

不只是IOT Hub,当我尝试使用MQTT发布/订阅时,IOT Central和Event Grid也会出现相同的错误。首先,它无法连接。有人能解决这个问题吗?esp 32不使用pubsubclient连接到azure服务,或者我应该以某种方式修改mqtt代理吗?

qojgxg4l

qojgxg4l1#

看起来你正在尝试使用PubSubClient示例连接到Azure IoT Hub。
这是您尝试与IoT Hub建立连接的线路。if (mqttClient.connect(deviceId, connectionString, ""))。我已经研究了PubSubClient文件的实现,并且在接受连接字符串作为参数的文件中没有定义connect方法的实现。你可以在你的机器上找到这个方法,可能在下面的目录下“C:\Users\Documents\Arduino\libraries\PubSubClient\src\PubSubClient. cpp”。我相信这是导致连接失败的原因。
如果你使用Arduino IDE来测试这个连接,可以使用azure-sdk-for-c-arduino源代码。链路How to Setup and Run Azure SDK for Embedded C IoT Hub Client on Espressif ESP32提供了建立连接所需的配置和设置步骤。此代码示例使用az_iot_hub_client,用于与IoT Hub建立连接。
如果您不喜欢使用Arduino IDE,请参考Connect an ESPRESSIF ESP32 using Azure IoT Middleware for FreeRTOS中提供的步骤来建立连接。
希望这能帮上忙。干杯!

相关问题