From 1d977c5bdfd99fea6270c86adce657a854e962a9 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Sat, 23 Mar 2024 11:54:53 +0700 Subject: [PATCH] working smart variable --- .../lib/ESPMegaPRO/SmartVariable.cpp | 45 ++++++++++++++++--- ESPMegaPRO-OS-SDK/src/main.cpp | 22 ++++++++- 2 files changed, 60 insertions(+), 7 deletions(-) diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/SmartVariable.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/SmartVariable.cpp index 66251ba..1a81bc5 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/SmartVariable.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/SmartVariable.cpp @@ -17,14 +17,22 @@ void SmartVariable::begin(size_t size) void SmartVariable::enableIoT(ESPMegaIoT *iot, const char *topic) { - bool iotEnabled = true; + this->iot = iot; + this->iotEnabled = true; + this->topic = topic; + ESP_LOGV("SmartVariable", "Binding MQTT Callback"); auto bindedMqttCallback = std::bind(&SmartVariable::handleMqttCallback, this, std::placeholders::_1, std::placeholders::_2); - this->iot->registerRelativeMqttCallback(bindedMqttCallback); + this->iot->registerMqttCallback(bindedMqttCallback); + ESP_LOGV("SmartVariable", "Binding MQTT Subscription"); + auto bindedMqttSubscription = std::bind(&SmartVariable::subscribeMqtt, this); + this->iot->registerSubscribeCallback(bindedMqttSubscription); + ESP_LOGI("SmartVariable", "Calling MQTT Subscribe"); this->subscribeMqtt(); } void SmartVariable::enableValueRequest(const char *valueRequestTopic) { + ESP_LOGD("SmartVariable", "Enabling Value Request"); this->useValueRequest = true; this->valueRequestTopic = valueRequestTopic; this->subscribeMqtt(); @@ -54,8 +62,18 @@ void SmartVariable::enableSetValue(const char *setValueTopic) void SmartVariable::publishValue() { - if (this->iotEnabled) + if (this->iotEnabled) { + if (this->value == nullptr) { + ESP_LOGE("SmartVariable", "Value is NULL"); + return; + } + if (this->topic == nullptr) { + ESP_LOGE("SmartVariable", "Topic is NULL"); + return; + } + ESP_LOGV("SmartVariable", "Publishing Value: %s to %s", this->value, this->topic); this->iot->publish(this->topic, this->value); + } } void SmartVariable::bindFRAM(FRAM *fram, uint32_t framAddress) @@ -112,12 +130,29 @@ void SmartVariable::handleMqttCallback(char *topic, char *payload) void SmartVariable::subscribeMqtt() { + if (this->iotEnabled) { - if (this->useValueRequest) + ESP_LOGV("SmartVariable", "IoT Enabled, running MQTT Subscribe"); + ESP_LOGV("SmartVariable", "Value Request: %d, Set Value: %d", this->useValueRequest, this->setValueEnabled); + if (this->useValueRequest) { + if (this->valueRequestTopic == nullptr) { + ESP_LOGE("SmartVariable", "Value Request Topic is NULL"); + return; + } + ESP_LOGV("SmartVariable", "Subscribing to %s", this->valueRequestTopic); this->iot->subscribe(this->valueRequestTopic); - if (this->setValueEnabled) + } + if (this->setValueEnabled) { + if (this->setValueTopic == nullptr) { + ESP_LOGE("SmartVariable", "Set Value Topic is NULL"); + return; + } + ESP_LOGV("SmartVariable", "Subscribing to %s", this->setValueTopic); this->iot->subscribe(this->setValueTopic); + } + ESP_LOGV("SmartVariable", "Publishing Value"); + this->publishValue(); } } diff --git a/ESPMegaPRO-OS-SDK/src/main.cpp b/ESPMegaPRO-OS-SDK/src/main.cpp index 9108fa4..39125b3 100644 --- a/ESPMegaPRO-OS-SDK/src/main.cpp +++ b/ESPMegaPRO-OS-SDK/src/main.cpp @@ -1,3 +1,7 @@ +/** + * @file main.cpp + * @brief Test firmware for the ESPMegaPRO OOP library +*/ #include #include #include @@ -125,7 +129,7 @@ void mqtt_callback(char *topic, char *payload) void setNetworkConfig() { NetworkConfig config = { - .ip = {192, 168, 0, 10}, + .ip = {192, 168, 0, 249}, .gateway = {192, 168, 0, 1}, .subnet = {255, 255, 255, 0}, .dns1 = {10, 192, 1, 1}, @@ -148,7 +152,7 @@ void setMqttConfig() .mqtt_port = 1883, .mqtt_useauth = false}; strcpy(config.mqtt_server, "192.168.0.26"); - strcpy(config.base_topic, "/espmegaoop"); + strcpy(config.base_topic, "/espmegacud"); espmega.iot->setMqttConfig(config); espmega.iot->saveMqttConfig(); } @@ -236,10 +240,15 @@ void setup() #ifdef SMART_VARIABLE_ENABLE ESP_LOGI("Initializer", "Initializing smart variable"); smartVar.begin(16); + ESP_LOGI("Initializer", "Binding smart variable to FRAM"); smartVar.bindFRAM(&espmega.fram, 8000); + ESP_LOGI("Initializer", "Enabling smart variable autosave"); smartVar.setValueAutoSave(true); + ESP_LOGI("Initializer", "Enabling IoT for smart variable"); smartVar.enableIoT(espmega.iot, "/smartvar"); + ESP_LOGI("Initializer", "Enabling smart variable set value"); smartVar.enableSetValue("/smartvar/set"); + ESP_LOGI("Initializer", "Enabling smart variable value request"); smartVar.enableValueRequest("/smartvar/request"); #endif ESP_LOGI("Initializer", "Setup complete"); @@ -306,5 +315,14 @@ void loop() Serial.print("SmartVar: "); Serial.println(smartVar.getValue()); } + + static bool last_smartvar_state = false; + static uint32_t last_smartvar_state_change = 0; + if (millis() - last_smartvar_state_change >= 5000) + { + last_smartvar_state_change = millis(); + last_smartvar_state = !last_smartvar_state; + smartVar.setValue(last_smartvar_state ? "true" : "false"); + } #endif } \ No newline at end of file