【esp32】解决以太网+mqtt堆栈溢出问题 报错 no mem for receive buffer

本文主要记录了 esp32 + 以太网 +mqtt 功能时遇到的堆栈溢出的情况,千里之堤毁于蚁穴,开发过程的不细心导致多付出了一天多的时间,记录于此,共勉。
本篇文章,点赞+收藏,可私信获取免费原文

📋 个人简介

【esp32】解决以太网+mqtt堆栈溢出问题 报错 no mem for receive buffer

1.程序架构:

if(以太网连接成功){
   
    mqtt_start();
}
// 创建一个任务,每间隔1s,将数据发到 mqtt broker
xTaskCreate(mqtt_publish_test, "mqtt_publish_test", 1024*4, NULL, configMAX_PRIORITIES, NULL);

2.问题场景描述:

esp32 开发板以太网连接上网,连接成功后通过 mqtt 将消息上传至服务器,但是存在问题

代码运行一段时间后,报错内存溢出 no mem for receive buffer ,然后就卡死了,只能手动重启

image-20230831191232772

3.问题定位

以太网口 task 运行时,会动态分配 ETH_MAX_PACKET_SIZE = 1522Byte 的内存,为什么程序的堆栈溢出呢??

image-20230831185358669

3.1监控堆栈

在 main 函数中,利用库函数监控堆栈的情况

ESP_LOGI("main", "[APP] Free memory: %d bytes", esp_get_free_heap_size());

监控结果如下:

发现系统的堆栈一直在减小,那最后堆栈不够用崩溃是无疑了

image-20230831190914010

运行一段时间后,报错点

image-20230831190855379

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
您可以使用以下代码来设置esp32 4G MQTT订阅温度传感器的主题: #include <PubSubClient.h> #include <WiFi.h> #include <Wire.h> #include <Adafruit_Sensor.h> #include <Adafruit_BME280.h> // Replace with your network credentials const char* ssid = "your_SSID"; const char* password = "your_PASSWORD"; // Replace with your MQTT Broker IP address const char* mqtt_server = "your_MQTT_BROKER_IP"; WiFiClient espClient; PubSubClient client(espClient); Adafruit_BME280 bme; void setup() { Serial.begin(115200); delay(100); // Connect to Wi-Fi network with SSID and password Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("WiFi connected!"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); // Connect to MQTT Broker client.setServer(mqtt_server, 1883); while (!client.connected()) { Serial.println("Connecting to MQTT Broker..."); if (client.connect("ESP32Client")) { Serial.println("Connected to MQTT Broker!"); } else { Serial.print("Failed with state "); Serial.print(client.state()); delay(2000); } } // Initialize BME280 sensor if (!bme.begin(0x76)) { Serial.println("Could not find a valid BME280 sensor, check wiring!"); while (1); } // Subscribe to temperature sensor topic client.subscribe("temperature_sensor"); } void loop() { // Check if MQTT client is connected if (!client.connected()) { reconnect(); } // Read temperature from BME280 sensor float temperature = bme.readTemperature(); // Publish temperature to MQTT Broker char temperature_str[10]; dtostrf(temperature, 4, 2, temperature_str); client.publish("temperature_sensor", temperature_str); // Wait for 5 seconds delay(5000); } void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.println("Attempting MQTT connection..."); // Attempt to connect if (client.connect("ESP32Client")) { Serial.println("Connected to MQTT Broker!"); // Subscribe to temperature sensor topic client.subscribe("temperature_sensor"); } else { Serial.print("Failed with state "); Serial.print(client.state()); delay(2000); } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

积跬步、至千里

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值