256dpi / arduino-mqtt

MQTT library for Arduino
MIT License
1.01k stars 230 forks source link

Mqtt bufferSize value cannot exceed 1024 * 43 otherwise with error "Guru Meditation Error" #297

Closed canterville12 closed 6 months ago

canterville12 commented 1 year ago

After uploading the code below, and having a buffer size larger than 1024 43, for example, bufferSize = 1024 50 the error message will appear as below. The code is uploaded to ESP32-CAM with esp version 2.0.2 and Arduino IDE 2.0.3

#include "secrets.h"
#include <WiFiClientSecure.h>
#include <MQTTClient.h>
#include "WiFi.h"
#include "esp_camera.h"

#define uS_TO_S_FACTOR    1000000  
#define TIME_TO_SLEEP     60        

#define PWDN_GPIO_NUM     32
#define RESET_GPIO_NUM    -1
#define XCLK_GPIO_NUM      0
#define SIOD_GPIO_NUM     26
#define SIOC_GPIO_NUM     27
#define Y9_GPIO_NUM       35
#define Y8_GPIO_NUM       34
#define Y7_GPIO_NUM       39
#define Y6_GPIO_NUM       36
#define Y5_GPIO_NUM       21
#define Y4_GPIO_NUM       19
#define Y3_GPIO_NUM       18
#define Y2_GPIO_NUM        5
#define VSYNC_GPIO_NUM    25
#define HREF_GPIO_NUM     23
#define PCLK_GPIO_NUM     22

const int bufferSize = 1024 * 50; // 32 KB

WiFiClientSecure net = WiFiClientSecure();
MQTTClient client(bufferSize);

void connectAWS()
{
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

  Serial.println("\n\n=====================");
  Serial.println("Connecting to Wi-Fi");
  Serial.println("=====================");

  while (WiFi.status() != WL_CONNECTED){
    delay(500);
    Serial.print(".");
  }

  net.setCACert(AWS_CERT_CA);
  net.setCertificate(AWS_CERT_CRT);
  net.setPrivateKey(AWS_CERT_PRIVATE);

  client.begin(AWS_IOT_ENDPOINT, 8883, net);
  client.setCleanSession(true);

  Serial.println("\n=====================");
  Serial.println("Connecting to AWS IoT");
  Serial.println("=====================");

  while (!client.connect(THINGNAME)) {
    Serial.print(".");
    delay(100);
  }

  if(!client.connected()){
    Serial.println("AWS IoT Timeout!");
    ESP.restart();
    return;
  }

  Serial.println("\n=====================");
  Serial.println("AWS IoT Connected!");
  Serial.println("=====================\n\n");
}

void disconnectAWS() {
  Serial.println("\n\n=====================");
  Serial.println("Disconnecting from AWS IoT");
  Serial.println("=====================");

  while (!client.disconnect()) {
    Serial.print(".");
    delay(100);
  }

  if(client.connected()){
    Serial.println("AWS IoT Timeout!");
    ESP.restart();
    return;
  }

  Serial.println("\n=====================");
  Serial.println("AWS IoT Disconnected!");
  Serial.println("=====================\n\n");
}

void cameraInit(){
  camera_config_t config;
  config.ledc_channel = LEDC_CHANNEL_0;
  config.ledc_timer = LEDC_TIMER_0;
  config.pin_d0 = Y2_GPIO_NUM;
  config.pin_d1 = Y3_GPIO_NUM;
  config.pin_d2 = Y4_GPIO_NUM;
  config.pin_d3 = Y5_GPIO_NUM;
  config.pin_d4 = Y6_GPIO_NUM;
  config.pin_d5 = Y7_GPIO_NUM;
  config.pin_d6 = Y8_GPIO_NUM;
  config.pin_d7 = Y9_GPIO_NUM;
  config.pin_xclk = XCLK_GPIO_NUM;
  config.pin_pclk = PCLK_GPIO_NUM;
  config.pin_vsync = VSYNC_GPIO_NUM;
  config.pin_href = HREF_GPIO_NUM;
  config.pin_sscb_sda = SIOD_GPIO_NUM;
  config.pin_sscb_scl = SIOC_GPIO_NUM;
  config.pin_pwdn = PWDN_GPIO_NUM;
  config.pin_reset = RESET_GPIO_NUM;
  config.xclk_freq_hz = 20000000;
  config.pixel_format = PIXFORMAT_JPEG;
  config.frame_size = FRAMESIZE_VGA; // 640x480
  config.jpeg_quality = 10;
  config.fb_count = 2;

  // camera init
  esp_err_t err = esp_camera_init(&config);
  if (err != ESP_OK) {
    Serial.printf("Camera init failed with error 0x%x", err);
    ESP.restart();
    return;
  }
}

void grabImage(){
  camera_fb_t * fb = esp_camera_fb_get();
  Serial.print(fb->len);
  if(fb != NULL && fb->format == PIXFORMAT_JPEG && fb->len < bufferSize){
    Serial.print("Image Length: ");
    Serial.print(fb->len);

    Serial.print("\t Topic: ");
    char topic[256];
    snprintf(topic, sizeof(topic), "%s/%s",ESP32CAM_PUBLISH_TOPIC, WiFi.macAddress().c_str());
    Serial.print(topic);

    Serial.print("\t Publish Image: ");

    bool result = client.publish(topic, (const char*)fb->buf, fb->len);
    Serial.println(result);

    if(!result){
      ESP.restart();
    }
  }
  esp_camera_fb_return(fb);
  delay(100);
}

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

  cameraInit();
  connectAWS();
  if(client.connected()) grabImage();
  disconnectAWS();

  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
  Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) + " Seconds");
  esp_deep_sleep_start();
}

void loop() {
}

The error is shown below, and I believe it happens at

while (!client.connect(THINGNAME))

Any help would be appreciated! Thanks!

Guru Meditation Error: Core 1 panic'ed (StoreProhibited). Exception was unhandled.

Core 1 register dump: PC : 0x40160ea7 PS : 0x00060230 A0 : 0x800d6324 A1 : 0x3ffb2670
A2 : 0xffffffff A3 : 0x0000c800 A4 : 0x00000010 A5 : 0x00000004
A6 : 0x3f400db1 A7 : 0xffffff7f A8 : 0x3ffb26b0 A9 : 0x00000000
A10 : 0x00001502 A11 : 0x00000000 A12 : 0x00001502 A13 : 0x00002680
A14 : 0x00000020 A15 : 0x000000bc SAR : 0x0000001c EXCCAUSE: 0x0000001d
EXCVADDR: 0x00000000 LBEG : 0x4008b3fc LEND : 0x4008b418 LCOUNT : 0x00000000

Backtrace:0x40160ea4:0x3ffb26700x400d6321:0x3ffb2690 0x400d6022:0x3ffb26e0 0x400d5d29:0x3ffb2730 0x400d30dd:0x3ffb27a0 0x400d333d:0x3ffb27c0 0x400d7b6a:0x3ffb2820

ELF file SHA256: 0000000000000000

Rebooting...

ets Jul 29 2019 12:21:46

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT) configsip: 0, SPIWP:0xee clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00 mode:DIO, clock div:1 load:0x3fff0030,len:1324 ho 0 tail 12 room 4 load:0x40078000,len:13508 load:0x40080400,len:3604 entry 0x400805f0

256dpi commented 6 months ago

This was probably due to allocating a massive amount of memory that was not available on the system. I'm closing this now, but please re-open if you suspect another issue.