MQTT 连接丢失:在循环中组合 HTTP 和 MQTT 调用时出现连接问题

问题描述 投票:0回答:1

我使用 HTTP 部分进行 URL 调用,MQTT 部分允许将消息发布到指定主题。但是在循环中组合 HTTP 和 MQTT 调用时遇到连接问题,MQTT 连接反复丢失。 如果我删除 HTTP 调用,则 MQTT 可以顺利工作而不会丢失连接。

模块:ESP8266 Wi-Fi 12E IDE:阿杜尼奥 请查找以下代码

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <PubSubClient.h>

const char* ssid = "OOB SMARTHOME";
const char* password = "OOB123456";

const char* serverUrl = "http://example.com/api";  // Replace with your server URL

const char* mqtt_server = "mqtt_broker_IP";
const char* mqtt_username = "mqtt_username";
const char* mqtt_password = "mqtt_password";

WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);

const short int   BUILTIN_LED1 = 2; //GPIO2 esp8266

void setup() {
  Serial.begin(115200);
  
  pinMode(BUILTIN_LED1, OUTPUT);
   digitalWrite(BUILTIN_LED1, HIGH); //LED OFF
    
  // 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");

  // Connect to MQTT broker
  mqttClient.setServer(mqtt_server, 1883);
  while (!mqttClient.connected()) {
    Serial.println("Connecting to MQTT...");
    if (mqttClient.connect("ESP8266lient123", mqtt_username, mqtt_password)) {
      Serial.println("Connected to MQTT");
      mqttClient.publish("123456789PB", "HELLO");
     mqttClient.subscribe("123456789SB");
    } else {
      Serial.print("Failed, rc=");
      Serial.print(mqttClient.state());
      Serial.println(" Trying again in 5 seconds...");
      delay(5000);
    }
  }
}

void reconnectMQTT() {
  while (!mqttClient.connected()) {
    Serial.println("Reconnecting to MQTT...");
    if (mqttClient.connect("ESP8266Client", mqtt_username, mqtt_password)) {
      Serial.println("Reconnected to MQTT");
    } else {
      Serial.print("Failed, rc=");
      Serial.print(mqttClient.state());
      Serial.println(" Trying again in 5 seconds...");
      delay(5000);
    }
  }
}

void loop() {
  // Your other non-blocking loop code can go here

  // Send data using HTTP
  sendHTTPData("81X24TJJB4SCENEAQ");  // Replace with your actual data payload

  // Publish to MQTT
  publishMQTT("Your MQTT payload");  // Replace with your actual MQTT payload

   if (!mqttClient.connected()) 
                    {
                      reconnectMQTT();  
                      digitalWrite(BUILTIN_LED1, HIGH); //LED OFF
                    }
                    else
                    {
                      digitalWrite(BUILTIN_LED1, LOW); //LED OFF
                      mqttClient.loop();
                    }

  delay(5000);  // Adjust the delay as needed
}

void sendHTTPData(const String& data) {
  HTTPClient http;

  // Specify the server and resource
  http.begin(wifiClient, serverUrl);  // Use ::begin(WiFiClient, url)

  // Add headers if needed
  // http.addHeader("Content-Type", "application/json");

  // Construct the POST request with data
  int httpCode = http.POST(data);

  // Check the HTTP response
  if (httpCode > 0) {
    Serial.print("HTTP Response code: ");
    Serial.println(httpCode);
    String payload = http.getString();
    Serial.println("HTTP Response payload: " + payload);
  } else {
    Serial.println("HTTP request failed");
  }

  // Close the connection
  http.end();
}

void publishMQTT(const String& payload) {
  // Publish to MQTT
  if (mqttClient.connected()) {
    mqttClient.publish("123456789PB", payload.c_str());
    Serial.println("Published to MQTT: " + payload);
  }
}

我想要通过代码功能解决此问题或有关此问题的任何建议。

c https embedded mqtt arduino-esp8266
1个回答
0
投票

您在循环中使用delay(5000),这可能会影响MQTT和HTTP调用,请考虑使用millis()函数而不是delay。您可以查看内置示例:blinkwithoutdelay 来了解这一点。

在更广泛的解决方案中,在 ESP32 设备上使用 RTOS 将为您带来更好的性能

© www.soinside.com 2019 - 2024. All rights reserved.