core 1 panic using mqtt through wifi on ttgo lora32

hi
i try to build a lora2mqtt gateway

i first test sample code to publish on MQTT on a wifi ESP32

then, i tested a pair of ttgo lora32, one as a transmitter, the other as a receiver

now, i want the receiver to transmit data to an mqtt broker on the wifi network but the MQTT_connect() function i used on the esp32 get a core panic

#include <SPI.h>            
#include <LoRa.h>
#include <WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

//LORA
const long frequency = 868E6;  // LoRa Frequency
int sf=7;
const int csPin = 18;          // LoRa radio chip select
const int resetPin = 14;        // LoRa radio reset
const int irqPin = 26;          // change for your board; must be a hardware interrupt pin


//WIFI
const char *ssid = "***"; // Wifi SSID
const char *password = "***"; // Wifi Password


//MQTT
#define MQTT_SERV "***"
#define MQTT_PORT 1883
#define MQTT_NAME "***"
#define MQTT_PASS "***"
char* GPS_FEED ="gps";


WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, MQTT_SERV, MQTT_PORT, MQTT_NAME, MQTT_PASS);
Adafruit_MQTT_Publish gps_feed = Adafruit_MQTT_Publish(&mqtt, GPS_FEED); 


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


  LoRa.setPins(csPin, resetPin, irqPin);


  Serial.println(ssid);


  WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("->WiFi connected");
  Serial.println("->IP address: ");
  Serial.println(WiFi.localIP());


  if (!LoRa.begin(frequency)) {
    Serial.println("LoRa init failed. Check your connections.");
    while (true);                       // if failed, do nothing
  }


  Serial.println("LoRa init succeeded.");
  Serial.println();
  Serial.println("LoRa Simple Gateway");
  Serial.println("Only receive messages from nodes");
  Serial.println("Tx: invertIQ enable");
  Serial.println("Rx: invertIQ disable");
  Serial.println();


  LoRa.onReceive(onReceive);
  LoRa.onTxDone(onTxDone);
  LoRa_rxMode();
}


void loop() {
  if (runEvery(10000)) { // repeat every 5000 millis


    String message = "HeLoRa World! ";
    message += "I'm a Gateway! ";
    message += millis();


    LoRa_sendMessage(message); // send a message


    Serial.println("Send Message!");
  }
}


void onReceive(int packetSize) {
  String message = "";
  while (LoRa.available()) {
    message += (char)LoRa.read();
  }


  Serial.print("Gateway Receive: ");
  Serial.println(message);
  
  char coord[90];
  message.toCharArray(coord, 90);
//  MQTT_connect();       //     <-- CORE PANIC IF UNCOMMENTED
//  gps_feed.publish(coord);
  
}


void MQTT_connect()      //    <---THIS IS THE FUNCTIUN WICH CAUSE THE CORE PANIC
{
  int8_t ret;
  // Stop if already connected.
  if (mqtt.connected())
  {
    return;
  }
  
    Serial.println("connectiong to mqtt");
  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) // connect will return 0 for connected
  {
    
       Serial.print(".");
    mqtt.disconnect();
    delay(5000);  // wait 5 seconds
    retries--;
//    if (retries == 0)
//    {
      // basically die and wait for WDT to reset me
//      while (1);
//    }
  }
}




void LoRa_rxMode(){
  LoRa.disableInvertIQ();               // normal mode
  LoRa.receive();                       // set receive mode
}


void LoRa_txMode(){
  LoRa.idle();                          // set standby mode
  LoRa.enableInvertIQ();                // active invert I and Q signals
}


void LoRa_sendMessage(String message) {
  LoRa_txMode();                        // set tx mode
  LoRa.beginPacket();                   // start packet
  LoRa.print(message);                  // add payload
  LoRa.endPacket(true);                 // finish packet and send it
}





void onTxDone() {
  Serial.println("TxDone");
  LoRa_rxMode();
}


boolean runEvery(unsigned long interval)
{
  static unsigned long previousMillis = 0;
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;
    return true;
  }
  return false;
}

and here is the error in serial :

00:28:25.283 -> .
00:28:25.283 -> ->WiFi connected
00:28:25.330 -> ->IP address: 
00:28:25.330 -> 192.168.1.66
00:28:25.330 -> LoRa init succeeded.
00:28:25.377 -> 
00:28:25.377 -> LoRa Simple Gateway
00:28:25.377 -> Only receive messages from nodes
00:28:25.424 -> Tx: invertIQ enable
00:28:25.424 -> Rx: invertIQ disable
00:28:25.471 -> 
00:28:25.471 -> Gateway Receive: {"lt":"45.758498","lg":"2.788455","al":"0.000000"}
00:28:25.518 -> connectiong to mqtt
00:28:25.754 -> Guru Meditation Error: Core  1 panic'ed (Interrupt wdt timeout on CPU1)
00:28:25.848 -> Core 1 register dump:
00:28:25.848 -> PC      : 0x4008c16e  PS      : 0x00060d34  A0      : 0x8008b387  A1      : 0x3ffbfe70  
00:28:25.940 -> A2      : 0x3ffcb710  A3      : 0x3ffb8074  A4      : 0x00000001  A5      : 0x00000001  
00:28:26.034 -> A6      : 0x00060d23  A7      : 0x00000000  A8      : 0x3ffb8074  A9      : 0x3ffb8074  
00:28:26.126 -> A10     : 0x00000018  A11     : 0x00000018  A12     : 0x00000001  A13     : 0x00000001  
00:28:26.220 -> A14     : 0x00060d21  A15     : 0x00000000  SAR     : 0x0000000e  EXCCAUSE: 0x00000006  
00:28:26.312 -> EXCVADDR: 0x00000000  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0xffffffff  
00:28:26.406 -> Core 1 was running in ISR context:
00:28:26.453 -> EPC1    : 0x40129a33  EPC2    : 0x00000000  EPC3    : 0x00000000  EPC4    : 0x4008c16e
00:28:26.547 -> 
00:28:26.547 -> ELF file SHA256: 0000000000000000
00:28:26.593 -> 
00:28:26.593 -> Backtrace: 0x4008c16e:0x3ffbfe70 0x4008b384:0x3ffbfe90 0x400896bb:0x3ffbfeb0 0x401299b1:0x3ffbfef0 0x4011713b:0x3ffbff10 0x401169b9:0x3ffbff40 0x40116b6c:0x3ffb

i hope someone here will know what is happening because i have no clue

thx

The panic is telling you what's wrong: "wdt timeout"

My experience is with the ESP8266, and the ESP32 is probably similar.
If the watchdog timer isn't periodically reset then it will automatically reset your ESP8266. The watchdog is reset every time loop() runs or you call delay() or yield() but if you have blocking code like a while or for loop, then the watchdog may time out, resulting in your reset.

Delay() doesn't cause WDT reset though because it automatically calls yield() to prevent that.The timeout for the hardware watchdog is 8.2 seconds, and 3.2 seconds for the software watchdog timeout
Don't block software watchdog too long with blocking code, otherwise it will trigger hardware watchdog reset.
You can reset the WDT inside your blocking code with:

ESP.wdt_reset()
or
yield();

thank you for your reply
so it is an interrupt timeout
i am surprised, because the loop take a few 100’s ms
Delay() cause WDT to reset, so i added a few Delay(1) in the MQTT_connect() code but without any positive effect.

ardupadawan:
thank you for your reply
so it is an interrupt timeout
i am surprised, because the loop take a few 100's ms
Delay() cause WDT to reset, so i added a few Delay(1) in the MQTT_connect() code but without any positive effect.

yield() is better. There is something in your code that is taking too long.

I looked at it again a bit closer.
You should be calling mqttConnect() in setup, then checking it on every loop.
onReceive() looks like the wrong place to be first connecting to the broker because if you don't check the connection periodically, MQTT will disconnect. And the 5-second delay in mqttConnect() could be your problem.

Here is how I do it:

loop(){
  Make sure we stay connected to the mqtt broker
    if (!client.connected()) {
      mqttConnect();
    }
    if (!client.loop()) {
      client.connect(connectName);
  }
  // Do other loop stuff
}

By the way, I am no MQTT expert- I just take good notes when I get great advice on the forum.
(IOW, I could be wrong).

Download and run the exception decoder: GitHub - me-no-dev/EspExceptionDecoder: Exception Stack Trace Decoder for ESP8266 and ESP32
It will trace back through the call stack and help you identify the offending function.

You wer right SteveMan, thank you
i moved MQTT_connect() in loop and it work fine
(MQTT_connect() check first if the connexion is avalable)

void loop() {
 if (runEvery(200)) {
 MQTT_connect();
 message.toCharArray(coord, 90);
 gps_feed.publish(coord);
 }
}

thx

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.