fix mqtt ptr init bug
This commit is contained in:
parent
443a02c319
commit
14767df9ec
|
@ -11,6 +11,9 @@ ESPMegaIoT::ESPMegaIoT() : mqtt(tcpClient)
|
|||
}
|
||||
active = false;
|
||||
mqtt_connected = false;
|
||||
this->user_mqtt_callback = nullptr;
|
||||
this->user_relative_mqtt_callback = nullptr;
|
||||
this->user_subscribe_callback = nullptr;
|
||||
}
|
||||
|
||||
ESPMegaIoT::~ESPMegaIoT()
|
||||
|
@ -177,6 +180,12 @@ bool ESPMegaIoT::connectToMqtt(char *client_id, char *mqtt_server, uint16_t mqtt
|
|||
mqtt.setServer(mqtt_server, mqtt_port);
|
||||
auto boundCallback = std::bind(&ESPMegaIoT::mqttCallback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
|
||||
mqtt.setCallback(boundCallback);
|
||||
if (mqtt_user == nullptr || mqtt_password == nullptr || strlen(mqtt_user) == 0 || strlen(mqtt_password) == 0)
|
||||
{
|
||||
mqtt_connected = false;
|
||||
ESP_LOGE("ESPMegaIoT", "MQTT Connection failed: Username or password not set but MQTT use_auth is true");
|
||||
return false;
|
||||
}
|
||||
if (mqtt.connect(client_id, mqtt_user, mqtt_password))
|
||||
{
|
||||
sessionKeepAlive();
|
||||
|
@ -197,13 +206,18 @@ bool ESPMegaIoT::connectToMqtt(char *client_id, char *mqtt_server, uint16_t mqtt
|
|||
}
|
||||
bool ESPMegaIoT::connectToMqtt(char *client_id, char *mqtt_server, uint16_t mqtt_port)
|
||||
{
|
||||
ESP_LOGD("ESPMegaIoT", "Setting MQTT server to %s:%d", mqtt_server, mqtt_port);
|
||||
mqtt.setServer(mqtt_server, mqtt_port);
|
||||
auto boundCallback = std::bind(&ESPMegaIoT::mqttCallback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
|
||||
ESP_LOGD("ESPMegaIoT", "Binding MQTT callback");
|
||||
mqtt.setCallback(boundCallback);
|
||||
if (mqtt.connect(client_id))
|
||||
{
|
||||
ESP_LOGD("ESPMegaIoT", "MQTT Connected, Calling session keep alive");
|
||||
sessionKeepAlive();
|
||||
ESP_LOGD("ESPMegaIoT", "Subscribing to topics");
|
||||
mqttSubscribe();
|
||||
ESP_LOGD("ESPMegaIoT", "Publishing reports");
|
||||
// Publish all cards
|
||||
for (int i = 0; i < 255; i++)
|
||||
{
|
||||
|
@ -212,7 +226,7 @@ bool ESPMegaIoT::connectToMqtt(char *client_id, char *mqtt_server, uint16_t mqtt
|
|||
components[i]->publishReport();
|
||||
}
|
||||
}
|
||||
ESP_LOGI("ESPMegaIoT", "MQTT Connected");
|
||||
ESP_LOGI("ESPMegaIoT", "MQTT Connected OK.");
|
||||
mqtt_connected = true;
|
||||
return true;
|
||||
}
|
||||
|
@ -234,16 +248,22 @@ void ESPMegaIoT::registerMqttCallback(void (*callback)(char *, char *))
|
|||
}
|
||||
void ESPMegaIoT::mqttSubscribe()
|
||||
{
|
||||
if (user_subscribe_callback != NULL)
|
||||
ESP_LOGD("ESPMegaIoT", "Begin MQTT Subscription");
|
||||
if (user_subscribe_callback != nullptr)
|
||||
{
|
||||
ESP_LOGD("ESPMegaIoT", "Subscribing user callback");
|
||||
user_subscribe_callback();
|
||||
mqtt.loop();
|
||||
}
|
||||
// Subscribe to all topics
|
||||
for (int i = 0; i < 255; i++)
|
||||
{
|
||||
ESP_LOGV("ESPMegaIoT","Scanning component %d", i);
|
||||
if (components[i] != NULL)
|
||||
{
|
||||
ESP_LOGD("ESPMegaIoT","Subscribing component %d", i);
|
||||
components[i]->subscribe();
|
||||
mqtt.loop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -292,6 +312,7 @@ void ESPMegaIoT::publishRelative(char *topic, char *payload)
|
|||
char absolute_topic[100];
|
||||
sprintf(absolute_topic, "%s/%s", base_topic, topic);
|
||||
mqtt.publish(absolute_topic, payload);
|
||||
mqtt.loop();
|
||||
}
|
||||
|
||||
void ESPMegaIoT::subscribeRelative(char *topic)
|
||||
|
@ -299,6 +320,7 @@ void ESPMegaIoT::subscribeRelative(char *topic)
|
|||
char absolute_topic[100];
|
||||
sprintf(absolute_topic, "%s/%s", base_topic, topic);
|
||||
mqtt.subscribe(absolute_topic);
|
||||
mqtt.loop();
|
||||
}
|
||||
|
||||
void ESPMegaIoT::registerSubscribeCallback(void (*callback)(void))
|
||||
|
@ -341,7 +363,6 @@ void ESPMegaIoT::saveNetworkConfig()
|
|||
fram->write8(IOT_FRAM_ADDRESS + 54, network_config.wifiUseAuth);
|
||||
fram->write(IOT_FRAM_ADDRESS + 55, (uint8_t *)network_config.ssid, 32);
|
||||
fram->write(IOT_FRAM_ADDRESS + 87, (uint8_t *)network_config.password, 32);
|
||||
|
||||
}
|
||||
|
||||
void ESPMegaIoT::ethernetBegin()
|
||||
|
@ -374,10 +395,16 @@ void ESPMegaIoT::saveMqttConfig()
|
|||
void ESPMegaIoT::connectToMqtt()
|
||||
{
|
||||
if (mqtt_config.mqtt_useauth)
|
||||
{
|
||||
ESP_LOGD("ESPMegaIoT", "Connecting to MQTT with auth");
|
||||
this->connectToMqtt(network_config.hostname, mqtt_config.mqtt_server, mqtt_config.mqtt_port, mqtt_config.mqtt_user, mqtt_config.mqtt_password);
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGD("ESPMegaIoT", "Connecting to MQTT without auth");
|
||||
this->connectToMqtt(network_config.hostname, mqtt_config.mqtt_server, mqtt_config.mqtt_port);
|
||||
}
|
||||
}
|
||||
|
||||
void ESPMegaIoT::connectNetwork()
|
||||
{
|
||||
|
|
|
@ -163,6 +163,9 @@ void InternalDisplay::saveMQTTConfig()
|
|||
// password_set -> a text input to set the mqtt password
|
||||
// topic_set -> a text input to set the mqtt base topic
|
||||
|
||||
// Send the stop bytes to flush the serial buffer
|
||||
this->sendStopBytes();
|
||||
|
||||
// Save the mqtt server
|
||||
if(!this->getStringToBuffer("mqttsv_set.txt", this->mqttConfig->mqtt_server, 16))
|
||||
return;
|
||||
|
@ -183,7 +186,10 @@ void InternalDisplay::saveMQTTConfig()
|
|||
return;
|
||||
|
||||
// Save the mqtt use auth
|
||||
this->mqttConfig->mqtt_useauth = this->getNumber("use_auth.val") == 1 ? true : false;
|
||||
uint8_t use_auth = this->getNumber("use_auth.val");
|
||||
Serial.print("use_auth: ");
|
||||
Serial.println(use_auth);
|
||||
this->mqttConfig->mqtt_useauth = use_auth == 1 ? true : false;
|
||||
this->iot->saveMqttConfig();
|
||||
ESP.restart();
|
||||
}
|
||||
|
|
|
@ -31,4 +31,4 @@ lib_deps = adafruit/Adafruit PWM Servo Driver Library@^2.4.1
|
|||
https://github.com/me-no-dev/ESPAsyncWebServer.git
|
||||
#esphome/ESPAsyncWebServer-esphome@^3.1.0
|
||||
monitor_speed = 115200
|
||||
build_flags = -DCORE_DEBUG_LEVEL=3
|
||||
build_flags = -DCORE_DEBUG_LEVEL=1
|
|
@ -3,7 +3,7 @@
|
|||
#include <ETH.h>
|
||||
#include <ClimateCard.hpp>
|
||||
|
||||
|
||||
// #define FRAM_DEBUG
|
||||
|
||||
// Demo PLC firmware using the ESPMegaPRO OOP library
|
||||
|
||||
|
@ -12,7 +12,8 @@ const uint16_t irCode[15][4][4][1] = {0};
|
|||
const char *mode_names[] = {"Off", "Fan-only", "Cool"};
|
||||
const char *fan_speed_names[] = {"Auto", "Low", "Medium", "High"};
|
||||
|
||||
size_t getInfraredCode(uint8_t mode, uint8_t fan_speed, uint8_t temperature, const uint16_t** codePtr) {
|
||||
size_t getInfraredCode(uint8_t mode, uint8_t fan_speed, uint8_t temperature, const uint16_t **codePtr)
|
||||
{
|
||||
// Change the code pointer to point to the IR timing array
|
||||
*codePtr = &(irCode[mode][fan_speed][temperature][0]);
|
||||
return sizeof(irCode[mode][fan_speed][temperature]) / sizeof(uint16_t);
|
||||
|
@ -25,19 +26,20 @@ AirConditioner ac = {
|
|||
.mode_names = mode_names,
|
||||
.fan_speeds = 4,
|
||||
.fan_speed_names = fan_speed_names,
|
||||
.getInfraredCode = &getInfraredCode
|
||||
};
|
||||
.getInfraredCode = &getInfraredCode};
|
||||
|
||||
ClimateCard climateCard = ClimateCard(14, ac);
|
||||
|
||||
void input_change_callback(uint8_t pin, uint8_t value) {
|
||||
void input_change_callback(uint8_t pin, uint8_t value)
|
||||
{
|
||||
Serial.print("Input change callback: ");
|
||||
Serial.print(pin);
|
||||
Serial.print(" ");
|
||||
Serial.println(value);
|
||||
}
|
||||
|
||||
void setNetworkConfig() {
|
||||
void setNetworkConfig()
|
||||
{
|
||||
NetworkConfig config = {
|
||||
.ip = {192, 168, 0, 11},
|
||||
.gateway = {192, 168, 0, 1},
|
||||
|
@ -54,48 +56,65 @@ void setNetworkConfig() {
|
|||
Serial.println("Setting network config");
|
||||
espmega.iot->setNetworkConfig(config);
|
||||
espmega.iot->saveNetworkConfig();
|
||||
|
||||
}
|
||||
|
||||
void setMqttConfig() {
|
||||
void setMqttConfig()
|
||||
{
|
||||
MqttConfig config = {
|
||||
.mqtt_port = 1883,
|
||||
.mqtt_useauth = false
|
||||
};
|
||||
.mqtt_useauth = false};
|
||||
strcpy(config.mqtt_server, "192.168.0.26");
|
||||
strcpy(config.base_topic, "/espmegaoop");
|
||||
espmega.iot->setMqttConfig(config);
|
||||
espmega.iot->saveMqttConfig();
|
||||
}
|
||||
|
||||
void setup() {
|
||||
void setup()
|
||||
{
|
||||
ESP_LOGI("Initializer", "Starting ESPMegaPRO OOP demo");
|
||||
espmega.begin();
|
||||
ESP_LOGI("Initializer", "Enabling IOT module");
|
||||
espmega.enableIotModule();
|
||||
ESP_LOGI("Initializer", "Enabling Ethernet");
|
||||
ETH.begin();
|
||||
ESP_LOGI("Initializer", "Binding Ethernet to IOT module");
|
||||
espmega.iot->bindEthernetInterface(Ð);
|
||||
ESP_LOGI("Initializer", "Loading network config");
|
||||
espmega.iot->loadNetworkConfig();
|
||||
ESP_LOGI("Initializer", "Connecting to network");
|
||||
espmega.iot->connectNetwork();
|
||||
ESP_LOGI("Initializer", "Loading MQTT config");
|
||||
espmega.iot->loadMqttConfig();
|
||||
ESP_LOGI("Initializer", "Connecting to MQTT");
|
||||
espmega.iot->connectToMqtt();
|
||||
ESP_LOGI("Initializer", "Registering cards 0");
|
||||
espmega.iot->registerCard(0);
|
||||
ESP_LOGI("Initializer", "Registering cards 1");
|
||||
espmega.iot->registerCard(1);
|
||||
ESP_LOGI("Initializer", "Registering Input change callback");
|
||||
espmega.inputs.registerCallback(input_change_callback);
|
||||
ESP_LOGI("Initializer", "Installing climate card");
|
||||
espmega.installCard(2, &climateCard);
|
||||
ESP_LOGI("Initializer", "Binding climate card to FRAM");
|
||||
climateCard.bindFRAM(&espmega.fram, 301);
|
||||
ESP_LOGI("Initializer", "Loading climate card state from FRAM");
|
||||
climateCard.loadStateFromFRAM();
|
||||
ESP_LOGI("Initializer", "Enabling climate card FRAM autosave");
|
||||
climateCard.setFRAMAutoSave(true);
|
||||
ESP_LOGI("Initializer", "Enabling internal display");
|
||||
espmega.enableInternalDisplay(&Serial);
|
||||
ESP_LOGI("Initializer", "Binding climate card to internal display");
|
||||
espmega.display->bindClimateCard(&climateCard);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Every 20 seconds, dump FRAM 0-500 to serial
|
||||
void loop() {
|
||||
void loop()
|
||||
{
|
||||
espmega.loop();
|
||||
#ifdef FRAM_DEBUG
|
||||
// Every 20 seconds, dump FRAM 0-500 to serial
|
||||
static uint32_t last_fram_dump = 0;
|
||||
if (millis() - last_fram_dump >= 20000) {
|
||||
if (millis() - last_fram_dump >= 20000)
|
||||
{
|
||||
last_fram_dump = millis();
|
||||
Serial.println("Dumping FRAM");
|
||||
espmega.dumpFRAMtoSerial(0, 500);
|
||||
|
|
Loading…
Reference in New Issue