Compare commits

..

3 Commits
1.2 ... master

5 changed files with 38 additions and 4 deletions

View File

@ -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
```

View File

@ -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();

View File

@ -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 {};
}

View File

@ -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));
@ -141,6 +145,9 @@ void mqttPublishState()
mqttClient.publish(
config.getMqttTopic(Config::MqttTopic::AvailabilityTopic).c_str(),
"1");
mqttClient.publish(
config.getMqttTopic(Config::MqttTopic::OutGuestureEnabled).c_str(),
String(config.data.gestureEnabled).c_str());
auto uptime = millis() / 1000;
mqttClient.publish(
@ -164,6 +171,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 +196,13 @@ 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(
config.getMqttTopic(Config::MqttTopic::OutGuestureEnabled).c_str(),
String(config.data.gestureEnabled).c_str());
}
mqttClient.publish(
@ -364,9 +381,11 @@ void evalDist(SensorData data)
void slowLoop()
{
SensorData data = getDist();
evalDist(data);
if (config.data.gestureEnabled)
{
SensorData data = getDist();
evalDist(data);
}
myLed.run();

View File

@ -5,7 +5,7 @@
#include <limits>
namespace {
const float minBrightness = 0.1;
const float minBrightness = 0.05;
}
MyLed::MyLed(int ledPin, int ledCount, bool effects)