Added guesture toggle, saved in config
This commit is contained in:
parent
fbcac7c884
commit
f9a92bceca
@ -30,6 +30,7 @@ It is also possible to send the same json configuration on mqtt using topic *lig
|
||||
"mqttuser": "mymqtt_user",
|
||||
"mqttpass": "7j%!fs#",
|
||||
"mqttport": "1883",
|
||||
"guestureEnabled" : "true"
|
||||
}
|
||||
```
|
||||
|
||||
@ -61,6 +62,9 @@ Meant for debugging there is a topic ***light/bedlamp/debug***. This topic will
|
||||
* Uptime (s) is sent repeatedly at every *state* update (default each 120s)
|
||||
* All incoming topics and payloads
|
||||
|
||||
### Guesture Enable topic
|
||||
Topic to activate or deactivate guestures for controlling the lamp ***light/bedlamp/guestureenabled/set***. Payload **1** or **0**.
|
||||
|
||||
## Home Assistant
|
||||
Adjust the name and topics to suite your configuration
|
||||
```
|
||||
|
@ -13,6 +13,7 @@ public:
|
||||
char mqttUser[10];
|
||||
char mqttPass[20];
|
||||
int mqttPort;
|
||||
bool gestureEnabled;
|
||||
uint8_t brightness;
|
||||
std::array<uint8_t, 3> color;
|
||||
} Data;
|
||||
@ -23,6 +24,8 @@ public:
|
||||
ConfigTopic,
|
||||
AvailabilityTopic,
|
||||
DebugTopic,
|
||||
InGuestureEnabled,
|
||||
OutGuestureEnabled,
|
||||
};
|
||||
|
||||
static Config& Instance();
|
||||
|
@ -72,6 +72,14 @@ String Config::getMqttTopic(MqttTopic topic)
|
||||
break;
|
||||
case (MqttTopic::DebugTopic):
|
||||
return String("light/" + String(data.hostname) + "/debug");
|
||||
break;
|
||||
case (MqttTopic::InGuestureEnabled):
|
||||
return String("light/" + String(data.hostname) +
|
||||
"/guestureenabled/set");
|
||||
break;
|
||||
case (MqttTopic::OutGuestureEnabled):
|
||||
return String("light/" + String(data.hostname) + "/guestureenabled");
|
||||
break;
|
||||
}
|
||||
return {};
|
||||
}
|
22
src/main.cpp
22
src/main.cpp
@ -55,6 +55,7 @@ void setConfig(String jsonConfig)
|
||||
String mqttUser = obj["mqttuser"];
|
||||
String mqttPass = obj["mqttpass"];
|
||||
int mqttPort = obj["mqttport"];
|
||||
bool gestureEnabled = obj["gestureEnabled"];
|
||||
Serial.printf("Parsed hostname: %s, ssid: %s, pass: %s\n",
|
||||
hostname.c_str(),
|
||||
ssid.c_str(),
|
||||
@ -74,6 +75,8 @@ void setConfig(String jsonConfig)
|
||||
sizeof(config.data.mqttPass));
|
||||
config.data.mqttPort = mqttPort;
|
||||
|
||||
config.data.gestureEnabled = gestureEnabled;
|
||||
|
||||
config.write();
|
||||
|
||||
Serial.println("rebooting...");
|
||||
@ -92,6 +95,7 @@ String configToString()
|
||||
jsonConfig["mqttuser"] = config.data.mqttUser;
|
||||
jsonConfig["mqttpass"] = config.data.mqttPass;
|
||||
jsonConfig["mqttport"] = config.data.mqttPort;
|
||||
jsonConfig["guestureenabled"] = config.data.gestureEnabled;
|
||||
jsonConfig["brightness"] = config.data.brightness;
|
||||
JsonArray color = jsonConfig.createNestedArray("color");
|
||||
color.add(config.data.color.at(0));
|
||||
@ -164,6 +168,9 @@ void mqttConnect()
|
||||
config.getMqttTopic(Config::MqttTopic::InTopic).c_str());
|
||||
mqttClient.subscribe(
|
||||
config.getMqttTopic(Config::MqttTopic::ConfigTopic).c_str());
|
||||
mqttClient.subscribe(
|
||||
config.getMqttTopic(Config::MqttTopic::InGuestureEnabled)
|
||||
.c_str());
|
||||
} else
|
||||
{
|
||||
Serial.printf("failed, rc=%d\n", mqttClient.state());
|
||||
@ -186,6 +193,10 @@ void mqttCallback(char* topic, byte* payload, unsigned int length)
|
||||
} else if (sTopic == config.getMqttTopic(Config::ConfigTopic))
|
||||
{
|
||||
setConfig(msg);
|
||||
} else if (sTopic == config.getMqttTopic(Config::InGuestureEnabled))
|
||||
{
|
||||
config.data.gestureEnabled = (msg == "1");
|
||||
config.write();
|
||||
}
|
||||
|
||||
mqttClient.publish(
|
||||
@ -251,6 +262,9 @@ void setup()
|
||||
mqttClient.publish(
|
||||
config.getMqttTopic(Config::MqttTopic::DebugTopic).c_str(),
|
||||
configToString().c_str());
|
||||
mqttClient.publish(
|
||||
config.getMqttTopic(Config::MqttTopic::InGuestureEnabled).c_str(),
|
||||
String(config.data.gestureEnabled).c_str());
|
||||
|
||||
webOTA.setup(&WiFi);
|
||||
}
|
||||
@ -364,9 +378,11 @@ void evalDist(SensorData data)
|
||||
|
||||
void slowLoop()
|
||||
{
|
||||
SensorData data = getDist();
|
||||
|
||||
evalDist(data);
|
||||
if (config.data.gestureEnabled)
|
||||
{
|
||||
SensorData data = getDist();
|
||||
evalDist(data);
|
||||
}
|
||||
|
||||
myLed.run();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user