Hello, everyone.
On my Adafruit HUZZAH32 - ESP32 Feather I would like to configure MQTT connection to Adafruit IO platform using WiFi manager (web captive portal), but the problem is that MQTT connection, publish and subscribe functions ask for global definitions (#define) and these, as we all know, are non rewrittable.
I am using ConfigOnSwitchFS example by khoih-prog developer. For MQTT broker I am using Adafruit MQTT and their modified for ESP32 mqtt_2subs_esp8266 example.
Basically, I modified ConfigOnSwitchFS by adding more parameter fields (forms) like MQTT username, password etc. and I am able to save these new paramaters to a SPIFFS file, but they are either char or int variables and adafruit's MQTT publish, subscribe and other functions won't accept them. They only accept #define's, like this:
#define AIO_SERVER_KEY "io.adafruit.com"
#define AIO_SERVERPORT_KEY "1883"
#define AIO_USERNAME_KEY "someone"
#define AIO_KEY_KEY "key_example_12345"
This won't be accepted in function calls, even though i successfully managed to update these variables through web server and SPIFFS:
char AIO_SERVER_KEY[20] = "io.adafruit.com";
int AIO_SERVERPORT_KEY = 1883;
char AIO_USERNAME_KEY[20] = "someone";
char AIO_KEY_KEY[40] = "key_example_12345";
And this is how whole setup for connection to Adafruit IO looks like:
// Create an ESP32 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt1(&client, AIO_SERVER_KEY, AIO_SERVERPORT_KEY, AIO_USERNAME_KEY, AIO_KEY_KEY);
// Feeds
Adafruit_MQTT_Subscribe BME280_onoff1 = Adafruit_MQTT_Subscribe(&mqtt1, AIO_USERNAME_KEY "/feeds/bme280_onoff");
Adafruit_MQTT_Publish Temperatura1 = Adafruit_MQTT_Publish(&mqtt1, AIO_USERNAME_KEY "/feeds/Temperatura");
This is in the source code (Adafruit_MQTT.h):
class Adafruit_MQTT_Publish {
public:
Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver, const char *feed, uint8_t qos = 0);
bool publish(const char *s);
bool publish(double f, uint8_t precision=2); // Precision controls the minimum number of digits after decimal.
// This might be ignored and a higher precision value sent.
bool publish(int32_t i);
bool publish(uint32_t i);
bool publish(uint8_t *b, uint16_t bLen);
private:
Adafruit_MQTT *mqtt;
const char *topic;
uint8_t qos;
};
class Adafruit_MQTT_Subscribe {
public:
Adafruit_MQTT_Subscribe(Adafruit_MQTT *mqttserver, const char *feedname, uint8_t q=0);
void setCallback(SubscribeCallbackUInt32Type callb);
void setCallback(SubscribeCallbackDoubleType callb);
void setCallback(SubscribeCallbackBufferType callb);
void setCallback(AdafruitIO_MQTT *io, SubscribeCallbackIOType callb);
void removeCallback(void);
const char *topic;
uint8_t qos;
uint8_t lastread[SUBSCRIPTIONDATALEN];
// Number valid bytes in lastread. Limited to SUBSCRIPTIONDATALEN-1 to
// ensure nul terminating lastread.
uint16_t datalen;
SubscribeCallbackUInt32Type callback_uint32t;
SubscribeCallbackDoubleType callback_double;
SubscribeCallbackBufferType callback_buffer;
SubscribeCallbackIOType callback_io;
AdafruitIO_MQTT *io_mqtt;
private:
Adafruit_MQTT *mqtt;
};
Additional info requirements: I read somewere that the initial MQTT parameters are stored in a flash memory and must always be present, that's why Adafruit example uses only #defines. So now I must somehow point function calls to take arguments from newly written parameters in flash memory, I guess using pointers (not experienced enough to use them, though).
My MQTT setup functions need to be done globally, that is before setup() because these function will be used later in code. I first call WiFi manager by clicking on a button, then I enter custom paramteres in web portal, after that those paramteres get saved in SPIFFS .txt file and are applied immedeately after reset. But, as mentioned, I wasn't able to incorporate these new parameters to connect to Adafruit IO by MQTT.
Please, advise.