remote variable tested and moved into production
This commit is contained in:
parent
df2543ad58
commit
3467c90e52
|
@ -206,7 +206,7 @@ void ESPMegaIoT::publishCard(uint8_t card_id)
|
|||
*
|
||||
* @param topic The topic to subscribe to
|
||||
*/
|
||||
void ESPMegaIoT::subscribe(char *topic)
|
||||
void ESPMegaIoT::subscribe(const char *topic)
|
||||
{
|
||||
mqtt.subscribe(topic);
|
||||
}
|
||||
|
@ -216,7 +216,7 @@ void ESPMegaIoT::subscribe(char *topic)
|
|||
*
|
||||
* @param topic The topic to unsubscribe from
|
||||
*/
|
||||
void ESPMegaIoT::unsubscribeFromTopic(char *topic)
|
||||
void ESPMegaIoT::unsubscribeFromTopic(const char *topic)
|
||||
{
|
||||
mqtt.unsubscribe(topic);
|
||||
}
|
||||
|
@ -227,7 +227,7 @@ void ESPMegaIoT::unsubscribeFromTopic(char *topic)
|
|||
* @param ssid The SSID of the wifi network
|
||||
* @param password The password of the wifi network
|
||||
*/
|
||||
void ESPMegaIoT::connectToWifi(char *ssid, char *password)
|
||||
void ESPMegaIoT::connectToWifi(const char *ssid, const char *password)
|
||||
{
|
||||
WiFi.begin(ssid, password);
|
||||
}
|
||||
|
@ -237,7 +237,7 @@ void ESPMegaIoT::connectToWifi(char *ssid, char *password)
|
|||
*
|
||||
* @param ssid The SSID of the wifi network
|
||||
*/
|
||||
void ESPMegaIoT::connectToWifi(char *ssid)
|
||||
void ESPMegaIoT::connectToWifi(const char *ssid)
|
||||
{
|
||||
WiFi.begin(ssid);
|
||||
}
|
||||
|
@ -488,7 +488,7 @@ void ESPMegaIoT::unregisterRelativeMqttCallback(uint8_t handler)
|
|||
* @param topic The topic to publish to
|
||||
* @param payload The payload to publish
|
||||
*/
|
||||
void ESPMegaIoT::publishRelative(char *topic, char *payload)
|
||||
void ESPMegaIoT::publishRelative(const char *topic, const char *payload)
|
||||
{
|
||||
char absolute_topic[100];
|
||||
sprintf(absolute_topic, "%s/%s", this->mqtt_config.base_topic, topic);
|
||||
|
@ -501,7 +501,7 @@ void ESPMegaIoT::publishRelative(char *topic, char *payload)
|
|||
*
|
||||
* @param topic The topic to subscribe to
|
||||
*/
|
||||
void ESPMegaIoT::subscribeRelative(char *topic)
|
||||
void ESPMegaIoT::subscribeRelative(const char *topic)
|
||||
{
|
||||
char absolute_topic[100];
|
||||
sprintf(absolute_topic, "%s/%s", this->mqtt_config.base_topic, topic);
|
||||
|
|
|
@ -78,13 +78,13 @@ public:
|
|||
void unregisterCard(uint8_t card_id);
|
||||
void publishCard(uint8_t card_id);
|
||||
// Publish topic appended with base topic
|
||||
void publishRelative(char *topic, char *payload);
|
||||
void publishRelative(const char *topic, const char *payload);
|
||||
// Subscribe topic appended with base topic
|
||||
void subscribeRelative(char *topic);
|
||||
void subscribe(char *topic);
|
||||
void unsubscribeFromTopic(char *topic);
|
||||
void connectToWifi(char *ssid, char *password);
|
||||
void connectToWifi(char *ssid);
|
||||
void subscribeRelative(const char *topic);
|
||||
void subscribe(const char *topic);
|
||||
void unsubscribeFromTopic(const char *topic);
|
||||
void connectToWifi(const char *ssid, const char *password);
|
||||
void connectToWifi(const char *ssid);
|
||||
void disconnectFromWifi();
|
||||
bool wifiConnected();
|
||||
void ethernetBegin();
|
||||
|
|
|
@ -27,7 +27,7 @@ bool ESPMegaPRO::begin()
|
|||
fram.begin(FRAM_ADDRESS);
|
||||
Serial.begin(115200);
|
||||
this->installCard(1, &outputs);
|
||||
outputs.bindFRAM(&fram, 0);
|
||||
outputs.bindFRAM(&fram, 0);
|
||||
uint8_t outputPinMap[16] = {8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7};
|
||||
outputs.loadPinMap(outputPinMap);
|
||||
outputs.loadFromFRAM();
|
||||
|
|
|
@ -8,34 +8,39 @@ RemoteVariable::~RemoteVariable() {
|
|||
delete this->topic;
|
||||
delete this->value;
|
||||
}
|
||||
void RemoteVariable::begin(size_t size, char* topic, ESPMegaIoT* iot, bool useValueRequest, char* valueRequestTopic) {
|
||||
void RemoteVariable::begin(size_t size, const char* topic, ESPMegaIoT* iot, bool useValueRequest, const char* valueRequestTopic) {
|
||||
this->iot = iot;
|
||||
this->size = size;
|
||||
topic = (char*)calloc(size, sizeof(char));
|
||||
this->topic = topic;
|
||||
this->useValueRequest = useValueRequest;
|
||||
this->valueRequestTopic = valueRequestTopic;
|
||||
value = (char*)calloc(size, sizeof(char));
|
||||
this->valueRequestTopic = nullptr;
|
||||
auto bindedMqttCallback = std::bind(&RemoteVariable::mqtt_callback, this, std::placeholders::_1, std::placeholders::_2);
|
||||
this->iot->registerMqttCallback(bindedMqttCallback);
|
||||
auto bindedSubscribe = std::bind(&RemoteVariable::subscribe, this);
|
||||
this->iot->registerSubscribeCallback(bindedSubscribe);
|
||||
this->subscribe();
|
||||
}
|
||||
|
||||
void RemoteVariable::begin(size_t size, char* topic, ESPMegaIoT* iot) {
|
||||
void RemoteVariable::begin(size_t size, const char* topic, ESPMegaIoT* iot) {
|
||||
this->begin(size, topic, iot, false, nullptr);
|
||||
}
|
||||
|
||||
void RemoteVariable::subscribe() {
|
||||
ESP_LOGD("RemoteVariable", "Subscribing to %s", this->topic);
|
||||
this->iot->subscribe(this->topic);
|
||||
if(this->useValueRequest) {
|
||||
ESP_LOGD("RemoteVariable", "Subscribing to %s", this->valueRequestTopic);
|
||||
this->requestValue();
|
||||
}
|
||||
}
|
||||
char* RemoteVariable::getValue() {
|
||||
return this->getValue();
|
||||
return this->value;
|
||||
}
|
||||
|
||||
void RemoteVariable::mqtt_callback(char* topic, char* payload) {
|
||||
if (strcmp(topic, this->topic) == 0) {
|
||||
ESP_LOGD("RemoteVariable", "Received MQTT message from %s", topic);
|
||||
strcpy(this->value, payload);
|
||||
}
|
||||
}
|
||||
|
@ -43,15 +48,16 @@ void RemoteVariable::mqtt_callback(char* topic, char* payload) {
|
|||
void RemoteVariable::requestValue() {
|
||||
if(!this->useValueRequest)
|
||||
return;
|
||||
ESP_LOGD("RemoteVariable", "Sending request to %s", this->valueRequestTopic);
|
||||
this->iot->publish(this->valueRequestTopic, "request");
|
||||
}
|
||||
|
||||
void RemoteVariable::enableSetValue(char* setValueTopic) {
|
||||
void RemoteVariable::enableSetValue(const char* setValueTopic) {
|
||||
this->useSetValue = true;
|
||||
this->setValueTopic = setValueTopic;
|
||||
}
|
||||
|
||||
void RemoteVariable::setValue(char* value) {
|
||||
void RemoteVariable::setValue(const char* value) {
|
||||
if(!this->useSetValue)
|
||||
return;
|
||||
this->iot->publish(this->setValueTopic, value);
|
||||
|
|
|
@ -5,21 +5,21 @@ class RemoteVariable
|
|||
public:
|
||||
RemoteVariable();
|
||||
~RemoteVariable();
|
||||
void begin(size_t size, char* topic, ESPMegaIoT* iot);
|
||||
void begin(size_t size, char* topic, ESPMegaIoT* iot, bool useValueRequest, char* valueRequestTopic);
|
||||
void setValue(char* value);
|
||||
void enableSetValue(char* setValueTopic);
|
||||
void begin(size_t size, const char* topic, ESPMegaIoT* iot);
|
||||
void begin(size_t size, const char* topic, ESPMegaIoT* iot, bool useValueRequest, const char* valueRequestTopic);
|
||||
void setValue(const char* value);
|
||||
void enableSetValue(const char* setValueTopic);
|
||||
void subscribe();
|
||||
void requestValue();
|
||||
char* getValue();
|
||||
private:
|
||||
void mqtt_callback(char* topic, char* payload);
|
||||
ESPMegaIoT* iot;
|
||||
char* topic;
|
||||
const char* topic;
|
||||
char* value;
|
||||
size_t size;
|
||||
bool useValueRequest;
|
||||
char* valueRequestTopic;
|
||||
const char* valueRequestTopic;
|
||||
bool useSetValue;
|
||||
char* setValueTopic;
|
||||
const char* setValueTopic;
|
||||
};
|
|
@ -3,6 +3,7 @@
|
|||
#include <ETH.h>
|
||||
#include <ClimateCard.hpp>
|
||||
#include <ESPMegaDisplayOTA.hpp>
|
||||
#include <RemoteVariable.hpp>
|
||||
|
||||
// #define FRAM_DEBUG
|
||||
// #define MQTT_DEBUG
|
||||
|
@ -17,6 +18,9 @@
|
|||
|
||||
ESPMegaPRO espmega = ESPMegaPRO();
|
||||
|
||||
// Remote Variable
|
||||
RemoteVariable testVar = RemoteVariable();
|
||||
|
||||
#ifdef LCD_OTA_ENABLE
|
||||
ESPMegaDisplayOTA internalDisplayOTA = ESPMegaDisplayOTA();
|
||||
#endif
|
||||
|
@ -191,6 +195,9 @@ void setup()
|
|||
#ifdef LCD_OTA_ENABLE
|
||||
internalDisplayOTA.begin("/display", espmega.display, espmega.webServer);
|
||||
#endif
|
||||
ESP_LOGI("Initializer", "Initializing testvar");
|
||||
testVar.begin(32, "/testvar", espmega.iot, true,"/testvar/request");
|
||||
testVar.enableSetValue("/testvar/set");
|
||||
}
|
||||
|
||||
void loop()
|
||||
|
@ -225,4 +232,13 @@ void loop()
|
|||
Serial.println(espmega.iot->mqttConnected() ? "Connected" : "Disconnected");
|
||||
}
|
||||
#endif
|
||||
// Print out testvar value every 5 seconds
|
||||
static uint32_t last_testvar_print = 0;
|
||||
if (millis() - last_testvar_print >= 5000)
|
||||
{
|
||||
last_testvar_print = millis();
|
||||
if (testVar.getValue() != nullptr)
|
||||
ESP_LOGI("TestVar", "Value: %s", testVar.getValue());
|
||||
testVar.setValue("Hello World");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue