Added debug information

This commit is contained in:
Philip Johansson 2020-07-15 14:51:50 +02:00
parent f5ea07c90a
commit a4f8106009
4 changed files with 42 additions and 11 deletions

View File

@ -33,7 +33,12 @@
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"typeinfo": "cpp"
"typeinfo": "cpp",
"complex": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"iomanip": "cpp",
"iostream": "cpp"
},
"editor.formatOnSave": true,
}

View File

@ -55,6 +55,12 @@ An identical json package is both sent and received to set and get the status of
### Configuration topic
See *Writing a configuration*. This can also be done using mqtt at topic ***light/bedlamp/relay/0/config***
### Debug topic
Meant for debugging there is a topic ***light/bedlamp/debug***. This topic will output:
* The configuration of the lamp at boot
* Uptime (s) is sent repeatedly at every *state* update (default each 120s)
* All incoming topics and payloads
## Home Assistant
Adjust the name and topics to suite your configuration
```

View File

@ -22,6 +22,8 @@ const int sensorPin = A0;
const int ledPin = D8;
const int ledCount = 7;
const int publishInterval = 120;
Config& config = Config::Instance();
EasyButton button(buttonPin);
@ -81,7 +83,7 @@ void setConfig(String jsonConfig)
String configToString()
{
DynamicJsonDocument doc(512);
DynamicJsonDocument doc(1024);
auto jsonConfig = doc["config"];
jsonConfig["hostname"] = config.data.hostname;
jsonConfig["ssid"] = config.data.ssid;
@ -139,6 +141,11 @@ void mqttPublishState()
mqttClient.publish(
config.getMqttTopic(Config::MqttTopic::AvailabilityTopic).c_str(),
"1");
auto uptime = millis() / 1000;
mqttClient.publish(
config.getMqttTopic(Config::MqttTopic::DebugTopic).c_str(),
String("{ \"uptime\":" + String(uptime) + "}").c_str());
}
}
@ -166,18 +173,24 @@ void mqttConnect()
void mqttCallback(char* topic, byte* payload, unsigned int length)
{
String msg(reinterpret_cast<char const*>(payload));
const String sTopic(topic);
String msg;
msg.concat(reinterpret_cast<char*>(payload), length);
Serial.printf("Message arrived [%s] \n", topic);
Serial.println(msg.c_str());
if (String(topic) == config.getMqttTopic(Config::InTopic))
if (sTopic == config.getMqttTopic(Config::InTopic))
{
myLed.set(msg);
mqttPublishState();
} else if (String(topic) == config.getMqttTopic(Config::ConfigTopic))
} else if (sTopic == config.getMqttTopic(Config::ConfigTopic))
{
setConfig(msg);
}
mqttClient.publish(
config.getMqttTopic(Config::MqttTopic::DebugTopic).c_str(),
String("{\"received\": { \"" + sTopic + "\": " + msg + " } }").c_str());
}
void offCallback()
@ -232,7 +245,7 @@ void setup()
ArduinoOTA.setHostname(config.data.hostname);
ArduinoOTA.begin();
tickerPublishStatus.attach(120, mqttPublishState);
tickerPublishStatus.attach(publishInterval, mqttPublishState);
mqttConnect();
mqttClient.publish(

View File

@ -2,6 +2,8 @@
#include <ArduinoJson.h>
#include <limits>
namespace {
const float minBrightness = 0.1;
}
@ -56,7 +58,7 @@ void MyLed::toggle()
void MyLed::set(String jsonState)
{
DynamicJsonDocument doc(1024);
DynamicJsonDocument doc(2048);
deserializeJson(doc, jsonState);
JsonObject obj = doc.as<JsonObject>();
Serial.println(jsonState.c_str());
@ -107,7 +109,7 @@ MyLed::State MyLed::getState()
void MyLed::setBrightness(float brightness)
{
brightness = max(minBrightness, brightness);
brightness = min(max(minBrightness, brightness), 1.0f);
if (_useEffects)
{
@ -151,8 +153,13 @@ void MyLed::registerOffCallback(CallbackFunc callback)
void MyLed::apply()
{
_strip.fill(_strip.Color(static_cast<uint8_t>(_color.at(0) * _brightness),
static_cast<uint8_t>(_color.at(1) * _brightness),
static_cast<uint8_t>(_color.at(2) * _brightness)));
uint8_t a = 254;
_strip.fill(
_strip.Color(min(static_cast<uint8_t>(_color.at(0) * _brightness),
std::numeric_limits<uint8_t>::max()),
min(static_cast<uint8_t>(_color.at(1) * _brightness),
std::numeric_limits<uint8_t>::max()),
min(static_cast<uint8_t>(_color.at(2) * _brightness),
std::numeric_limits<uint8_t>::max())));
_strip.show();
}