0

I wish to combine into a single library Wifi + MQTT, for a multi devices project.

right after boot, getting Wifi - I get an error the moment MQTT server is trying to connect: mqttClient.connect(DEVICE_TOPIC, MQTT_USER, MQTT_PASS, MQTTavltopic, 0, true, "offline");

yields :

WiFi connected
IP address: 192.168.3.186
Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC : 0x400d26bd PS : 0x00060630 A0 : 0x800d28e8 A1 : 0x3ffb1eb0 
A2 : 0x3ffc116c A3 : 0x3f4010ef A4 : 0x3f40108c A5 : 0x3f4010b7 
A6 : 0x3ffc106c A7 : 0x00000000 A8 : 0x800d26b6 A9 : 0x3ffb1e90 
A10 : 0x00000000 A11 : 0x3ffc116c A12 : 0x0000000e A13 : 0x00000010 
A14 : 0x0000000e A15 : 0x3ffb1d94 SAR : 0x0000000c EXCCAUSE: 0x0000001c 
EXCVADDR: 0x00000000 LBEG : 0x400014fd LEND : 0x4000150d LCOUNT : 0xfffffffc 
Backtrace: 0x400d26bd:0x3ffb1eb0 0x400d28e5:0x3ffb1ef0 0x400d0fa6:0x3ffb1f20 0x400d103b:0x3ffb1f50 0x400d0e59:0x3ffb1f90 0x400d3651:0x3ffb1fb0 0x40088b7d:0x3ffb1fd0
Rebooting...

I'm sharing both .h and .cpp files. Library is at its begining - so don't pay attention for irrelevant/ immature parts.

TNX

this is .h file:

#ifndef myIOT32_h
#define myIOT32_h
#include "Arduino.h"
// #include <ESPmDNS.h>
// #include <WiFiUdp.h>
// #include <ArduinoOTA.h>
#include "time.h"
#include "WiFi.h"
#include "secrets.h"
#include <PubSubClient.h>
class myIOT32
{
#define DEVICE_TOPIC "ESP32_SLEEP"
#define MQTT_PREFIX "myHome"
#define MQTT_GROUP ""
private:
 char MQTTmsgtopic[50];
 char MQTTlogtopic[50];
 char MQTTavltopic[50];
 char MQTTdevtopic[50];
 char MQTTlastctopic[50];
 struct tm _timeinfo;
 time_t _epoch_time;
 bool _wifi_status = false;
 char *wifi_ssid; // = "WIFI_NETWORK_BY_USER";
 char *wifi_pass; // = "WIFI_PASSWORD_BY_USER";
 char *mqtt_server;
 char *user = "";
 char *passw = "";
 WiFiClient espClient;
 PubSubClient mqttClient;
public:
 myIOT32(char *devTopic = "no-name", char *ssid = SSID_ID, char *wifi_p = PASS_WIFI, char *mqtt_broker = MQTT_SERVER1, char *mqttU = MQTT_USER, char *mqttP = MQTT_PASS, int port = 1883);
 void looper();
 void start();
 void mqtt_pubmsg(char *msg);
 bool use_wifi = true;
 void startMQTT();
private:
 void MQTTloop();
 void startNTP(const int gmtOffset_sec, const int daylightOffset_sec, const char *ntpServer);
 bool startWifi();
 void getTime();
 void MQTTcallback(char *topic, byte *payload, unsigned int length);
 void createTopics();
 void connectMQTT();
 void subscribeMQTT();
};

endif

and my .cpp file

#include "myIOTesp32.h"
myIOT32::myIOT32(char *devTopic, char *ssid, char *wifi_p, char *mqtt_broker, char *mqttU, char *mqttP, int port)
{
 mqtt_server = mqtt_broker;
 user = mqttU;
 passw = mqttP;
 wifi_ssid = ssid;
 wifi_pass = wifi_p;
}
void myIOT32::start()
{
 if(startWifi()){
 getTime();
 startMQTT();
 }
}
void myIOT32::looper()
{
 MQTTloop();
}
void myIOT32::MQTTcallback(char *topic, byte *payload, unsigned int length)
{
 char incoming_msg[150];
 char msg[100];
 Serial.print("Message arrived [");
 Serial.print(topic);
 Serial.print("] ");
 for (int i = 0; i < length; i++)
 {
 Serial.print((char)payload[i]);
 incoming_msg[i] = (char)payload[i];
 }
 incoming_msg[length] = 0;
 Serial.println();
}
void myIOT32::createTopics()
{
 sprintf(MQTTmsgtopic, "%s/%s", MQTT_PREFIX, "Messages");
 sprintf(MQTTlogtopic, "%s/%s", MQTT_PREFIX, "log");
 sprintf(MQTTdevtopic, "%s/%s", MQTT_PREFIX, DEVICE_TOPIC);
 sprintf(MQTTavltopic, "%s/%s", MQTTdevtopic, "Avail");
 sprintf(MQTTlastctopic, "%s/%s", MQTTdevtopic, "nexton_command");
}
void myIOT32::connectMQTT()
{
 if (!mqttClient.connected())
 {
 mqttClient.connect(DEVICE_TOPIC, MQTT_USER, MQTT_PASS, MQTTavltopic, 0, true, "offline");
 }
}
void myIOT32::subscribeMQTT()
{
 mqttClient.subscribe(MQTTdevtopic);
 mqttClient.subscribe(MQTTlastctopic);
}
void myIOT32::mqtt_pubmsg(char *msg)
{
 mqttClient.publish(MQTTmsgtopic, msg);
 Serial.print(MQTTmsgtopic);
 Serial.print("/");
 Serial.println(msg);
}
void myIOT32::startMQTT()
{
 mqttClient.setServer(mqtt_server, 1883);
 mqttClient.setCallback(std::bind(&myIOT32::MQTTcallback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
 createTopics();
 connectMQTT();
 subscribeMQTT();
}
void myIOT32::MQTTloop()
{
 static long lastReconnectAttempt = 0;
 if (mqttClient.connected())
 {
 mqttClient.loop();
 }
 else
 {
 long now = millis();
 Serial.println("MQTT Server- NOT connected");
 if (now - lastReconnectAttempt > 5000)
 {
 lastReconnectAttempt = now;
 connectMQTT();
 subscribeMQTT();
 }
 }
}
void myIOT32::startNTP(const int gmtOffset_sec = 2 * 3600, const int daylightOffset_sec = 0, const char *ntpServer = "pool.ntp.org")
{
 configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
}
bool myIOT32::startWifi()
{
 long beginwifi = millis();
 WiFi.begin(wifi_ssid, wifi_pass);
 while (WiFi.status() != WL_CONNECTED && millis() - beginwifi < 30000)
 {
 delay(200);
 Serial.print(".");
 }
 if (WiFi.status() == WL_CONNECTED)
 {
 Serial.println("");
 Serial.println("WiFi connected");
 Serial.print("IP address: ");
 Serial.println(WiFi.localIP());
 startNTP();
 return 1;
 }
 else
 {
 Serial.println("NO-WiFi");
 return 0;
 }
}
void myIOT32::getTime()
{
 int a = 0;
 while (a < 3)
 {
 if (getLocalTime(&_timeinfo))
 {
 delay(100);
 time(&_epoch_time);
 }
 a++;
 }
}
asked Jun 5, 2020 at 17:05
0

1 Answer 1

0
myIOT32::myIOT32(char *devTopic, char *ssid, char *wifi_p, char *mqtt_broker, char *mqttU, char *mqttP, int port)
 : mqttClient(espClient) // <--- this part was missing *****
{
answered Jun 6, 2020 at 17:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.