C语言 MQTT连接丢失:在循环中组合HTTP和MQTT调用时的连接问题

btxsgosb  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(108)

我正在使用HTTP部分进行URL调用,MQTT部分允许将消息发布到指定的主题。但是在循环中组合HTTP和MQTT调用时遇到连接问题,MQTT连接多次丢失。如果我删除HTTP调用,则MQTT可以顺利工作,而不会丢失连接。
模块:ESP8266 Wi-Fi 12 E IDE:Ardunio

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

字符串
我想这个问题的解决方案与代码功能或有关这个问题的任何建议.

57hvy0tb

57hvy0tb1#

你在循环中使用了delay(5000),这可能会影响MQTT和HTTP调用,考虑使用米利斯()函数代替delay。你可以检查内置的例子:blinkwithoutdelay。
在更广泛的解决方案中,在ESP32设备上使用RTOS将为您提供给予更好的性能

相关问题