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