working smart variable
This commit is contained in:
parent
1b2a270d38
commit
1d977c5bdf
|
@ -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,13 +130,30 @@ 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();
|
||||
}
|
||||
}
|
||||
|
||||
int32_t SmartVariable::getIntValue()
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
/**
|
||||
* @file main.cpp
|
||||
* @brief Test firmware for the ESPMegaPRO OOP library
|
||||
*/
|
||||
#include <ESPMegaProOS.hpp>
|
||||
#include <InternalDisplay.hpp>
|
||||
#include <ETH.h>
|
||||
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue