Hi,
I would like to know what's wrong with (this) approach of including file in another .ino sketch just for separating parts of the program.
I have main file:
#include <mqtt.h>
void setup() {
Serial.begin(115200);
pinMode(16, OUTPUT);
digitalWrite(16, LOW);
// delay(42);
startWifi();
delay(42);
reconnMqtt();
}
unsigned long next_fire = 0;
void loop() {
mqttLoop();
}
and the included one mqtt.h
:
#ifndef Mqtt_h
#define Mqtt_h
#include <PubSubClient.h>
#include "WiFi.h"
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
bool on = false;
void callback(char* topic, byte* payload, unsigned int length);
WiFiClient wifi;
IPAddress server(192, 168, 0, 107);
PubSubClient client(server, 1883, callback, wifi);
bool startWifi() {
WiFiManager wm;
bool res = wm.autoConnect("AutoConnectAP", "password"); // password protected ap
if (!res) {
Serial.println("Failed to connect wifi");
} else {
Serial.println("wifi connected...yeey :)");
}
return res;
}
void reconnMqtt() {
client.setBufferSize(2048);
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP32C3")) {
Serial.println("connected");
client.subscribe("ping");
client.subscribe("cam");
client.subscribe("switch");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
String message = "";
String domain = String(topic);
for (int i = 0; i < length; i++) {
message += (char)payload[i];
// Serial.print((char)payload[i]);
}
Serial.println(message);
// shoot();
if(domain == "ping") {
client.publish("pong", message.c_str());
}
if (domain == "cam") {
if (message == "shoot") {
Serial.println("should be shooting");
}
}
if (domain == "switch") {
on = !on;
Serial.print("will turn: ");
Serial.println(on);
digitalWrite(16, on);
}
}
void mqttLoop() {
client.loop();
}
#endif
this ends with this stacktrace and I googled and found answers such that it's caused by (wrong?) memory allocation for a variable (I suppose it would be WiFiClient
in my case).
it crashes as follows:
16:00:39.304 -> Attempting MQTT connection...Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
16:00:39.336 ->
16:00:39.336 -> Core 1 register dump:
16:00:39.336 -> PC : 0x40091691 PS : 0x00060d30 A0 : 0x800919e4 A1 : 0x3ffb1fc0
16:00:39.336 -> A2 : 0xffffffff A3 : 0x00060d23 A4 : 0x00060d20 A5 : 0x3f40c004
16:00:39.336 -> A6 : 0x000000fd A7 : 0xb33fffff A8 : 0x00000001 A9 : 0x00000004
16:00:39.336 -> A10 : 0x00060d23 A11 : 0x000000a6 A12 : 0x00000bb8 A13 : 0x3f40c100
16:00:39.369 -> A14 : 0x00000077 A15 : 0x6b00a8c0 SAR : 0x0000001d EXCCAUSE: 0x0000001c
16:00:39.369 -> EXCVADDR: 0x0000000f LBEG : 0x4008a794 LEND : 0x4008a7aa LCOUNT : 0xffffffff
16:00:39.369 ->
16:00:39.369 ->
16:00:39.369 -> Backtrace: 0x4009168e:0x3ffb1fc0 0x400919e1:0x3ffb1fe0 0x40091405:0x3ffb2000 0x4009153c:0x3ffb2020 0x4008374a:0x3ffb2040 0x4008397a:0x3ffb2070 0x400f631f:0x3ffb2090 0x400d5d5a:0x3ffb2100 0x400d547b:0x3ffb2160 0x400d3665:0x3ffb21a0 0x400d37f6:0x3ffb2210 0x400d2e2a:0x3ffb2240 0x400d2ec9:0x3ffb2260 0x400e9423:0x3ffb2290
16:00:39.401 ->
when I put it all in a single file it works, so I'm doing something wrong and would like to know about it more, please.
I'm using Esp32-Cam board with AI-Thinker ESP32-CAM board selected in Arduino Studio 2.3.2
Thanks!