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